Skip to content

Commit 0ba66a3

Browse files
committed
Convert bool + double properties to other types - #39
1 parent 8ce7b38 commit 0ba66a3

File tree

2 files changed

+267
-0
lines changed

2 files changed

+267
-0
lines changed

messageproperties_test.go

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,3 +1417,246 @@ func TestPropertyTypesIntConversion(t *testing.T) {
14171417
assert.Equal(t, float64(0), gotDoubleUnsetPropValue)
14181418

14191419
}
1420+
1421+
/*
1422+
* Test the conversion between different int message properties and other data types.
1423+
*/
1424+
func TestPropertyTypesBoolConversion(t *testing.T) {
1425+
1426+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
1427+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
1428+
assert.Nil(t, cfErr)
1429+
1430+
// Creates a connection to the queue manager, using defer to close it automatically
1431+
// at the end of the function (if it was created successfully)
1432+
context, ctxErr := cf.CreateContext()
1433+
assert.Nil(t, ctxErr)
1434+
if context != nil {
1435+
defer context.Close()
1436+
}
1437+
1438+
msg := context.CreateTextMessage()
1439+
1440+
// Set up some different int properties
1441+
truePropName := "intOne"
1442+
trueValue := true
1443+
falsePropName := "intZero"
1444+
falseValue := false
1445+
1446+
msg.SetBooleanProperty(truePropName, trueValue)
1447+
msg.SetBooleanProperty(falsePropName, falseValue)
1448+
1449+
// Set up objects for send/receive
1450+
queue := context.CreateQueue("DEV.QUEUE.1")
1451+
consumer, errCons := context.CreateConsumer(queue)
1452+
if consumer != nil {
1453+
defer consumer.Close()
1454+
}
1455+
assert.Nil(t, errCons)
1456+
1457+
// Now send the message and get it back again, to check that it roundtripped.
1458+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, msg)
1459+
assert.Nil(t, errSend)
1460+
1461+
rcvMsg, errRvc := consumer.ReceiveNoWait()
1462+
assert.Nil(t, errRvc)
1463+
assert.NotNil(t, rcvMsg)
1464+
1465+
// Check bool properties were set correctly
1466+
gotTrueValue, gotTrueErr := rcvMsg.GetBooleanProperty(truePropName)
1467+
gotFalseValue, gotFalseErr := rcvMsg.GetBooleanProperty(falsePropName)
1468+
assert.Nil(t, gotTrueErr)
1469+
assert.Nil(t, gotFalseErr)
1470+
assert.Equal(t, trueValue, gotTrueValue)
1471+
assert.Equal(t, falseValue, gotFalseValue)
1472+
1473+
// Convert back as string
1474+
gotStrTrueValue, gotTrueErr := rcvMsg.GetStringProperty(truePropName)
1475+
gotStrFalseValue, gotFalseErr := rcvMsg.GetStringProperty(falsePropName)
1476+
assert.Nil(t, gotTrueErr)
1477+
assert.Nil(t, gotFalseErr)
1478+
assert.Equal(t, "true", *gotStrTrueValue)
1479+
assert.Equal(t, "false", *gotStrFalseValue)
1480+
1481+
// Convert back as int
1482+
gotIntTrueValue, gotTrueErr := rcvMsg.GetIntProperty(truePropName)
1483+
gotIntFalseValue, gotFalseErr := rcvMsg.GetIntProperty(falsePropName)
1484+
assert.Nil(t, gotTrueErr)
1485+
assert.Nil(t, gotFalseErr)
1486+
assert.Equal(t, 1, gotIntTrueValue)
1487+
assert.Equal(t, 0, gotIntFalseValue)
1488+
1489+
// Convert back as double
1490+
gotDoubleTrueValue, gotTrueErr := rcvMsg.GetDoubleProperty(truePropName)
1491+
gotDoubleFalseValue, gotFalseErr := rcvMsg.GetDoubleProperty(falsePropName)
1492+
assert.Nil(t, gotTrueErr)
1493+
assert.Nil(t, gotFalseErr)
1494+
assert.Equal(t, float64(1), gotDoubleTrueValue)
1495+
assert.Equal(t, float64(0), gotDoubleFalseValue)
1496+
1497+
}
1498+
1499+
/*
1500+
* Test the conversion between different int message properties and other data types.
1501+
*/
1502+
func TestPropertyTypesDoubleConversion(t *testing.T) {
1503+
1504+
// Loads CF parameters from connection_info.json and applicationApiKey.json in the Downloads directory
1505+
cf, cfErr := mqjms.CreateConnectionFactoryFromDefaultJSONFiles()
1506+
assert.Nil(t, cfErr)
1507+
1508+
// Creates a connection to the queue manager, using defer to close it automatically
1509+
// at the end of the function (if it was created successfully)
1510+
context, ctxErr := cf.CreateContext()
1511+
assert.Nil(t, ctxErr)
1512+
if context != nil {
1513+
defer context.Close()
1514+
}
1515+
1516+
msg := context.CreateTextMessage()
1517+
1518+
unsetPropName := "thisPropertyIsNotSet"
1519+
1520+
// Set up some different int properties
1521+
doubleOnePropName := "intOne"
1522+
doubleOneValue := float64(1)
1523+
doubleZeroPropName := "intZero"
1524+
doubleZeroValue := float64(0)
1525+
doubleMinusOnePropName := "intMinusOne"
1526+
doubleMinusOneValue := float64(-1)
1527+
1528+
doubleLargePosPropName := "largePositive"
1529+
doubleLargePosValue := float64(48632675)
1530+
doubleLargeNegPropName := "largeNegative"
1531+
doubleLargeNegValue := float64(-3789753467)
1532+
1533+
doubleLargeDecimalPropName := "largePositiveDecimal"
1534+
doubleLargeDecimalValue := float64(3867493.68473625)
1535+
doubleLargeNegativeDecimalPropName := "largeNegativeDecimal"
1536+
doubleLargeNegativeDecimalValue := float64(-87654335674.383656)
1537+
1538+
msg.SetDoubleProperty(doubleOnePropName, doubleOneValue)
1539+
msg.SetDoubleProperty(doubleZeroPropName, doubleZeroValue)
1540+
msg.SetDoubleProperty(doubleMinusOnePropName, doubleMinusOneValue)
1541+
msg.SetDoubleProperty(doubleLargePosPropName, doubleLargePosValue)
1542+
msg.SetDoubleProperty(doubleLargeNegPropName, doubleLargeNegValue)
1543+
msg.SetDoubleProperty(doubleLargeDecimalPropName, doubleLargeDecimalValue)
1544+
msg.SetDoubleProperty(doubleLargeNegativeDecimalPropName, doubleLargeNegativeDecimalValue)
1545+
1546+
// Set up objects for send/receive
1547+
queue := context.CreateQueue("DEV.QUEUE.1")
1548+
consumer, errCons := context.CreateConsumer(queue)
1549+
if consumer != nil {
1550+
defer consumer.Close()
1551+
}
1552+
assert.Nil(t, errCons)
1553+
1554+
// Now send the message and get it back again, to check that it roundtripped.
1555+
errSend := context.CreateProducer().SetTimeToLive(10000).Send(queue, msg)
1556+
assert.Nil(t, errSend)
1557+
1558+
rcvMsg, errRvc := consumer.ReceiveNoWait()
1559+
assert.Nil(t, errRvc)
1560+
assert.NotNil(t, rcvMsg)
1561+
1562+
// Check double properties were set correctly
1563+
gotDoubleOneValue, gotOneErr := rcvMsg.GetDoubleProperty(doubleOnePropName)
1564+
gotDoubleZeroValue, gotZeroErr := rcvMsg.GetDoubleProperty(doubleZeroPropName)
1565+
gotDoubleMinusOneValue, gotMinusOneErr := rcvMsg.GetDoubleProperty(doubleMinusOnePropName)
1566+
gotDoubleLargePosValue, gotLargePosErr := rcvMsg.GetDoubleProperty(doubleLargePosPropName)
1567+
gotDoubleLargeNegValue, gotLargeNegErr := rcvMsg.GetDoubleProperty(doubleLargeNegPropName)
1568+
gotDoubleLargePosDecimalValue, gotLargeDecPosErr := rcvMsg.GetDoubleProperty(doubleLargeDecimalPropName)
1569+
gotDoubleLargeNegDecimalValue, gotLargeDecNegErr := rcvMsg.GetDoubleProperty(doubleLargeNegativeDecimalPropName)
1570+
gotDoubleUnsetPropValue, gotUnsetErr := rcvMsg.GetDoubleProperty(unsetPropName)
1571+
assert.Nil(t, gotOneErr)
1572+
assert.Nil(t, gotZeroErr)
1573+
assert.Nil(t, gotMinusOneErr)
1574+
assert.Nil(t, gotLargePosErr)
1575+
assert.Nil(t, gotLargeNegErr)
1576+
assert.Nil(t, gotLargeDecPosErr)
1577+
assert.Nil(t, gotLargeDecNegErr)
1578+
assert.Nil(t, gotUnsetErr)
1579+
assert.Equal(t, float64(1), gotDoubleOneValue)
1580+
assert.Equal(t, float64(0), gotDoubleZeroValue)
1581+
assert.Equal(t, float64(-1), gotDoubleMinusOneValue)
1582+
assert.Equal(t, float64(48632675), gotDoubleLargePosValue)
1583+
assert.Equal(t, float64(-3789753467), gotDoubleLargeNegValue)
1584+
assert.Equal(t, float64(3867493.68473625), gotDoubleLargePosDecimalValue)
1585+
assert.Equal(t, float64(-87654335674.383656), gotDoubleLargeNegDecimalValue)
1586+
assert.Equal(t, float64(0), gotDoubleUnsetPropValue)
1587+
1588+
// Convert back as int
1589+
gotIntOneValue, gotOneErr := rcvMsg.GetIntProperty(doubleOnePropName)
1590+
gotIntZeroValue, gotZeroErr := rcvMsg.GetIntProperty(doubleZeroPropName)
1591+
gotIntMinusOneValue, gotMinusOneErr := rcvMsg.GetIntProperty(doubleMinusOnePropName)
1592+
gotIntLargePosValue, gotLargePosErr := rcvMsg.GetIntProperty(doubleLargePosPropName)
1593+
gotIntLargeNegValue, gotLargeNegErr := rcvMsg.GetIntProperty(doubleLargeNegPropName)
1594+
gotIntLargePosDecimalValue, gotLargeDecPosErr := rcvMsg.GetIntProperty(doubleLargeDecimalPropName)
1595+
gotIntLargeNegDecimalValue, gotLargeDecNegErr := rcvMsg.GetIntProperty(doubleLargeNegativeDecimalPropName)
1596+
gotIntUnsetPropValue, gotUnsetErr := rcvMsg.GetIntProperty(unsetPropName)
1597+
assert.Nil(t, gotOneErr)
1598+
assert.Nil(t, gotZeroErr)
1599+
assert.Nil(t, gotMinusOneErr)
1600+
assert.Nil(t, gotLargePosErr)
1601+
assert.Nil(t, gotLargeNegErr)
1602+
assert.Nil(t, gotLargeDecPosErr)
1603+
assert.Nil(t, gotLargeDecNegErr)
1604+
assert.Nil(t, gotUnsetErr)
1605+
assert.Equal(t, 1, gotIntOneValue)
1606+
assert.Equal(t, 0, gotIntZeroValue)
1607+
assert.Equal(t, -1, gotIntMinusOneValue)
1608+
assert.Equal(t, 48632675, gotIntLargePosValue)
1609+
assert.Equal(t, -3789753467, gotIntLargeNegValue)
1610+
assert.Equal(t, 3867494, gotIntLargePosDecimalValue)
1611+
assert.Equal(t, -87654335674, gotIntLargeNegDecimalValue)
1612+
assert.Equal(t, 0, gotIntUnsetPropValue)
1613+
1614+
// Convert back as string
1615+
gotStrOneValue, gotOneErr := rcvMsg.GetStringProperty(doubleOnePropName)
1616+
gotStrZeroValue, gotZeroErr := rcvMsg.GetStringProperty(doubleZeroPropName)
1617+
gotStrMinusOneValue, gotMinusOneErr := rcvMsg.GetStringProperty(doubleMinusOnePropName)
1618+
gotStrLargePosValue, gotLargePosErr := rcvMsg.GetStringProperty(doubleLargePosPropName)
1619+
gotStrLargeNegValue, gotLargeNegErr := rcvMsg.GetStringProperty(doubleLargeNegPropName)
1620+
gotStrLargePosDecimalValue, gotLargeDecPosErr := rcvMsg.GetStringProperty(doubleLargeDecimalPropName)
1621+
gotStrLargeNegDecimalValue, gotLargeDecNegErr := rcvMsg.GetStringProperty(doubleLargeNegativeDecimalPropName)
1622+
assert.Nil(t, gotOneErr)
1623+
assert.Nil(t, gotZeroErr)
1624+
assert.Nil(t, gotMinusOneErr)
1625+
assert.Nil(t, gotLargePosErr)
1626+
assert.Nil(t, gotLargeNegErr)
1627+
assert.Nil(t, gotLargeDecPosErr)
1628+
assert.Nil(t, gotLargeDecNegErr)
1629+
assert.Nil(t, gotUnsetErr)
1630+
assert.Equal(t, "1", *gotStrOneValue)
1631+
assert.Equal(t, "0", *gotStrZeroValue)
1632+
assert.Equal(t, "-1", *gotStrMinusOneValue)
1633+
assert.Equal(t, "4.8632675e+07", *gotStrLargePosValue)
1634+
assert.Equal(t, "-3.789753467e+09", *gotStrLargeNegValue)
1635+
assert.Equal(t, "3.86749368473625e+06", *gotStrLargePosDecimalValue)
1636+
assert.Equal(t, "-8.765433567438365e+10", *gotStrLargeNegDecimalValue)
1637+
1638+
// Convert back as bool
1639+
gotBoolOneValue, gotOneErr := rcvMsg.GetBooleanProperty(doubleOnePropName)
1640+
gotBoolZeroValue, gotZeroErr := rcvMsg.GetBooleanProperty(doubleZeroPropName)
1641+
gotBoolMinusOneValue, gotMinusOneErr := rcvMsg.GetBooleanProperty(doubleMinusOnePropName)
1642+
gotBoolLargePosValue, gotLargePosErr := rcvMsg.GetBooleanProperty(doubleLargePosPropName)
1643+
gotBoolLargeNegValue, gotLargeNegErr := rcvMsg.GetBooleanProperty(doubleLargeNegPropName)
1644+
gotBoolLargePosDecimalValue, gotLargeDecPosErr := rcvMsg.GetBooleanProperty(doubleLargeDecimalPropName)
1645+
gotBoolLargeNegDecimalValue, gotLargeDecNegErr := rcvMsg.GetBooleanProperty(doubleLargeNegativeDecimalPropName)
1646+
assert.Nil(t, gotOneErr)
1647+
assert.Nil(t, gotZeroErr)
1648+
assert.Nil(t, gotMinusOneErr)
1649+
assert.Nil(t, gotLargePosErr)
1650+
assert.Nil(t, gotLargeNegErr)
1651+
assert.Nil(t, gotLargeDecPosErr)
1652+
assert.Nil(t, gotLargeDecNegErr)
1653+
assert.Nil(t, gotUnsetErr)
1654+
assert.Equal(t, true, gotBoolOneValue)
1655+
assert.Equal(t, false, gotBoolZeroValue)
1656+
assert.Equal(t, false, gotBoolMinusOneValue)
1657+
assert.Equal(t, false, gotBoolLargePosValue)
1658+
assert.Equal(t, false, gotBoolLargeNegValue)
1659+
assert.Equal(t, false, gotBoolLargePosDecimalValue)
1660+
assert.Equal(t, false, gotBoolLargeNegDecimalValue)
1661+
1662+
}

