Skip to content

Commit 0e09bbe

Browse files
committed
Support for boolean message properties - #39
1 parent 7973594 commit 0e09bbe

File tree

4 files changed

+337
-9
lines changed

4 files changed

+337
-9
lines changed

bytesmessage_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestBytesMessageNilBody(t *testing.T) {
8989
assert.Equal(t, 0, msg2.GetBodyLength())
9090
assert.Equal(t, []byte{}, *msg2.ReadBytes())
9191
default:
92-
assert.Fail(t, "Got something other than a text message")
92+
assert.Fail(t, "Got something other than a bytes message")
9393
}
9494

9595
}
@@ -138,7 +138,7 @@ func TestBytesMessageWithBody(t *testing.T) {
138138
assert.Equal(t, len(msgBody), msg2.GetBodyLength())
139139
assert.Equal(t, msgBody, *msg2.ReadBytes())
140140
default:
141-
assert.Fail(t, "Got something other than a text message")
141+
assert.Fail(t, "Got something other than a bytes message")
142142
}
143143

144144
}
@@ -186,7 +186,7 @@ func TestBytesMessageInitWithBytes(t *testing.T) {
186186
assert.Equal(t, len(msgBody), msg2.GetBodyLength())
187187
assert.Equal(t, msgBody, *msg2.ReadBytes())
188188
default:
189-
assert.Fail(t, "Got something other than a text message")
189+
assert.Fail(t, "Got something other than a bytes message")
190190
}
191191

192192
}
@@ -231,7 +231,7 @@ func TestBytesMessageProducerSendBytes(t *testing.T) {
231231
assert.Equal(t, 13, msg2.GetBodyLength())
232232
assert.Equal(t, msgBody, *msg2.ReadBytes())
233233
default:
234-
assert.Fail(t, "Got something other than a text message")
234+
assert.Fail(t, "Got something other than a bytes message")
235235
}
236236

237237
}

