Skip to content

Commit 158e5ad

Browse files
authored
Merge pull request #48 from matrober-uk/msgwithspaces
Prevent trimming of whitespace on receive - #45
2 parents 5eb5e7d + c5dc4d3 commit 158e5ad

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

mqjms/ConsumerImpl.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (consumer ConsumerImpl) receiveInternal(gmo *ibmmq.MQGMO) (jms20subset.Mess
107107
var msgBodyStr *string
108108

109109
if datalen > 0 {
110-
strContent := strings.TrimSpace(string(buffer[:datalen]))
110+
strContent := string(buffer[:datalen])
111111
msgBodyStr = &strContent
112112
}
113113

textmessage_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,68 @@ func TestTextMessageEmptyBody(t *testing.T) {
137137
}
138138

139139
}
140+
141+
/*
142+
* Test the behaviour for send/receive of a text message with a body that starts
143+
* and ends with spaces. Reported issue that the trailing spaces are trimmed from
144+
* the returned message.
145+
*/
146+
func TestTextMessageWithSpaces(t *testing.T) {
147+
148+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
149+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
150+
assert.Nil(t, cfErr)
151+
152+
// Creates a connection to the queue manager, using defer to close it automatically
153+
// at the end of the function (if it was created successfully)
154+
context, ctxErr := cf.CreateContext()
155+
assert.Nil(t, ctxErr)
156+
if context != nil {
157+
defer context.Close()
158+
}
159+
160+
// Create a TextMessage
161+
msgText := " This is some text with spaces before and after "
162+
msg := context.CreateTextMessageWithString(msgText)
163+
164+
// Now send the message and get it back again.
165+
queue := context.CreateQueue("DEV.QUEUE.1")
166+
producer := context.CreateProducer().SetTimeToLive(5000)
167+
errSend := producer.Send(queue, msg)
168+
assert.Nil(t, errSend)
169+
170+
consumer, errCons := context.CreateConsumer(queue)
171+
assert.Nil(t, errCons)
172+
if consumer != nil {
173+
defer consumer.Close()
174+
}
175+
176+
rcvMsg, errRvc := consumer.ReceiveNoWait()
177+
assert.Nil(t, errRvc)
178+
assert.NotNil(t, rcvMsg)
179+
180+
switch msg := rcvMsg.(type) {
181+
case jms20subset.TextMessage:
182+
assert.Equal(t, msgText, *msg.GetText())
183+
default:
184+
assert.Fail(t, "Got something other than a text message")
185+
}
186+
187+
// Create a TextMessage consisting of all spaces.
188+
msgText2 := " "
189+
msg2 := context.CreateTextMessageWithString(msgText2)
190+
errSend = producer.Send(queue, msg2)
191+
assert.Nil(t, errSend)
192+
193+
rcvMsg2, errRvc2 := consumer.ReceiveNoWait()
194+
assert.Nil(t, errRvc2)
195+
assert.NotNil(t, rcvMsg2)
196+
197+
switch msg := rcvMsg2.(type) {
198+
case jms20subset.TextMessage:
199+
assert.Equal(t, msgText2, *msg.GetText())
200+
default:
201+
assert.Fail(t, "Got something other than a text message")
202+
}
203+
204+
}

0 commit comments

Comments
 (0)