Skip to content

Commit b6f5f71

Browse files
committed
Final tidy up for message property support - #39
1 parent 0ba66a3 commit b6f5f71

File tree

4 files changed

+78
-57
lines changed

4 files changed

+78
-57
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ your own error handling or logging.
117117
* Send/receive a slice of bytes (BytesMessage) - [bytesmessage_test.go](bytesmessage_test.go)
118118
* Receive with wait [receivewithwait_test.go](receivewithwait_test.go)
119119
* Send a message as Persistent or NonPersistent - [deliverymode_test.go](deliverymode_test.go)
120+
* Set a message property of type string, int, double or boolean - [messageproperties_test.go](messageproperties_test.go)
120121
* Get by CorrelationID - [getbycorrelid_test.go](getbycorrelid_test.go)
121122
* Request/reply messaging pattern - [requestreply_test.go](requestreply_test.go)
122123
* Send and receive under a local transaction - [local_transaction_test.go](local_transaction_test.go)

jms20subset/Message.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type Message interface {
8484

8585
// GetPropertyNames returns a slice of strings containing the name of every message
8686
// property on this message.
87-
// Returns a zero length slice if no message properties are defined.
87+
// Returns a zero length slice if no message properties are set.
8888
GetPropertyNames() ([]string, JMSException)
8989

9090
// ClearProperties removes all message properties from this message.

messageproperties_test.go

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,10 @@ import (
1717
"github.com/stretchr/testify/assert"
1818
)
1919

20-
/*
21-
* mq-golang: SetMP, DltMP, InqMP
22-
* https://github.com/ibm-messaging/mq-golang/blob/95e9b8b09a1fc167747de7d066c49adb86e14dda/ibmmq/mqi.go#L1080
23-
*
24-
* mq-golang sample application to set properties
25-
* https://github.com/ibm-messaging/mq-golang/blob/master/samples/amqsprop.go#L49
26-
*
27-
* JMS: SetStringProperty, GetStringProperty,
28-
* https://github.com/eclipse-ee4j/messaging/blob/master/api/src/main/java/jakarta/jms/Message.java#L1119
29-
*
30-
* Property conversion between types
31-
*
32-
*/
33-
3420
/*
3521
* Test the creation of a text message with a string property.
3622
*/
37-
func TestStringPropertyTextMsg(t *testing.T) {
23+
func TestPropertyStringTextMsg(t *testing.T) {
3824

3925
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
4026
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -133,6 +119,13 @@ func TestStringPropertyTextMsg(t *testing.T) {
133119
assert.Nil(t, propErr)
134120
assert.Nil(t, gotPropValue)
135121

122+
// Error checking on property names
123+
emptyNameValue, emptyNameErr := rcvMsg.GetStringProperty("")
124+
assert.NotNil(t, emptyNameErr)
125+
assert.Equal(t, "2513", emptyNameErr.GetErrorCode())
126+
assert.Equal(t, "MQRC_PROPERTY_NAME_LENGTH_ERR", emptyNameErr.GetReason())
127+
assert.Nil(t, emptyNameValue)
128+
136129
}
137130

138131
/*
@@ -475,7 +468,7 @@ func TestPropertyClearProperties(t *testing.T) {
475468
/*
476469
* Test send and receive of a text message with a string property and no content.
477470
*/
478-
func TestStringPropertyTextMessageNilBody(t *testing.T) {
471+
func TestPropertyStringTextMessageNilBody(t *testing.T) {
479472

480473
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
481474
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -532,7 +525,7 @@ func TestStringPropertyTextMessageNilBody(t *testing.T) {
532525
* body. It's difficult to distinguish nil and empty string so we are expecting
533526
* that the received message will contain a nil body.
534527
*/
535-
func TestStringPropertyTextMessageEmptyBody(t *testing.T) {
528+
func TestPropertyStringTextMessageEmptyBody(t *testing.T) {
536529

537530
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
538531
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -599,7 +592,7 @@ func TestStringPropertyTextMessageEmptyBody(t *testing.T) {
599592
/*
600593
* Test the creation of a text message with an int property.
601594
*/
602-
func TestIntProperty(t *testing.T) {
595+
func TestPropertyInt(t *testing.T) {
603596

604597
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
605598
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -689,7 +682,7 @@ func TestIntProperty(t *testing.T) {
689682
gotPropValue, propErr = rcvMsg.GetIntProperty(propName)
690683
assert.Nil(t, propErr)
691684
assert.Equal(t, propValue, gotPropValue)
692-
propExists, propErr = txtMsg.PropertyExists(propName)
685+
propExists, propErr = rcvMsg.PropertyExists(propName)
693686
assert.Nil(t, propErr)
694687
assert.True(t, propExists) // now exists
695688

@@ -704,16 +697,23 @@ func TestIntProperty(t *testing.T) {
704697
gotPropValue, propErr = rcvMsg.GetIntProperty(unsetPropName)
705698
assert.Nil(t, propErr)
706699
assert.Equal(t, 0, gotPropValue)
707-
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
700+
propExists, propErr = rcvMsg.PropertyExists(unsetPropName)
708701
assert.Nil(t, propErr)
709702
assert.True(t, propExists) // exists, even though it is set to zero
710703

704+
// Error checking on property names
705+
emptyNameValue, emptyNameErr := rcvMsg.GetStringProperty("")
706+
assert.NotNil(t, emptyNameErr)
707+
assert.Equal(t, "2513", emptyNameErr.GetErrorCode())
708+
assert.Equal(t, "MQRC_PROPERTY_NAME_LENGTH_ERR", emptyNameErr.GetReason())
709+
assert.Nil(t, emptyNameValue)
710+
711711
}
712712

713713
/*
714714
* Test the creation of a text message with a double property.
715715
*/
716-
func TestDoubleProperty(t *testing.T) {
716+
func TestPropertyDouble(t *testing.T) {
717717

718718
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
719719
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -803,7 +803,7 @@ func TestDoubleProperty(t *testing.T) {
803803
gotPropValue, propErr = rcvMsg.GetDoubleProperty(propName)
804804
assert.Nil(t, propErr)
805805
assert.Equal(t, propValue, gotPropValue)
806-
propExists, propErr = txtMsg.PropertyExists(propName)
806+
propExists, propErr = rcvMsg.PropertyExists(propName)
807807
assert.Nil(t, propErr)
808808
assert.True(t, propExists) // now exists
809809

@@ -818,16 +818,23 @@ func TestDoubleProperty(t *testing.T) {
818818
gotPropValue, propErr = rcvMsg.GetDoubleProperty(unsetPropName)
819819
assert.Nil(t, propErr)
820820
assert.Equal(t, float64(0), gotPropValue)
821-
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
821+
propExists, propErr = rcvMsg.PropertyExists(unsetPropName)
822822
assert.Nil(t, propErr)
823823
assert.True(t, propExists) // exists, even though it is set to zero
824824

825+
// Error checking on property names
826+
emptyNameValue, emptyNameErr := rcvMsg.GetStringProperty("")
827+
assert.NotNil(t, emptyNameErr)
828+
assert.Equal(t, "2513", emptyNameErr.GetErrorCode())
829+
assert.Equal(t, "MQRC_PROPERTY_NAME_LENGTH_ERR", emptyNameErr.GetReason())
830+
assert.Nil(t, emptyNameValue)
831+
825832
}
826833

827834
/*
828835
* Test the creation of a text message with a boolean property.
829836
*/
830-
func TestBooleanProperty(t *testing.T) {
837+
func TestPropertyBoolean(t *testing.T) {
831838

832839
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
833840
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -917,7 +924,7 @@ func TestBooleanProperty(t *testing.T) {
917924
gotPropValue, propErr = rcvMsg.GetBooleanProperty(propName)
918925
assert.Nil(t, propErr)
919926
assert.Equal(t, propValue, gotPropValue)
920-
propExists, propErr = txtMsg.PropertyExists(propName)
927+
propExists, propErr = rcvMsg.PropertyExists(propName)
921928
assert.Nil(t, propErr)
922929
assert.True(t, propExists) // now exists
923930

@@ -932,10 +939,17 @@ func TestBooleanProperty(t *testing.T) {
932939
gotPropValue, propErr = rcvMsg.GetBooleanProperty(unsetPropName)
933940
assert.Nil(t, propErr)
934941
assert.Equal(t, false, gotPropValue)
935-
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
942+
propExists, propErr = rcvMsg.PropertyExists(unsetPropName)
936943
assert.Nil(t, propErr)
937944
assert.True(t, propExists) // exists, even though it is set to zero
938945

946+
// Error checking on property names
947+
emptyNameValue, emptyNameErr := rcvMsg.GetStringProperty("")
948+
assert.NotNil(t, emptyNameErr)
949+
assert.Equal(t, "2513", emptyNameErr.GetErrorCode())
950+
assert.Equal(t, "MQRC_PROPERTY_NAME_LENGTH_ERR", emptyNameErr.GetReason())
951+
assert.Nil(t, emptyNameValue)
952+
939953
}
940954

941955
/*
@@ -1092,7 +1106,7 @@ func TestPropertyBytesMsg(t *testing.T) {
10921106
/*
10931107
* Test the conversion between string message properties and other data types.
10941108
*/
1095-
func TestPropertyTypesStringConversion(t *testing.T) {
1109+
func TestPropertyConversionString(t *testing.T) {
10961110

10971111
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
10981112
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -1283,7 +1297,7 @@ func TestPropertyTypesStringConversion(t *testing.T) {
12831297
/*
12841298
* Test the conversion between different int message properties and other data types.
12851299
*/
1286-
func TestPropertyTypesIntConversion(t *testing.T) {
1300+
func TestPropertyConversionInt(t *testing.T) {
12871301

12881302
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
12891303
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -1421,7 +1435,7 @@ func TestPropertyTypesIntConversion(t *testing.T) {
14211435
/*
14221436
* Test the conversion between different int message properties and other data types.
14231437
*/
1424-
func TestPropertyTypesBoolConversion(t *testing.T) {
1438+
func TestPropertyConversionBool(t *testing.T) {
14251439

14261440
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
14271441
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
@@ -1499,7 +1513,7 @@ func TestPropertyTypesBoolConversion(t *testing.T) {
14991513
/*
15001514
* Test the conversion between different int message properties and other data types.
15011515
*/
1502-
func TestPropertyTypesDoubleConversion(t *testing.T) {
1516+
func TestPropertyConversionDouble(t *testing.T) {
15031517

15041518
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
15051519
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()

mqjms/MessageImpl.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323

2424
const MessageImpl_PROPERTY_CONVERT_FAILED_REASON string = "MQJMS_E_BAD_TYPE"
2525
const MessageImpl_PROPERTY_CONVERT_FAILED_CODE string = "1055"
26+
const MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_REASON string = "MQJMS_E_UNSUPPORTED_TYPE"
27+
const MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_CODE string = "1056 "
2628

2729
// MessageImpl contains the IBM MQ specific attributes that are
2830
// common to all types of message.
@@ -316,7 +318,7 @@ func (msg *MessageImpl) SetStringProperty(name string, value *string) jms20subse
316318
// Returns nil if the named property is not set.
317319
func (msg *MessageImpl) GetStringProperty(name string) (*string, jms20subset.JMSException) {
318320

319-
var valueStr string
321+
var valueStrPtr *string
320322
var retErr jms20subset.JMSException
321323

322324
impo := ibmmq.NewMQIMPO()
@@ -330,36 +332,42 @@ func (msg *MessageImpl) GetStringProperty(name string) (*string, jms20subset.JMS
330332

331333
switch valueTyped := value.(type) {
332334
case string:
333-
valueStr = valueTyped
335+
valueStrPtr = &valueTyped
334336
case int64:
335-
valueStr = strconv.FormatInt(valueTyped, 10)
337+
valueStr := strconv.FormatInt(valueTyped, 10)
338+
valueStrPtr = &valueStr
336339
if parseErr != nil {
337340
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
338341
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
339342
}
340343
case bool:
341-
valueStr = strconv.FormatBool(valueTyped)
344+
valueStr := strconv.FormatBool(valueTyped)
345+
valueStrPtr = &valueStr
342346
case float64:
343-
valueStr = fmt.Sprintf("%g", valueTyped)
347+
valueStr := fmt.Sprintf("%g", valueTyped)
348+
valueStrPtr = &valueStr
344349
default:
345-
// TODO - other conversions
350+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_REASON,
351+
MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_CODE, parseErr)
346352
}
347353
} else {
348354

349355
mqret := err.(*ibmmq.MQReturn)
350356
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
351357
// This indicates that the requested property does not exist.
352-
// valueStr will remain with its default value of nil
358+
// valueStr will remain with its default value
353359
return nil, nil
354360
} else {
355361
// Err was not nil
356362
rcInt := int(mqret.MQRC)
357363
errCode := strconv.Itoa(rcInt)
358364
reason := ibmmq.MQItoString("RC", rcInt)
359365
retErr = jms20subset.CreateJMSException(reason, errCode, mqret)
366+
367+
valueStrPtr = nil
360368
}
361369
}
362-
return &valueStr, retErr
370+
return valueStrPtr, retErr
363371
}
364372

365373
// SetIntProperty enables an application to set a int-type message property.
@@ -404,31 +412,29 @@ func (msg *MessageImpl) GetIntProperty(name string) (int, jms20subset.JMSExcepti
404412
valueRet = int(valueTyped)
405413
case string:
406414
valueRet, parseErr = strconv.Atoi(valueTyped)
407-
if parseErr != nil {
408-
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
409-
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
410-
}
411415
case bool:
412416
if valueTyped {
413417
valueRet = 1
414418
}
415419
case float64:
416420
s := fmt.Sprintf("%.0f", valueTyped)
417421
valueRet, parseErr = strconv.Atoi(s)
418-
if parseErr != nil {
419-
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
420-
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
421-
}
422422
default:
423-
// TODO - other conversions
424-
//fmt.Println("Other type", value, reflect.TypeOf(value))
423+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_REASON,
424+
MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_CODE, parseErr)
425425
}
426+
427+
if parseErr != nil {
428+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
429+
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
430+
}
431+
426432
} else {
427433

428434
mqret := err.(*ibmmq.MQReturn)
429435
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
430436
// This indicates that the requested property does not exist.
431-
// valueRet will remain with its default value of nil
437+
// valueRet will remain with its default value
432438
return 0, nil
433439
} else {
434440
// Err was not nil
@@ -494,15 +500,15 @@ func (msg *MessageImpl) GetDoubleProperty(name string) (float64, jms20subset.JMS
494500
valueRet = 1
495501
}
496502
default:
497-
// TODO - other conversions
498-
//fmt.Println("Other type", value, reflect.TypeOf(value))
503+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_REASON,
504+
MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_CODE, parseErr)
499505
}
500506
} else {
501507

502508
mqret := err.(*ibmmq.MQReturn)
503509
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
504510
// This indicates that the requested property does not exist.
505-
// valueRet will remain with its default value of nil
511+
// valueRet will remain with its default value
506512
return 0, nil
507513
} else {
508514
// Err was not nil
@@ -572,15 +578,15 @@ func (msg *MessageImpl) GetBooleanProperty(name string) (bool, jms20subset.JMSEx
572578
valueRet = true
573579
}
574580
default:
575-
// TODO - other conversions
576-
//fmt.Println("Other type", value, reflect.TypeOf(value))
581+
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_REASON,
582+
MessageImpl_PROPERTY_CONVERT_NOTSUPPORTED_CODE, parseErr)
577583
}
578584
} else {
579585

580586
mqret := err.(*ibmmq.MQReturn)
581587
if mqret.MQRC == ibmmq.MQRC_PROPERTY_NOT_AVAILABLE {
582588
// This indicates that the requested property does not exist.
583-
// valueRet will remain with its default value of nil
589+
// valueRet will remain with its default value
584590
return false, nil
585591
} else {
586592
// Err was not nil
@@ -603,7 +609,7 @@ func (msg *MessageImpl) PropertyExists(name string) (bool, jms20subset.JMSExcept
603609

604610
// GetPropertyNames returns a slice of strings containing the name of every message
605611
// property on this message.
606-
// Returns a zero length slice if no message properties are defined.
612+
// Returns a zero length slice if no message properties are set.
607613
func (msg *MessageImpl) GetPropertyNames() ([]string, jms20subset.JMSException) {
608614

609615
_, propNames, retErr := msg.getPropertiesInternal("")

0 commit comments

Comments
 (0)