jms20subset/Message.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ type Message interface {
7272
// Returns 0 if the named property is not set.
7373
GetDoubleProperty(name string) (float64, JMSException)
7474

75+
// SetBooleanProperty enables an application to set a bool-type message property.
76+
SetBooleanProperty(name string, value bool) JMSException
77+
78+
// GetBooleanProperty returns the bool value of a named message property.
79+
// Returns false if the named property is not set.
80+
GetBooleanProperty(name string) (bool, JMSException)
81+
7582
// PropertyExists returns true if the named message property exists on this message.
7683
PropertyExists(name string) (bool, JMSException)
7784

messageproperties_test.go

Lines changed: 267 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ import (
2727
* JMS: SetStringProperty, GetStringProperty,
2828
* https://github.com/eclipse-ee4j/messaging/blob/master/api/src/main/java/jakarta/jms/Message.java#L1119
2929
*
30-
* Double
31-
* Boolean
32-
*
33-
* BytesMessage
30+
* Property conversion between types
3431
*
3532
*/
3633

@@ -714,7 +711,7 @@ func TestIntProperty(t *testing.T) {
714711
}
715712

716713
/*
717-
* Test the creation of a text message with an int property.
714+
* Test the creation of a text message with a double property.
718715
*/
719716
func TestDoubleProperty(t *testing.T) {
720717

@@ -826,3 +823,268 @@ func TestDoubleProperty(t *testing.T) {
826823
assert.True(t, propExists) // exists, even though it is set to zero
827824

828825
}
826+
827+
/*
828+
* Test the creation of a text message with a boolean property.
829+
*/
830+
func TestBooleanProperty(t *testing.T) {
831+
832+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
833+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
834+
assert.Nil(t, cfErr)
835+
836+
// Creates a connection to the queue manager, using defer to close it automatically
837+
// at the end of the function (if it was created successfully)
838+
context, ctxErr := cf.CreateContext()
839+
assert.Nil(t, ctxErr)
840+
if context != nil {
841+
defer context.Close()
842+
}
843+
844+
// Create a TextMessage and check that we can populate it
845+
msgBody := "BooleanPropertyRequestMsg"
846+
txtMsg := context.CreateTextMessage()
847+
txtMsg.SetText(msgBody)
848+
849+
propName := "myProperty"
850+
propValue := true
851+
852+
// Test the empty value before the property is set.
853+
gotPropValue, propErr := txtMsg.GetBooleanProperty(propName)
854+
assert.Nil(t, propErr)
855+
assert.Equal(t, false, gotPropValue)
856+
propExists, propErr := txtMsg.PropertyExists(propName)
857+
assert.Nil(t, propErr)
858+
assert.False(t, propExists)
859+
860+
// Test the ability to set properties before the message is sent.
861+
retErr := txtMsg.SetBooleanProperty(propName, propValue)
862+
assert.Nil(t, retErr)
863+
gotPropValue, propErr = txtMsg.GetBooleanProperty(propName)
864+
assert.Nil(t, propErr)
865+
assert.Equal(t, propValue, gotPropValue)
866+
assert.Equal(t, msgBody, *txtMsg.GetText())
867+
propExists, propErr = txtMsg.PropertyExists(propName)
868+
assert.Nil(t, propErr)
869+
assert.True(t, propExists) // now exists
870+
871+
propName2 := "myProperty2"
872+
propValue2 := false
873+
retErr = txtMsg.SetBooleanProperty(propName2, propValue2)
874+
assert.Nil(t, retErr)
875+
gotPropValue, propErr = txtMsg.GetBooleanProperty(propName2)
876+
assert.Nil(t, propErr)
877+
assert.Equal(t, propValue2, gotPropValue)
878+
879+
// Set a property then try to "unset" it by setting to 0
880+
unsetPropName := "mySendThenRemovedString"
881+
unsetPropValue := true
882+
retErr = txtMsg.SetBooleanProperty(unsetPropName, unsetPropValue)
883+
assert.Nil(t, retErr)
884+
gotPropValue, propErr = txtMsg.GetBooleanProperty(unsetPropName)
885+
assert.Nil(t, propErr)
886+
assert.Equal(t, unsetPropValue, gotPropValue)
887+
retErr = txtMsg.SetBooleanProperty(unsetPropName, false)
888+
assert.Nil(t, retErr)
889+
gotPropValue, propErr = txtMsg.GetBooleanProperty(unsetPropName)
890+
assert.Nil(t, propErr)
891+
assert.Equal(t, false, gotPropValue)
892+
893+
// Set up objects for send/receive
894+
queue := context.CreateQueue("DEV.QUEUE.1")
895+
consumer, errCons := context.CreateConsumer(queue)
896+
if consumer != nil {
897+
defer consumer.Close()
898+
}
899+
assert.Nil(t, errCons)
900+
901+
// Now send the message and get it back again, to check that it roundtripped.
902+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, txtMsg)
903+
assert.Nil(t, errSend)
904+
905+
rcvMsg, errRvc := consumer.ReceiveNoWait()
906+
assert.Nil(t, errRvc)
907+
assert.NotNil(t, rcvMsg)
908+
909+
switch msg := rcvMsg.(type) {
910+
case jms20subset.TextMessage:
911+
assert.Equal(t, msgBody, *msg.GetText())
912+
default:
913+
assert.Fail(t, "Got something other than a text message")
914+
}
915+
916+
// Check property is available on received message.
917+
gotPropValue, propErr = rcvMsg.GetBooleanProperty(propName)
918+
assert.Nil(t, propErr)
919+
assert.Equal(t, propValue, gotPropValue)
920+
propExists, propErr = txtMsg.PropertyExists(propName)
921+
assert.Nil(t, propErr)
922+
assert.True(t, propExists) // now exists
923+
924+
gotPropValue, propErr = rcvMsg.GetBooleanProperty(propName2)
925+
assert.Nil(t, propErr)
926+
assert.Equal(t, propValue2, gotPropValue)
927+
928+
// Properties that are not set should return nil
929+
gotPropValue, propErr = rcvMsg.GetBooleanProperty("nonExistentProperty")
930+
assert.Nil(t, propErr)
931+
assert.Equal(t, false, gotPropValue)
932+
gotPropValue, propErr = rcvMsg.GetBooleanProperty(unsetPropName)
933+
assert.Nil(t, propErr)
934+
assert.Equal(t, false, gotPropValue)
935+
propExists, propErr = txtMsg.PropertyExists(unsetPropName)
936+
assert.Nil(t, propErr)
937+
assert.True(t, propExists) // exists, even though it is set to zero
938+
939+
}
940+
941+
/*
942+
* Test the creation of a bytes message with message properties.
943+
*/
944+
func TestPropertyBytesMsg(t *testing.T) {
945+
946+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
947+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
948+
assert.Nil(t, cfErr)
949+
950+
// Creates a connection to the queue manager, using defer to close it automatically
951+
// at the end of the function (if it was created successfully)
952+
context, ctxErr := cf.CreateContext()
953+
assert.Nil(t, ctxErr)
954+
if context != nil {
955+
defer context.Close()
956+
}
957+
958+
// Create a BytesMessage
959+
msgBody := []byte{'b', 'y', 't', 'e', 's', 'p', 'r', 'o', 'p', 'e', 'r', 't', 'i', 'e', 's'}
960+
bytesMsg := context.CreateBytesMessage()
961+
bytesMsg.WriteBytes(msgBody)
962+
assert.Equal(t, 15, bytesMsg.GetBodyLength())
963+
assert.Equal(t, msgBody, *bytesMsg.ReadBytes())
964+
965+
stringPropName := "myProperty"
966+
stringPropValue := "myValue"
967+
968+
// Test the empty value before the property is set.
969+
gotPropValue, propErr := bytesMsg.GetStringProperty(stringPropName)
970+
assert.Nil(t, propErr)
971+
assert.Nil(t, gotPropValue)
972+
973+
// Test the ability to set properties before the message is sent.
974+
retErr := bytesMsg.SetStringProperty(stringPropName, &stringPropValue)
975+
assert.Nil(t, retErr)
976+
gotPropValue, propErr = bytesMsg.GetStringProperty(stringPropName)
977+
assert.Nil(t, propErr)
978+
assert.Equal(t, stringPropValue, *gotPropValue)
979+
980+
// Send an empty string property as well
981+
emptyPropName := "myEmptyString"
982+
emptyPropValue := ""
983+
retErr = bytesMsg.SetStringProperty(emptyPropName, &emptyPropValue)
984+
assert.Nil(t, retErr)
985+
gotPropValue, propErr = bytesMsg.GetStringProperty(emptyPropName)
986+
assert.Nil(t, propErr)
987+
assert.Equal(t, emptyPropValue, *gotPropValue)
988+
989+
// Now an int property
990+
intPropName := "myIntProperty"
991+
intPropValue := 553786
992+
retErr = bytesMsg.SetIntProperty(intPropName, intPropValue)
993+
assert.Nil(t, retErr)
994+
gotIntPropValue, propErr := bytesMsg.GetIntProperty(intPropName)
995+
assert.Nil(t, propErr)
996+
assert.Equal(t, intPropValue, gotIntPropValue)
997+
998+
// Now a double property
999+
doublePropName := "myDoubleProperty"
1000+
doublePropValue := float64(3.1415926535)
1001+
retErr = bytesMsg.SetDoubleProperty(doublePropName, doublePropValue)
1002+
assert.Nil(t, retErr)
1003+
gotDoublePropValue, propErr := bytesMsg.GetDoubleProperty(doublePropName)
1004+
assert.Nil(t, propErr)
1005+
assert.Equal(t, doublePropValue, gotDoublePropValue)
1006+
1007+
// Now a bool property
1008+
boolPropName := "myBoolProperty"
1009+
boolPropValue := true
1010+
retErr = bytesMsg.SetBooleanProperty(boolPropName, boolPropValue)
1011+
assert.Nil(t, retErr)
1012+
gotBoolPropValue, propErr := bytesMsg.GetBooleanProperty(boolPropName)
1013+
assert.Nil(t, propErr)
1014+
assert.Equal(t, boolPropValue, gotBoolPropValue)
1015+
1016+
// Set up objects for send/receive
1017+
queue := context.CreateQueue("DEV.QUEUE.1")
1018+
consumer, errCons := context.CreateConsumer(queue)
1019+
if consumer != nil {
1020+
defer consumer.Close()
1021+
}
1022+
assert.Nil(t, errCons)
1023+
1024+
// Now send the message and get it back again, to check that it roundtripped.
1025+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, bytesMsg)
1026+
assert.Nil(t, errSend)
1027+
1028+
rcvMsg, errRvc := consumer.ReceiveNoWait()
1029+
assert.Nil(t, errRvc)
1030+
assert.NotNil(t, rcvMsg)
1031+
1032+
switch msg2 := rcvMsg.(type) {
1033+
case jms20subset.BytesMessage:
1034+
assert.Equal(t, len(msgBody), msg2.GetBodyLength())
1035+
assert.Equal(t, msgBody, *msg2.ReadBytes())
1036+
default:
1037+
assert.Fail(t, "Got something other than a bytes message")
1038+
}
1039+
1040+
// Check property is available on received message.
1041+
propExists, propErr := rcvMsg.PropertyExists(stringPropName)
1042+
assert.Nil(t, propErr)
1043+
assert.True(t, propExists)
1044+
gotPropValue, propErr = rcvMsg.GetStringProperty(stringPropName)
1045+
assert.Nil(t, propErr)
1046+
assert.Equal(t, stringPropValue, *gotPropValue)
1047+
1048+
// Check the empty string property.
1049+
propExists, propErr = rcvMsg.PropertyExists(emptyPropName)
1050+
assert.Nil(t, propErr)
1051+
assert.True(t, propExists)
1052+
gotPropValue, propErr = rcvMsg.GetStringProperty(emptyPropName)
1053+
assert.Nil(t, propErr)
1054+
assert.Equal(t, emptyPropValue, *gotPropValue)
1055+
1056+
// Properties that are not set should return nil
1057+
nonExistPropName := "nonExistentProperty"
1058+
propExists, propErr = rcvMsg.PropertyExists(nonExistPropName)
1059+
assert.Nil(t, propErr)
1060+
assert.False(t, propExists)
1061+
gotPropValue, propErr = rcvMsg.GetStringProperty(nonExistPropName)
1062+
assert.Nil(t, propErr)
1063+
assert.Nil(t, gotPropValue)
1064+
1065+
propExists, propErr = rcvMsg.PropertyExists(intPropName)
1066+
assert.Nil(t, propErr)
1067+
assert.True(t, propExists)
1068+
gotIntPropValue, propErr = rcvMsg.GetIntProperty(intPropName)
1069+
assert.Nil(t, propErr)
1070+
assert.Equal(t, intPropValue, gotIntPropValue)
1071+
1072+
propExists, propErr = rcvMsg.PropertyExists(doublePropName)
1073+
assert.Nil(t, propErr)
1074+
assert.True(t, propExists)
1075+
gotDoublePropValue, propErr = rcvMsg.GetDoubleProperty(doublePropName)
1076+
assert.Nil(t, propErr)
1077+
assert.Equal(t, doublePropValue, gotDoublePropValue)
1078+
1079+
propExists, propErr = rcvMsg.PropertyExists(boolPropName)
1080+
assert.Nil(t, propErr)
1081+
assert.True(t, propExists)
1082+
gotBoolPropValue, propErr = rcvMsg.GetBooleanProperty(boolPropName)
1083+
assert.Nil(t, propErr)
1084+
assert.Equal(t, boolPropValue, gotBoolPropValue)
1085+
1086+
allPropNames, getNamesErr := rcvMsg.GetPropertyNames()
1087+
assert.Nil(t, getNamesErr)
1088+
assert.Equal(t, 5, len(allPropNames))
1089+
1090+
}

0 commit comments

Comments
 (0)