0001: package org.mockejb.jms.test;
0002:
0003: import javax.jms.*;
0004:
0005: import org.mockejb.jms.MessageUtility;
0006: import org.mockejb.jms.StreamMessageImpl;
0007:
0008: /**
0009: * Tests <code>StreamMessageImpl</code>.
0010: *
0011: * @author Dimitar Gospodinov
0012: */
0013: public class StreamMessageImplTest extends MessageTester {
0014:
0015: private StreamMessage msg;
0016:
0017: public StreamMessageImplTest(String name) {
0018: super (name);
0019: }
0020:
0021: protected void setUp() throws Exception {
0022: msg = new StreamMessageImpl();
0023: message = msg;
0024: super .setUp();
0025: }
0026:
0027: protected void tearDown() throws Exception {
0028: msg = null;
0029: }
0030:
0031: public void testMessageNotReadable() throws JMSException {
0032:
0033: try {
0034: msg.readBoolean();
0035: fail();
0036: } catch (MessageNotReadableException ex) {
0037: }
0038: try {
0039: msg.readByte();
0040: fail();
0041: } catch (MessageNotReadableException ex) {
0042: }
0043: try {
0044: msg.readBytes(new byte[5]);
0045: fail();
0046: } catch (MessageNotReadableException ex) {
0047: }
0048: try {
0049: msg.readChar();
0050: fail();
0051: } catch (MessageNotReadableException ex) {
0052: }
0053: try {
0054: msg.readDouble();
0055: fail();
0056: } catch (MessageNotReadableException ex) {
0057: }
0058: try {
0059: msg.readFloat();
0060: fail();
0061: } catch (MessageNotReadableException ex) {
0062: }
0063: try {
0064: msg.readInt();
0065: fail();
0066: } catch (MessageNotReadableException ex) {
0067: }
0068: try {
0069: msg.readLong();
0070: fail();
0071: } catch (MessageNotReadableException ex) {
0072: }
0073: try {
0074: msg.readShort();
0075: fail();
0076: } catch (MessageNotReadableException ex) {
0077: }
0078: try {
0079: msg.readString();
0080: fail();
0081: } catch (MessageNotReadableException ex) {
0082: }
0083: try {
0084: msg.readObject();
0085: fail();
0086: } catch (MessageNotReadableException ex) {
0087: }
0088: checkMessageAttributes();
0089: }
0090:
0091: public void testMessageNotWritable() throws JMSException {
0092:
0093: msg.reset();
0094:
0095: try {
0096: msg.writeBoolean(true);
0097: fail();
0098: } catch (MessageNotWriteableException ex) {
0099: }
0100: try {
0101: msg.writeByte((byte) 1);
0102: fail();
0103: } catch (MessageNotWriteableException ex) {
0104: }
0105: try {
0106: msg.writeBytes(new byte[5]);
0107: fail();
0108: } catch (MessageNotWriteableException ex) {
0109: }
0110: try {
0111: msg.writeBytes(null);
0112: fail();
0113: } catch (MessageNotWriteableException ex) {
0114: }
0115: try {
0116: msg.writeBytes(new byte[5], 0, 1);
0117: fail();
0118: } catch (MessageNotWriteableException ex) {
0119: }
0120: try {
0121: msg.writeBytes(null, 0, 1);
0122: fail();
0123: } catch (MessageNotWriteableException ex) {
0124: }
0125: try {
0126: msg.writeChar('a');
0127: fail();
0128: } catch (MessageNotWriteableException ex) {
0129: }
0130: try {
0131: msg.writeDouble(123.33);
0132: fail();
0133: } catch (MessageNotWriteableException ex) {
0134: }
0135: try {
0136: msg.writeFloat((float) 22.22);
0137: fail();
0138: } catch (MessageNotWriteableException ex) {
0139: }
0140: try {
0141: msg.writeInt(122);
0142: fail();
0143: } catch (MessageNotWriteableException ex) {
0144: }
0145: try {
0146: msg.writeLong(11);
0147: fail();
0148: } catch (MessageNotWriteableException ex) {
0149: }
0150: try {
0151: msg.writeShort((short) 88);
0152: fail();
0153: } catch (MessageNotWriteableException ex) {
0154: }
0155: try {
0156: msg.writeObject(null);
0157: fail();
0158: } catch (MessageNotWriteableException ex) {
0159: }
0160: try {
0161: msg.writeString("string");
0162: fail();
0163: } catch (MessageNotWriteableException ex) {
0164: }
0165: checkMessageAttributes();
0166: }
0167:
0168: private void checkTestStreamMessage(StreamMessage msg)
0169: throws JMSException {
0170: byte[] bytesData = new byte[4];
0171: assertTrue(msg.readBoolean());
0172: assertEquals(msg.readBytes(new byte[1]), -1);
0173: assertNull(msg.readObject());
0174: assertEquals(msg.readByte(), (byte) 255);
0175:
0176: assertEquals(msg.readBytes(bytesData), 4);
0177: assertEquals(bytesData[0], (byte) 1);
0178: assertEquals(bytesData[1], (byte) 2);
0179: assertEquals(bytesData[2], (byte) 3);
0180: assertEquals(bytesData[3], (byte) 4);
0181: assertEquals(msg.readBytes(bytesData), -1);
0182: assertEquals(msg.readBytes(bytesData), 1);
0183: assertEquals(bytesData[0], (byte) 2);
0184: assertEquals(msg.readBytes(bytesData), -1);
0185:
0186: assertEquals(msg.readChar(), 'a');
0187: assertEquals(msg.readDouble(), 1.77E307, 0.01E307);
0188: assertEquals(msg.readFloat(), (float) 44.9991, (float) 0.0001);
0189: assertEquals(msg.readInt(), 65536);
0190: assertEquals(msg.readLong(), 9999222233338888L);
0191: assertEquals(msg.readShort(), (short) 32000);
0192: assertEquals(msg.readString(), "String 1");
0193: assertEquals(msg.readString(), "String 2");
0194:
0195: /*
0196: * At this point all bytes from msg have been read. Any read must throw EOF
0197: * exception.
0198: */
0199: try {
0200: msg.readBoolean();
0201: fail();
0202: } catch (MessageEOFException ex) {
0203: }
0204: try {
0205: msg.readByte();
0206: fail();
0207: } catch (MessageEOFException ex) {
0208: }
0209: try {
0210: msg.readBytes(new byte[1]);
0211: fail();
0212: } catch (MessageEOFException ex) {
0213: }
0214: try {
0215: msg.readChar();
0216: fail();
0217: } catch (MessageEOFException ex) {
0218: }
0219: try {
0220: msg.readShort();
0221: fail();
0222: } catch (MessageEOFException ex) {
0223: }
0224: try {
0225: msg.readInt();
0226: fail();
0227: } catch (MessageEOFException ex) {
0228: }
0229: try {
0230: msg.readLong();
0231: fail();
0232: } catch (MessageEOFException ex) {
0233: }
0234: try {
0235: msg.readFloat();
0236: fail();
0237: } catch (MessageEOFException ex) {
0238: }
0239: try {
0240: msg.readDouble();
0241: fail();
0242: } catch (MessageEOFException ex) {
0243: }
0244: try {
0245: msg.readObject();
0246: fail();
0247: } catch (MessageEOFException ex) {
0248: }
0249: try {
0250: msg.readString();
0251: fail();
0252: } catch (MessageEOFException ex) {
0253: }
0254: }
0255:
0256: public void testStreamMessage() throws Throwable {
0257:
0258: byte[] bytesData1 = { 1, 2, 3, 4 };
0259:
0260: // Write to BytesMessage and DataOutputStream and then compare
0261:
0262: msg.writeBoolean(true);
0263: msg.writeBytes(null);
0264: msg.writeObject(null);
0265: msg.writeByte((byte) 255);
0266: msg.writeBytes(bytesData1);
0267: msg.writeBytes(bytesData1, 1, 1);
0268: msg.writeChar('a');
0269: msg.writeDouble(1.77E307);
0270: msg.writeFloat((float) 44.9991);
0271: msg.writeInt(65536);
0272: msg.writeLong(9999222233338888L);
0273: msg.writeShort((short) 32000);
0274: msg.writeString("String 1");
0275: msg.writeString("String 2");
0276:
0277: int repetitions = 2;
0278: while (repetitions-- > 0) {
0279: msg.reset();
0280: checkTestStreamMessage(msg);
0281: }
0282: msg.reset();
0283:
0284: StreamMessage msg1 = new StreamMessageImpl(msg);
0285: msg1.reset();
0286: checkTestStreamMessage(msg1);
0287:
0288: checkMessageAttributes(msg1);
0289: checkMessageAttributes();
0290:
0291: assertTrue(MessageUtility.compare(msg, msg1));
0292: }
0293:
0294: public void testBoolean() throws JMSException {
0295:
0296: msg.writeBoolean(true);
0297: try {
0298: msg.readBoolean();
0299: fail();
0300: } catch (MessageNotReadableException ex) {
0301: }
0302: msg.reset();
0303: assertTrue(msg.readBoolean());
0304: msg.reset();
0305: assertEquals(msg.readString(), "true");
0306: msg.reset();
0307: Object o = msg.readObject();
0308: assertTrue(o instanceof Boolean);
0309: assertTrue(((Boolean) o).booleanValue());
0310:
0311: try {
0312: msg.readBoolean();
0313: fail();
0314: } catch (MessageEOFException ex) {
0315: }
0316:
0317: msg.reset();
0318: try {
0319: msg.readChar();
0320: fail();
0321: } catch (MessageFormatException ex) {
0322: }
0323: try {
0324: msg.readByte();
0325: fail();
0326: } catch (MessageFormatException ex) {
0327: }
0328: try {
0329: msg.readShort();
0330: fail();
0331: } catch (MessageFormatException ex) {
0332: }
0333: try {
0334: msg.readInt();
0335: fail();
0336: } catch (MessageFormatException ex) {
0337: }
0338: try {
0339: msg.readLong();
0340: fail();
0341: } catch (MessageFormatException ex) {
0342: }
0343: try {
0344: msg.readFloat();
0345: fail();
0346: } catch (MessageFormatException ex) {
0347: }
0348: try {
0349: msg.readDouble();
0350: fail();
0351: } catch (MessageFormatException ex) {
0352: }
0353: try {
0354: msg.readBytes(new byte[1]);
0355: fail();
0356: } catch (MessageFormatException ex) {
0357: }
0358:
0359: checkMessageAttributes();
0360: }
0361:
0362: public void testByte() throws JMSException {
0363:
0364: msg.writeByte((byte) 127);
0365: try {
0366: msg.readByte();
0367: fail();
0368: } catch (MessageNotReadableException ex) {
0369: }
0370: msg.reset();
0371: assertEquals(msg.readByte(), (byte) 127);
0372: msg.reset();
0373: assertEquals(msg.readShort(), (short) 127);
0374: msg.reset();
0375: assertEquals(msg.readInt(), 127);
0376: msg.reset();
0377: assertEquals(msg.readLong(), 127L);
0378: msg.reset();
0379: assertEquals(msg.readString(), Byte.valueOf("127").toString());
0380: msg.reset();
0381: Object o = msg.readObject();
0382: assertTrue(o instanceof Byte);
0383: assertEquals(((Byte) o).byteValue(), (byte) 127);
0384:
0385: try {
0386: msg.readByte();
0387: fail();
0388: } catch (MessageEOFException ex) {
0389: }
0390:
0391: msg.reset();
0392: try {
0393: msg.readBoolean();
0394: fail();
0395: } catch (MessageFormatException ex) {
0396: }
0397: try {
0398: msg.readChar();
0399: fail();
0400: } catch (MessageFormatException ex) {
0401: }
0402: try {
0403: msg.readFloat();
0404: fail();
0405: } catch (MessageFormatException ex) {
0406: }
0407: try {
0408: msg.readDouble();
0409: fail();
0410: } catch (MessageFormatException ex) {
0411: }
0412: try {
0413: msg.readBytes(new byte[1]);
0414: fail();
0415: } catch (MessageFormatException ex) {
0416: }
0417: checkMessageAttributes();
0418: }
0419:
0420: public void testChar() throws JMSException {
0421:
0422: msg.writeChar('e');
0423: try {
0424: msg.readChar();
0425: fail();
0426: } catch (MessageNotReadableException ex) {
0427: }
0428: msg.reset();
0429: assertEquals(msg.readChar(), 'e');
0430: msg.reset();
0431: assertEquals(msg.readString(), "e");
0432: msg.reset();
0433: Object o = msg.readObject();
0434: assertTrue(o instanceof Character);
0435: assertEquals(((Character) o).charValue(), 'e');
0436:
0437: try {
0438: msg.readChar();
0439: fail();
0440: } catch (MessageEOFException ex) {
0441: }
0442:
0443: msg.reset();
0444: try {
0445: msg.readBoolean();
0446: fail();
0447: } catch (MessageFormatException ex) {
0448: }
0449: try {
0450: msg.readByte();
0451: fail();
0452: } catch (MessageFormatException ex) {
0453: }
0454: try {
0455: msg.readShort();
0456: fail();
0457: } catch (MessageFormatException ex) {
0458: }
0459: try {
0460: msg.readInt();
0461: fail();
0462: } catch (MessageFormatException ex) {
0463: }
0464: try {
0465: msg.readLong();
0466: fail();
0467: } catch (MessageFormatException ex) {
0468: }
0469: try {
0470: msg.readFloat();
0471: fail();
0472: } catch (MessageFormatException ex) {
0473: }
0474: try {
0475: msg.readDouble();
0476: fail();
0477: } catch (MessageFormatException ex) {
0478: }
0479: try {
0480: msg.readBytes(new byte[1]);
0481: fail();
0482: } catch (MessageFormatException ex) {
0483: }
0484:
0485: checkMessageAttributes();
0486: }
0487:
0488: public void testShort() throws JMSException {
0489:
0490: msg.writeShort((short) 29000);
0491: try {
0492: msg.readShort();
0493: fail();
0494: } catch (MessageNotReadableException ex) {
0495: }
0496: msg.reset();
0497: assertEquals(msg.readShort(), (short) 29000);
0498: msg.reset();
0499: assertEquals(msg.readInt(), 29000);
0500: msg.reset();
0501: assertEquals(msg.readLong(), 29000L);
0502: msg.reset();
0503: assertEquals(msg.readString(), Short.valueOf("29000")
0504: .toString());
0505: msg.reset();
0506: Object o = msg.readObject();
0507: assertTrue(o instanceof Short);
0508: assertEquals(((Short) o).shortValue(), (short) 29000);
0509:
0510: try {
0511: msg.readShort();
0512: fail();
0513: } catch (MessageEOFException ex) {
0514: }
0515:
0516: msg.reset();
0517: try {
0518: msg.readBoolean();
0519: fail();
0520: } catch (MessageFormatException ex) {
0521: }
0522: try {
0523: msg.readByte();
0524: fail();
0525: } catch (MessageFormatException ex) {
0526: }
0527: try {
0528: msg.readChar();
0529: fail();
0530: } catch (MessageFormatException ex) {
0531: }
0532: try {
0533: msg.readFloat();
0534: fail();
0535: } catch (MessageFormatException ex) {
0536: }
0537: try {
0538: msg.readDouble();
0539: fail();
0540: } catch (MessageFormatException ex) {
0541: }
0542: try {
0543: msg.readBytes(new byte[1]);
0544: fail();
0545: } catch (MessageFormatException ex) {
0546: }
0547:
0548: checkMessageAttributes();
0549: }
0550:
0551: public void testInt() throws JMSException {
0552:
0553: msg.writeInt(120000);
0554: try {
0555: msg.readInt();
0556: fail();
0557: } catch (MessageNotReadableException ex) {
0558: }
0559: msg.reset();
0560: assertEquals(msg.readInt(), 120000);
0561: msg.reset();
0562: assertEquals(msg.readLong(), 120000L);
0563: msg.reset();
0564: assertEquals(msg.readString(), Integer.valueOf("120000")
0565: .toString());
0566: msg.reset();
0567: Object o = msg.readObject();
0568: assertTrue(o instanceof Integer);
0569: assertEquals(((Integer) o).intValue(), 120000);
0570:
0571: try {
0572: msg.readInt();
0573: fail();
0574: } catch (MessageEOFException ex) {
0575: }
0576: msg.reset();
0577: try {
0578: msg.readBoolean();
0579: fail();
0580: } catch (MessageFormatException ex) {
0581: }
0582: try {
0583: msg.readByte();
0584: fail();
0585: } catch (MessageFormatException ex) {
0586: }
0587: try {
0588: msg.readChar();
0589: fail();
0590: } catch (MessageFormatException ex) {
0591: }
0592: try {
0593: msg.readShort();
0594: fail();
0595: } catch (MessageFormatException ex) {
0596: }
0597: try {
0598: msg.readFloat();
0599: fail();
0600: } catch (MessageFormatException ex) {
0601: }
0602: try {
0603: msg.readDouble();
0604: fail();
0605: } catch (MessageFormatException ex) {
0606: }
0607: try {
0608: msg.readBytes(new byte[1]);
0609: fail();
0610: } catch (MessageFormatException ex) {
0611: }
0612:
0613: checkMessageAttributes();
0614: }
0615:
0616: public void testLong() throws JMSException {
0617:
0618: msg.writeLong(222200002222L);
0619: try {
0620: msg.readLong();
0621: fail();
0622: } catch (MessageNotReadableException ex) {
0623: }
0624: msg.reset();
0625: assertEquals(msg.readLong(), 222200002222L);
0626: msg.reset();
0627: assertEquals(msg.readString(), Long.valueOf("222200002222")
0628: .toString());
0629: msg.reset();
0630: Object o = msg.readObject();
0631: assertTrue(o instanceof Long);
0632: assertEquals(((Long) o).longValue(), 222200002222L);
0633:
0634: try {
0635: msg.readLong();
0636: fail();
0637: } catch (MessageEOFException ex) {
0638: }
0639:
0640: msg.reset();
0641: try {
0642: msg.readBoolean();
0643: fail();
0644: } catch (MessageFormatException ex) {
0645: }
0646: try {
0647: msg.readByte();
0648: fail();
0649: } catch (MessageFormatException ex) {
0650: }
0651: try {
0652: msg.readChar();
0653: fail();
0654: } catch (MessageFormatException ex) {
0655: }
0656: try {
0657: msg.readShort();
0658: fail();
0659: } catch (MessageFormatException ex) {
0660: }
0661: try {
0662: msg.readInt();
0663: fail();
0664: } catch (MessageFormatException ex) {
0665: }
0666: try {
0667: msg.readFloat();
0668: fail();
0669: } catch (MessageFormatException ex) {
0670: }
0671: try {
0672: msg.readDouble();
0673: fail();
0674: } catch (MessageFormatException ex) {
0675: }
0676: try {
0677: msg.readBytes(new byte[1]);
0678: fail();
0679: } catch (MessageFormatException ex) {
0680: }
0681:
0682: checkMessageAttributes();
0683: }
0684:
0685: public void testFloat() throws JMSException {
0686:
0687: msg.writeFloat((float) 1678.1234);
0688: try {
0689: msg.readFloat();
0690: fail();
0691: } catch (MessageNotReadableException ex) {
0692: }
0693: msg.reset();
0694: assertEquals(msg.readFloat(), (float) 1678.1234, (float) 0.0001);
0695: msg.reset();
0696: assertEquals(msg.readDouble(), 1678.1234, 0.0001);
0697: msg.reset();
0698: assertEquals(msg.readString(), Float.valueOf("1678.1234")
0699: .toString());
0700: msg.reset();
0701: Object o = msg.readObject();
0702: assertTrue(o instanceof Float);
0703: assertEquals(((Float) o).floatValue(), (float) 1678.1234,
0704: (float) 0.0001);
0705:
0706: try {
0707: msg.readFloat();
0708: fail();
0709: } catch (MessageEOFException ex) {
0710: }
0711:
0712: msg.reset();
0713: try {
0714: msg.readBoolean();
0715: fail();
0716: } catch (MessageFormatException ex) {
0717: }
0718: try {
0719: msg.readByte();
0720: fail();
0721: } catch (MessageFormatException ex) {
0722: }
0723: try {
0724: msg.readChar();
0725: fail();
0726: } catch (MessageFormatException ex) {
0727: }
0728: try {
0729: msg.readShort();
0730: fail();
0731: } catch (MessageFormatException ex) {
0732: }
0733: try {
0734: msg.readInt();
0735: fail();
0736: } catch (MessageFormatException ex) {
0737: }
0738: try {
0739: msg.readLong();
0740: fail();
0741: } catch (MessageFormatException ex) {
0742: }
0743: try {
0744: msg.readBytes(new byte[1]);
0745: fail();
0746: } catch (MessageFormatException ex) {
0747: }
0748: checkMessageAttributes();
0749: }
0750:
0751: public void testDouble() throws JMSException {
0752:
0753: msg.writeDouble(9.989667788E304);
0754: try {
0755: msg.readDouble();
0756: fail();
0757: } catch (MessageNotReadableException ex) {
0758: }
0759: msg.reset();
0760: assertEquals(msg.readDouble(), 9.989667788E304, 0.000000001E304);
0761: msg.reset();
0762: assertEquals(msg.readString(), Double
0763: .valueOf("9.989667788E304").toString());
0764: msg.reset();
0765: Object o = msg.readObject();
0766: assertTrue(o instanceof Double);
0767: assertEquals(((Double) o).doubleValue(), 9.989667788E304,
0768: 0.000000001E304);
0769:
0770: try {
0771: msg.readDouble();
0772: fail();
0773: } catch (MessageEOFException ex) {
0774: }
0775: msg.reset();
0776: try {
0777: msg.readBoolean();
0778: fail();
0779: } catch (MessageFormatException ex) {
0780: }
0781: try {
0782: msg.readByte();
0783: fail();
0784: } catch (MessageFormatException ex) {
0785: }
0786: try {
0787: msg.readChar();
0788: fail();
0789: } catch (MessageFormatException ex) {
0790: }
0791: try {
0792: msg.readShort();
0793: fail();
0794: } catch (MessageFormatException ex) {
0795: }
0796: try {
0797: msg.readInt();
0798: fail();
0799: } catch (MessageFormatException ex) {
0800: }
0801: try {
0802: msg.readLong();
0803: fail();
0804: } catch (MessageFormatException ex) {
0805: }
0806: try {
0807: msg.readFloat();
0808: fail();
0809: } catch (MessageFormatException ex) {
0810: }
0811: try {
0812: msg.readBytes(new byte[1]);
0813: fail();
0814: } catch (MessageFormatException ex) {
0815: }
0816:
0817: checkMessageAttributes();
0818: }
0819:
0820: public void testString() throws JMSException {
0821:
0822: msg.writeString("123");
0823: try {
0824: msg.readString();
0825: fail();
0826: } catch (MessageNotReadableException ex) {
0827: }
0828: msg.reset();
0829: assertEquals(msg.readString(), "123");
0830: msg.reset();
0831: assertEquals(msg.readByte(), Byte.valueOf("123").byteValue());
0832: msg.reset();
0833: Object o = msg.readObject();
0834: assertTrue(o instanceof String);
0835: assertEquals(o.toString(), "123");
0836:
0837: try {
0838: msg.readString();
0839: fail();
0840: } catch (MessageEOFException ex) {
0841: }
0842:
0843: msg.clearBody();
0844:
0845: msg.writeObject("124");
0846: msg.reset();
0847: o = msg.readObject();
0848: assertTrue(o instanceof String);
0849: assertEquals(o.toString(), "124");
0850: msg.reset();
0851: assertEquals(msg.readShort(), Short.valueOf("124").shortValue());
0852: msg.reset();
0853: assertEquals(msg.readInt(), Integer.valueOf("124").intValue());
0854: msg.reset();
0855: assertEquals(msg.readLong(), Long.valueOf("124").longValue());
0856: msg.reset();
0857: try {
0858: msg.readChar();
0859: fail();
0860: } catch (MessageFormatException ex) {
0861: }
0862: msg.clearBody();
0863: msg.writeString("a");
0864: msg.reset();
0865: try {
0866: msg.readChar();
0867: fail();
0868: } catch (MessageFormatException ex) {
0869: }
0870: try {
0871: msg.readBytes(new byte[1]);
0872: fail();
0873: } catch (MessageFormatException ex) {
0874: }
0875: msg.clearBody();
0876: msg.writeString("123.55");
0877: msg.reset();
0878: assertEquals(msg.readFloat(), Float.valueOf("123.55")
0879: .floatValue(), (float) 0.01);
0880: msg.reset();
0881: assertEquals(msg.readDouble(), Double.valueOf("123.55")
0882: .doubleValue(), 0.01);
0883:
0884: msg.clearBody();
0885: msg.writeString(Boolean.TRUE.toString());
0886: msg.reset();
0887: assertTrue(msg.readBoolean());
0888:
0889: msg.clearBody();
0890: msg.writeString(null);
0891: msg.reset();
0892: assertNull(msg.readObject());
0893: msg.reset();
0894: assertNull(msg.readString());
0895:
0896: checkMessageAttributes();
0897: }
0898:
0899: public void testObject() throws JMSException {
0900: try {
0901: msg.writeObject(new Object());
0902: fail();
0903: } catch (MessageFormatException ex) {
0904: }
0905: msg.writeObject(null);
0906: try {
0907: msg.readObject();
0908: fail();
0909: } catch (MessageNotReadableException ex) {
0910: }
0911: msg.writeObject(new Boolean(false));
0912: msg.writeObject(new Byte((byte) 101));
0913: msg.writeObject(new byte[] { 11, 22, 33 });
0914: msg.writeObject(new Character('t'));
0915: msg.writeObject(new Short((short) 29999));
0916: msg.writeObject(new Integer(65537));
0917: msg.writeObject(new Long(888880000011111L));
0918: msg.writeObject(new Float((float) 7788.001));
0919: msg.writeObject(new Double(-1.889911001E-301));
0920: msg.writeObject(new String("String"));
0921:
0922: msg.reset();
0923:
0924: try {
0925: msg.writeObject(null);
0926: fail();
0927: } catch (MessageNotWriteableException ex) {
0928: }
0929: assertNull(msg.readObject());
0930: Object o = msg.readObject();
0931: assertTrue(o instanceof Boolean);
0932: assertFalse(((Boolean) o).booleanValue());
0933: o = msg.readObject();
0934: assertTrue(o instanceof Byte);
0935: assertEquals(((Byte) o).byteValue(), (byte) 101);
0936: o = msg.readObject();
0937: assertTrue(o instanceof byte[]);
0938: assertEquals(((byte[]) o).length, 3);
0939: assertEquals(((byte[]) o)[0], (byte) 11);
0940: assertEquals(((byte[]) o)[1], (byte) 22);
0941: assertEquals(((byte[]) o)[2], (byte) 33);
0942: o = msg.readObject();
0943: assertTrue(o instanceof Character);
0944: assertEquals(((Character) o).charValue(), 't');
0945: o = msg.readObject();
0946: assertTrue(o instanceof Short);
0947: assertEquals(((Short) o).shortValue(), (short) 29999);
0948: o = msg.readObject();
0949: assertTrue(o instanceof Integer);
0950: assertEquals(((Integer) o).intValue(), 65537);
0951: o = msg.readObject();
0952: assertTrue(o instanceof Long);
0953: assertEquals(((Long) o).longValue(), 888880000011111L);
0954: o = msg.readObject();
0955: assertTrue(o instanceof Float);
0956: assertEquals(((Float) o).floatValue(), (float) 7788.001,
0957: (float) 0.001);
0958: o = msg.readObject();
0959: assertTrue(o instanceof Double);
0960: assertEquals(((Double) o).doubleValue(), -1.889911001E-301,
0961: 0.000000001E-301);
0962: o = msg.readObject();
0963: assertTrue(o instanceof String);
0964: assertEquals(o, "String");
0965:
0966: checkMessageAttributes();
0967: }
0968:
0969: public void testByteArray() throws JMSException {
0970:
0971: byte[] bytes1 = { 1, 0, 1, 0, 11, 127, -128, 11, 15 };
0972:
0973: msg.writeBytes(bytes1);
0974: msg.writeBytes(bytes1, 2, 5);
0975: for (int i = 0; i < bytes1.length; bytes1[i++] = 0)
0976: ;
0977:
0978: try {
0979: msg.readBytes(new byte[1]);
0980: fail();
0981: } catch (MessageNotReadableException ex) {
0982: }
0983: try {
0984: msg.writeBytes(bytes1, -1, 1);
0985: fail();
0986: } catch (IllegalArgumentException ex) {
0987: }
0988: try {
0989: msg.writeBytes(bytes1, 1, -1);
0990: fail();
0991: } catch (IllegalArgumentException ex) {
0992: }
0993: try {
0994: msg.writeBytes(bytes1, 1, 111);
0995: fail();
0996: } catch (IllegalArgumentException ex) {
0997: }
0998:
0999: msg.writeBytes(null);
1000: msg.writeBytes(null, 0, 0);
1001: msg.reset();
1002:
1003: bytes1 = new byte[6];
1004: assertEquals(msg.readBytes(bytes1), 6);
1005: assertEquals(bytes1[0], (byte) 1);
1006: assertEquals(bytes1[1], (byte) 0);
1007: assertEquals(bytes1[2], (byte) 1);
1008: assertEquals(bytes1[3], (byte) 0);
1009: assertEquals(bytes1[4], (byte) 11);
1010: assertEquals(bytes1[5], (byte) 127);
1011: assertEquals(msg.readBytes(bytes1), 3);
1012: assertEquals(bytes1[0], (byte) -128);
1013: assertEquals(bytes1[1], (byte) 11);
1014: assertEquals(bytes1[2], (byte) 15);
1015: assertEquals(bytes1[3], (byte) 0);
1016: assertEquals(bytes1[4], (byte) 11);
1017: assertEquals(bytes1[5], (byte) 127);
1018: assertEquals(msg.readBytes(bytes1), -1);
1019: assertEquals(bytes1[0], (byte) -128);
1020: assertEquals(bytes1[1], (byte) 11);
1021: assertEquals(bytes1[2], (byte) 15);
1022: assertEquals(bytes1[3], (byte) 0);
1023: assertEquals(bytes1[4], (byte) 11);
1024: assertEquals(bytes1[5], (byte) 127);
1025:
1026: assertEquals(msg.readBytes(bytes1), 5);
1027: assertEquals(bytes1[0], (byte) 1);
1028: assertEquals(bytes1[1], (byte) 0);
1029: assertEquals(bytes1[2], (byte) 11);
1030: assertEquals(bytes1[3], (byte) 127);
1031: assertEquals(bytes1[4], (byte) -128);
1032: assertEquals(bytes1[5], (byte) 127);
1033: assertEquals(msg.readBytes(bytes1), -1);
1034: assertEquals(bytes1[0], (byte) 1);
1035: assertEquals(bytes1[1], (byte) 0);
1036: assertEquals(bytes1[2], (byte) 11);
1037: assertEquals(bytes1[3], (byte) 127);
1038: assertEquals(bytes1[4], (byte) -128);
1039: assertEquals(bytes1[5], (byte) 127);
1040:
1041: // Read two null values and check that the bytes array is not modified
1042: assertEquals(msg.readBytes(bytes1), -1);
1043: assertEquals(bytes1[0], (byte) 1);
1044: assertEquals(bytes1[1], (byte) 0);
1045: assertEquals(bytes1[2], (byte) 11);
1046: assertEquals(bytes1[3], (byte) 127);
1047: assertEquals(bytes1[4], (byte) -128);
1048: assertEquals(bytes1[5], (byte) 127);
1049:
1050: assertEquals(msg.readBytes(bytes1), -1);
1051: assertEquals(bytes1[0], (byte) 1);
1052: assertEquals(bytes1[1], (byte) 0);
1053: assertEquals(bytes1[2], (byte) 11);
1054: assertEquals(bytes1[3], (byte) 127);
1055: assertEquals(bytes1[4], (byte) -128);
1056: assertEquals(bytes1[5], (byte) 127);
1057:
1058: try {
1059: msg.readObject();
1060: fail();
1061: } catch (MessageEOFException ex) {
1062: }
1063:
1064: msg.reset();
1065:
1066: try {
1067: msg.readBoolean();
1068: fail();
1069: } catch (MessageFormatException ex) {
1070: }
1071: try {
1072: msg.readByte();
1073: fail();
1074: } catch (MessageFormatException ex) {
1075: }
1076: try {
1077: msg.readChar();
1078: fail();
1079: } catch (MessageFormatException ex) {
1080: }
1081: try {
1082: msg.readShort();
1083: fail();
1084: } catch (MessageFormatException ex) {
1085: }
1086: try {
1087: msg.readInt();
1088: fail();
1089: } catch (MessageFormatException ex) {
1090: }
1091: try {
1092: msg.readLong();
1093: fail();
1094: } catch (MessageFormatException ex) {
1095: }
1096: try {
1097: msg.readFloat();
1098: fail();
1099: } catch (MessageFormatException ex) {
1100: }
1101: try {
1102: msg.readDouble();
1103: fail();
1104: } catch (MessageFormatException ex) {
1105: }
1106: try {
1107: msg.readString();
1108: fail();
1109: } catch (MessageFormatException ex) {
1110: }
1111: checkMessageAttributes();
1112: }
1113:
1114: public void testClone() throws JMSException {
1115:
1116: msg.writeByte((byte) 253);
1117: msg.writeBytes(new byte[] { 55, 66, 77 });
1118: msg.writeString("string");
1119:
1120: StreamMessage msg1 = new StreamMessageImpl(msg);
1121:
1122: try {
1123: msg.readObject();
1124: fail();
1125: } catch (MessageNotReadableException ex) {
1126: }
1127: try {
1128: msg1.readObject();
1129: fail();
1130: } catch (MessageNotReadableException ex) {
1131: }
1132:
1133: assertTrue(MessageUtility.compare(msg, msg1));
1134:
1135: msg.writeChar('u');
1136: msg1.writeChar('x');
1137:
1138: msg.reset();
1139: assertEquals(msg.readByte(), (byte) 253);
1140: byte[] bytes = new byte[3];
1141: assertEquals(msg.readBytes(bytes), 3);
1142: assertEquals(msg.readBytes(bytes), -1);
1143: assertEquals(bytes[0], (byte) 55);
1144: assertEquals(bytes[1], (byte) 66);
1145: assertEquals(bytes[2], (byte) 77);
1146: assertEquals(msg.readString(), "string");
1147: assertEquals(msg.readChar(), 'u');
1148: try {
1149: msg.readObject();
1150: fail();
1151: } catch (MessageEOFException ex) {
1152: }
1153:
1154: msg1.reset();
1155: assertEquals(msg1.readByte(), (byte) 253);
1156: bytes = new byte[3];
1157: assertEquals(msg1.readBytes(bytes), 3);
1158: assertEquals(msg1.readBytes(bytes), -1);
1159: assertEquals(bytes[0], (byte) 55);
1160: assertEquals(bytes[1], (byte) 66);
1161: assertEquals(bytes[2], (byte) 77);
1162: assertEquals(msg1.readString(), "string");
1163: assertEquals(msg1.readChar(), 'x');
1164: try {
1165: msg1.readObject();
1166: fail();
1167: } catch (MessageEOFException ex) {
1168: }
1169:
1170: msg.reset();
1171: msg.readObject();
1172: msg.readObject();
1173: msg1 = new StreamMessageImpl(msg);
1174:
1175: assertEquals(msg.readString(), "string");
1176: assertEquals(msg.readChar(), 'u');
1177: try {
1178: msg.readObject();
1179: fail();
1180: } catch (MessageEOFException ex) {
1181: }
1182:
1183: msg1.writeChar('f');
1184: msg1.reset();
1185:
1186: checkMessageAttributes(msg1);
1187:
1188: assertEquals(msg1.readByte(), (byte) 253);
1189: bytes = new byte[3];
1190: assertEquals(msg1.readBytes(bytes), 3);
1191: assertEquals(msg1.readBytes(bytes), -1);
1192: assertEquals(bytes[0], (byte) 55);
1193: assertEquals(bytes[1], (byte) 66);
1194: assertEquals(bytes[2], (byte) 77);
1195: assertEquals(msg1.readString(), "string");
1196: assertEquals(msg1.readChar(), 'u');
1197: assertEquals(msg1.readChar(), 'f');
1198: try {
1199: msg1.readObject();
1200: fail();
1201: } catch (MessageEOFException ex) {
1202: }
1203:
1204: checkMessageAttributes(msg1);
1205: checkMessageAttributes();
1206: }
1207:
1208: }
|