    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.activemq.message;
    19
    20  import junit.framework.TestCase;
    21
    22  import javax.jms.DeliveryMode;
    23  import javax.jms.JMSException;
    24  import java.io.ByteArrayInputStream;
    25  import java.io.ByteArrayOutputStream;
    26  import java.io.DataInputStream;
    27  import java.io.DataOutputStream;
    28  import java.io.IOException;
    29
    30  /***
    31   * @version $Revision: 1.1 $
    32   */
    33  public abstract class WireFormatTestSupport extends TestCase {
    34      protected WireFormat wireFormat;
    35
    36      public void testMessage() throws Exception {
    37          ActiveMQMessage expected = createMessage();
    38          Packet actual = writeThenReadPacket(expected);
    39          assertMessage(expected, (ActiveMQMessage) actual);
    40      }
    41
    42      public void testTextMessage() throws Exception {
    43          ActiveMQTextMessage expected = createTextMessage();
    44          Packet actual = writeThenReadPacket(expected);
    45          assertTextMessage(expected, (ActiveMQTextMessage) actual);
    46      }
    47
    48      public void testObjectMessage() throws Exception {
    49          ActiveMQObjectMessage expected = createObjectMessage();
    50          Packet actual = writeThenReadPacket(expected);
    51          assertObjectMessage(expected, (ActiveMQObjectMessage) actual);
    52      }
    53
    54      public void testMapMessage() throws Exception {
    55          ActiveMQMapMessage expected = createMapMessage();
    56          Packet actual = writeThenReadPacket(expected);
    57          assertMapMessage(expected, (ActiveMQMapMessage) actual);
    58      }
    59
    60      protected void assertTextMessage(ActiveMQTextMessage expected, ActiveMQTextMessage actual) throws JMSException {
    61          assertMessage(expected, actual);
    62          assertEquals("text", expected.getText(), actual.getText());
    63      }
    64
    65      protected ActiveMQTextMessage createTextMessage() throws JMSException {
    66          ActiveMQTextMessage answer = new ActiveMQTextMessage();
    67          answer.setText("This is some text");
    68          configureMessage(answer);
    69          return answer;
    70      }
    71
    72      protected void assertObjectMessage(ActiveMQObjectMessage expected, ActiveMQObjectMessage actual) throws JMSException {
    73          assertMessage(expected, actual);
    74          assertEquals("object", expected.getObject(), actual.getObject());
    75      }
    76
    77      protected ActiveMQObjectMessage createObjectMessage() throws JMSException {
    78          ActiveMQObjectMessage answer = new ActiveMQObjectMessage();
    79          answer.setObject("This is some text");
    80          configureMessage(answer);
    81          return answer;
    82      }
    83
    84      protected void assertMapMessage(ActiveMQMapMessage expected, ActiveMQMapMessage actual) throws JMSException {
    85          assertMessage(expected, actual);
    86          assertEquals("propertyBool", expected.getBooleanProperty("propertyBool"), actual.getBooleanProperty("propertyBool"));
    87          assertEquals("propertyByte", expected.getByteProperty("propertyByte"), actual.getByteProperty("propertyByte"));
    88          assertEquals("propertyDouble", expected.getDoubleProperty("propertyDouble"), actual.getDoubleProperty("propertyDouble"));
    89          assertEquals("propertyFloat", expected.getFloatProperty("propertyFloat"), actual.getFloatProperty("propertyFloat"));
    90          assertEquals("propertyInt", expected.getIntProperty("propertyInt"), actual.getIntProperty("propertyInt"));
    91          assertEquals("propertyLong", expected.getLongProperty("propertyLong"), actual.getLongProperty("propertyLong"));
    92          assertEquals("propertyShort", expected.getShortProperty("propertyShort"), actual.getShortProperty("propertyShort"));
    93          assertEquals("propertyString", expected.getStringProperty("propertyString"), actual.getStringProperty("propertyString"));
    94      }
    95
    96      protected ActiveMQMapMessage createMapMessage() throws JMSException {
    97          ActiveMQMapMessage answer = new ActiveMQMapMessage();
    98          answer.setBooleanProperty("propertyBool", true);
    99          answer.setByteProperty("propertyByte", (byte) 44);
    100         answer.setDoubleProperty("propertyDouble", 123.456);
    101         answer.setFloatProperty("propertyFloat", (float) 22.223456);
    102         answer.setIntProperty("propertyInt", 1234567);
    103         answer.setLongProperty("propertyLong", 1234567890L);
    104         answer.setShortProperty("propertyShort", (short) 1234);
    105         answer.setStringProperty("propertyString", "This is some text");
    106         configureMessage(answer);
    107         return answer;
    108     }
    109
    110     protected void assertMessage(ActiveMQMessage expected, ActiveMQMessage actual) throws JMSException {
    111         assertEquals("getBooleanProperty", expected.getBooleanProperty("fooBool"), actual.getBooleanProperty("fooBool"));
    112         assertEquals("getByteProperty", expected.getByteProperty("fooByte"), actual.getByteProperty("fooByte"));
    113         //assertEquals("consumerID", expected.getConsumerId(), actual.getConsumerId());
    114         assertEquals("getEntryBrokerName", expected.getEntryBrokerName(), actual.getEntryBrokerName());
    115         assertEquals("getJMSClientID", expected.getJMSClientID(), actual.getJMSClientID());
    116         assertEquals("getJMSCorrelationID", expected.getJMSCorrelationID(), actual.getJMSCorrelationID());
    117         assertEquals("getJMSDeliveryMode", expected.getJMSDeliveryMode(), actual.getJMSDeliveryMode());
    118         assertEquals("getJMSDestination", expected.getJMSDestination(), actual.getJMSDestination());
    119         assertEquals("getJMSExpiration", expected.getJMSExpiration(), actual.getJMSExpiration());
    120         assertEquals("getJMSMessageID", expected.getJMSMessageID(), actual.getJMSMessageID());
    121         assertEquals("getJMSPriority", expected.getJMSPriority(), actual.getJMSPriority());
    122         assertEquals("getJMSReplyTo", expected.getJMSReplyTo(), actual.getJMSReplyTo());
    123         assertEquals("getJMSType", expected.getJMSType(), actual.getJMSType());
    124         assertEquals("getProducerID", expected.getProducerID(), actual.getProducerID());
    125         assertEquals("getStringProperty", expected.getStringProperty("fooString"), actual.getStringProperty("fooString"));
    126         assertEquals("getTransactionId", expected.getTransactionId(), actual.getTransactionId());
    127     }
    128
    129     protected ActiveMQMessage createMessage() throws JMSException {
    130         ActiveMQMessage answer = new ActiveMQMessage();
    131         configureMessage(answer);
    132         return answer;
    133     }
    134
    135     protected void configureMessage(ActiveMQMessage answer) throws JMSException {
    136         answer.setBooleanProperty("fooBool", true);
    137         answer.setByteProperty("fooByte", (byte) 12);
    138         answer.setConsumerId("consumerId");
    139         answer.setEntryBrokerName("entryBroker");
    140         answer.setJMSClientID("myClientID");
    141         answer.setJMSCorrelationID("myCorrelationID");
    142         answer.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
    143         answer.setJMSDestination(new ActiveMQTopic("FOO.BAR"));
    144         answer.setJMSExpiration(1234);
    145         answer.setJMSMessageID("message:123");
    146         answer.setJMSPriority(2);
    147         answer.setJMSReplyTo(new ActiveMQQueue("BAR.REPLY"));
    148         answer.setJMSType("Cheddar");
    149         answer.setProducerID("myProducerID");
    150         answer.setStringProperty("fooString", "Whatnot");
    151         answer.setTransactionId("myTxnID");
    152     }
    153
    154     protected void setUp() throws Exception {
    155         wireFormat = createWireFormat();
    156     }
    157
    158     protected abstract WireFormat createWireFormat();
    159
    160
    161     protected void assertEquals(String message, double expected, double actual) {
    162         assertEquals(message, new Double(expected), new Double(actual));
    163     }
    164
    165     protected Packet writeThenReadPacket(ActiveMQMessage expected) throws IOException, JMSException {
    166         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    167         wireFormat.writePacket(expected, new DataOutputStream(buffer));
    168         Packet actual = wireFormat.readPacket(createDataIn(buffer));
    169         return actual;
    170     }
    171
    172     protected DataInputStream createDataIn(ByteArrayOutputStream buffer) {
    173         return new DataInputStream(new ByteArrayInputStream(buffer.toByteArray()));
    174     }
    175 }