001: package com.mockrunner.test.jms;
002:
003: import java.util.Arrays;
004:
005: import javax.jms.JMSException;
006: import javax.jms.MessageEOFException;
007: import javax.jms.MessageFormatException;
008: import javax.jms.MessageNotReadableException;
009: import javax.jms.MessageNotWriteableException;
010:
011: import junit.framework.TestCase;
012:
013: import com.mockrunner.mock.jms.MockStreamMessage;
014:
015: public class MockStreamMessageTest extends TestCase {
016: public void testReadWrite() throws Exception {
017: MockStreamMessage message = new MockStreamMessage();
018: message.writeString("test");
019: try {
020: message.readString();
021: fail();
022: } catch (MessageNotReadableException exc) {
023: //should throw exception
024: }
025: message.reset();
026: try {
027: message.writeByte((byte) 1);
028: fail();
029: } catch (MessageNotWriteableException exc) {
030: //should throw exception
031: }
032: assertEquals("test", message.readString());
033: try {
034: message.readInt();
035: fail();
036: } catch (MessageEOFException exc) {
037: //should throw exception
038: }
039: message = new MockStreamMessage();
040: message.writeString("123");
041: message.writeString("456");
042: message.writeString("789");
043: message.writeString("111");
044: message.writeString("222");
045: message.writeString("333");
046: message.reset();
047: assertEquals(123, message.readByte());
048: assertEquals(456, message.readShort());
049: assertEquals(789, message.readInt());
050: assertEquals(111, message.readLong());
051: assertEquals(222, message.readFloat(), 0);
052: assertEquals(333, message.readDouble(), 0);
053: try {
054: message.readBoolean();
055: fail();
056: } catch (MessageEOFException exc) {
057: //should throw exception
058: }
059: message = new MockStreamMessage();
060: message.writeLong(1);
061: message.reset();
062: try {
063: message.readInt();
064: fail();
065: } catch (MessageFormatException exc) {
066: //should throw exception
067: }
068: message = new MockStreamMessage();
069: message.writeByte((byte) 1);
070: message.writeByte((byte) 1);
071: message.writeByte((byte) 1);
072: message.writeByte((byte) 1);
073: message.reset();
074: assertEquals(1, message.readByte());
075: assertEquals(1, message.readShort());
076: assertEquals(1, message.readInt());
077: assertEquals(1, message.readLong());
078: message = new MockStreamMessage();
079: message.writeBoolean(false);
080: message.reset();
081: try {
082: message.readFloat();
083: fail();
084: } catch (MessageFormatException exc) {
085: //should throw exception
086: }
087: message = new MockStreamMessage();
088: message.writeBoolean(false);
089: message.writeBoolean(true);
090: message.reset();
091: assertEquals(false, message.readBoolean());
092: assertEquals("true", message.readString());
093: message.clearBody();
094: message.writeDouble(123.4);
095: message.reset();
096: assertEquals("123.4", message.readString());
097: message.clearBody();
098: message.writeString("a");
099: message.reset();
100: try {
101: message.readChar();
102: fail();
103: } catch (MessageFormatException exc) {
104: //should throw exception
105: }
106: message.clearBody();
107: message.writeChar('a');
108: message.reset();
109: assertEquals("a", message.readString());
110: message = new MockStreamMessage();
111: byte[] data = new byte[] { 1, 2, 3, 4 };
112: message.writeBytes(data);
113: message.writeBytes(data);
114: message.reset();
115: data = new byte[4];
116: int number = message.readBytes(data);
117: assertEquals(4, number);
118: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4 }));
119: data = (byte[]) message.readObject();
120: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4 }));
121: message = new MockStreamMessage();
122: data = new byte[] { 1, 2, 3, 4 };
123: message.writeBytes(data);
124: message.reset();
125: data = new byte[5];
126: number = message.readBytes(data);
127: assertEquals(4, number);
128: assertTrue(Arrays.equals(data, new byte[] { 1, 2, 3, 4, 0 }));
129: data = new byte[] { 1, 2, 3, 4 };
130: message.clearBody();
131: message.writeBytes(data, 3, 1);
132: message.reset();
133: data = new byte[5];
134: number = message.readBytes(data);
135: assertEquals(1, number);
136: assertTrue(Arrays.equals(data, new byte[] { 4, 0, 0, 0, 0 }));
137: data = new byte[] { 1, 2, 3, 4 };
138: message.clearBody();
139: message.writeBytes(data);
140: message.reset();
141: data = new byte[1];
142: number = message.readBytes(data);
143: assertEquals(1, number);
144: assertTrue(Arrays.equals(data, new byte[] { 1 }));
145: data = new byte[] { 1, 2, 3, 4 };
146: message.clearBody();
147: message.writeBytes(data);
148: message.reset();
149: data = new byte[0];
150: number = message.readBytes(data);
151: assertEquals(0, number);
152: data = new byte[] { 1, 2, 3, 4 };
153: message.clearBody();
154: message.writeBytes(data);
155: message.reset();
156: number = message.readBytes(null);
157: assertEquals(-1, number);
158: message = new MockStreamMessage();
159: message.writeObject(null);
160: message.writeObject(null);
161: message.writeObject(null);
162: message.writeObject(new Integer(3));
163: message.writeObject("4");
164: message.writeObject(new Byte((byte) 5));
165: message.reset();
166: try {
167: message.readChar();
168: fail();
169: } catch (NullPointerException exc) {
170: //should throw exception
171: }
172: try {
173: message.readDouble();
174: fail();
175: } catch (NullPointerException exc) {
176: //should throw exception
177: }
178: assertNull(message.readObject());
179: assertEquals(3, message.readLong());
180: assertEquals(4, message.readByte());
181: try {
182: message.readFloat();
183: fail();
184: } catch (MessageFormatException exc) {
185: //should throw exception
186: }
187:
188: try {
189: message.readFloat();
190: fail();
191: } catch (MessageEOFException exc) {
192: //should throw exception
193: }
194: }
195:
196: public void testEquals() throws Exception {
197: MockStreamMessage message1 = new MockStreamMessage();
198: message1.writeString("test");
199: message1.writeObject(new Long(1));
200: message1.writeBytes(new byte[] { 1, 2, 3 });
201: assertTrue(message1.equals(message1));
202: MockStreamMessage message2 = null;
203: assertFalse(message1.equals(message2));
204: message2 = new MockStreamMessage();
205: assertFalse(message1.equals(message2));
206: assertFalse(message2.equals(message1));
207: message2.writeString("test");
208: message2.writeBytes(new byte[] { 1, 2, 3 });
209: message2.writeObject(new Long(1));
210: assertFalse(message1.equals(message2));
211: assertFalse(message2.equals(message1));
212: message2 = new MockStreamMessage();
213: message2.writeString("test");
214: message2.writeObject(new Long(1));
215: message2.writeBytes(new byte[] {});
216: assertFalse(message1.equals(message2));
217: assertFalse(message2.equals(message1));
218: message2 = new MockStreamMessage();
219: message2.writeString("test");
220: message2.writeObject(new Long(1));
221: message2.writeBytes(new byte[] { 1, 2, 3 });
222: assertTrue(message1.equals(message2));
223: assertTrue(message2.equals(message1));
224: assertEquals(message1.hashCode(), message2.hashCode());
225: message2.writeString(null);
226: assertFalse(message1.equals(message2));
227: assertFalse(message2.equals(message1));
228: message1.writeString(null);
229: assertTrue(message1.equals(message2));
230: assertTrue(message2.equals(message1));
231: assertEquals(message1.hashCode(), message2.hashCode());
232: message1.reset();
233: assertTrue(message1.equals(message2));
234: assertTrue(message2.equals(message1));
235: assertEquals(message1.hashCode(), message2.hashCode());
236: }
237:
238: public void testClone() throws Exception {
239: MockStreamMessage message = new MockStreamMessage();
240: MockStreamMessage newMessage = (MockStreamMessage) message
241: .clone();
242: assertNotSame(message, newMessage);
243: assertEquals(message, newMessage);
244: message = new MockStreamMessage();
245: message.writeByte((byte) 1);
246: message.writeBytes(new byte[] { 1, 2, 3, 4, 5 });
247: message.writeBoolean(true);
248: newMessage = (MockStreamMessage) message.clone();
249: assertNotSame(message, newMessage);
250: assertEquals(message, newMessage);
251: try {
252: newMessage.readBoolean();
253: fail();
254: } catch (JMSException exc) {
255: //should throw exception
256: }
257: newMessage.reset();
258: assertEquals(1, newMessage.readByte());
259: byte[] myArray = new byte[5];
260: newMessage.readBytes(myArray);
261: assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4, 5 }, myArray));
262: assertTrue(newMessage.readBoolean());
263: message = new MockStreamMessage();
264: message.writeString("test1");
265: message.writeString("test2");
266: message.reset();
267: newMessage = (MockStreamMessage) message.clone();
268: assertEquals("test1", message.readString());
269: assertEquals("test2", message.readString());
270: }
271:
272: public void testToString() throws Exception {
273: MockStreamMessage message = new MockStreamMessage();
274: assertEquals(MockStreamMessage.class.getName() + ": []",
275: message.toString());
276: message.writeInt(12);
277: message.writeString("abc");
278: assertEquals(MockStreamMessage.class.getName() + ": [12, abc]",
279: message.toString());
280: }
281: }
|