@@ -80,8 +80,8 @@ func TestBytesMessageNilBody(t *testing.T) {
8080 }
8181 assert .Nil (t , errCons )
8282
83- rcvMsg , errRvc := consumer .ReceiveNoWait ()
84- assert .Nil (t , errRvc )
83+ rcvMsg , errRcv := consumer .ReceiveNoWait ()
84+ assert .Nil (t , errRcv )
8585 assert .NotNil (t , rcvMsg )
8686
8787 switch msg2 := rcvMsg .(type ) {
@@ -95,7 +95,7 @@ func TestBytesMessageNilBody(t *testing.T) {
9595}
9696
9797/*
98- * Test send and receive of a bytes message with no content.
98+ * Test send and receive of a bytes message with some basic content.
9999 */
100100func TestBytesMessageWithBody (t * testing.T ) {
101101
@@ -111,7 +111,7 @@ func TestBytesMessageWithBody(t *testing.T) {
111111 defer context .Close ()
112112 }
113113
114- // Create a BytesMessage, and check it has nil content.
114+ // Create a BytesMessage
115115 msgBody := []byte {'b' , 'y' , 't' , 'e' , 's' , 'm' , 'e' , 's' , 's' , 'a' , 'g' , 'e' }
116116 msg := context .CreateBytesMessage ()
117117 msg .WriteBytes (msgBody )
@@ -129,8 +129,56 @@ func TestBytesMessageWithBody(t *testing.T) {
129129 }
130130 assert .Nil (t , errCons )
131131
132- rcvMsg , errRvc := consumer .ReceiveNoWait ()
133- assert .Nil (t , errRvc )
132+ rcvMsg , errRcv := consumer .ReceiveNoWait ()
133+ assert .Nil (t , errRcv )
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+ }
145+
146+ /*
147+ * Test send and receive of a bytes message with init'd at create time.
148+ */
149+ func TestBytesMessageInitWithBytes (t * testing.T ) {
150+
151+ // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
152+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
153+ assert .Nil (t , cfErr )
154+
155+ // Creates a connection to the queue manager, using defer to close it automatically
156+ // at the end of the function (if it was created successfully)
157+ context , ctxErr := cf .CreateContext ()
158+ assert .Nil (t , ctxErr )
159+ if context != nil {
160+ defer context .Close ()
161+ }
162+
163+ // Create a BytesMessage, and check it has nil content.
164+ msgBody := []byte {'b' , 'y' , 't' , 'e' , 's' , 'm' , 'e' , 's' , 's' , 'a' , 'g' , 'e' , 'A' , 'B' , 'C' }
165+ msg := context .CreateBytesMessageWithBytes (msgBody )
166+ assert .Equal (t , 15 , msg .GetBodyLength ())
167+ assert .Equal (t , msgBody , * msg .ReadBytes ())
168+
169+ // Now send the message and get it back again, to check that it roundtripped.
170+ queue := context .CreateQueue ("DEV.QUEUE.1" )
171+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).Send (queue , msg )
172+ assert .Nil (t , errSend )
173+
174+ consumer , errCons := context .CreateConsumer (queue )
175+ if consumer != nil {
176+ defer consumer .Close ()
177+ }
178+ assert .Nil (t , errCons )
179+
180+ rcvMsg , errRcv := consumer .Receive (2000 ) // also try receive with wait
181+ assert .Nil (t , errRcv )
134182 assert .NotNil (t , rcvMsg )
135183
136184 switch msg2 := rcvMsg .(type ) {
@@ -142,3 +190,196 @@ func TestBytesMessageWithBody(t *testing.T) {
142190 }
143191
144192}
193+
194+ /*
195+ * Test Producer SendBytes shortcut
196+ */
197+ func TestBytesMessageProducerSendBytes (t * testing.T ) {
198+
199+ // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
200+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
201+ assert .Nil (t , cfErr )
202+
203+ // Creates a connection to the queue manager, using defer to close it automatically
204+ // at the end of the function (if it was created successfully)
205+ context , ctxErr := cf .CreateContext ()
206+ assert .Nil (t , ctxErr )
207+ if context != nil {
208+ defer context .Close ()
209+ }
210+
211+ // Create a BytesMessage, and check it has nil content.
212+ msgBody := []byte {'b' , 'y' , 't' , 'e' , 's' , 'p' , 'r' , 'o' , 'd' , 'u' , 'c' , 'e' , 'r' }
213+
214+ // Now send the message and get it back again, to check that it roundtripped.
215+ queue := context .CreateQueue ("DEV.QUEUE.1" )
216+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).SendBytes (queue , msgBody )
217+ assert .Nil (t , errSend )
218+
219+ consumer , errCons := context .CreateConsumer (queue )
220+ if consumer != nil {
221+ defer consumer .Close ()
222+ }
223+ assert .Nil (t , errCons )
224+
225+ rcvMsg , errRcv := consumer .ReceiveNoWait ()
226+ assert .Nil (t , errRcv )
227+ assert .NotNil (t , rcvMsg )
228+
229+ switch msg2 := rcvMsg .(type ) {
230+ case jms20subset.BytesMessage :
231+ assert .Equal (t , 13 , msg2 .GetBodyLength ())
232+ assert .Equal (t , msgBody , * msg2 .ReadBytes ())
233+ default :
234+ assert .Fail (t , "Got something other than a text message" )
235+ }
236+
237+ }
238+
239+ /*
240+ * Test Consumer ReceiveBytesBodyNoWait shortcut
241+ */
242+ func TestBytesMessageConsumerReceiveBytesBodyNoWait (t * testing.T ) {
243+
244+ // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
245+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
246+ assert .Nil (t , cfErr )
247+
248+ // Creates a connection to the queue manager, using defer to close it automatically
249+ // at the end of the function (if it was created successfully)
250+ context , ctxErr := cf .CreateContext ()
251+ assert .Nil (t , ctxErr )
252+ if context != nil {
253+ defer context .Close ()
254+ }
255+
256+ queue := context .CreateQueue ("DEV.QUEUE.1" )
257+ consumer , errCons := context .CreateConsumer (queue )
258+ if consumer != nil {
259+ defer consumer .Close ()
260+ }
261+ assert .Nil (t , errCons )
262+
263+ // Check the behaviour if there is no message to receive.
264+ var expectedNil * []byte // uninitialized
265+ rcvBytes , errRcv := consumer .ReceiveBytesBodyNoWait ()
266+ assert .Nil (t , errRcv )
267+ assert .Equal (t , expectedNil , rcvBytes )
268+
269+ // Create a BytesMessage, and check it has nil content.
270+ msgBody := []byte {'b' , 'y' , 't' , 'e' , 's' , 'n' , 'o' , 'w' , 'a' , 'i' , 't' }
271+
272+ // Now send the message and get it back again, to check that it roundtripped.
273+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).SendBytes (queue , msgBody )
274+ assert .Nil (t , errSend )
275+
276+ rcvBytes , errRcv = consumer .ReceiveBytesBodyNoWait ()
277+ assert .Nil (t , errRcv )
278+ assert .Equal (t , msgBody , * rcvBytes )
279+
280+ }
281+
282+ /*
283+ * Test Consumer ReceiveBytesBody shortcut
284+ */
285+ func TestBytesMessageConsumerReceiveBytesBody (t * testing.T ) {
286+
287+ // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
288+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
289+ assert .Nil (t , cfErr )
290+
291+ // Creates a connection to the queue manager, using defer to close it automatically
292+ // at the end of the function (if it was created successfully)
293+ context , ctxErr := cf .CreateContext ()
294+ assert .Nil (t , ctxErr )
295+ if context != nil {
296+ defer context .Close ()
297+ }
298+
299+ queue := context .CreateQueue ("DEV.QUEUE.1" )
300+ consumer , errCons := context .CreateConsumer (queue )
301+ if consumer != nil {
302+ defer consumer .Close ()
303+ }
304+ assert .Nil (t , errCons )
305+
306+ // Check the behaviour if there is no message to receive.
307+ var expectedNil * []byte // uninitialized
308+ rcvBytes , errRcv := consumer .ReceiveBytesBody (250 )
309+ assert .Nil (t , errRcv )
310+ assert .Equal (t , expectedNil , rcvBytes )
311+
312+ // Create a BytesMessage, and check it has nil content.
313+ msgBody := []byte {'b' , 'y' , 't' , 'e' , 's' , 'w' , 'a' , 'i' , 't' }
314+
315+ // Now send the message and get it back again, to check that it roundtripped.
316+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).SendBytes (queue , msgBody )
317+ assert .Nil (t , errSend )
318+
319+ rcvBytes , errRcv = consumer .ReceiveBytesBody (500 )
320+ assert .Nil (t , errRcv )
321+ assert .Equal (t , msgBody , * rcvBytes )
322+
323+ }
324+
325+ /*
326+ * Test Consumer ReceiveBytesBody/ReceiveStringBody shortcuts when unexpected
327+ * types of messages are received.
328+ */
329+ func TestBytesMessageConsumerMixedMessageErrors (t * testing.T ) {
330+
331+ // Loads CF parameters from connection_info.json and apiKey.json in the Downloads directory
332+ cf , cfErr := mqjms .CreateConnectionFactoryFromDefaultJSONFiles ()
333+ assert .Nil (t , cfErr )
334+
335+ // Creates a connection to the queue manager, using defer to close it automatically
336+ // at the end of the function (if it was created successfully)
337+ context , ctxErr := cf .CreateContext ()
338+ assert .Nil (t , ctxErr )
339+ if context != nil {
340+ defer context .Close ()
341+ }
342+
343+ queue := context .CreateQueue ("DEV.QUEUE.1" )
344+ consumer , errCons := context .CreateConsumer (queue )
345+ if consumer != nil {
346+ defer consumer .Close ()
347+ }
348+ assert .Nil (t , errCons )
349+
350+ // Send a BytesMessage, try to receive as text with no wait.
351+ msgBodyBytes := []byte {'b' , 'y' , 't' , 'e' , 's' , '1' , '2' , '3' , '4' }
352+ errSend := context .CreateProducer ().SetTimeToLive (5000 ).SendBytes (queue , msgBodyBytes )
353+ assert .Nil (t , errSend )
354+ rcvStr , errRcv := consumer .ReceiveStringBodyNoWait ()
355+ assert .Nil (t , rcvStr )
356+ assert .Equal (t , "MQJMS6068" , errRcv .GetErrorCode ())
357+ assert .Equal (t , "MQJMS_DIR_MIN_NOTTEXT" , errRcv .GetReason ())
358+
359+ // Send a BytesMessage, try to receive as text with wait.
360+ errSend = context .CreateProducer ().SetTimeToLive (5000 ).SendBytes (queue , msgBodyBytes )
361+ assert .Nil (t , errSend )
362+ rcvStr , errRcv = consumer .ReceiveStringBody (200 )
363+ assert .Nil (t , rcvStr )
364+ assert .Equal (t , "MQJMS6068" , errRcv .GetErrorCode ())
365+ assert .Equal (t , "MQJMS_DIR_MIN_NOTTEXT" , errRcv .GetReason ())
366+
367+ // Send a TextMessage, try to receive as bytes with no wait.
368+ msgBodyStr := "TextMessage is not Bytes"
369+ errSend = context .CreateProducer ().SetTimeToLive (5000 ).SendString (queue , msgBodyStr )
370+ assert .Nil (t , errSend )
371+ rcvBytes , errRcv := consumer .ReceiveBytesBodyNoWait ()
372+ var expectedNil * []byte // uninitialized
373+ assert .Equal (t , expectedNil , rcvBytes )
374+ assert .Equal (t , "MQJMS6068" , errRcv .GetErrorCode ())
375+ assert .Equal (t , "MQJMS_DIR_MIN_NOTBYTES" , errRcv .GetReason ())
376+
377+ // Send a TextMessage, try to receive as bytes with wait.
378+ errSend = context .CreateProducer ().SetTimeToLive (5000 ).SendString (queue , msgBodyStr )
379+ assert .Nil (t , errSend )
380+ rcvBytes , errRcv = consumer .ReceiveBytesBody (200 )
381+ assert .Equal (t , expectedNil , rcvBytes )
382+ assert .Equal (t , "MQJMS6068" , errRcv .GetErrorCode ())
383+ assert .Equal (t , "MQJMS_DIR_MIN_NOTBYTES" , errRcv .GetReason ())
384+
385+ }
0 commit comments