    1   /*
    2    * Created on Mar 5, 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
    9   import java.io.IOException;
    10
    11  import javax.jms.JMSException;
    12
    13  import org.codehaus.activemq.message.ActiveMQStreamMessage;
    14  import org.codehaus.activemq.message.Packet;
    15
    16  import junit.framework.TestCase;
    17
    18  /***
    19   *
    20   * To change the template for this generated type comment go to
    21   * Window - Preferences - Java - Code Generation - Code and Comments
    22   */
    23  public class ActiveMQStreamMessageTest extends TestCase
    24  {
    25
    26  	public static void main(String[] args)
    27  	{
    28  		junit.textui.TestRunner.run(ActiveMQStreamMessageTest.class);
    29  	}
    30
    31  	/*
    32  	 * @see TestCase#setUp()
    33  	 */
    34  	protected void setUp() throws Exception
    35  	{
    36  		super.setUp();
    37  	}
    38
    39  	/*
    40  	 * @see TestCase#tearDown()
    41  	 */
    42  	protected void tearDown() throws Exception
    43  	{
    44  		super.tearDown();
    45  	}
    46
    47  	/***
    48  	 * Constructor for ActiveMQStreamMessageTest.
    49  	 * @param arg0
    50  	 */
    51  	public ActiveMQStreamMessageTest(String arg0)
    52  	{
    53  		super(arg0);
    54  	}
    55
    56  	public void testGetPacketType()
    57  	{
    58  		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    59  		assertTrue(msg.getPacketType() == Packet.ACTIVEMQ_STREAM_MESSAGE);
    60  	}
    61
    62  	public void testShallowCopy()
    63  	{
    64  		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    65  		byte[] data = new byte[50];
    66
    67  		try {
    68              msg.setBodyAsBytes(data);
    69              ActiveMQStreamMessage msg2 = (ActiveMQStreamMessage)msg.shallowCopy();
    70  			assertTrue(msg2.getBodyAsBytes() != null && msg2.getBodyAsBytes()==msg.getBodyAsBytes());
    71  		}catch(Exception ioe){
    72  			ioe.printStackTrace();
    73  			assertTrue(false);
    74  		}
    75  	}
    76
    77  	public void testDeepCopy()
    78  	{
    79  		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    80  		byte[] data = new byte[50];
    81  		msg.setBodyAsBytes(data);
    82
    83  		try {
    84              ActiveMQStreamMessage msg2 = (ActiveMQStreamMessage)msg.deepCopy();
    85  			assertTrue(msg2.getBodyAsBytes() != null &&
    86  					msg2.getBodyAsBytes()!=msg.getBodyAsBytes() &&
    87  					msg2.getBodyAsBytes().length==data.length);
    88  		}catch(Exception ioe){
    89  			ioe.printStackTrace();
    90  			assertTrue(false);
    91  		}
    92  	}
    93
    94  	public void testSetData()
    95  	{
    96
    97  		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    98  		byte[] data = new byte[50];
    99  		msg.setBodyAsBytes(data);
    100 		try {
    101 			assertTrue(data==msg.getBodyAsBytes());
    102 		}catch(IOException ioe){
    103 			ioe.printStackTrace();
    104 			assertTrue(false);
    105 		}
    106 	}
    107
    108
    109 	public void testReadBoolean()
    110 	{
    111 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    112 		try {
    113 			msg.writeBoolean(true);
    114 			msg.reset();
    115 			assertTrue(msg.readBoolean());
    116 			msg.reset();
    117 			assertTrue(msg.readString().equals("true"));
    118 		}catch(JMSException jmsEx){
    119 			jmsEx.printStackTrace();
    120 			assertTrue(false);
    121 		}
    122 	}
    123
    124 	public void testreadByte()
    125 	{
    126 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    127 		try {
    128 			byte test = (byte)4;
    129 			msg.writeByte(test);
    130 			msg.reset();
    131 			assertTrue(msg.readByte()==test);
    132 			msg.reset();
    133 			assertTrue(msg.readShort()==test);
    134 			msg.reset();
    135 			assertTrue(msg.readInt()==test);
    136 			msg.reset();
    137 			assertTrue(msg.readLong()==test);
    138 			msg.reset();
    139 			assertTrue(msg.readString().equals(new Byte(test).toString()));
    140 		}catch(JMSException jmsEx){
    141 			jmsEx.printStackTrace();
    142 			assertTrue(false);
    143 		}
    144 	}
    145
    146 	public void testReadShort()
    147 	{
    148 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    149 		try {
    150 			short test = (short)4;
    151 			msg.writeShort(test);
    152 			msg.reset();
    153 			assertTrue(msg.readShort()==test);
    154 			msg.reset();
    155 			assertTrue(msg.readInt()==test);
    156 			msg.reset();
    157 			assertTrue(msg.readLong()==test);
    158 			msg.reset();
    159 			assertTrue(msg.readString().equals(new Short(test).toString()));
    160 		}catch(JMSException jmsEx){
    161 			jmsEx.printStackTrace();
    162 			assertTrue(false);
    163 		}
    164 	}
    165
    166 	public void testReadChar()
    167 	{
    168 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    169 		try {
    170 			char test = 'z';
    171 			msg.writeChar(test);
    172 			msg.reset();
    173 			assertTrue(msg.readChar()==test);
    174 			msg.reset();
    175 			assertTrue(msg.readString().equals(new Character(test).toString()));
    176 		}catch(JMSException jmsEx){
    177 			jmsEx.printStackTrace();
    178 			assertTrue(false);
    179 		}
    180 	}
    181
    182 	public void testReadInt()
    183 	{
    184 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    185 		try {
    186 			int test = 4;
    187 			msg.writeInt(test);
    188 			msg.reset();
    189 			assertTrue(msg.readInt()==test);
    190 			msg.reset();
    191 			assertTrue(msg.readLong()==test);
    192 			msg.reset();
    193 			assertTrue(msg.readString().equals(new Integer(test).toString()));
    194 		}catch(JMSException jmsEx){
    195 			jmsEx.printStackTrace();
    196 			assertTrue(false);
    197 		}
    198 	}
    199
    200 	public void testReadLong()
    201 	{
    202 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    203 		try {
    204 			long test = 4l;
    205 			msg.writeLong(test);
    206 			msg.reset();
    207 			assertTrue(msg.readLong()==test);
    208 			msg.reset();
    209 			assertTrue(msg.readString().equals(new Long(test).toString()));
    210 		}catch(JMSException jmsEx){
    211 			jmsEx.printStackTrace();
    212 			assertTrue(false);
    213 		}
    214 	}
    215
    216 	public void testReadFloat()
    217 	{
    218 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    219 		try {
    220 			float test = 4.4f;
    221 			msg.writeFloat(test);
    222 			msg.reset();
    223 			assertTrue(msg.readFloat()==test);
    224 			msg.reset();
    225 			assertTrue(msg.readDouble()==test);
    226 			msg.reset();
    227 			assertTrue(msg.readString().equals(new Float(test).toString()));
    228 		}catch(JMSException jmsEx){
    229 			jmsEx.printStackTrace();
    230 			assertTrue(false);
    231 		}
    232 	}
    233
    234 	public void testReadDouble()
    235 	{
    236 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    237 		try {
    238 			double test = 4.4d;
    239 			msg.writeDouble(test);
    240 			msg.reset();
    241 			assertTrue(msg.readDouble()==test);
    242 			msg.reset();
    243 			assertTrue(msg.readString().equals(new Double(test).toString()));
    244 		}catch(JMSException jmsEx){
    245 			jmsEx.printStackTrace();
    246 			assertTrue(false);
    247 		}
    248 	}
    249
    250 	public void testReadString()
    251 	{
    252 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    253 		try {
    254 			byte testByte = (byte)2;
    255 			msg.writeString(new Byte(testByte).toString());
    256 			msg.reset();
    257 			assertTrue(msg.readByte()==testByte);
    258 			msg.clearBody();
    259 			short testShort = 3;
    260 			msg.writeString(new Short(testShort).toString());
    261 			msg.reset();
    262 			assertTrue(msg.readShort()==testShort);
    263 			msg.clearBody();
    264 			int testInt = 4;
    265 			msg.writeString(new Integer(testInt).toString());
    266 			msg.reset();
    267 			assertTrue(msg.readInt()==testInt);
    268 			msg.clearBody();
    269 			long testLong = 6l;
    270 			msg.writeString(new Long(testLong).toString());
    271 			msg.reset();
    272 			assertTrue(msg.readLong()==testLong);
    273 			msg.clearBody();
    274 			float testFloat = 6.6f;
    275 			msg.writeString(new Float(testFloat).toString());
    276 			msg.reset();
    277 			assertTrue(msg.readFloat()==testFloat);
    278 			msg.clearBody();
    279 			double testDouble = 7.7d;
    280 			msg.writeString(new Double(testDouble).toString());
    281 			msg.reset();
    282 			assertTrue(msg.readDouble()==testDouble);
    283 			msg.clearBody();
    284 			msg.writeString("true");
    285 			msg.reset();
    286 			assertTrue(msg.readBoolean());
    287
    288
    289 		}catch(JMSException jmsEx){
    290 			jmsEx.printStackTrace();
    291 			assertTrue(false);
    292 		}
    293 	}
    294
    295 	public void testReadBytes()
    296 	{
    297 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    298 		try {
    299 			byte[] test = new byte[50];
    300 			for(int i =0; i< test.length;i++){
    301 				test[i]=(byte)i;
    302 			}
    303 			msg.writeBytes(test);
    304 			msg.reset();
    305 			byte[] valid = new byte[test.length];
    306 			msg.readBytes(valid);
    307 			for(int i = 0; i < valid.length;i++){
    308 				assertTrue(valid[i]==test[i]);
    309 			}
    310 		}catch(JMSException jmsEx){
    311 			jmsEx.printStackTrace();
    312 			assertTrue(false);
    313 		}
    314 	}
    315
    316 	public void testReadObject()
    317 	{
    318 		ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
    319 		try {
    320 			byte testByte = (byte)2;
    321 			msg.writeByte(testByte);
    322 			msg.reset();
    323 			assertTrue(((Byte)msg.readObject()).byteValue()==testByte);
    324 			msg.clearBody();
    325
    326 			short testShort = 3;
    327 			msg.writeShort(testShort);
    328 			msg.reset();
    329 			assertTrue(((Short)msg.readObject()).shortValue()==testShort);
    330 			msg.clearBody();
    331
    332 			int testInt = 4;
    333 			msg.writeInt(testInt);
    334 			msg.reset();
    335 			assertTrue(((Integer)msg.readObject()).intValue()==testInt);
    336 			msg.clearBody();
    337
    338 			long testLong = 6l;
    339 			msg.writeLong(testLong);
    340 			msg.reset();
    341 			assertTrue(((Long)msg.readObject()).longValue()==testLong);
    342 			msg.clearBody();
    343
    344 			float testFloat = 6.6f;
    345 			msg.writeFloat(testFloat);
    346 			msg.reset();
    347 			assertTrue(((Float)msg.readObject()).floatValue()==testFloat);
    348 			msg.clearBody();
    349
    350 			double testDouble = 7.7d;
    351 			msg.writeDouble(testDouble);
    352 			msg.reset();
    353 			assertTrue(((Double)msg.readObject()).doubleValue()==testDouble);
    354 			msg.clearBody();
    355
    356 			char testChar = 'z';
    357 			msg.writeChar(testChar);
    358 			msg.reset();
    359 			assertTrue(((Character)msg.readObject()).charValue()==testChar);
    360 			msg.clearBody();
    361
    362 			byte[] data = new byte[50];
    363 			for(int i = 0; i < data.length;i++){
    364 				data[i] = (byte)i;
    365 			}
    366 			msg.writeBytes(data);
    367 			msg.reset();
    368 			byte[] valid = (byte[])msg.readObject();
    369 			assertTrue(valid.length==data.length);
    370 			for(int i =0; i < valid.length;i++){
    371 				assertTrue(valid[i]==data[i]);
    372 			}
    373 			msg.clearBody();
    374 			msg.writeBoolean(true);
    375 			msg.reset();
    376 			assertTrue(((Boolean)msg.readObject()).booleanValue());
    377
    378
    379 		}catch(JMSException jmsEx){
    380 			jmsEx.printStackTrace();
    381 			assertTrue(false);
    382 		}
    383 	}
    384
    385
    386 }