Add JMSUtilsTest
Project: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/commit/15f18ccf Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/tree/15f18ccf Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/diff/15f18ccf Branch: refs/heads/jms-dev-1.1.0 Commit: 15f18ccfc2959fc2a305ea38b6c31f289890a76d Parents: fb50854 Author: zhangke <zhangke_beij...@qq.com> Authored: Mon Feb 27 23:31:05 2017 +0800 Committer: zhangke <zhangke_beij...@qq.com> Committed: Mon Feb 27 23:31:05 2017 +0800 ---------------------------------------------------------------------- .../rocketmq/jms/DeliverMessageService.java | 4 +- .../rocketmq/jms/RocketMQConnectionFactory.java | 6 +- .../jms/msg/convert/JMS2RMQMessageConvert.java | 3 +- .../apache/rocketmq/jms/support/JMSUtils.java | 52 +++++ .../apache/rocketmq/jms/support/JmsUtils.java | 52 ----- .../rocketmq/jms/msg/JMSMapMessageTest.java | 70 +++++++ .../rocketmq/jms/msg/JmsMapMessageTest.java | 70 ------- .../jms/support/DirectTypeConverterTest.java | 52 ----- .../rocketmq/jms/support/JMSUtilsTest.java | 44 ++++ .../jms/support/ObjectTypeCastTest.java | 52 +++++ .../jms/support/PrimitiveTypeCastTest.java | 210 +++++++++++++++++++ .../jms/support/PrimitiveTypeConvertTest.java | 210 ------------------- 12 files changed, 435 insertions(+), 390 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/main/java/org/apache/rocketmq/jms/DeliverMessageService.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/DeliverMessageService.java b/core/src/main/java/org/apache/rocketmq/jms/DeliverMessageService.java index 0ff5c73..da37adf 100644 --- a/core/src/main/java/org/apache/rocketmq/jms/DeliverMessageService.java +++ b/core/src/main/java/org/apache/rocketmq/jms/DeliverMessageService.java @@ -39,7 +39,7 @@ import org.apache.rocketmq.common.ServiceThread; import org.apache.rocketmq.common.message.MessageExt; import org.apache.rocketmq.common.message.MessageQueue; import org.apache.rocketmq.jms.msg.convert.RMQ2JMSMessageConvert; -import org.apache.rocketmq.jms.support.JmsUtils; +import org.apache.rocketmq.jms.support.JMSUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,7 +81,7 @@ public class DeliverMessageService extends ServiceThread { this.destination = destination; this.consumerGroup = consumerGroup; - this.topicName = JmsUtils.getTopicName(destination); + this.topicName = JMSUtils.getDestinationName(destination); createAndStartRocketMQPullConsumer(); http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/main/java/org/apache/rocketmq/jms/RocketMQConnectionFactory.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/RocketMQConnectionFactory.java b/core/src/main/java/org/apache/rocketmq/jms/RocketMQConnectionFactory.java index 501a37d..aff3839 100644 --- a/core/src/main/java/org/apache/rocketmq/jms/RocketMQConnectionFactory.java +++ b/core/src/main/java/org/apache/rocketmq/jms/RocketMQConnectionFactory.java @@ -22,7 +22,7 @@ import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSContext; import javax.jms.JMSException; -import org.apache.rocketmq.jms.support.JmsUtils; +import org.apache.rocketmq.jms.support.JMSUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +51,7 @@ public class RocketMQConnectionFactory implements ConnectionFactory { public RocketMQConnectionFactory(String nameServerAddress) { this.nameServerAddress = nameServerAddress; - this.clientId = JmsUtils.uuid(); + this.clientId = JMSUtils.uuid(); } public RocketMQConnectionFactory(String nameServerAddress, String clientId) { @@ -79,7 +79,7 @@ public class RocketMQConnectionFactory implements ConnectionFactory { } private Connection createRocketMQConnection(String userName, String password) throws JMSException { - final String instanceName = JmsUtils.uuid(); + final String instanceName = JMSUtils.uuid(); RocketMQConnection connection = new RocketMQConnection(this.nameServerAddress, this.clientId, instanceName); log.info("Create a connection successfully[instanceName:{},clientIdentifier:{},userName:{}", instanceName, clientId, userName); http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/main/java/org/apache/rocketmq/jms/msg/convert/JMS2RMQMessageConvert.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/msg/convert/JMS2RMQMessageConvert.java b/core/src/main/java/org/apache/rocketmq/jms/msg/convert/JMS2RMQMessageConvert.java index 059fdde..7495171 100644 --- a/core/src/main/java/org/apache/rocketmq/jms/msg/convert/JMS2RMQMessageConvert.java +++ b/core/src/main/java/org/apache/rocketmq/jms/msg/convert/JMS2RMQMessageConvert.java @@ -21,6 +21,7 @@ import java.util.Map; import javax.jms.JMSException; import org.apache.rocketmq.common.message.MessageExt; import org.apache.rocketmq.jms.msg.AbstractJMSMessage; +import org.apache.rocketmq.jms.support.JMSUtils; import static org.apache.rocketmq.jms.msg.enums.JMSHeaderEnum.JMSExpiration; import static org.apache.rocketmq.jms.msg.enums.JMSHeaderEnum.JMSMessageID; @@ -42,7 +43,7 @@ public class JMS2RMQMessageConvert { } private static void handleHeader(AbstractJMSMessage jmsMsg, MessageExt rmqMsg) { - rmqMsg.setTopic(jmsMsg.getJMSDestination().toString()); + rmqMsg.setTopic(JMSUtils.getDestinationName(jmsMsg.getJMSDestination())); rmqMsg.putUserProperty(JMSMessageID.name(), jmsMsg.getJMSMessageID()); rmqMsg.setBornTimestamp(jmsMsg.getJMSTimestamp()); rmqMsg.putUserProperty(JMSExpiration.name(), String.valueOf(jmsMsg.getJMSExpiration())); http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/main/java/org/apache/rocketmq/jms/support/JMSUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/support/JMSUtils.java b/core/src/main/java/org/apache/rocketmq/jms/support/JMSUtils.java new file mode 100644 index 0000000..ba5431a --- /dev/null +++ b/core/src/main/java/org/apache/rocketmq/jms/support/JMSUtils.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.jms.support; + +import java.util.UUID; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.JMSRuntimeException; +import javax.jms.Queue; +import javax.jms.Topic; + +public class JMSUtils { + + public static String getDestinationName(Destination destination) { + try { + String topicName; + if (destination instanceof Topic) { + topicName = ((Topic) destination).getTopicName(); + } + else if (destination instanceof Queue) { + topicName = ((Queue) destination).getQueueName(); + } + else { + throw new JMSException(String.format("Unsupported Destination type:", destination.getClass())); + } + return topicName; + } + catch (JMSException e) { + throw new JMSRuntimeException(e.getMessage()); + } + } + + + public static String uuid() { + return UUID.randomUUID().toString(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/main/java/org/apache/rocketmq/jms/support/JmsUtils.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/support/JmsUtils.java b/core/src/main/java/org/apache/rocketmq/jms/support/JmsUtils.java deleted file mode 100644 index af56544..0000000 --- a/core/src/main/java/org/apache/rocketmq/jms/support/JmsUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.rocketmq.jms.support; - -import java.util.UUID; -import javax.jms.Destination; -import javax.jms.JMSException; -import javax.jms.JMSRuntimeException; -import javax.jms.Queue; -import javax.jms.Topic; - -public class JmsUtils { - - public static String getTopicName(Destination destination) { - try { - String topicName; - if (destination instanceof Topic) { - topicName = ((Topic) destination).getTopicName(); - } - else if (destination instanceof Queue) { - topicName = ((Queue) destination).getQueueName(); - } - else { - throw new JMSException(String.format("Unsupported Destination type:", destination.getClass())); - } - return topicName; - } - catch (JMSException e) { - throw new JMSRuntimeException(e.getMessage()); - } - } - - - public static String uuid() { - return UUID.randomUUID().toString(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/msg/JMSMapMessageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/msg/JMSMapMessageTest.java b/core/src/test/java/org/apache/rocketmq/jms/msg/JMSMapMessageTest.java new file mode 100644 index 0000000..cb34653 --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/msg/JMSMapMessageTest.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.jms.msg; + +import javax.jms.JMSException; +import javax.jms.MessageNotWriteableException; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class JMSMapMessageTest { + + @Test + public void testGetBoolean() throws Exception { + JMSMapMessage msg = new JMSMapMessage(); + + // get an empty value will return false + assertThat(msg.getBoolean("man"), is(false)); + + // get an not empty value + msg.setBoolean("man", true); + assertThat(msg.getBoolean("man"), is(true)); + + // key is null + try { + msg.getBoolean(null); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + + // in read-only model + msg.setReadOnly(true); + try { + msg.setBoolean("man", true); + assertTrue(false); + } + catch (MessageNotWriteableException e) { + assertTrue(true); + } + + // both read and write are allowed after clearBody() + msg.clearBody(); + msg.setBoolean("man", false); + msg.getBoolean("man"); + + // map is empty after clearBody() + msg.clearBody(); + assertThat(msg.getBoolean("man"), is(false)); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/msg/JmsMapMessageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/msg/JmsMapMessageTest.java b/core/src/test/java/org/apache/rocketmq/jms/msg/JmsMapMessageTest.java deleted file mode 100644 index 7befeb7..0000000 --- a/core/src/test/java/org/apache/rocketmq/jms/msg/JmsMapMessageTest.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.rocketmq.jms.msg; - -import javax.jms.JMSException; -import javax.jms.MessageNotWriteableException; -import org.junit.Test; - -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -public class JmsMapMessageTest { - - @Test - public void testGetBoolean() throws Exception { - JMSMapMessage msg = new JMSMapMessage(); - - // get an empty value will return false - assertThat(msg.getBoolean("man"), is(false)); - - // get an not empty value - msg.setBoolean("man", true); - assertThat(msg.getBoolean("man"), is(true)); - - // key is null - try { - msg.getBoolean(null); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - - // in read-only model - msg.setReadOnly(true); - try { - msg.setBoolean("man", true); - assertTrue(false); - } - catch (MessageNotWriteableException e) { - assertTrue(true); - } - - // both read and write are allowed after clearBody() - msg.clearBody(); - msg.setBoolean("man", false); - msg.getBoolean("man"); - - // map is empty after clearBody() - msg.clearBody(); - assertThat(msg.getBoolean("man"), is(false)); - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/support/DirectTypeConverterTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/DirectTypeConverterTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/DirectTypeConverterTest.java deleted file mode 100644 index 2ea70f0..0000000 --- a/core/src/test/java/org/apache/rocketmq/jms/support/DirectTypeConverterTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.rocketmq.jms.support; - -import org.junit.Test; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; - -public class DirectTypeConverterTest { - - @Test - public void testConvert2String() throws Exception { - assertThat(ObjectTypeCast.cast2String("name"), is("name")); - } - - @Test - public void testConvert2Long() throws Exception { - assertThat(ObjectTypeCast.cast2Long(100l), is(100l)); - } - - @Test - public void testConvert2Integer() throws Exception { - assertThat(ObjectTypeCast.cast2Integer(100), is(100)); - } - - @Test - public void testConvert2Boolean() throws Exception { - assertThat(ObjectTypeCast.cast2Boolean(true), is(true)); - } - - @Test - public void testConvert2Object() throws Exception { - final ObjectTypeCast obj = new ObjectTypeCast(); - assertThat(ObjectTypeCast.cast2Object(obj, ObjectTypeCast.class), is(obj)); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/support/JMSUtilsTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/JMSUtilsTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/JMSUtilsTest.java new file mode 100644 index 0000000..0be8445 --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/JMSUtilsTest.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.jms.support; + +import org.apache.rocketmq.jms.RocketMQQueue; +import org.apache.rocketmq.jms.RocketMQTopic; +import org.junit.Test; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.notNullValue; +import static org.junit.Assert.assertThat; + +public class JMSUtilsTest { + + @Test + public void getTopicName() throws Exception { + RocketMQTopic topic = new RocketMQTopic("topic"); + assertThat(JMSUtils.getDestinationName(topic), is("topic")); + + RocketMQQueue queue = new RocketMQQueue("queue"); + assertThat(JMSUtils.getDestinationName(queue), is("queue")); + } + + @Test + public void uuid() throws Exception { + assertThat(JMSUtils.uuid(), notNullValue()); + } + +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/support/ObjectTypeCastTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/ObjectTypeCastTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/ObjectTypeCastTest.java new file mode 100644 index 0000000..21fc50b --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/ObjectTypeCastTest.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.jms.support; + +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class ObjectTypeCastTest { + + @Test + public void testConvert2String() throws Exception { + assertThat(ObjectTypeCast.cast2String("name"), is("name")); + } + + @Test + public void testConvert2Long() throws Exception { + assertThat(ObjectTypeCast.cast2Long(100l), is(100l)); + } + + @Test + public void testConvert2Integer() throws Exception { + assertThat(ObjectTypeCast.cast2Integer(100), is(100)); + } + + @Test + public void testConvert2Boolean() throws Exception { + assertThat(ObjectTypeCast.cast2Boolean(true), is(true)); + } + + @Test + public void testConvert2Object() throws Exception { + final ObjectTypeCast obj = new ObjectTypeCast(); + assertThat(ObjectTypeCast.cast2Object(obj, ObjectTypeCast.class), is(obj)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeCastTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeCastTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeCastTest.java new file mode 100644 index 0000000..53ae0da --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeCastTest.java @@ -0,0 +1,210 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.jms.support; + +import java.util.Date; +import javax.jms.JMSException; +import org.junit.Test; + +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Boolean; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Byte; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2ByteArray; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Char; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Double; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Float; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Int; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Long; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Short; +import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2String; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +public class PrimitiveTypeCastTest { + + @Test + public void testConvert2Boolean() throws Exception { + assertThat(cast2Boolean(new Boolean(true)), is(true)); + assertThat(cast2Boolean(null), is(false)); + + assertThat(cast2Boolean("true"), is(true)); + assertThat(cast2Boolean("hello"), is(false)); + + try { + cast2Boolean(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Byte() throws Exception { + final byte b = Byte.parseByte("101", 2); + assertThat(cast2Byte(b), is(b)); + + assertThat(cast2Byte(new String("5")), is(b)); + try { + assertThat(cast2Byte(null), is(b)); + assertTrue(false); + } + catch (RuntimeException e) { + assertTrue(true); + } + + try { + cast2Byte("abc"); + assertTrue(false); + } + catch (RuntimeException e) { + assertTrue(true); + } + + try { + cast2Byte(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Short() throws Exception { + final Short s = new Short("12"); + assertThat(cast2Short(s), is(s)); + + assertThat(cast2Short("3"), is(new Short("3"))); + + try { + cast2Short(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Char() throws Exception { + final char c = 'a'; + assertThat(cast2Char(c), is(c)); + + try { + cast2Char("a"); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Int() throws Exception { + assertThat(cast2Int(12), is(12)); + + assertThat(cast2Int("12"), is(12)); + assertThat(cast2Int(Byte.parseByte("11", 2)), is(3)); + + try { + cast2Int(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Long() throws Exception { + assertThat(cast2Long(12), is(12l)); + + assertThat(cast2Long("12"), is(12l)); + + try { + cast2Int(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Float() throws Exception { + assertThat(cast2Float(12.00f), is(12f)); + + assertThat(cast2Float("12.00"), is(12f)); + + try { + cast2Float(12); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Double() throws Exception { + assertThat(cast2Double(12.00d), is(12d)); + + assertThat(cast2Double("12.00"), is(12d)); + assertThat(cast2Double(12.00f), is(12d)); + + try { + cast2Double(12); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2String() throws Exception { + assertThat(cast2String(12.00d), is("12.0")); + + assertThat(cast2String("12.00"), is("12.00")); + assertThat(cast2String(true), is("true")); + + try { + cast2String(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2ByteArray() throws Exception { + byte[] arr = new byte[] {Byte.parseByte("11", 2), Byte.parseByte("101", 2)}; + + assertThat(cast2ByteArray(arr), is(arr)); + + try { + cast2ByteArray("10"); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/15f18ccf/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeConvertTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeConvertTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeConvertTest.java deleted file mode 100644 index e297b46..0000000 --- a/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeConvertTest.java +++ /dev/null @@ -1,210 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.rocketmq.jms.support; - -import java.util.Date; -import javax.jms.JMSException; -import org.junit.Test; - -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Boolean; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Byte; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2ByteArray; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Char; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Double; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Float; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Int; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Long; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2Short; -import static org.apache.rocketmq.jms.support.PrimitiveTypeCast.cast2String; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -public class PrimitiveTypeConvertTest { - - @Test - public void testConvert2Boolean() throws Exception { - assertThat(cast2Boolean(new Boolean(true)), is(true)); - assertThat(cast2Boolean(null), is(false)); - - assertThat(cast2Boolean("true"), is(true)); - assertThat(cast2Boolean("hello"), is(false)); - - try { - cast2Boolean(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Byte() throws Exception { - final byte b = Byte.parseByte("101", 2); - assertThat(cast2Byte(b), is(b)); - - assertThat(cast2Byte(new String("5")), is(b)); - try { - assertThat(cast2Byte(null), is(b)); - assertTrue(false); - } - catch (RuntimeException e) { - assertTrue(true); - } - - try { - cast2Byte("abc"); - assertTrue(false); - } - catch (RuntimeException e) { - assertTrue(true); - } - - try { - cast2Byte(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Short() throws Exception { - final Short s = new Short("12"); - assertThat(cast2Short(s), is(s)); - - assertThat(cast2Short("3"), is(new Short("3"))); - - try { - cast2Short(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Char() throws Exception { - final char c = 'a'; - assertThat(cast2Char(c), is(c)); - - try { - cast2Char("a"); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Int() throws Exception { - assertThat(cast2Int(12), is(12)); - - assertThat(cast2Int("12"), is(12)); - assertThat(cast2Int(Byte.parseByte("11", 2)), is(3)); - - try { - cast2Int(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Long() throws Exception { - assertThat(cast2Long(12), is(12l)); - - assertThat(cast2Long("12"), is(12l)); - - try { - cast2Int(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Float() throws Exception { - assertThat(cast2Float(12.00f), is(12f)); - - assertThat(cast2Float("12.00"), is(12f)); - - try { - cast2Float(12); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2Double() throws Exception { - assertThat(cast2Double(12.00d), is(12d)); - - assertThat(cast2Double("12.00"), is(12d)); - assertThat(cast2Double(12.00f), is(12d)); - - try { - cast2Double(12); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2String() throws Exception { - assertThat(cast2String(12.00d), is("12.0")); - - assertThat(cast2String("12.00"), is("12.00")); - assertThat(cast2String(true), is("true")); - - try { - cast2String(new Date()); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } - - @Test - public void testConvert2ByteArray() throws Exception { - byte[] arr = new byte[] {Byte.parseByte("11", 2), Byte.parseByte("101", 2)}; - - assertThat(cast2ByteArray(arr), is(arr)); - - try { - cast2ByteArray("10"); - assertTrue(false); - } - catch (JMSException e) { - assertTrue(true); - } - } -} \ No newline at end of file