Skip to content

Commit 86ba88c

Browse files
committed
PropertyExists, ClearProperties and GetPropertyNames - #39
1 parent c848f77 commit 86ba88c

File tree

3 files changed

+393
-11
lines changed

3 files changed

+393
-11
lines changed

jms20subset/Message.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,19 @@ type Message interface {
4848
// jms20subset.DeliveryMode_PERSISTENT and jms20subset.DeliveryMode_NON_PERSISTENT
4949
GetJMSDeliveryMode() int
5050

51+
// TODO documentation
5152
SetStringProperty(name string, value *string) JMSException
5253

54+
// TODO documentation
5355
// Returns string property, or nil if the property is not set.
5456
GetStringProperty(name string) *string
57+
58+
// TODO documentation
59+
PropertyExists(name string) (bool, JMSException)
60+
61+
// TODO documentation
62+
GetPropertyNames() ([]string, JMSException)
63+
64+
// TODO documentation
65+
ClearProperties() JMSException
5566
}

messageproperties_test.go

Lines changed: 310 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ import (
2828
* https://github.com/eclipse-ee4j/messaging/blob/master/api/src/main/java/jakarta/jms/Message.java#L1119
2929
*
3030
* JMS: PropertyExists, ClearProperties, GetPropertyNames
31+
* boolean propertyExists(String name) throws JMSException;
32+
* void clearProperties() throws JMSException;
33+
* Enumeration getPropertyNames() throws JMSException;
3134
*/
3235

3336
/*
@@ -116,6 +119,313 @@ func TestStringPropertyTextMsg(t *testing.T) {
116119

117120
}
118121

122+
/*
123+
* Test the Exists and GetNames functions for message properties
124+
*/
125+
func TestPropertyExistsGetNames(t *testing.T) {
126+
127+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
128+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
129+
assert.Nil(t, cfErr)
130+
131+
// Creates a connection to the queue manager, using defer to close it automatically
132+
// at the end of the function (if it was created successfully)
133+
context, ctxErr := cf.CreateContext()
134+
assert.Nil(t, ctxErr)
135+
if context != nil {
136+
defer context.Close()
137+
}
138+
139+
// Create a TextMessage and check that we can populate it
140+
msgBody := "ExistsGetNames-test"
141+
txtMsg := context.CreateTextMessage()
142+
txtMsg.SetText(msgBody)
143+
144+
propName := "myProperty"
145+
propValue := "myValue"
146+
147+
// Test the empty value before the property is set.
148+
assert.Nil(t, txtMsg.GetStringProperty(propName))
149+
propExists, propErr := txtMsg.PropertyExists(propName)
150+
assert.Nil(t, propErr)
151+
assert.False(t, propExists)
152+
allPropNames, getNamesErr := txtMsg.GetPropertyNames()
153+
assert.Nil(t, getNamesErr)
154+
assert.Equal(t, 0, len(allPropNames))
155+
156+
// Test the ability to set properties before the message is sent.
157+
retErr := txtMsg.SetStringProperty(propName, &propValue)
158+
assert.Nil(t, retErr)
159+
assert.Equal(t, propValue, *txtMsg.GetStringProperty(propName))
160+
propExists, propErr = txtMsg.PropertyExists(propName)
161+
assert.Nil(t, propErr)
162+
assert.True(t, propExists) // now exists
163+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
164+
assert.Nil(t, getNamesErr)
165+
assert.Equal(t, 1, len(allPropNames))
166+
assert.Equal(t, propName, allPropNames[0])
167+
168+
propName2 := "myPropertyTwo"
169+
propValue2 := "myValueTwo"
170+
retErr = txtMsg.SetStringProperty(propName2, &propValue2)
171+
assert.Nil(t, retErr)
172+
assert.Equal(t, propValue2, *txtMsg.GetStringProperty(propName2))
173+
propExists, propErr = txtMsg.PropertyExists(propName2)
174+
assert.Nil(t, propErr)
175+
assert.True(t, propExists) // now exists
176+
// Check the first property again to be sure
177+
propExists, propErr = txtMsg.PropertyExists(propName)
178+
assert.Nil(t, propErr)
179+
assert.True(t, propExists) // now exists
180+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
181+
assert.Nil(t, getNamesErr)
182+
assert.Equal(t, 2, len(allPropNames))
183+
assert.Equal(t, propName, allPropNames[0])
184+
assert.Equal(t, propName2, allPropNames[1])
185+
186+
// Set a property then try to unset it by setting to nil
187+
unsetPropName := "mySendThenRemovedString"
188+
unsetPropValue := "someValueThatWillBeOverwritten"
189+
retErr = txtMsg.SetStringProperty(unsetPropName, &unsetPropValue)
190+
assert.Nil(t, retErr)
191+
assert.Equal(t, unsetPropValue, *txtMsg.GetStringProperty(unsetPropName))
192+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
193+
assert.Nil(t, propErr)
194+
assert.True(t, propExists)
195+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
196+
assert.Nil(t, getNamesErr)
197+
assert.Equal(t, 3, len(allPropNames))
198+
retErr = txtMsg.SetStringProperty(unsetPropName, nil)
199+
assert.Nil(t, retErr)
200+
assert.Nil(t, txtMsg.GetStringProperty(unsetPropName))
201+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
202+
assert.Nil(t, propErr)
203+
assert.False(t, propExists)
204+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
205+
assert.Nil(t, getNamesErr)
206+
assert.Equal(t, 2, len(allPropNames))
207+
208+
// Set up objects for send/receive
209+
queue := context.CreateQueue("DEV.QUEUE.1")
210+
consumer, errCons := context.CreateConsumer(queue)
211+
if consumer != nil {
212+
defer consumer.Close()
213+
}
214+
assert.Nil(t, errCons)
215+
216+
// Now send the message and get it back again, to check that it roundtripped.
217+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, txtMsg)
218+
assert.Nil(t, errSend)
219+
220+
rcvMsg, errRvc := consumer.ReceiveNoWait()
221+
assert.Nil(t, errRvc)
222+
assert.NotNil(t, rcvMsg)
223+
224+
switch msg := rcvMsg.(type) {
225+
case jms20subset.TextMessage:
226+
assert.Equal(t, msgBody, *msg.GetText())
227+
default:
228+
assert.Fail(t, "Got something other than a text message")
229+
}
230+
231+
// Check property is available on received message.
232+
propExists, propErr = rcvMsg.PropertyExists(propName)
233+
assert.Nil(t, propErr)
234+
assert.True(t, propExists) // now exists
235+
236+
propExists, propErr = rcvMsg.PropertyExists(propName2)
237+
assert.Nil(t, propErr)
238+
assert.True(t, propExists) // now exists
239+
240+
// Check GetPropertyNames
241+
allPropNames, getNamesErr = rcvMsg.GetPropertyNames()
242+
assert.Nil(t, getNamesErr)
243+
assert.Equal(t, 2, len(allPropNames))
244+
assert.Equal(t, propName, allPropNames[0])
245+
assert.Equal(t, propName2, allPropNames[1])
246+
247+
// Properties that are not set should return nil
248+
nonExistentPropName := "nonExistentProperty"
249+
assert.Nil(t, rcvMsg.GetStringProperty(nonExistentPropName))
250+
propExists, propErr = rcvMsg.PropertyExists(nonExistentPropName)
251+
assert.Nil(t, propErr)
252+
assert.False(t, propExists)
253+
254+
// Check for the unset property
255+
propExists, propErr = rcvMsg.PropertyExists(unsetPropName)
256+
assert.Nil(t, propErr)
257+
assert.False(t, propExists)
258+
259+
}
260+
261+
/*
262+
* Test the ClearProperties function for message properties
263+
*/
264+
func TestPropertyClearProperties(t *testing.T) {
265+
266+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
267+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
268+
assert.Nil(t, cfErr)
269+
270+
// Creates a connection to the queue manager, using defer to close it automatically
271+
// at the end of the function (if it was created successfully)
272+
context, ctxErr := cf.CreateContext()
273+
assert.Nil(t, ctxErr)
274+
if context != nil {
275+
defer context.Close()
276+
}
277+
278+
// Create a TextMessage and check that we can populate it
279+
msgBody := "ExistsClearProperties-test"
280+
txtMsg := context.CreateTextMessage()
281+
txtMsg.SetText(msgBody)
282+
283+
propName := "myProperty"
284+
propValue := "myValue"
285+
286+
// Test the ability to set properties before the message is sent.
287+
retErr := txtMsg.SetStringProperty(propName, &propValue)
288+
assert.Nil(t, retErr)
289+
assert.Equal(t, propValue, *txtMsg.GetStringProperty(propName))
290+
propExists, propErr := txtMsg.PropertyExists(propName)
291+
assert.Nil(t, propErr)
292+
assert.True(t, propExists) // now exists
293+
allPropNames, getNamesErr := txtMsg.GetPropertyNames()
294+
assert.Nil(t, getNamesErr)
295+
assert.Equal(t, 1, len(allPropNames))
296+
assert.Equal(t, propName, allPropNames[0])
297+
298+
clearErr := txtMsg.ClearProperties()
299+
assert.Nil(t, clearErr)
300+
assert.Nil(t, txtMsg.GetStringProperty(propName))
301+
propExists, propErr = txtMsg.PropertyExists(propName)
302+
assert.Nil(t, propErr)
303+
assert.False(t, propExists)
304+
305+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
306+
assert.Nil(t, getNamesErr)
307+
assert.Equal(t, 0, len(allPropNames))
308+
309+
propName2 := "myPropertyTwo"
310+
propValue2 := "myValueTwo"
311+
312+
// Set multiple properties
313+
retErr = txtMsg.SetStringProperty(propName, &propValue)
314+
assert.Nil(t, retErr)
315+
assert.Equal(t, propValue, *txtMsg.GetStringProperty(propName))
316+
retErr = txtMsg.SetStringProperty(propName2, &propValue2)
317+
assert.Nil(t, retErr)
318+
assert.Equal(t, propValue2, *txtMsg.GetStringProperty(propName2))
319+
propExists, propErr = txtMsg.PropertyExists(propName2)
320+
assert.Nil(t, propErr)
321+
assert.True(t, propExists) // now exists
322+
// Check the first property again to be sure
323+
propExists, propErr = txtMsg.PropertyExists(propName)
324+
assert.Nil(t, propErr)
325+
assert.True(t, propExists) // now exists
326+
327+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
328+
assert.Nil(t, getNamesErr)
329+
assert.Equal(t, 2, len(allPropNames))
330+
331+
// Set a property then try to unset it by setting to nil
332+
unsetPropName := "mySendThenRemovedString"
333+
unsetPropValue := "someValueThatWillBeOverwritten"
334+
retErr = txtMsg.SetStringProperty(unsetPropName, &unsetPropValue)
335+
assert.Nil(t, retErr)
336+
assert.Equal(t, unsetPropValue, *txtMsg.GetStringProperty(unsetPropName))
337+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
338+
assert.Nil(t, propErr)
339+
assert.True(t, propExists)
340+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
341+
assert.Nil(t, getNamesErr)
342+
assert.Equal(t, 3, len(allPropNames))
343+
assert.Equal(t, propName, allPropNames[0])
344+
assert.Equal(t, propName2, allPropNames[1])
345+
assert.Equal(t, unsetPropName, allPropNames[2])
346+
retErr = txtMsg.SetStringProperty(unsetPropName, nil)
347+
assert.Nil(t, retErr)
348+
assert.Nil(t, txtMsg.GetStringProperty(unsetPropName))
349+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
350+
assert.Nil(t, propErr)
351+
assert.False(t, propExists)
352+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
353+
assert.Nil(t, getNamesErr)
354+
assert.Equal(t, 2, len(allPropNames))
355+
assert.Equal(t, propName, allPropNames[0])
356+
assert.Equal(t, propName2, allPropNames[1])
357+
358+
clearErr = txtMsg.ClearProperties()
359+
assert.Nil(t, clearErr)
360+
assert.Nil(t, txtMsg.GetStringProperty(propName))
361+
propExists, propErr = txtMsg.PropertyExists(propName)
362+
assert.Nil(t, propErr)
363+
assert.False(t, propExists)
364+
allPropNames, getNamesErr = txtMsg.GetPropertyNames()
365+
assert.Nil(t, getNamesErr)
366+
assert.Equal(t, 0, len(allPropNames))
367+
368+
// Set up objects for send/receive
369+
queue := context.CreateQueue("DEV.QUEUE.1")
370+
consumer, errCons := context.CreateConsumer(queue)
371+
if consumer != nil {
372+
defer consumer.Close()
373+
}
374+
assert.Nil(t, errCons)
375+
376+
// Now send the message and get it back again, to check that it roundtripped.
377+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, txtMsg)
378+
assert.Nil(t, errSend)
379+
380+
rcvMsg, errRvc := consumer.ReceiveNoWait()
381+
assert.Nil(t, errRvc)
382+
assert.NotNil(t, rcvMsg)
383+
384+
switch msg := rcvMsg.(type) {
385+
case jms20subset.TextMessage:
386+
assert.Equal(t, msgBody, *msg.GetText())
387+
default:
388+
assert.Fail(t, "Got something other than a text message")
389+
}
390+
391+
// Check property is available on received message.
392+
propExists, propErr = rcvMsg.PropertyExists(propName)
393+
assert.Nil(t, propErr)
394+
assert.False(t, propExists)
395+
396+
propExists, propErr = rcvMsg.PropertyExists(propName2)
397+
assert.Nil(t, propErr)
398+
assert.False(t, propExists)
399+
400+
allPropNames, getNamesErr = rcvMsg.GetPropertyNames()
401+
assert.Nil(t, getNamesErr)
402+
assert.Equal(t, 0, len(allPropNames))
403+
404+
// Properties that are not set should return nil
405+
nonExistentPropName := "nonExistentProperty"
406+
assert.Nil(t, rcvMsg.GetStringProperty(nonExistentPropName))
407+
propExists, propErr = rcvMsg.PropertyExists(nonExistentPropName)
408+
assert.Nil(t, propErr)
409+
assert.False(t, propExists)
410+
411+
// Check for the unset property
412+
propExists, propErr = rcvMsg.PropertyExists(unsetPropName)
413+
assert.Nil(t, propErr)
414+
assert.False(t, propExists)
415+
416+
// Finally try clearing everything on the received message
417+
clearErr = rcvMsg.ClearProperties()
418+
assert.Nil(t, clearErr)
419+
assert.Nil(t, rcvMsg.GetStringProperty(propName))
420+
propExists, propErr = rcvMsg.PropertyExists(propName)
421+
assert.Nil(t, propErr)
422+
assert.False(t, propExists)
423+
allPropNames, getNamesErr = rcvMsg.GetPropertyNames()
424+
assert.Nil(t, getNamesErr)
425+
assert.Equal(t, 0, len(allPropNames))
426+
427+
}
428+
119429
/*
120430
* Test send and receive of a text message with a string property and no content.
121431
*/

0 commit comments

Comments
 (0)