001: package org.mockejb.jms.test;
002:
003: import javax.jms.*;
004:
005: import org.mockejb.jms.BytesMessageImpl;
006: import org.mockejb.jms.MessageUtility;
007:
008: import java.io.*;
009:
010: /**
011: * Tests <code>BytesMessageImpl</code>.
012: *
013: * @author Dimitar Gospodinov
014: */
015: public class BytesMessageImplTest extends MessageTester {
016:
017: private BytesMessage msg;
018:
019: public BytesMessageImplTest(String name) {
020: super (name);
021: }
022:
023: protected void setUp() throws Exception {
024: msg = new BytesMessageImpl();
025: message = msg;
026: super .setUp();
027: }
028:
029: protected void tearDown() throws Exception {
030: msg = null;
031: }
032:
033: public void testMessageNotReadable() throws JMSException {
034:
035: // Uncomment if using JMS 1.1 or later.
036: // try {
037: // msg.getBodyLength();
038: // fail();
039: // } catch (MessageNotReadableException ex) {
040: // }
041: try {
042: msg.readBoolean();
043: fail();
044: } catch (MessageNotReadableException ex) {
045: }
046: try {
047: msg.readByte();
048: fail();
049: } catch (MessageNotReadableException ex) {
050: }
051: try {
052: msg.readBytes(new byte[5]);
053: fail();
054: } catch (MessageNotReadableException ex) {
055: }
056: try {
057: msg.readBytes(new byte[5], 5);
058: fail();
059: } catch (MessageNotReadableException ex) {
060: }
061: try {
062: msg.readChar();
063: fail();
064: } catch (MessageNotReadableException ex) {
065: }
066: try {
067: msg.readDouble();
068: fail();
069: } catch (MessageNotReadableException ex) {
070: }
071: try {
072: msg.readFloat();
073: fail();
074: } catch (MessageNotReadableException ex) {
075: }
076: try {
077: msg.readInt();
078: fail();
079: } catch (MessageNotReadableException ex) {
080: }
081: try {
082: msg.readLong();
083: fail();
084: } catch (MessageNotReadableException ex) {
085: }
086: try {
087: msg.readShort();
088: fail();
089: } catch (MessageNotReadableException ex) {
090: }
091: try {
092: msg.readUnsignedByte();
093: fail();
094: } catch (MessageNotReadableException ex) {
095: }
096: try {
097: msg.readUnsignedShort();
098: fail();
099: } catch (MessageNotReadableException ex) {
100: }
101: try {
102: msg.readUTF();
103: fail();
104: } catch (MessageNotReadableException ex) {
105: }
106: checkMessageAttributes();
107: }
108:
109: public void testMessageNotWritable() throws JMSException {
110:
111: msg.reset();
112:
113: // Uncomment if using JMS 1.1 or later.
114: // assertEquals(msg.getBodyLength(), 0);
115:
116: try {
117: msg.writeBoolean(true);
118: fail();
119: } catch (MessageNotWriteableException ex) {
120: }
121: try {
122: msg.writeByte((byte) 1);
123: fail();
124: } catch (MessageNotWriteableException ex) {
125: }
126: try {
127: msg.writeBytes(new byte[5]);
128: fail();
129: } catch (MessageNotWriteableException ex) {
130: }
131: try {
132: msg.writeBytes(null);
133: fail();
134: } catch (MessageNotWriteableException ex) {
135: }
136: try {
137: msg.writeBytes(new byte[5], 0, 1);
138: fail();
139: } catch (MessageNotWriteableException ex) {
140: }
141: try {
142: msg.writeChar('a');
143: fail();
144: } catch (MessageNotWriteableException ex) {
145: }
146: try {
147: msg.writeDouble(123.33);
148: fail();
149: } catch (MessageNotWriteableException ex) {
150: }
151: try {
152: msg.writeFloat((float) 22.22);
153: fail();
154: } catch (MessageNotWriteableException ex) {
155: }
156: try {
157: msg.writeInt(122);
158: fail();
159: } catch (MessageNotWriteableException ex) {
160: }
161: try {
162: msg.writeLong(11);
163: fail();
164: } catch (MessageNotWriteableException ex) {
165: }
166: try {
167: msg.writeShort((short) 88);
168: fail();
169: } catch (MessageNotWriteableException ex) {
170: }
171: try {
172: msg.writeObject(null);
173: fail();
174: } catch (MessageNotWriteableException ex) {
175: }
176: try {
177: msg.writeUTF("utf");
178: fail();
179: } catch (MessageNotWriteableException ex) {
180: }
181: checkMessageAttributes();
182: }
183:
184: public void testBytesMessage() throws Throwable {
185:
186: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
187: DataOutputStream dataOut = new DataOutputStream(bytesOut);
188:
189: byte[] bytesData1 = { 1, 2, 3, 4, 5 };
190:
191: // Write to BytesMessage and DataOutputStream and then compare
192:
193: msg.writeBoolean(true);
194: dataOut.writeBoolean(true);
195: msg.writeByte((byte) 255);
196: dataOut.writeByte((byte) 255);
197: msg.writeBytes(bytesData1);
198: dataOut.write(bytesData1);
199: msg.writeBytes(bytesData1, 1, 2);
200: dataOut.write(bytesData1, 1, 2);
201: msg.writeChar('a');
202: dataOut.writeChar('a');
203: msg.writeDouble(1.77E307);
204: dataOut.writeDouble(1.77E307);
205: msg.writeFloat((float) 44.9991);
206: dataOut.writeFloat((float) 44.9991);
207: msg.writeInt(65536);
208: dataOut.writeInt(65536);
209: msg.writeLong(9999222233338888L);
210: dataOut.writeLong(9999222233338888L);
211: msg.writeShort((short) 32000);
212: dataOut.writeShort(32000);
213: msg.writeUTF("UTF");
214: dataOut.writeUTF("UTF");
215:
216: byte[] bytes1 = bytesOut.toByteArray();
217:
218: msg.reset();
219: compareBytes(bytes1, msg);
220:
221: /*
222: * At this point all bytes from msg have been read. Any read must throw EOF
223: * exception, except <code>readBytes</code> methods, which must return
224: * -1.
225: */
226: try {
227: msg.readUTF();
228: fail();
229: } catch (MessageEOFException ex) {
230: }
231: try {
232: msg.readShort();
233: fail();
234: } catch (MessageEOFException ex) {
235: }
236: try {
237: msg.readLong();
238: fail();
239: } catch (MessageEOFException ex) {
240: }
241: try {
242: msg.readInt();
243: fail();
244: } catch (MessageEOFException ex) {
245: }
246: try {
247: msg.readFloat();
248: fail();
249: } catch (MessageEOFException ex) {
250: }
251: try {
252: msg.readDouble();
253: fail();
254: } catch (MessageEOFException ex) {
255: }
256: try {
257: msg.readChar();
258: fail();
259: } catch (MessageEOFException ex) {
260: }
261: try {
262: msg.readByte();
263: fail();
264: } catch (MessageEOFException ex) {
265: }
266: try {
267: msg.readBoolean();
268: fail();
269: } catch (MessageEOFException ex) {
270: }
271: assertEquals(msg.readBytes(new byte[50]), -1);
272: assertEquals(msg.readBytes(new byte[50], 5), -1);
273: try {
274: msg.readBytes(new byte[50], -11);
275: fail();
276: } catch (IndexOutOfBoundsException ex) {
277: }
278:
279: msg.reset();
280:
281: byte[] bytes2 = new byte[7];
282:
283: assertTrue(msg.readBoolean());
284: assertEquals(msg.readByte(), (byte) 255);
285: assertEquals(msg.readBytes(bytes2), 7);
286: assertEquals(bytes2[0], 1);
287: assertEquals(bytes2[1], 2);
288: assertEquals(bytes2[2], 3);
289: assertEquals(bytes2[3], 4);
290: assertEquals(bytes2[4], 5);
291: assertEquals(bytes2[5], 2);
292: assertEquals(bytes2[6], 3);
293: assertEquals(msg.readChar(), 'a');
294: assertEquals(msg.readDouble(), 1.77E307, 0.01E307);
295: assertEquals(msg.readFloat(), (float) 44.9991, (float) 0.0001);
296: assertEquals(msg.readInt(), 65536);
297: assertEquals(msg.readLong(), 9999222233338888L);
298: assertEquals(msg.readShort(), (short) 32000);
299: assertEquals(msg.readUTF(), "UTF");
300:
301: msg.reset();
302: msg.readBoolean();
303: msg.readByte();
304: msg.readBytes(bytes2, 6);
305:
306: assertTrue(MessageUtility.compare(msg,
307: new BytesMessageImpl(msg)));
308:
309: assertEquals(msg.readByte(), (byte) 3);
310: assertEquals(msg.readChar(), 'a');
311: assertEquals(msg.readDouble(), 1.77E307, 0.01E307);
312: assertEquals(msg.readFloat(), (float) 44.9991, (float) 0.0001);
313: assertEquals(msg.readInt(), 65536);
314: assertEquals(msg.readLong(), 9999222233338888L);
315: assertEquals(msg.readShort(), (short) 32000);
316: assertEquals(msg.readUTF(), "UTF");
317:
318: checkMessageAttributes();
319: }
320:
321: public void testReadOfUTF() throws JMSException {
322:
323: msg.writeLong(0x0001FFFF0001FF00L);
324: msg.writeShort((short) 29999);
325: msg.writeLong(0);
326: msg.writeUTF("An UTF");
327: msg.reset();
328:
329: try {
330: msg.readUTF();
331: fail();
332: } catch (MessageFormatException ex) {
333: }
334: assertEquals(msg.readInt(), 0x1FFFF);
335: try {
336: msg.readUTF();
337: fail();
338: } catch (MessageFormatException ex) {
339: }
340: assertEquals(msg.readInt(), 0x0001FF00);
341: assertEquals(msg.readShort(), (short) 29999);
342: assertEquals(msg.readLong(), 0);
343: assertEquals(msg.readUTF(), "An UTF");
344: }
345:
346: public void testCloneOfWritableMessage() throws JMSException {
347:
348: msg.writeByte((byte) 253);
349: msg.writeByte((byte) 254);
350: msg.writeByte((byte) 255);
351:
352: BytesMessage msg1 = new BytesMessageImpl(msg);
353:
354: try {
355: msg.readByte();
356: fail();
357: } catch (MessageNotReadableException ex) {
358: }
359: msg.writeChar('X');
360: msg.reset();
361:
362: byte[] bytes = new byte[5];
363: assertEquals(msg.readBytes(bytes), 5);
364: assertEquals(bytes[0], (byte) 253);
365: assertEquals(bytes[1], (byte) 254);
366: assertEquals(bytes[2], (byte) 255);
367: assertEquals(bytes[3], (byte) (8 >> 'X'));
368: assertEquals(bytes[4], (byte) 'X');
369: msg.reset();
370: bytes = new byte[3];
371: assertEquals(msg.readBytes(bytes, 3), 3);
372:
373: try {
374: msg1.readByte();
375: fail();
376: } catch (MessageNotReadableException ex) {
377: }
378: msg1.reset();
379: compareBytes(bytes, msg1);
380:
381: checkMessageAttributes(msg1);
382: checkMessageAttributes();
383: }
384:
385: private void compareBytes(byte[] sourceBytes, BytesMessage msg)
386: throws JMSException {
387: byte[] msgBytes1 = new byte[sourceBytes.length];
388: byte[] msgBytes2 = new byte[sourceBytes.length];
389:
390: assertEquals(msg.readBytes(msgBytes1), sourceBytes.length);
391: assertEquals(msg.readBytes(msgBytes2), -1);
392: for (int i = 0; i < sourceBytes.length; i++) {
393: assertEquals(sourceBytes[i], msgBytes1[i]);
394: assertEquals(msgBytes2[i], 0);
395: }
396: }
397:
398: }
|