forked from ibm-messaging/mq-golang-jms20
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectionfactory_test.go
84 lines (66 loc) · 2.11 KB
/
connectionfactory_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright (c) IBM Corporation 2019
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package main
import (
"log"
"os"
"testing"
"github.com/ibm-messaging/mq-golang-jms20/mqjms"
"github.com/stretchr/testify/assert"
)
/*
* Test the ability to populate a ConnectionFactory from contents of the two
* JSON files stored on the file system.
*/
func TestLoadCFFromJSON(t *testing.T) {
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
cf, err := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
if err != nil {
log.Fatal(err)
}
assert.Equal(t, mqjms.TransportType_CLIENT, cf.TransportType)
// Creates a connection to the queue manager, using defer to close it automatically
// at the end of the function (if it was created successfully)
context, errCtx := cf.CreateContext()
if context != nil {
defer context.Close()
}
if errCtx != nil {
log.Fatal(errCtx)
}
// Equivalent to a JNDI lookup or other declarative definition
queue := context.CreateQueue("DEV.QUEUE.1")
// Send a message!
context.CreateProducer().SendString(queue, "My first message")
// Clean up the message to avoid problems with other tests.
consumer, errCons := context.CreateConsumer(queue)
if errCons != nil {
log.Fatal(errCons)
}
consumer.ReceiveStringBodyNoWait()
consumer.Close()
}
/*
* Illustrate the ability to populate a ConnectionFactory object using code
* of the application developer's choosing, which could be extended to implement
* other helper methods for creating ConnectionFactory objects.
*/
func TestProgrammaticConnFactory(t *testing.T) {
// Initialise the attributes of the CF in whatever way you like
cf := mqjms.ConnectionFactoryImpl{
QMName: "QM_ONE",
Hostname: "random.hostname.com",
PortNumber: 1414,
ChannelName: "SYSTEM.APP.SVRCONN",
UserName: os.Getenv("MQ_SAMP_USERID"),
Password: os.Getenv("MQ_SAMP_PASSWORD"),
}
assert.NotNil(t, cf)
}