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.MessageNotReadableException;
008: import javax.jms.MessageNotWriteableException;
009:
010: import junit.framework.TestCase;
011:
012: import com.mockrunner.mock.jms.MockBytesMessage;
013:
014: public class MockBytesMessageTest extends TestCase {
015: public void testReadWrite() throws Exception {
016: MockBytesMessage message = new MockBytesMessage();
017: message.writeChar('t');
018: message.writeUTF("est");
019: message.writeByte((byte) 1);
020: message.writeLong(2345);
021: message.reset();
022: try {
023: message.writeDouble(678.9);
024: fail();
025: } catch (MessageNotWriteableException exc) {
026: //should throw exception
027: }
028: assertEquals('t', message.readChar());
029: assertEquals("est", message.readUTF());
030: assertEquals(1, message.readByte());
031: assertEquals(2345, message.readLong());
032: try {
033: message.readByte();
034: fail();
035: } catch (MessageEOFException exc) {
036: //should throw exception
037: }
038: byte[] data = new byte[] { 1, 2, 3, 4, 5 };
039: message = new MockBytesMessage();
040: message.writeBytes(data);
041: message.writeBytes(data, 1, 2);
042: message.reset();
043: try {
044: message.writeBytes(data);
045: fail();
046: } catch (MessageNotWriteableException exc) {
047: //should throw exception
048: }
049: data = new byte[7];
050: message.readBytes(data);
051: assertTrue(Arrays.equals(data,
052: new byte[] { 1, 2, 3, 4, 5, 2, 3 }));
053: try {
054: message.readByte();
055: fail();
056: } catch (MessageEOFException exc) {
057: //should throw exception
058: }
059: message.clearBody();
060: try {
061: message.readByte();
062: fail();
063: } catch (MessageNotReadableException exc) {
064: //should throw exception
065: }
066: message.writeDouble(1.2);
067: try {
068: message.readDouble();
069: fail();
070: } catch (MessageNotReadableException exc) {
071: //should throw exception
072: }
073: message.reset();
074: assertEquals(1.2, message.readDouble(), 0);
075: message = new MockBytesMessage();
076: message.writeObject(Boolean.TRUE);
077: message.writeObject(new Long(1));
078: message.writeObject("xyz");
079: message.writeObject(new byte[] { 1 });
080: message.reset();
081: assertEquals(true, message.readBoolean());
082: assertEquals(1, message.readLong());
083: assertEquals("xyz", message.readUTF());
084: data = new byte[1];
085: message.readBytes(data, 1);
086: assertTrue(Arrays.equals(data, new byte[] { 1 }));
087: }
088:
089: public void testGetBodyLength() throws Exception {
090: MockBytesMessage message = new MockBytesMessage();
091: message.writeInt(3);
092: try {
093: message.getBodyLength();
094: fail();
095: } catch (MessageNotReadableException exc) {
096: //should throw exception
097: }
098: message.reset();
099: assertEquals(4, message.getBodyLength());
100: message.clearBody();
101: try {
102: message.getBodyLength();
103: fail();
104: } catch (MessageNotReadableException exc) {
105: //should throw exception
106: }
107: message.reset();
108: assertEquals(0, message.getBodyLength());
109: }
110:
111: public void testEquals() throws Exception {
112: MockBytesMessage message1 = new MockBytesMessage();
113: message1.writeChar('t');
114: message1.writeUTF("est");
115: message1.writeByte((byte) 1);
116: message1.writeLong(2345);
117: assertTrue(message1.equals(message1));
118: MockBytesMessage message2 = new MockBytesMessage();
119: message2.writeChar('t');
120: message2.writeUTF("est");
121: message2.writeByte((byte) 1);
122: assertFalse(message1.equals(message2));
123: assertFalse(message2.equals(message1));
124: message2.writeLong(2345);
125: assertTrue(message1.equals(message2));
126: assertTrue(message2.equals(message1));
127: assertEquals(message1.hashCode(), message2.hashCode());
128: message1.reset();
129: assertTrue(message1.equals(message2));
130: assertTrue(message2.equals(message1));
131: assertTrue(new MockBytesMessage()
132: .equals(new MockBytesMessage()));
133: assertEquals(new MockBytesMessage().hashCode(),
134: new MockBytesMessage().hashCode());
135: message1 = new MockBytesMessage();
136: message1.writeUTF("test");
137: message1.writeInt(3);
138: message2 = new MockBytesMessage();
139: message2.writeInt(3);
140: message2.writeUTF("test");
141: assertFalse(message1.equals(message2));
142: }
143:
144: public void testClone() throws Exception {
145: MockBytesMessage message = new MockBytesMessage();
146: MockBytesMessage newMessage = (MockBytesMessage) message
147: .clone();
148: assertNotSame(message, newMessage);
149: assertEquals(message, newMessage);
150: try {
151: newMessage.getBodyLength();
152: fail();
153: } catch (JMSException exc) {
154: //should throw exception
155: }
156: newMessage.writeUTF("test");
157: message.reset();
158: newMessage.reset();
159: try {
160: message.readUTF();
161: fail();
162: } catch (Exception exc) {
163: //should throw exception
164: }
165: assertEquals("test", newMessage.readUTF());
166: message = new MockBytesMessage();
167: message.writeDouble(1.0);
168: newMessage = (MockBytesMessage) message.clone();
169: assertNotSame(message, newMessage);
170: assertEquals(message, newMessage);
171: try {
172: newMessage.getBodyLength();
173: fail();
174: } catch (JMSException exc) {
175: //should throw exception
176: }
177: message.reset();
178: newMessage.reset();
179: assertEquals(1.0, message.readDouble(), 0);
180: assertEquals(1.0, newMessage.readDouble(), 0);
181: message = new MockBytesMessage();
182: message.writeLong(12345);
183: message.reset();
184: newMessage = (MockBytesMessage) message.clone();
185: assertNotSame(message, newMessage);
186: assertEquals(message, newMessage);
187: }
188:
189: public void testGetBytes() throws Exception {
190: MockBytesMessage message = new MockBytesMessage();
191: assertTrue(Arrays.equals(new byte[0], message.getBytes()));
192: message.writeBytes(new byte[] { 1, 2, 3 });
193: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, message
194: .getBytes()));
195: message.getBytes()[0] = 5;
196: assertTrue(Arrays.equals(new byte[] { 1, 2, 3 }, message
197: .getBytes()));
198: MockBytesMessage otherMessage = new MockBytesMessage();
199: otherMessage.writeBytes(new byte[] { 1, 2, 3 });
200: assertTrue(message.equals(otherMessage));
201: }
202:
203: public void testToString() throws Exception {
204: MockBytesMessage message = new MockBytesMessage();
205: assertEquals(MockBytesMessage.class.getName() + ": []", message
206: .toString());
207: message.writeBytes(new byte[] { 1 });
208: assertEquals(MockBytesMessage.class.getName() + ": [1]",
209: message.toString());
210: message.writeBytes(new byte[] { 1, 2, 3 });
211: assertEquals(MockBytesMessage.class.getName()
212: + ": [1, 1, 2, 3]", message.toString());
213: }
214: }
|