Implement RocketMQMapMessage
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/ec4228ea Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/tree/ec4228ea Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/diff/ec4228ea Branch: refs/heads/jms-dev-1.1.0 Commit: ec4228ea44a7a0698dc72120ce1a806e2b529b77 Parents: 1b3bb98 Author: zhangke <zhangke_beij...@qq.com> Authored: Wed Feb 22 00:13:29 2017 +0800 Committer: zhangke <zhangke_beij...@qq.com> Committed: Wed Feb 22 00:13:29 2017 +0800 ---------------------------------------------------------------------- .../rocketmq/jms/msg/RocketMQMapMessage.java | 210 +++++++++++++++++++ .../rocketmq/jms/msg/RocketMQMessage.java | 24 +-- .../jms/support/DirectTypeConverter.java | 75 +++++++ .../jms/support/PrimitiveTypeConverter.java | 194 +++++++++++++++++ .../rocketmq/jms/support/TypeConverter.java | 71 ------- .../jms/msg/RocketMQMapMessageTest.java | 70 +++++++ .../jms/support/DirectTypeConverterTest.java | 52 +++++ .../jms/support/PrimitiveTypeConvertTest.java | 210 +++++++++++++++++++ .../rocketmq/jms/support/TypeConverterTest.java | 52 ----- 9 files changed, 823 insertions(+), 135 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMapMessage.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMapMessage.java b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMapMessage.java new file mode 100644 index 0000000..79351ee --- /dev/null +++ b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMapMessage.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.msg; + +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; +import javax.jms.JMSException; +import javax.jms.MapMessage; +import javax.jms.MessageNotWriteableException; +import org.apache.commons.lang.StringUtils; + +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Boolean; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Byte; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2ByteArray; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Char; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Double; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Float; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Int; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Long; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Short; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2String; + +/** + * Message can only be accessed by a thread at a time. + */ +public class RocketMQMapMessage extends RocketMQMessage implements MapMessage { + + private Map<String, Object> map; + + protected boolean readOnly; + + public RocketMQMapMessage(Map<String, Object> map) { + this.map = map; + } + + public RocketMQMapMessage() { + this.map = new HashMap(); + } + + @Override public boolean getBoolean(String name) throws JMSException { + checkName(name); + + return convert2Boolean(map.get(name)); + } + + private void checkName(String name) throws JMSException { + if (StringUtils.isBlank(name)) { + throw new JMSException("Name is required"); + } + } + + @Override public byte getByte(String name) throws JMSException { + checkName(name); + + return convert2Byte(map.get(name)); + } + + @Override public short getShort(String name) throws JMSException { + checkName(name); + + return convert2Short(map.get(name)); + } + + @Override public char getChar(String name) throws JMSException { + checkName(name); + + return convert2Char(map.get(name)); + } + + @Override public int getInt(String name) throws JMSException { + checkName(name); + + return convert2Int(map.get(name)); + } + + @Override public long getLong(String name) throws JMSException { + checkName(name); + + return convert2Long(map.get(name)); + } + + @Override public float getFloat(String name) throws JMSException { + checkName(name); + + return convert2Float(map.get(name)); + } + + @Override public double getDouble(String name) throws JMSException { + checkName(name); + + return convert2Double(map.get(name)); + } + + @Override public String getString(String name) throws JMSException { + checkName(name); + + return convert2String(map.get(name)); + } + + @Override public byte[] getBytes(String name) throws JMSException { + checkName(name); + + return convert2ByteArray(map.get(name)); + } + + @Override public Object getObject(String name) throws JMSException { + checkName(name); + + return map.get(name); + } + + @Override public Enumeration getMapNames() throws JMSException { + return Collections.enumeration(map.keySet()); + } + + @Override public void setBoolean(String name, boolean value) throws JMSException { + putProperty(name, value); + } + + private void putProperty(String name, Object obj) throws JMSException { + if (isReadOnly()) { + throw new MessageNotWriteableException("Message is not writable"); + } + + checkName(name); + + map.put(name, obj); + } + + @Override public void setByte(String name, byte value) throws JMSException { + putProperty(name, value); + } + + @Override public void setShort(String name, short value) throws JMSException { + putProperty(name, value); + } + + @Override public void setChar(String name, char value) throws JMSException { + putProperty(name, value); + } + + @Override public void setInt(String name, int value) throws JMSException { + putProperty(name, value); + } + + @Override public void setLong(String name, long value) throws JMSException { + putProperty(name, value); + } + + @Override public void setFloat(String name, float value) throws JMSException { + putProperty(name, value); + } + + @Override public void setDouble(String name, double value) throws JMSException { + putProperty(name, value); + } + + @Override public void setString(String name, String value) throws JMSException { + putProperty(name, value); + } + + @Override public void setBytes(String name, byte[] value) throws JMSException { + putProperty(name, value); + } + + @Override public void setBytes(String name, byte[] value, int offset, int length) throws JMSException { + putProperty(name, value); + } + + @Override public void setObject(String name, Object value) throws JMSException { + putProperty(name, value); + } + + @Override public boolean itemExists(String name) throws JMSException { + checkName(name); + + return map.containsKey(name); + } + + @Override public void clearBody() { + super.clearBody(); + this.map.clear(); + this.readOnly = false; + } + + protected boolean isReadOnly() { + return this.readOnly; + } + + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMessage.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMessage.java b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMessage.java index 29fe00b..0db4f5e 100644 --- a/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMessage.java +++ b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQMessage.java @@ -28,7 +28,7 @@ import javax.jms.MessageNotWriteableException; import org.apache.commons.lang.builder.ToStringBuilder; import org.apache.rocketmq.jms.Constant; import org.apache.rocketmq.jms.support.JmsHelper; -import org.apache.rocketmq.jms.support.TypeConverter; +import org.apache.rocketmq.jms.support.DirectTypeConverter; //todo: add unit test after finishing JMS Properties public class RocketMQMessage implements javax.jms.Message { @@ -41,7 +41,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public String getJMSMessageID() { - return TypeConverter.convert2String(headers.get(Constant.JMS_MESSAGE_ID)); + return DirectTypeConverter.convert2String(headers.get(Constant.JMS_MESSAGE_ID)); } /** @@ -61,7 +61,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public long getJMSTimestamp() { if (headers.containsKey(Constant.JMS_TIMESTAMP)) { - return TypeConverter.convert2Long(headers.get(Constant.JMS_TIMESTAMP)); + return DirectTypeConverter.convert2Long(headers.get(Constant.JMS_TIMESTAMP)); } return 0; } @@ -94,7 +94,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public String getJMSCorrelationID() { if (headers.containsKey(Constant.JMS_CORRELATION_ID)) { - return TypeConverter.convert2String(headers.get(Constant.JMS_CORRELATION_ID)); + return DirectTypeConverter.convert2String(headers.get(Constant.JMS_CORRELATION_ID)); } return null; } @@ -107,7 +107,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public Destination getJMSReplyTo() { if (headers.containsKey(Constant.JMS_REPLY_TO)) { - return TypeConverter.convert2Object(headers.get(Constant.JMS_REPLY_TO), Destination.class); + return DirectTypeConverter.convert2Object(headers.get(Constant.JMS_REPLY_TO), Destination.class); } return null; } @@ -125,7 +125,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public Destination getJMSDestination() { if (headers.containsKey(Constant.JMS_DESTINATION)) { - return TypeConverter.convert2Object(headers.get(Constant.JMS_DESTINATION), Destination.class); + return DirectTypeConverter.convert2Object(headers.get(Constant.JMS_DESTINATION), Destination.class); } return null; } @@ -138,7 +138,7 @@ public class RocketMQMessage implements javax.jms.Message { @SuppressWarnings("unchecked") public <T> T getBody(Class<T> clazz) throws JMSException { if (clazz.isInstance(body)) { - return TypeConverter.convert2Object(body, clazz); + return DirectTypeConverter.convert2Object(body, clazz); } else { throw new IllegalArgumentException("The class " + clazz @@ -149,7 +149,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public int getJMSDeliveryMode() { if (headers.containsKey(Constant.JMS_DELIVERY_MODE)) { - return TypeConverter.convert2Integer(headers.get(Constant.JMS_DELIVERY_MODE)); + return DirectTypeConverter.convert2Integer(headers.get(Constant.JMS_DELIVERY_MODE)); } return 0; } @@ -173,7 +173,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public boolean getJMSRedelivered() { return headers.containsKey(Constant.JMS_REDELIVERED) - && TypeConverter.convert2Boolean(headers.get(Constant.JMS_REDELIVERED)); + && DirectTypeConverter.convert2Boolean(headers.get(Constant.JMS_REDELIVERED)); } @Override @@ -183,7 +183,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public String getJMSType() { - return TypeConverter.convert2String(headers.get(Constant.JMS_TYPE)); + return DirectTypeConverter.convert2String(headers.get(Constant.JMS_TYPE)); } @Override @@ -198,7 +198,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public long getJMSExpiration() { if (headers.containsKey(Constant.JMS_EXPIRATION)) { - return TypeConverter.convert2Long(headers.get(Constant.JMS_EXPIRATION)); + return DirectTypeConverter.convert2Long(headers.get(Constant.JMS_EXPIRATION)); } return 0; } @@ -215,7 +215,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public int getJMSPriority() { if (headers.containsKey(Constant.JMS_PRIORITY)) { - return TypeConverter.convert2Integer(headers.get(Constant.JMS_PRIORITY)); + return DirectTypeConverter.convert2Integer(headers.get(Constant.JMS_PRIORITY)); } return 5; } http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/main/java/org/apache/rocketmq/jms/support/DirectTypeConverter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/support/DirectTypeConverter.java b/core/src/main/java/org/apache/rocketmq/jms/support/DirectTypeConverter.java new file mode 100644 index 0000000..eb4c967 --- /dev/null +++ b/core/src/main/java/org/apache/rocketmq/jms/support/DirectTypeConverter.java @@ -0,0 +1,75 @@ +/* + * 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; + +/** + * Converter that convert object directly, which means Integer can only be + * converted to Integer,rather than Integer and Long. + */ +public class DirectTypeConverter { + + public static String convert2String(Object obj) { + if (obj == null) { + return null; + } + if (String.class.isInstance(obj)) { + return (String) obj; + } + throw new ClassCastException("To converted object is " + obj.getClass() + ", not String.class"); + } + + public static Long convert2Long(Object obj) { + if (obj == null) { + return null; + } + if (Long.class.isInstance(obj)) { + return (Long) obj; + } + throw new ClassCastException("To converted object is " + obj.getClass() + ", not Long.class"); + } + + public static Integer convert2Integer(Object obj) { + if (obj == null) { + return null; + } + if (Integer.class.isInstance(obj)) { + return (Integer) obj; + } + throw new ClassCastException("To converted object is " + obj.getClass() + ", not Integer.class"); + } + + public static Boolean convert2Boolean(Object obj) { + if (obj == null) { + return null; + } + if (Boolean.class.isInstance(obj)) { + return (Boolean) obj; + } + throw new ClassCastException("To converted object is " + obj.getClass() + ", not Boolean.class"); + } + + public static <T> T convert2Object(Object obj, Class<T> target) { + if (obj == null) { + return null; + } + if (target.isInstance(obj)) { + return (T) obj; + } + throw new ClassCastException("To converted object is " + obj.getClass() + ", not " + target.getSimpleName() + ".class"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/main/java/org/apache/rocketmq/jms/support/PrimitiveTypeConverter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/support/PrimitiveTypeConverter.java b/core/src/main/java/org/apache/rocketmq/jms/support/PrimitiveTypeConverter.java new file mode 100644 index 0000000..6d51eee --- /dev/null +++ b/core/src/main/java/org/apache/rocketmq/jms/support/PrimitiveTypeConverter.java @@ -0,0 +1,194 @@ +/* + * 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 javax.jms.JMSException; +import javax.jms.MapMessage; + +/** + * Primitive type converter, according to the conversion table in {@link MapMessage}. + */ +public class PrimitiveTypeConverter { + + public static boolean convert2Boolean(Object obj) throws JMSException { + if (obj == null) { + return Boolean.valueOf(null); + } + + if (Boolean.class.isInstance(obj)) { + return (Boolean) obj; + } + if (String.class.isInstance(obj)) { + return Boolean.valueOf((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static byte convert2Byte(Object obj) throws JMSException { + if (obj == null) { + return Byte.valueOf(null); + } + + if (Byte.class.isInstance(obj)) { + return (Byte) obj; + } + if (String.class.isInstance(obj)) { + return Byte.valueOf((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static short convert2Short(Object obj) throws JMSException { + if (obj == null) { + return Short.valueOf(null); + } + + if (Byte.class.isInstance(obj)) { + return ((Byte) obj).shortValue(); + } + if (Short.class.isInstance(obj)) { + return (Short) obj; + } + if (String.class.isInstance(obj)) { + return Short.valueOf((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static char convert2Char(Object obj) throws JMSException { + if (obj == null) { + throw new NullPointerException("Obj is required"); + } + + if (Character.class.isInstance(obj)) { + return (Character) obj; + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static int convert2Int(Object obj) throws JMSException { + if (obj == null) { + return Integer.valueOf(null); + } + + if (Byte.class.isInstance(obj)) { + return ((Byte) obj).intValue(); + } + if (Short.class.isInstance(obj)) { + return ((Short) obj).intValue(); + } + if (Integer.class.isInstance(obj)) { + return (Integer) obj; + } + if (String.class.isInstance(obj)) { + return Integer.parseInt((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static long convert2Long(Object obj) throws JMSException { + if (obj == null) { + return Long.valueOf(null); + } + + if (Byte.class.isInstance(obj)) { + return ((Byte) obj).longValue(); + } + if (Short.class.isInstance(obj)) { + return ((Short) obj).longValue(); + } + if (Integer.class.isInstance(obj)) { + return ((Integer) obj).longValue(); + } + if (Long.class.isInstance(obj)) { + return (Long) obj; + } + if (String.class.isInstance(obj)) { + return Long.parseLong((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static float convert2Float(Object obj) throws JMSException { + if (obj == null) { + return Float.valueOf(null); + } + + if (Float.class.isInstance(obj)) { + return (Float) obj; + } + if (String.class.isInstance(obj)) { + return Float.parseFloat((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static double convert2Double(Object obj) throws JMSException { + if (obj == null) { + return Double.valueOf(null); + } + + if (Float.class.isInstance(obj)) { + return ((Float) obj).doubleValue(); + } + if (Double.class.isInstance(obj)) { + return (Double) obj; + } + if (String.class.isInstance(obj)) { + return Double.parseDouble((String) obj); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static String convert2String(Object obj) throws JMSException { + if (obj == null) { + return String.valueOf(null); + } + + if (Boolean.class.isInstance(obj) + || Byte.class.isInstance(obj) + || Short.class.isInstance(obj) + || Character.class.isInstance(obj) + || Integer.class.isInstance(obj) + || Long.class.isInstance(obj) + || Float.class.isInstance(obj) + || Double.class.isInstance(obj) + || String.class.isInstance(obj) + ) { + return obj.toString(); + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } + + public static byte[] convert2ByteArray(Object obj) throws JMSException { + if (obj instanceof byte[]) { + return (byte[]) obj; + } + + throw new JMSException("Incorrect type[" + obj.getClass() + "] to convert"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/main/java/org/apache/rocketmq/jms/support/TypeConverter.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/support/TypeConverter.java b/core/src/main/java/org/apache/rocketmq/jms/support/TypeConverter.java deleted file mode 100644 index 388e580..0000000 --- a/core/src/main/java/org/apache/rocketmq/jms/support/TypeConverter.java +++ /dev/null @@ -1,71 +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; - -public class TypeConverter { - - public static String convert2String(Object obj) { - if (obj == null) { - return null; - } - if (String.class.isInstance(obj)) { - return (String) obj; - } - throw new ClassCastException("To converted object is " + obj.getClass() + ", not String.class"); - } - - public static Long convert2Long(Object obj) { - if (obj == null) { - return null; - } - if (Long.class.isInstance(obj)) { - return (Long) obj; - } - throw new ClassCastException("To converted object is " + obj.getClass() + ", not Long.class"); - } - - public static Integer convert2Integer(Object obj) { - if (obj == null) { - return null; - } - if (Integer.class.isInstance(obj)) { - return (Integer) obj; - } - throw new ClassCastException("To converted object is " + obj.getClass() + ", not Integer.class"); - } - - public static Boolean convert2Boolean(Object obj) { - if (obj == null) { - return null; - } - if (Boolean.class.isInstance(obj)) { - return (Boolean) obj; - } - throw new ClassCastException("To converted object is " + obj.getClass() + ", not Boolean.class"); - } - - public static <T> T convert2Object(Object obj, Class<T> target) { - if (obj == null) { - return null; - } - if (target.isInstance(obj)) { - return (T) obj; - } - throw new ClassCastException("To converted object is " + obj.getClass() + ", not " + target.getSimpleName() + ".class"); - } -} http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQMapMessageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQMapMessageTest.java b/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQMapMessageTest.java new file mode 100644 index 0000000..2a7597f --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQMapMessageTest.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 RocketMQMapMessageTest { + + @Test + public void testGetBoolean() throws Exception { + RocketMQMapMessage msg = new RocketMQMapMessage(); + + // 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/ec4228ea/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 new file mode 100644 index 0000000..93b0d03 --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/DirectTypeConverterTest.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 DirectTypeConverterTest { + + @Test + public void testConvert2String() throws Exception { + assertThat(DirectTypeConverter.convert2String("name"), is("name")); + } + + @Test + public void testConvert2Long() throws Exception { + assertThat(DirectTypeConverter.convert2Long(100l), is(100l)); + } + + @Test + public void testConvert2Integer() throws Exception { + assertThat(DirectTypeConverter.convert2Integer(100), is(100)); + } + + @Test + public void testConvert2Boolean() throws Exception { + assertThat(DirectTypeConverter.convert2Boolean(true), is(true)); + } + + @Test + public void testConvert2Object() throws Exception { + final DirectTypeConverter obj = new DirectTypeConverter(); + assertThat(DirectTypeConverter.convert2Object(obj, DirectTypeConverter.class), is(obj)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/ec4228ea/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 new file mode 100644 index 0000000..c6a3aad --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/PrimitiveTypeConvertTest.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.PrimitiveTypeConverter.convert2Boolean; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Byte; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2ByteArray; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Char; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Double; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Float; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Int; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Long; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2Short; +import static org.apache.rocketmq.jms.support.PrimitiveTypeConverter.convert2String; +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(convert2Boolean(new Boolean(true)), is(true)); + assertThat(convert2Boolean(null), is(false)); + + assertThat(convert2Boolean("true"), is(true)); + assertThat(convert2Boolean("hello"), is(false)); + + try { + convert2Boolean(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Byte() throws Exception { + final byte b = Byte.parseByte("101", 2); + assertThat(convert2Byte(b), is(b)); + + assertThat(convert2Byte(new String("5")), is(b)); + try { + assertThat(convert2Byte(null), is(b)); + assertTrue(false); + } + catch (RuntimeException e) { + assertTrue(true); + } + + try { + convert2Byte("abc"); + assertTrue(false); + } + catch (RuntimeException e) { + assertTrue(true); + } + + try { + convert2Byte(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Short() throws Exception { + final Short s = new Short("12"); + assertThat(convert2Short(s), is(s)); + + assertThat(convert2Short("3"), is(new Short("3"))); + + try { + convert2Short(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Char() throws Exception { + final char c = 'a'; + assertThat(convert2Char(c), is(c)); + + try { + convert2Char("a"); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Int() throws Exception { + assertThat(convert2Int(12), is(12)); + + assertThat(convert2Int("12"), is(12)); + assertThat(convert2Int(Byte.parseByte("11", 2)), is(3)); + + try { + convert2Int(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Long() throws Exception { + assertThat(convert2Long(12), is(12l)); + + assertThat(convert2Long("12"), is(12l)); + + try { + convert2Int(new Date()); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Float() throws Exception { + assertThat(convert2Float(12.00f), is(12f)); + + assertThat(convert2Float("12.00"), is(12f)); + + try { + convert2Float(12); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2Double() throws Exception { + assertThat(convert2Double(12.00d), is(12d)); + + assertThat(convert2Double("12.00"), is(12d)); + assertThat(convert2Double(12.00f), is(12d)); + + try { + convert2Double(12); + assertTrue(false); + } + catch (JMSException e) { + assertTrue(true); + } + } + + @Test + public void testConvert2String() throws Exception { + assertThat(convert2String(12.00d), is("12.0")); + + assertThat(convert2String("12.00"), is("12.00")); + assertThat(convert2String(true), is("true")); + + try { + convert2String(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(convert2ByteArray(arr), is(arr)); + + try { + convert2ByteArray("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/ec4228ea/core/src/test/java/org/apache/rocketmq/jms/support/TypeConverterTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/support/TypeConverterTest.java b/core/src/test/java/org/apache/rocketmq/jms/support/TypeConverterTest.java deleted file mode 100644 index 9a648c9..0000000 --- a/core/src/test/java/org/apache/rocketmq/jms/support/TypeConverterTest.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 TypeConverterTest { - - @Test - public void testConvert2String() throws Exception { - assertThat(TypeConverter.convert2String("name"), is("name")); - } - - @Test - public void testConvert2Long() throws Exception { - assertThat(TypeConverter.convert2Long(100l), is(100l)); - } - - @Test - public void testConvert2Integer() throws Exception { - assertThat(TypeConverter.convert2Integer(100), is(100)); - } - - @Test - public void testConvert2Boolean() throws Exception { - assertThat(TypeConverter.convert2Boolean(true), is(true)); - } - - @Test - public void testConvert2Object() throws Exception { - final TypeConverter obj = new TypeConverter(); - assertThat(TypeConverter.convert2Object(obj, TypeConverter.class), is(obj)); - } -} \ No newline at end of file