mqjms/MessageImpl.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,10 @@ func (msg *MessageImpl) GetStringProperty(name string) (*string, jms20subset.JMS
337337
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
338338
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
339339
}
340+
case bool:
341+
valueStr = strconv.FormatBool(valueTyped)
342+
case float64:
343+
valueStr = fmt.Sprintf("%g", valueTyped)
340344
default:
341345
// TODO - other conversions
342346
}
@@ -404,6 +408,17 @@ func (msg *MessageImpl) GetIntProperty(name string) (int, jms20subset.JMSExcepti
404408
retErr = jms20subset.CreateJMSException(MessageImpl_PROPERTY_CONVERT_FAILED_REASON,
405409
MessageImpl_PROPERTY_CONVERT_FAILED_CODE, parseErr)
406410
}
411+
case bool:
412+
if valueTyped {
413+
valueRet = 1
414+
}
415+
case float64:
416+
s := fmt.Sprintf("%.0f", valueTyped)
417+
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+
}
407422
default:
408423
// TODO - other conversions
409424
//fmt.Println("Other type", value, reflect.TypeOf(value))
@@ -474,6 +489,10 @@ func (msg *MessageImpl) GetDoubleProperty(name string) (float64, jms20subset.JMS
474489
}
475490
case int64:
476491
valueRet = float64(valueTyped)
492+
case bool:
493+
if valueTyped {
494+
valueRet = 1
495+
}
477496
default:
478497
// TODO - other conversions
479498
//fmt.Println("Other type", value, reflect.TypeOf(value))
@@ -547,6 +566,11 @@ func (msg *MessageImpl) GetBooleanProperty(name string) (bool, jms20subset.JMSEx
547566
if valueTyped == 1 {
548567
valueRet = true
549568
}
569+
case float64:
570+
// Conversion from float64 to bool is true iff n=1
571+
if valueTyped == 1 {
572+
valueRet = true
573+
}
550574
default:
551575
// TODO - other conversions
552576
//fmt.Println("Other type", value, reflect.TypeOf(value))

0 commit comments

Comments
 (0)