Repository: incubator-rocketmq-externals Updated Branches: refs/heads/jms-dev-1.1.0 [created] 59d503c1f
Complete un-implement method in RocketMQByteMessage and add unit test 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/551d332f Tree: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/tree/551d332f Diff: http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/diff/551d332f Branch: refs/heads/jms-dev-1.1.0 Commit: 551d332f8cc2169c9a166c072d6588ba55e1107c Parents: 0ca963e Author: zhangke <[email protected]> Authored: Mon Feb 20 23:18:08 2017 +0800 Committer: zhangke <[email protected]> Committed: Mon Feb 20 23:18:08 2017 +0800 ---------------------------------------------------------------------- .../rocketmq/jms/msg/RocketMQBytesMessage.java | 280 ++++++++++++++++--- .../rocketmq/jms/msg/RocketMQMessage.java | 85 +++--- .../rocketmq/jms/support/TypeConverter.java | 71 +++++ .../rocketmq/jms/msg/BytesMessageTest.java | 103 ------- .../jms/msg/RocketMQBytesMessageTest.java | 107 +++++++ .../rocketmq/jms/support/TypeConverterTest.java | 52 ++++ 6 files changed, 523 insertions(+), 175 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/551d332f/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessage.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessage.java b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessage.java index cbac882..f172fc2 100644 --- a/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessage.java +++ b/core/src/main/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessage.java @@ -23,24 +23,26 @@ import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.EOFException; import java.io.IOException; +import javax.jms.IllegalStateRuntimeException; import javax.jms.JMSException; import javax.jms.MessageEOFException; import javax.jms.MessageFormatException; import javax.jms.MessageNotReadableException; import javax.jms.MessageNotWriteableException; - import org.apache.rocketmq.jms.support.JmsHelper; /** - * The <CODE>BytesMessage</CODE> methods are based largely on those found in <CODE>java.io.DataInputStream</CODE> and - * <CODE>java.io.DataOutputStream</CODE>. <P> Notice:Although the JMS API allows the use of message properties with byte - * messages, they are typically not used, since the inclusion of properties may affect the format. <P> + * RocketMQ ByteMessage. */ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.BytesMessage { + + private byte[] bytesIn; private DataInputStream dataAsInput; - private DataOutputStream dataAsOutput; + private ByteArrayOutputStream bytesOut; - private byte[] bytesIn; + private DataOutputStream dataAsOutput; + + protected boolean readOnly; /** * Message created for reading @@ -48,16 +50,20 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B * @param data */ public RocketMQBytesMessage(byte[] data) { - bytesIn = data; - dataAsInput = new DataInputStream(new ByteArrayInputStream(data, 0, data.length)); + this.bytesIn = data; + this.dataAsInput = new DataInputStream(new ByteArrayInputStream(data, 0, data.length)); + this.readOnly = true; + this.writeOnly = false; } /** * Message created to be sent */ public RocketMQBytesMessage() { - bytesOut = new ByteArrayOutputStream(); - dataAsOutput = new DataOutputStream(bytesOut); + this.bytesOut = new ByteArrayOutputStream(); + this.dataAsOutput = new DataOutputStream(this.bytesOut); + this.readOnly = false; + this.writeOnly = true; } public long getBodyLength() throws JMSException { @@ -68,64 +74,156 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B * @return the data */ public byte[] getData() { - if (bytesOut != null) { + if (isWriteOnly()) { return bytesOut.toByteArray(); } - else { + else if (isReadOnly()) { return bytesIn; } - + else { + throw new IllegalStateRuntimeException("Message must be in write only or read only status"); + } } public boolean readBoolean() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readBoolean(); + } + catch (IOException e) { + throw handleInputException(e); + } + } + + private void checkIsReadOnly() throws MessageNotReadableException { + if (!isReadOnly()) { + throw new MessageNotReadableException("Not readable"); + } + if (dataAsInput == null) { + throw new MessageNotReadableException("No data to read"); + } } public byte readByte() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readByte(); + } + catch (IOException e) { + throw handleInputException(e); + } } public int readUnsignedByte() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readUnsignedByte(); + } + catch (IOException e) { + throw handleInputException(e); + } } public short readShort() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readShort(); + } + catch (IOException e) { + throw handleInputException(e); + } } public int readUnsignedShort() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readUnsignedShort(); + } + catch (IOException e) { + throw handleInputException(e); + } } public char readChar() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readChar(); + } + catch (IOException e) { + throw handleInputException(e); + } } public int readInt() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readInt(); + } + catch (IOException e) { + throw handleInputException(e); + } } public long readLong() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readLong(); + } + catch (IOException e) { + throw handleInputException(e); + } } public float readFloat() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readFloat(); + } + catch (IOException e) { + throw handleInputException(e); + } } public double readDouble() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readDouble(); + } + catch (IOException e) { + throw handleInputException(e); + } } public String readUTF() throws JMSException { - throw new UnsupportedOperationException("Unsupported!"); + checkIsReadOnly(); + + try { + return dataAsInput.readUTF(); + } + catch (IOException e) { + throw handleInputException(e); + } } public int readBytes(byte[] value) throws JMSException { + checkIsReadOnly(); + return readBytes(value, value.length); } public int readBytes(byte[] value, int length) throws JMSException { + checkIsReadOnly(); + if (length > value.length) { throw new IndexOutOfBoundsException("length must be smaller than the length of value"); } @@ -156,42 +254,126 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B } public void writeBoolean(boolean value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeBoolean(value); + } + catch (IOException e) { + throw handleOutputException(e); + } + } + + private void initializeWriteIfNecessary() { + if (bytesOut == null) { + bytesOut = new ByteArrayOutputStream(); + } + if (dataAsOutput == null) { + dataAsOutput = new DataOutputStream(bytesOut); + } } public void writeByte(byte value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeByte(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeShort(short value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeShort(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeChar(char value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeChar(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeInt(int value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeInt(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeLong(long value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeLong(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeFloat(float value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeFloat(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeDouble(double value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeDouble(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeUTF(String value) throws JMSException { - JmsHelper.handleUnSupportedException(); + checkIsWriteOnly(); + initializeWriteIfNecessary(); + + try { + dataAsOutput.writeUTF(value); + } + catch (IOException e) { + throw handleOutputException(e); + } } public void writeBytes(byte[] value) throws JMSException { + checkIsWriteOnly(); + initializeWriteIfNecessary(); + if (dataAsOutput == null) { throw new MessageNotWriteableException("Message is not writable! "); } @@ -204,6 +386,9 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B } public void writeBytes(byte[] value, int offset, int length) throws JMSException { + checkIsWriteOnly(); + initializeWriteIfNecessary(); + if (dataAsOutput == null) { throw new MessageNotWriteableException("Message is not writable! "); } @@ -216,11 +401,34 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B } public void writeObject(Object value) throws JMSException { + checkIsWriteOnly(); + initializeWriteIfNecessary(); + JmsHelper.handleUnSupportedException(); } public void reset() throws JMSException { - JmsHelper.handleUnSupportedException(); + try { + if (bytesOut != null) { + bytesOut.reset(); + } + if (this.dataAsInput != null) { + this.dataAsInput.reset(); + } + + this.readOnly = true; + } + catch (IOException e) { + throw new JMSException(e.getMessage()); + } + } + + @Override public void clearBody() { + super.clearBody(); + this.bytesOut = null; + this.dataAsOutput = null; + this.dataAsInput = null; + this.bytesIn = null; } private JMSException handleOutputException(final IOException e) { @@ -242,4 +450,8 @@ public class RocketMQBytesMessage extends RocketMQMessage implements javax.jms.B ex.setLinkedException(e); return ex; } + + protected boolean isReadOnly() { + return readOnly; + } } http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/551d332f/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 5b138a1..474e646 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 @@ -19,33 +19,28 @@ package org.apache.rocketmq.jms.msg; import com.google.common.collect.Maps; import com.google.common.io.BaseEncoding; -import org.apache.commons.lang.builder.ToStringBuilder; -import org.apache.rocketmq.jms.Constant; -import org.apache.rocketmq.jms.support.JmsHelper; - -import javax.jms.Destination; -import javax.jms.JMSException; import java.io.Serializable; import java.util.Enumeration; import java.util.Map; +import javax.jms.Destination; +import javax.jms.JMSException; +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; public class RocketMQMessage implements javax.jms.Message { - /** - * Message properties - */ + protected Map<String, Object> properties = Maps.newHashMap(); - /** - * Message headers - */ protected Map<String, Object> headers = Maps.newHashMap(); - /** - * Message body - */ protected Serializable body; + protected boolean writeOnly; + @Override public String getJMSMessageID() { - return (String) headers.get(Constant.JMS_MESSAGE_ID); + return TypeConverter.convert2String(headers.get(Constant.JMS_MESSAGE_ID)); } /** @@ -65,7 +60,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public long getJMSTimestamp() { if (headers.containsKey(Constant.JMS_TIMESTAMP)) { - return (Long) headers.get(Constant.JMS_TIMESTAMP); + return TypeConverter.convert2Long(headers.get(Constant.JMS_TIMESTAMP)); } return 0; } @@ -81,7 +76,8 @@ public class RocketMQMessage implements javax.jms.Message { if (jmsCorrelationID != null) { try { return BaseEncoding.base64().decode(jmsCorrelationID); - } catch (Exception e) { + } + catch (Exception e) { return jmsCorrelationID.getBytes(); } } @@ -97,7 +93,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public String getJMSCorrelationID() { if (headers.containsKey(Constant.JMS_CORRELATION_ID)) { - return (String) headers.get(Constant.JMS_CORRELATION_ID); + return TypeConverter.convert2String(headers.get(Constant.JMS_CORRELATION_ID)); } return null; } @@ -110,7 +106,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public Destination getJMSReplyTo() { if (headers.containsKey(Constant.JMS_REPLY_TO)) { - return (Destination) headers.get(Constant.JMS_REPLY_TO); + return TypeConverter.convert2Object(headers.get(Constant.JMS_REPLY_TO), Destination.class); } return null; } @@ -128,7 +124,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public Destination getJMSDestination() { if (headers.containsKey(Constant.JMS_DESTINATION)) { - return (Destination) headers.get(Constant.JMS_DESTINATION); + return TypeConverter.convert2Object(headers.get(Constant.JMS_DESTINATION), Destination.class); } return null; } @@ -141,17 +137,18 @@ public class RocketMQMessage implements javax.jms.Message { @SuppressWarnings("unchecked") public <T> T getBody(Class<T> clazz) throws JMSException { if (clazz.isInstance(body)) { - return (T) body; - } else { + return TypeConverter.convert2Object(body, clazz); + } + else { throw new IllegalArgumentException("The class " + clazz - + " is unknown to this implementation"); + + " is unknown to this implementation"); } } @Override public int getJMSDeliveryMode() { if (headers.containsKey(Constant.JMS_DELIVERY_MODE)) { - return (Integer) headers.get(Constant.JMS_DELIVERY_MODE); + return TypeConverter.convert2Integer(headers.get(Constant.JMS_DELIVERY_MODE)); } return 0; } @@ -175,7 +172,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public boolean getJMSRedelivered() { return headers.containsKey(Constant.JMS_REDELIVERED) - && (Boolean) headers.get(Constant.JMS_REDELIVERED); + && TypeConverter.convert2Boolean(headers.get(Constant.JMS_REDELIVERED)); } @Override @@ -185,7 +182,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public String getJMSType() { - return (String) headers.get(Constant.JMS_TYPE); + return TypeConverter.convert2String(headers.get(Constant.JMS_TYPE)); } @Override @@ -200,7 +197,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public long getJMSExpiration() { if (headers.containsKey(Constant.JMS_EXPIRATION)) { - return (Long) headers.get(Constant.JMS_EXPIRATION); + return TypeConverter.convert2Long(headers.get(Constant.JMS_EXPIRATION)); } return 0; } @@ -217,7 +214,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public int getJMSPriority() { if (headers.containsKey(Constant.JMS_PRIORITY)) { - return (Integer) headers.get(Constant.JMS_PRIORITY); + return TypeConverter.convert2Integer(headers.get(Constant.JMS_PRIORITY)); } return 5; } @@ -252,6 +249,7 @@ public class RocketMQMessage implements javax.jms.Message { @Override public void clearBody() { this.body = null; + this.writeOnly = true; } @Override @@ -263,7 +261,7 @@ public class RocketMQMessage implements javax.jms.Message { public boolean getBooleanProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Boolean ? (Boolean) value : Boolean.valueOf(value.toString()); + return Boolean.valueOf(value.toString()); } return false; } @@ -272,7 +270,7 @@ public class RocketMQMessage implements javax.jms.Message { public byte getByteProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Byte ? (Byte) value : Byte.valueOf(value.toString()); + return Byte.valueOf(value.toString()); } return 0; } @@ -281,7 +279,7 @@ public class RocketMQMessage implements javax.jms.Message { public short getShortProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Short ? (Short) value : Short.valueOf(value.toString()); + return Short.valueOf(value.toString()); } return 0; } @@ -290,7 +288,7 @@ public class RocketMQMessage implements javax.jms.Message { public int getIntProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Integer ? (Integer) value : Integer.valueOf(value.toString()); + return Integer.valueOf(value.toString()); } return 0; } @@ -299,7 +297,7 @@ public class RocketMQMessage implements javax.jms.Message { public long getLongProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Long ? (Long) value : Long.valueOf(value.toString()); + return Long.valueOf(value.toString()); } return 0L; } @@ -308,7 +306,7 @@ public class RocketMQMessage implements javax.jms.Message { public float getFloatProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Float ? (Float) value : Float.valueOf(value.toString()); + return Float.valueOf(value.toString()); } return 0f; } @@ -317,7 +315,7 @@ public class RocketMQMessage implements javax.jms.Message { public double getDoubleProperty(String name) throws JMSException { if (propertyExists(name)) { Object value = getObjectProperty(name); - return value instanceof Double ? (Double) value : Double.valueOf(value.toString()); + return Double.valueOf(value.toString()); } return 0d; } @@ -412,9 +410,20 @@ public class RocketMQMessage implements javax.jms.Message { public void setObjectProperty(String name, Object value) { if (value instanceof Number || value instanceof String || value instanceof Boolean) { this.properties.put(name, value); - } else { + } + else { throw new IllegalArgumentException( - "Value should be boolean, byte, short, int, long, float, double, and String."); + "Value should be boolean, byte, short, int, long, float, double, and String."); + } + } + + protected boolean isWriteOnly() { + return writeOnly; + } + + protected void checkIsWriteOnly() throws MessageNotWriteableException { + if (!writeOnly) { + throw new MessageNotWriteableException("Not writable"); } } http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/551d332f/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 new file mode 100644 index 0000000..388e580 --- /dev/null +++ b/core/src/main/java/org/apache/rocketmq/jms/support/TypeConverter.java @@ -0,0 +1,71 @@ +/* + * 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/551d332f/core/src/test/java/org/apache/rocketmq/jms/msg/BytesMessageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/msg/BytesMessageTest.java b/core/src/test/java/org/apache/rocketmq/jms/msg/BytesMessageTest.java deleted file mode 100644 index 5adf987..0000000 --- a/core/src/test/java/org/apache/rocketmq/jms/msg/BytesMessageTest.java +++ /dev/null @@ -1,103 +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 org.junit.Assert; -import org.junit.Test; - -public class BytesMessageTest { - - private byte[] receiveData = "receive data test".getBytes(); - private byte[] sendData = "send data test".getBytes(); - - @Test - public void testGetData() throws Exception { - RocketMQBytesMessage readMessage = new RocketMQBytesMessage(receiveData); - - System.out.println(new String(readMessage.getData())); - Assert.assertEquals(new String(receiveData), new String(readMessage.getData())); - - RocketMQBytesMessage sendMessage = new RocketMQBytesMessage(); - sendMessage.writeBytes(sendData, 0, sendData.length); - - System.out.println(new String(sendMessage.getData())); - Assert.assertEquals(new String(sendData), new String(sendMessage.getData())); - - } - - @Test - public void testGetBodyLength() throws Exception { - - RocketMQBytesMessage jmsRocketMQBytesMessage = new RocketMQBytesMessage(receiveData); - - System.out.println(jmsRocketMQBytesMessage.getBodyLength()); - Assert.assertEquals(jmsRocketMQBytesMessage.getBodyLength(), receiveData.length); - } - - @Test - public void testReadBytes() throws Exception { - RocketMQBytesMessage jmsRocketMQBytesMessage = new RocketMQBytesMessage(receiveData); - - Assert.assertEquals(jmsRocketMQBytesMessage.getBodyLength(), receiveData.length); - byte[] receiveValue = new byte[receiveData.length]; - jmsRocketMQBytesMessage.readBytes(receiveValue); - - System.out.println(new String(receiveValue)); - Assert.assertEquals(new String(receiveValue), new String(receiveData)); - - } - - @Test - public void testReadBytes1() throws Exception { - RocketMQBytesMessage jmsRocketMQBytesMessage = new RocketMQBytesMessage(receiveData); - - byte[] receiveValue1 = new byte[2]; - jmsRocketMQBytesMessage.readBytes(receiveValue1, 2); - System.out.println(new String(receiveValue1)); - Assert.assertEquals(new String(receiveData).substring(0, 2), new String(receiveValue1)); - - byte[] receiceValue2 = new byte[2]; - jmsRocketMQBytesMessage.readBytes(receiceValue2, 2); - System.out.println(new String(receiceValue2)); - Assert.assertEquals(new String(receiveData).substring(2, 4), new String(receiceValue2)); - - } - - @Test - public void testWriteBytes() throws Exception { - RocketMQBytesMessage jmsJmsRocketMQBytesMessage = new RocketMQBytesMessage(); - jmsJmsRocketMQBytesMessage.writeBytes(sendData); - - System.out.println(new String(jmsJmsRocketMQBytesMessage.getData())); - Assert.assertEquals(new String(jmsJmsRocketMQBytesMessage.getData()), new String(sendData)); - - } - - @Test - public void testException() throws Exception { - RocketMQBytesMessage jmsJmsRocketMQBytesMessage = new RocketMQBytesMessage(); - - byte[] receiveValue = new byte[receiveData.length]; -// Throws out NullPointerException -// jmsRocketMQBytesMessage.readBytes(receiveValue); - - RocketMQBytesMessage sendMessage = new RocketMQBytesMessage(sendData); -// Throws out NullPointerException -// sendMessage.writeBytes("hello again".getBytes()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/551d332f/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessageTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessageTest.java b/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessageTest.java new file mode 100644 index 0000000..d641454 --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/msg/RocketMQBytesMessageTest.java @@ -0,0 +1,107 @@ +/* + * 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.MessageNotReadableException; +import javax.jms.MessageNotWriteableException; +import org.junit.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; + +public class RocketMQBytesMessageTest { + + private byte[] receiveData = "receive data test".getBytes(); + private byte[] sendData = "send data test".getBytes(); + + @Test + public void testGetData() throws Exception { + RocketMQBytesMessage readMessage = new RocketMQBytesMessage(receiveData); + assertThat(new String(receiveData), is(new String(readMessage.getData()))); + + RocketMQBytesMessage sendMessage = new RocketMQBytesMessage(); + sendMessage.writeBytes(sendData, 0, sendData.length); + assertThat(new String(sendData), is(new String(sendMessage.getData()))); + } + + @Test + public void testGetBodyLength() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + assertThat(msg.getBodyLength(), is(new Long(receiveData.length))); + } + + @Test + public void testReadBytes1() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + byte[] receiveValue = new byte[receiveData.length]; + msg.readBytes(receiveValue); + assertThat(new String(receiveValue), is(new String(receiveData))); + + } + + @Test + public void testReadBytes2() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + + byte[] receiveValue1 = new byte[2]; + msg.readBytes(receiveValue1); + assertThat(new String(receiveData).substring(0, 2), is(new String(receiveValue1))); + + byte[] receiveValue2 = new byte[2]; + msg.readBytes(receiveValue2); + assertThat(new String(receiveData).substring(2, 4), is(new String(receiveValue2))); + + } + + @Test + public void testWriteBytes() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(); + msg.writeBytes(sendData); + assertThat(new String(msg.getData()), is(new String(sendData))); + } + + @Test(expected = MessageNotReadableException.class) + public void testNotReadableException() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(); + msg.writeBoolean(true); + msg.readBoolean(); + } + + @Test(expected = MessageNotWriteableException.class) + public void testNotWritableException() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + msg.writeBoolean(true); + } + + @Test + public void testClearBody() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + msg.clearBody(); + msg.writeBoolean(true); + } + + @Test + public void testReset() throws Exception { + RocketMQBytesMessage msg = new RocketMQBytesMessage(receiveData); + byte[] b = new byte[2]; + msg.readBytes(b); + msg.reset(); + msg.readBytes(b); + assertThat(new String(receiveData).substring(0, 2), is(new String(b))); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-rocketmq-externals/blob/551d332f/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 new file mode 100644 index 0000000..9a648c9 --- /dev/null +++ b/core/src/test/java/org/apache/rocketmq/jms/support/TypeConverterTest.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 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
