Skip to content

Commit c848f77

Browse files
committed
Detect string properties that are not set - #39
1 parent b263de6 commit c848f77

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

jms20subset/Message.go

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

51-
SetStringProperty(name string, value string) JMSException
51+
SetStringProperty(name string, value *string) JMSException
52+
53+
// Returns string property, or nil if the property is not set.
5254
GetStringProperty(name string) *string
5355
}

messageproperties_test.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,31 @@ func TestStringPropertyTextMsg(t *testing.T) {
5656
propValue := "myValue"
5757

5858
// Test the empty value before the property is set.
59-
// TODO - it would be nicer if this was nil rather than empty string, however it
60-
// doesn't look like that is supported by the mq-golang library itself.
61-
assert.Equal(t, "", *txtMsg.GetStringProperty(propName))
59+
assert.Nil(t, txtMsg.GetStringProperty(propName))
6260

6361
// Test the ability to set properties before the message is sent.
64-
retErr := txtMsg.SetStringProperty(propName, propValue)
62+
retErr := txtMsg.SetStringProperty(propName, &propValue)
6563
assert.Nil(t, retErr)
6664
assert.Equal(t, propValue, *txtMsg.GetStringProperty(propName))
6765
assert.Equal(t, msgBody, *txtMsg.GetText())
6866

67+
// Send an empty string property as well
68+
emptyPropName := "myEmptyString"
69+
emptyPropValue := ""
70+
retErr = txtMsg.SetStringProperty(emptyPropName, &emptyPropValue)
71+
assert.Nil(t, retErr)
72+
assert.Equal(t, emptyPropValue, *txtMsg.GetStringProperty(emptyPropName))
73+
74+
// Set a property then try to unset it by setting to nil
75+
unsetPropName := "mySendThenRemovedString"
76+
unsetPropValue := "someValueThatWillBeOverwritten"
77+
retErr = txtMsg.SetStringProperty(unsetPropName, &unsetPropValue)
78+
assert.Nil(t, retErr)
79+
assert.Equal(t, unsetPropValue, *txtMsg.GetStringProperty(unsetPropName))
80+
retErr = txtMsg.SetStringProperty(unsetPropName, nil)
81+
assert.Nil(t, retErr)
82+
assert.Nil(t, txtMsg.GetStringProperty(unsetPropName))
83+
6984
// Set up objects for send/receive
7085
queue := context.CreateQueue("DEV.QUEUE.1")
7186
consumer, errCons := context.CreateConsumer(queue)
@@ -92,6 +107,13 @@ func TestStringPropertyTextMsg(t *testing.T) {
92107
// Check property is available on received message.
93108
assert.Equal(t, propValue, *rcvMsg.GetStringProperty(propName))
94109

110+
// Check the empty string property.
111+
assert.Equal(t, emptyPropValue, *rcvMsg.GetStringProperty(emptyPropName))
112+
113+
// Properties that are not set should return nil
114+
assert.Nil(t, rcvMsg.GetStringProperty("nonExistentProperty"))
115+
assert.Nil(t, rcvMsg.GetStringProperty(unsetPropName))
116+
95117
}
96118

97119
/*
@@ -117,7 +139,7 @@ func TestStringPropertyTextMessageNilBody(t *testing.T) {
117139

118140
propName := "myProperty2"
119141
propValue := "myValue2"
120-
retErr := msg.SetStringProperty(propName, propValue)
142+
retErr := msg.SetStringProperty(propName, &propValue)
121143
assert.Nil(t, retErr)
122144

123145
// Now send the message and get it back again, to check that it roundtripped.
@@ -172,12 +194,12 @@ func TestStringPropertyTextMessageEmptyBody(t *testing.T) {
172194

173195
propAName := "myPropertyA"
174196
propAValue := "myValueA"
175-
retErr := msg.SetStringProperty(propAName, propAValue)
197+
retErr := msg.SetStringProperty(propAName, &propAValue)
176198
assert.Nil(t, retErr)
177199

178200
propBName := "myPropertyB"
179201
propBValue := "myValueB"
180-
retErr = msg.SetStringProperty(propBName, propBValue)
202+
retErr = msg.SetStringProperty(propBName, &propBValue)
181203
assert.Nil(t, retErr)
182204

183205
// Now send the message and get it back again.

mqjms/MessageImpl.go

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,13 +274,26 @@ func (msg *MessageImpl) GetApplName() string {
274274
return applName
275275
}
276276

277-
func (msg *MessageImpl) SetStringProperty(name string, value string) jms20subset.JMSException {
277+
func (msg *MessageImpl) SetStringProperty(name string, value *string) jms20subset.JMSException {
278278
var retErr jms20subset.JMSException
279279

280-
smpo := ibmmq.NewMQSMPO()
281-
pd := ibmmq.NewMQPD()
280+
var linkedErr error
281+
282+
if value != nil {
283+
// Looking to set a value
284+
var valueStr string
285+
valueStr = *value
286+
287+
smpo := ibmmq.NewMQSMPO()
288+
pd := ibmmq.NewMQPD()
282289

283-
linkedErr := msg.msgHandle.SetMP(smpo, name, pd, value)
290+
linkedErr = msg.msgHandle.SetMP(smpo, name, pd, valueStr)
291+
} else {
292+
// Looking to unset a value
293+
dmpo := ibmmq.NewMQDMPO()
294+
295+
linkedErr = msg.msgHandle.DltMP(dmpo, name)
296+
}
284297

285298
if linkedErr != nil {
286299
rcInt := int(linkedErr.(*ibmmq.MQReturn).MQRC)
@@ -298,13 +311,59 @@ func (msg *MessageImpl) GetStringProperty(name string) *string {
298311
impo := ibmmq.NewMQIMPO()
299312
pd := ibmmq.NewMQPD()
300313

301-
_, value, _ := msg.msgHandle.InqMP(impo, pd, name)
314+
_, value, err := msg.msgHandle.InqMP(impo, pd, name)
302315

303-
switch valueTyped := value.(type) {
304-
case string:
305-
valueStr = valueTyped
306-
default:
307-
// TODO - other conversions
316+
if err == nil {
317+
switch valueTyped := value.(type) {
318+
case string:
319+
valueStr = valueTyped
320+
default:
321+
// TODO - other conversions
322+
}
323+
} else {
324+
325+
mqret := err.(*ibmmq.MQReturn)
326+
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
327+
// This indicates that the requested property does not exist.
328+
// valueStr will remain with its default value of nil
329+
return nil
330+
} else {
331+
// Err was not nil
332+
fmt.Println(err) // TODO - finish error handling
333+
}
308334
}
309335
return &valueStr
310336
}
337+
338+
func (msg *MessageImpl) propertyExists(name string) bool {
339+
340+
impo := ibmmq.NewMQIMPO()
341+
pd := ibmmq.NewMQPD()
342+
343+
impo.Options = ibmmq.MQIMPO_CONVERT_VALUE | ibmmq.MQIMPO_INQ_FIRST
344+
for propsToRead := true; propsToRead; {
345+
346+
gotName, gotValue, err := msg.msgHandle.InqMP(impo, pd, "%")
347+
impo.Options = ibmmq.MQIMPO_CONVERT_VALUE | ibmmq.MQIMPO_INQ_NEXT
348+
if err != nil {
349+
mqret := err.(*ibmmq.MQReturn)
350+
if mqret.MQRC != ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
351+
fmt.Println(err)
352+
} else {
353+
// Read all properties
354+
return false
355+
}
356+
357+
propsToRead = false
358+
} else if gotName == name {
359+
// Found the matching property name (shortcut)
360+
return true
361+
} else {
362+
fmt.Printf("no property match to '%s' - gotName: '%s' gotValue '%v' \n", name, gotName, gotValue)
363+
}
364+
365+
}
366+
367+
// Went through all properties and didn't find a match
368+
return false
369+
}

0 commit comments

Comments
 (0)