    1   /*
    2    * Created on Mar 4, 2004
    3    *
    4    * To change the template for this generated file go to
    5    * Window - Preferences - Java - Code Generation - Code and Comments
    6    */
    7   package org.codehaus.activemq.message;
    8   import java.io.IOException;
    9
    10  import javax.jms.JMSException;
    11  import javax.jms.MessageFormatException;
    12
    13  import org.codehaus.activemq.message.ActiveMQBytesMessage;
    14  import org.codehaus.activemq.message.Packet;
    15
    16  import junit.framework.TestCase;
    17  /***
    18   *
    19   * To change the template for this generated type comment go to Window -
    20   * Preferences - Java - Code Generation - Code and Comments
    21   */
    22  public class ActiveMQBytesMessageTest extends TestCase {
    23  	public static void main(String[] args) {
    24  		junit.textui.TestRunner.run(ActiveMQBytesMessageTest.class);
    25  	}
    26  	/*
    27  	 * @see TestCase#setUp()
    28  	 */
    29  	protected void setUp() throws Exception {
    30  		super.setUp();
    31  	}
    32  	/*
    33  	 * @see TestCase#tearDown()
    34  	 */
    35  	protected void tearDown() throws Exception {
    36  		super.tearDown();
    37  	}
    38  	/***
    39  	 * Constructor for ActiveMQBytesMessageTest.
    40  	 *
    41  	 * @param arg0
    42  	 */
    43  	public ActiveMQBytesMessageTest(String arg0) {
    44  		super(arg0);
    45  	}
    46  	public void testGetPacketType() {
    47  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    48  		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_BYTES_MESSAGE);
    49  	}
    50  	public void testShallowCopy() throws Exception {
    51  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    52  		byte[] data = new byte[50];
    53  		msg.setBodyAsBytes(data);
    54  		ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) msg.shallowCopy();
    55  		try {
    56  			assertTrue(msg2.getBodyAsBytes() != null
    57  					&& msg2.getBodyAsBytes() == msg.getBodyAsBytes());
    58  		} catch (IOException ioe) {
    59  			ioe.printStackTrace();
    60  			assertTrue(false);
    61  		}
    62  	}
    63  	public void testDeepCopy() throws Exception {
    64  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    65  		byte[] data = new byte[50];
    66  		msg.setBodyAsBytes(data);
    67  		ActiveMQBytesMessage msg2 = (ActiveMQBytesMessage) msg.deepCopy();
    68  		try {
    69  			assertTrue(msg2.getBodyAsBytes() != null
    70  					&& msg2.getBodyAsBytes() != msg.getBodyAsBytes()
    71  					&& msg2.getBodyAsBytes().length == data.length);
    72  		} catch (IOException ioe) {
    73  			ioe.printStackTrace();
    74  			assertTrue(false);
    75  		}
    76  	}
    77  	public void testSetData() {
    78  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    79  		byte[] data = new byte[50];
    80  		msg.setBodyAsBytes(data);
    81  		try {
    82  			assertTrue(data == msg.getBodyAsBytes());
    83  		} catch (IOException ioe) {
    84  			ioe.printStackTrace();
    85  			assertTrue(false);
    86  		}
    87  	}
    88  	public void testGetBodyLength() {
    89  		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    90  		int len = 10;
    91  		try {
    92  			for (int i = 0; i < len; i++) {
    93  				msg.writeLong(5l);
    94  			}
    95  		} catch (JMSException ex) {
    96  			ex.printStackTrace();
    97  		}
    98  		try {
    99  			msg.reset();
    100 			assertTrue(msg.getBodyLength() == (len * 8));
    101 		} catch (Throwable e) {
    102 			e.printStackTrace();
    103 			assertTrue(false);
    104 		}
    105 	}
    106 	public void testReadBoolean() {
    107 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    108 		try {
    109 			msg.writeBoolean(true);
    110 			msg.reset();
    111 			assertTrue(msg.readBoolean());
    112 		} catch (JMSException jmsEx) {
    113 			jmsEx.printStackTrace();
    114 			assertTrue(false);
    115 		}
    116 	}
    117 	public void testReadByte() {
    118 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    119 		try {
    120 			msg.writeByte((byte) 2);
    121 			msg.reset();
    122 			assertTrue(msg.readByte() == 2);
    123 		} catch (JMSException jmsEx) {
    124 			jmsEx.printStackTrace();
    125 			assertTrue(false);
    126 		}
    127 	}
    128 	public void testReadUnsignedByte() {
    129 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    130 		try {
    131 			msg.writeByte((byte) 2);
    132 			msg.reset();
    133 			assertTrue(msg.readUnsignedByte() == 2);
    134 		} catch (JMSException jmsEx) {
    135 			jmsEx.printStackTrace();
    136 			assertTrue(false);
    137 		}
    138 	}
    139 	public void testReadShort() {
    140 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    141 		try {
    142 			msg.writeShort((short) 3000);
    143 			msg.reset();
    144 			assertTrue(msg.readShort() == 3000);
    145 		} catch (JMSException jmsEx) {
    146 			jmsEx.printStackTrace();
    147 			assertTrue(false);
    148 		}
    149 	}
    150 	public void testReadUnsignedShort() {
    151 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    152 		try {
    153 			msg.writeShort((short) 3000);
    154 			msg.reset();
    155 			assertTrue(msg.readUnsignedShort() == 3000);
    156 		} catch (JMSException jmsEx) {
    157 			jmsEx.printStackTrace();
    158 			assertTrue(false);
    159 		}
    160 	}
    161 	public void testReadChar() {
    162 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    163 		try {
    164 			msg.writeChar('a');
    165 			msg.reset();
    166 			assertTrue(msg.readChar() == 'a');
    167 		} catch (JMSException jmsEx) {
    168 			jmsEx.printStackTrace();
    169 			assertTrue(false);
    170 		}
    171 	}
    172 	public void testReadInt() {
    173 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    174 		try {
    175 			msg.writeInt(3000);
    176 			msg.reset();
    177 			assertTrue(msg.readInt() == 3000);
    178 		} catch (JMSException jmsEx) {
    179 			jmsEx.printStackTrace();
    180 			assertTrue(false);
    181 		}
    182 	}
    183 	public void testReadLong() {
    184 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    185 		try {
    186 			msg.writeLong(3000);
    187 			msg.reset();
    188 			assertTrue(msg.readLong() == 3000);
    189 		} catch (JMSException jmsEx) {
    190 			jmsEx.printStackTrace();
    191 			assertTrue(false);
    192 		}
    193 	}
    194 	public void testReadFloat() {
    195 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    196 		try {
    197 			msg.writeFloat(3.3f);
    198 			msg.reset();
    199 			assertTrue(msg.readFloat() == 3.3f);
    200 		} catch (JMSException jmsEx) {
    201 			jmsEx.printStackTrace();
    202 			assertTrue(false);
    203 		}
    204 	}
    205 	public void testReadDouble() {
    206 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    207 		try {
    208 			msg.writeDouble(3.3d);
    209 			msg.reset();
    210 			assertTrue(msg.readDouble() == 3.3d);
    211 		} catch (JMSException jmsEx) {
    212 			jmsEx.printStackTrace();
    213 			assertTrue(false);
    214 		}
    215 	}
    216 	public void testReadUTF() {
    217 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    218 		try {
    219 			String str = "this is a test";
    220 			msg.writeUTF(str);
    221 			msg.reset();
    222 			assertTrue(msg.readUTF().equals(str));
    223 		} catch (JMSException jmsEx) {
    224 			jmsEx.printStackTrace();
    225 			assertTrue(false);
    226 		}
    227 	}
    228 	/*
    229 	 * Class to test for int readBytes(byte[])
    230 	 */
    231 	public void testReadBytesbyteArray() {
    232 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    233 		try {
    234 			byte[] data = new byte[50];
    235 			for (int i = 0; i < data.length; i++) {
    236 				data[i] = (byte) i;
    237 			}
    238 			msg.writeBytes(data);
    239 			msg.reset();
    240 			byte[] test = new byte[data.length];
    241 			msg.readBytes(test);
    242 			for (int i = 0; i < test.length; i++) {
    243 				assertTrue(test[i] == i);
    244 			}
    245 		} catch (JMSException jmsEx) {
    246 			jmsEx.printStackTrace();
    247 			assertTrue(false);
    248 		}
    249 	}
    250 	public void testWriteObject() {
    251 		ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
    252 		try {
    253 			msg.writeObject(new Boolean(true));
    254 			msg.writeObject(new Long(300l));
    255 			msg.writeObject(new Float(3.3f));
    256 			msg.writeObject(new Character('q'));
    257 			msg.writeObject(new Byte((byte) 1));
    258 			msg.writeObject(new Short((short) 3));
    259 			msg.writeObject(new String("fred"));
    260 			try {
    261 				msg.writeObject(new Object());
    262 				assertTrue(false);
    263 			} catch (MessageFormatException mfe) {
    264 				assertTrue(true);
    265 			}
    266 		} catch (JMSException jmsEx) {
    267 			jmsEx.printStackTrace();
    268 			assertTrue(false);
    269 		}
    270 	}
    271 }