|
| 1 | +/* |
| 2 | + * Copyright (c) IBM Corporation 2020 |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + */ |
| 10 | +package main |
| 11 | + |
| 12 | +import ( |
| 13 | + "testing" |
| 14 | + |
| 15 | + "github.com/ibm-messaging/mq-golang-jms20/jms20subset" |
| 16 | + "github.com/ibm-messaging/mq-golang-jms20/mqjms" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | +) |
| 19 | + |
| 20 | +/* |
| 21 | + * Test the creation of a bytes message and setting the content. |
| 22 | + */ |
| 23 | +func TestBytesMessageBody(t *testing.T) { |
| 24 | + |
| 25 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 26 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 27 | + assert.Nil(t, cfErr) |
| 28 | + |
| 29 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 30 | + // at the end of the function (if it was created successfully) |
| 31 | + context, ctxErr := cf.CreateContext() |
| 32 | + assert.Nil(t, ctxErr) |
| 33 | + if context != nil { |
| 34 | + defer context.Close() |
| 35 | + } |
| 36 | + |
| 37 | + // Create a BytesMessage and check that we can populate it |
| 38 | + msgBody := []byte{'a', 'e', 'i', 'o', 'u'} |
| 39 | + msg := context.CreateBytesMessage() |
| 40 | + msg.WriteBytes(msgBody) |
| 41 | + assert.Equal(t, 5, msg.GetBodyLength()) |
| 42 | + assert.Equal(t, msgBody, *msg.ReadBytes()) |
| 43 | + |
| 44 | + // Create an empty BytesMessage and check that we query it without errors |
| 45 | + msg = context.CreateBytesMessage() |
| 46 | + assert.Equal(t, 0, msg.GetBodyLength()) |
| 47 | + assert.Equal(t, []byte{}, *msg.ReadBytes()) |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +/* |
| 52 | + * Test send and receive of a bytes message with no content. |
| 53 | + */ |
| 54 | +func TestBytesMessageNilBody(t *testing.T) { |
| 55 | + |
| 56 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 57 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 58 | + assert.Nil(t, cfErr) |
| 59 | + |
| 60 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 61 | + // at the end of the function (if it was created successfully) |
| 62 | + context, ctxErr := cf.CreateContext() |
| 63 | + assert.Nil(t, ctxErr) |
| 64 | + if context != nil { |
| 65 | + defer context.Close() |
| 66 | + } |
| 67 | + |
| 68 | + // Create a BytesMessage, and check it has nil content. |
| 69 | + msg := context.CreateBytesMessage() |
| 70 | + assert.Equal(t, []byte{}, *msg.ReadBytes()) |
| 71 | + |
| 72 | + // Now send the message and get it back again, to check that it roundtripped. |
| 73 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 74 | + errSend := context.CreateProducer().SetTimeToLive(5000).Send(queue, msg) |
| 75 | + assert.Nil(t, errSend) |
| 76 | + |
| 77 | + consumer, errCons := context.CreateConsumer(queue) |
| 78 | + if consumer != nil { |
| 79 | + defer consumer.Close() |
| 80 | + } |
| 81 | + assert.Nil(t, errCons) |
| 82 | + |
| 83 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 84 | + assert.Nil(t, errRvc) |
| 85 | + assert.NotNil(t, rcvMsg) |
| 86 | + |
| 87 | + switch msg2 := rcvMsg.(type) { |
| 88 | + case jms20subset.BytesMessage: |
| 89 | + assert.Equal(t, 0, msg2.GetBodyLength()) |
| 90 | + assert.Equal(t, []byte{}, *msg2.ReadBytes()) |
| 91 | + default: |
| 92 | + assert.Fail(t, "Got something other than a text message") |
| 93 | + } |
| 94 | + |
| 95 | +} |
| 96 | + |
| 97 | +/* |
| 98 | + * Test send and receive of a bytes message with no content. |
| 99 | + */ |
| 100 | +func TestBytesMessageWithBody(t *testing.T) { |
| 101 | + |
| 102 | + // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory |
| 103 | + cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles() |
| 104 | + assert.Nil(t, cfErr) |
| 105 | + |
| 106 | + // Creates a connection to the queue manager, using defer to close it automatically |
| 107 | + // at the end of the function (if it was created successfully) |
| 108 | + context, ctxErr := cf.CreateContext() |
| 109 | + assert.Nil(t, ctxErr) |
| 110 | + if context != nil { |
| 111 | + defer context.Close() |
| 112 | + } |
| 113 | + |
| 114 | + // Create a BytesMessage, and check it has nil content. |
| 115 | + msgBody := []byte{'b', 'y', 't', 'e', 's', 'm', 'e', 's', 's', 'a', 'g', 'e'} |
| 116 | + msg := context.CreateBytesMessage() |
| 117 | + msg.WriteBytes(msgBody) |
| 118 | + assert.Equal(t, 12, msg.GetBodyLength()) |
| 119 | + assert.Equal(t, msgBody, *msg.ReadBytes()) |
| 120 | + |
| 121 | + // Now send the message and get it back again, to check that it roundtripped. |
| 122 | + queue := context.CreateQueue("DEV.QUEUE.1") |
| 123 | + errSend := context.CreateProducer().SetTimeToLive(5000).Send(queue, msg) |
| 124 | + assert.Nil(t, errSend) |
| 125 | + |
| 126 | + consumer, errCons := context.CreateConsumer(queue) |
| 127 | + if consumer != nil { |
| 128 | + defer consumer.Close() |
| 129 | + } |
| 130 | + assert.Nil(t, errCons) |
| 131 | + |
| 132 | + rcvMsg, errRvc := consumer.ReceiveNoWait() |
| 133 | + assert.Nil(t, errRvc) |
| 134 | + assert.NotNil(t, rcvMsg) |
| 135 | + |
| 136 | + switch msg2 := rcvMsg.(type) { |
| 137 | + case jms20subset.BytesMessage: |
| 138 | + assert.Equal(t, len(msgBody), msg2.GetBodyLength()) |
| 139 | + assert.Equal(t, msgBody, *msg2.ReadBytes()) |
| 140 | + default: |
| 141 | + assert.Fail(t, "Got something other than a text message") |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments