001: package org.mockejb.jms.test;
002:
003: import junit.framework.TestCase;
004: import javax.jms.*;
005:
006: import org.mockejb.jms.MessageImpl;
007: import org.mockejb.jms.MockQueue;
008:
009: import java.util.Enumeration;
010:
011: /**
012: * Tests <code>MessageImpl</code>.
013: *
014: * @author Dimitar Gospodinov
015: */
016: public class MessageImplTest extends TestCase {
017:
018: public MessageImplTest(String name) {
019: super (name);
020: }
021:
022: public void testHeaderFields() throws JMSException {
023:
024: String jmsCorrelationId = "CorrelationId";
025: int jmsDeliveryMode = DeliveryMode.NON_PERSISTENT;
026: Destination jmsDestination = new MockQueue("Destination");
027: long jmsExpiration = 6789;
028: String jmsMessageId = "ID:MyID";
029: int jmsPriority = 6;
030: Destination jmsReplyTo = new MockQueue("ReplyToQueue");
031: long jmsTimestamp = 12345;
032: String jmsType = "MyType";
033:
034: Message msg = new MessageImpl();
035:
036: msg.setJMSCorrelationID(jmsCorrelationId);
037: msg.setJMSDeliveryMode(jmsDeliveryMode);
038: msg.setJMSDestination(jmsDestination);
039: msg.setJMSExpiration(jmsExpiration);
040: msg.setJMSMessageID(jmsMessageId);
041: msg.setJMSPriority(jmsPriority);
042: msg.setJMSReplyTo(jmsReplyTo);
043: msg.setJMSTimestamp(jmsTimestamp);
044: msg.setJMSType(jmsType);
045:
046: assertEquals(jmsCorrelationId, msg.getJMSCorrelationID());
047: assertEquals(jmsDeliveryMode, msg.getJMSDeliveryMode());
048: assertSame(jmsDestination, msg.getJMSDestination());
049: assertEquals(jmsExpiration, msg.getJMSExpiration());
050: assertEquals(jmsMessageId, msg.getJMSMessageID());
051: assertEquals(jmsPriority, msg.getJMSPriority());
052: assertSame(jmsReplyTo, msg.getJMSReplyTo());
053: assertEquals(jmsTimestamp, msg.getJMSTimestamp());
054: assertEquals(jmsType, msg.getJMSType());
055:
056: assertFalse(msg.getJMSRedelivered());
057: msg.setJMSRedelivered(true);
058: assertFalse(msg.getJMSRedelivered());
059:
060: Message msg1 = new MessageImpl(msg);
061: assertEquals(msg1.getJMSCorrelationID(), msg
062: .getJMSCorrelationID());
063: assertEquals(msg1.getJMSDeliveryMode(), msg
064: .getJMSDeliveryMode());
065: assertSame(msg1.getJMSDestination(), msg.getJMSDestination());
066: assertEquals(msg1.getJMSExpiration(), msg.getJMSExpiration());
067: assertEquals(msg1.getJMSMessageID(), msg.getJMSMessageID());
068: assertEquals(msg1.getJMSPriority(), msg.getJMSPriority());
069: assertSame(msg1.getJMSReplyTo(), msg.getJMSReplyTo());
070: assertEquals(msg1.getJMSTimestamp(), msg.getJMSTimestamp());
071: assertEquals(msg1.getJMSType(), msg.getJMSType());
072: assertEquals(msg1.getJMSRedelivered(), msg.getJMSRedelivered());
073: }
074:
075: public void testProperties() throws JMSException {
076: Message msg = new MessageImpl();
077:
078: assertFalse(msg.getPropertyNames().hasMoreElements());
079: assertNull(msg.getObjectProperty("some name"));
080:
081: msg.setBooleanProperty("boolean", false);
082: msg.setByteProperty("byte", (byte) 127);
083: msg.setShortProperty("short", (short) 8192);
084: msg.setShortProperty("short", (short) 8193);
085: msg.setIntProperty("int", 65536);
086: msg.setLongProperty("long", 400111000111L);
087: msg.setFloatProperty("float", (float) 34.8);
088: msg.setDoubleProperty("double", 23232311111.8821);
089: msg.setStringProperty("string", "str_val");
090: Object obj = Long.valueOf("900111000111");
091: msg.setObjectProperty("obj_long", obj);
092: msg.setBooleanProperty("boolean1", true);
093:
094: checkProperties(msg, obj);
095: checkProperties(new MessageImpl(msg), obj);
096:
097: msg.clearProperties();
098: assertFalse(msg.getPropertyNames().hasMoreElements());
099:
100: try {
101: msg.setObjectProperty("obj", new Object());
102: fail();
103: } catch (MessageFormatException ex) {
104: }
105: }
106:
107: private void checkProperties(Message msg, Object objPropertyValue)
108: throws JMSException {
109: Enumeration e = msg.getPropertyNames();
110: int i = 0;
111: while (e.hasMoreElements()) {
112: String name = (String) e.nextElement();
113: if (name.equals("boolean")) {
114: assertFalse(msg.getBooleanProperty("boolean"));
115: } else if (name.equals("byte")) {
116: assertEquals(msg.getByteProperty("byte"), (byte) 127);
117: } else if (name.equals("short")) {
118: assertEquals(msg.getShortProperty("short"),
119: (short) 8193);
120: } else if (name.equals("int")) {
121: assertEquals(msg.getIntProperty("int"), 65536);
122: } else if (name.equals("long")) {
123: assertEquals(msg.getLongProperty("long"), 400111000111L);
124: } else if (name.equals("float")) {
125: assertEquals(msg.getFloatProperty("float"),
126: (float) 34.8, (float) 0.1);
127: } else if (name.equals("double")) {
128: assertEquals(msg.getDoubleProperty("double"),
129: 23232311111.8821, 0.0001);
130: } else if (name.equals("string")) {
131: assertEquals(msg.getStringProperty("string"), "str_val");
132: } else if (name.equals("obj_long")) {
133: assertSame(msg.getObjectProperty("obj_long"),
134: objPropertyValue);
135: } else if (name.equals("boolean1")) {
136: assertTrue(msg.getBooleanProperty("boolean1"));
137: } else {
138: fail();
139: }
140: i++;
141: }
142: assertEquals(i, 10);
143: }
144:
145: public void testBooleanProperty() throws JMSException {
146: Message msg = new MessageImpl();
147:
148: msg.setBooleanProperty("boolean", true);
149: assertTrue(msg.propertyExists("boolean"));
150: assertTrue(msg.getBooleanProperty("boolean"));
151: assertEquals(msg.getStringProperty("boolean"), "true");
152: assertTrue(msg.getObjectProperty("boolean") instanceof Boolean);
153: assertTrue(((Boolean) msg.getObjectProperty("boolean"))
154: .booleanValue());
155:
156: msg.setBooleanProperty("boolean", false);
157: assertFalse(msg.getBooleanProperty("boolean"));
158:
159: msg.setObjectProperty("boolean", Boolean.FALSE);
160: assertFalse(msg.getBooleanProperty("boolean"));
161:
162: try {
163: msg.getByteProperty("boolean");
164: fail();
165: } catch (MessageFormatException ex) {
166: }
167: try {
168: msg.getShortProperty("boolean");
169: fail();
170: } catch (MessageFormatException ex) {
171: }
172: try {
173: msg.getIntProperty("boolean");
174: fail();
175: } catch (MessageFormatException ex) {
176: }
177: try {
178: msg.getLongProperty("boolean");
179: fail();
180: } catch (MessageFormatException ex) {
181: }
182: try {
183: msg.getFloatProperty("boolean");
184: fail();
185: } catch (MessageFormatException ex) {
186: }
187: try {
188: msg.getDoubleProperty("boolean");
189: fail();
190: } catch (MessageFormatException ex) {
191: }
192:
193: Message msg1 = new MessageImpl(msg);
194: assertTrue(msg1.propertyExists("boolean"));
195: assertFalse(msg1.getBooleanProperty("boolean"));
196: }
197:
198: public void testByteProperty() throws JMSException {
199: Message msg = new MessageImpl();
200:
201: msg.setByteProperty("byte", (byte) 127);
202: assertTrue(msg.propertyExists("byte"));
203: assertEquals(msg.getByteProperty("byte"), (byte) 127);
204: assertEquals(msg.getShortProperty("byte"), (short) 127);
205: assertEquals(msg.getIntProperty("byte"), 127);
206: assertEquals(msg.getLongProperty("byte"), 127L);
207: assertEquals(msg.getStringProperty("byte"), Byte.valueOf("127")
208: .toString());
209: assertTrue(msg.getObjectProperty("byte") instanceof Byte);
210: assertEquals(
211: ((Byte) msg.getObjectProperty("byte")).byteValue(),
212: (byte) 127);
213:
214: msg.setObjectProperty("byte", new Byte((byte) 126));
215: assertEquals(msg.getByteProperty("byte"), (byte) 126);
216:
217: try {
218: msg.getBooleanProperty("byte");
219: fail();
220: } catch (MessageFormatException ex) {
221: }
222: try {
223: msg.getFloatProperty("byte");
224: fail();
225: } catch (MessageFormatException ex) {
226: }
227: try {
228: msg.getDoubleProperty("byte");
229: fail();
230: } catch (MessageFormatException ex) {
231: }
232:
233: msg.setByteProperty("byte", (byte) 125);
234: assertEquals(msg.getByteProperty("byte"), (byte) 125);
235:
236: Message msg1 = new MessageImpl(msg);
237: assertTrue(msg1.propertyExists("byte"));
238: assertEquals(msg1.getByteProperty("byte"), (byte) 125);
239: }
240:
241: public void testShortProperty() throws JMSException {
242: Message msg = new MessageImpl();
243: msg.setShortProperty("short", (short) 29000);
244: assertTrue(msg.propertyExists("short"));
245: assertEquals(msg.getShortProperty("short"), (short) 29000);
246: assertEquals(msg.getIntProperty("short"), 29000);
247: assertEquals(msg.getLongProperty("short"), 29000L);
248: assertEquals(msg.getStringProperty("short"), Short.valueOf(
249: "29000").toString());
250: assertTrue(msg.getObjectProperty("short") instanceof Short);
251: assertEquals(((Short) msg.getObjectProperty("short"))
252: .shortValue(), (short) 29000);
253:
254: msg.setObjectProperty("short", new Short((short) 29001));
255: assertTrue(msg.getObjectProperty("short") instanceof Short);
256: assertEquals(((Short) msg.getObjectProperty("short"))
257: .shortValue(), (short) 29001);
258:
259: try {
260: msg.getBooleanProperty("short");
261: fail();
262: } catch (MessageFormatException ex) {
263: }
264: try {
265: msg.getByteProperty("short");
266: fail();
267: } catch (MessageFormatException ex) {
268: }
269: try {
270: msg.getFloatProperty("short");
271: fail();
272: } catch (MessageFormatException ex) {
273: }
274: try {
275: msg.getDoubleProperty("short");
276: fail();
277: } catch (MessageFormatException ex) {
278: }
279:
280: msg.setShortProperty("short", (short) 29002);
281: assertEquals(msg.getShortProperty("short"), (short) 29002);
282:
283: Message msg1 = new MessageImpl(msg);
284: assertTrue(msg1.propertyExists("short"));
285: assertEquals(msg1.getShortProperty("short"), (short) 29002);
286: }
287:
288: public void testIntProperty() throws JMSException {
289: Message msg = new MessageImpl();
290: msg.setIntProperty("int", 120000);
291: assertTrue(msg.propertyExists("int"));
292: assertEquals(msg.getIntProperty("int"), 120000);
293: assertEquals(msg.getLongProperty("int"), 120000L);
294: assertEquals(msg.getStringProperty("int"), Integer.valueOf(
295: "120000").toString());
296: assertTrue(msg.getObjectProperty("int") instanceof Integer);
297: assertEquals(((Integer) msg.getObjectProperty("int"))
298: .intValue(), 120000);
299:
300: msg.setObjectProperty("int", new Integer(120001));
301: assertTrue(msg.getObjectProperty("int") instanceof Integer);
302: assertEquals(((Integer) msg.getObjectProperty("int"))
303: .intValue(), 120001);
304:
305: try {
306: msg.getBooleanProperty("int");
307: fail();
308: } catch (MessageFormatException ex) {
309: }
310: try {
311: msg.getByteProperty("int");
312: fail();
313: } catch (MessageFormatException ex) {
314: }
315: try {
316: msg.getShortProperty("int");
317: fail();
318: } catch (MessageFormatException ex) {
319: }
320: try {
321: msg.getFloatProperty("int");
322: fail();
323: } catch (MessageFormatException ex) {
324: }
325: try {
326: msg.getDoubleProperty("int");
327: fail();
328: } catch (MessageFormatException ex) {
329: }
330:
331: msg.setIntProperty("int", 240000);
332: assertEquals(msg.getIntProperty("int"), 240000);
333:
334: Message msg1 = new MessageImpl(msg);
335: assertTrue(msg1.propertyExists("int"));
336: assertEquals(msg1.getIntProperty("int"), 240000);
337: }
338:
339: public void testLongProperty() throws JMSException {
340: Message msg = new MessageImpl();
341: msg.setLongProperty("long", 222200002222L);
342: assertTrue(msg.propertyExists("long"));
343: assertEquals(msg.getLongProperty("long"), 222200002222L);
344: assertEquals(msg.getStringProperty("long"), Long.valueOf(
345: "222200002222").toString());
346: assertTrue(msg.getObjectProperty("long") instanceof Long);
347: assertEquals(
348: ((Long) msg.getObjectProperty("long")).longValue(),
349: 222200002222L);
350:
351: msg.setObjectProperty("long", new Long(222299992222L));
352: assertTrue(msg.getObjectProperty("long") instanceof Long);
353: assertEquals(
354: ((Long) msg.getObjectProperty("long")).longValue(),
355: 222299992222L);
356:
357: try {
358: msg.getBooleanProperty("long");
359: fail();
360: } catch (MessageFormatException ex) {
361: }
362: try {
363: msg.getByteProperty("long");
364: fail();
365: } catch (MessageFormatException ex) {
366: }
367: try {
368: msg.getShortProperty("long");
369: fail();
370: } catch (MessageFormatException ex) {
371: }
372: try {
373: msg.getIntProperty("long");
374: fail();
375: } catch (MessageFormatException ex) {
376: }
377: try {
378: msg.getFloatProperty("long");
379: fail();
380: } catch (MessageFormatException ex) {
381: }
382: try {
383: msg.getDoubleProperty("long");
384: fail();
385: } catch (MessageFormatException ex) {
386: }
387:
388: msg.setLongProperty("long", 222288882222L);
389: assertEquals(msg.getLongProperty("long"), 222288882222L);
390:
391: Message msg1 = new MessageImpl(msg);
392: assertTrue(msg1.propertyExists("long"));
393: assertEquals(msg1.getLongProperty("long"), 222288882222L);
394: }
395:
396: public void testFloatProperty() throws JMSException {
397: Message msg = new MessageImpl();
398: msg.setFloatProperty("float", (float) 1678.1234);
399: assertTrue(msg.propertyExists("float"));
400: assertEquals(msg.getFloatProperty("float"), (float) 1678.1234,
401: (float) 0.0001);
402: assertEquals(msg.getDoubleProperty("float"), 1678.1234, 0.0001);
403: assertEquals(msg.getStringProperty("float"), Float.valueOf(
404: "1678.1234").toString());
405: assertTrue(msg.getObjectProperty("float") instanceof Float);
406: assertEquals(((Float) msg.getObjectProperty("float"))
407: .floatValue(), (float) 1678.1234, (float) 0.0001);
408:
409: msg.setObjectProperty("float", new Float((float) 1778.1234));
410: assertTrue(msg.getObjectProperty("float") instanceof Float);
411: assertEquals(((Float) msg.getObjectProperty("float"))
412: .floatValue(), (float) 1778.1234, (float) 0.0001);
413:
414: try {
415: msg.getBooleanProperty("float");
416: fail();
417: } catch (MessageFormatException ex) {
418: }
419: try {
420: msg.getByteProperty("float");
421: fail();
422: } catch (MessageFormatException ex) {
423: }
424: try {
425: msg.getShortProperty("float");
426: fail();
427: } catch (MessageFormatException ex) {
428: }
429: try {
430: msg.getIntProperty("float");
431: fail();
432: } catch (MessageFormatException ex) {
433: }
434: try {
435: msg.getLongProperty("float");
436: fail();
437: } catch (MessageFormatException ex) {
438: }
439:
440: msg.setFloatProperty("float", (float) 1677.1004);
441: assertEquals(msg.getFloatProperty("float"), (float) 1677.1004,
442: 0.0001);
443:
444: Message msg1 = new MessageImpl(msg);
445: assertTrue(msg1.propertyExists("float"));
446: assertEquals(msg1.getFloatProperty("float"), (float) 1677.1004,
447: 0.0001);
448: }
449:
450: public void testDoubleProperty() throws JMSException {
451: Message msg = new MessageImpl();
452: msg.setDoubleProperty("double", 9.989667788E304);
453: assertTrue(msg.propertyExists("double"));
454: assertEquals(msg.getDoubleProperty("double"), 9.989667788E304,
455: 0.000000001E304);
456: assertEquals(msg.getStringProperty("double"), Double.valueOf(
457: "9.989667788E304").toString());
458: assertTrue(msg.getObjectProperty("double") instanceof Double);
459: assertEquals(((Double) msg.getObjectProperty("double"))
460: .doubleValue(), 9.989667788E304, 0.000000001E304);
461:
462: msg.setObjectProperty("double", new Double(9.911111788E304));
463: assertTrue(msg.getObjectProperty("double") instanceof Double);
464: assertEquals(((Double) msg.getObjectProperty("double"))
465: .doubleValue(), 9.911111788E304, 0.000000001E304);
466:
467: try {
468: msg.getBooleanProperty("double");
469: fail();
470: } catch (MessageFormatException ex) {
471: }
472: try {
473: msg.getByteProperty("double");
474: fail();
475: } catch (MessageFormatException ex) {
476: }
477: try {
478: msg.getShortProperty("double");
479: fail();
480: } catch (MessageFormatException ex) {
481: }
482: try {
483: msg.getIntProperty("double");
484: fail();
485: } catch (MessageFormatException ex) {
486: }
487: try {
488: msg.getLongProperty("double");
489: fail();
490: } catch (MessageFormatException ex) {
491: }
492: try {
493: msg.getFloatProperty("double");
494: fail();
495: } catch (MessageFormatException ex) {
496: }
497:
498: msg.setDoubleProperty("double", -8.7654321E-303);
499: assertEquals(msg.getDoubleProperty("double"), -8.7654321E-303,
500: 0.0000001E-303);
501:
502: Message msg1 = new MessageImpl(msg);
503: assertTrue(msg1.propertyExists("double"));
504: assertEquals(msg1.getDoubleProperty("double"), -8.7654321E-303,
505: 0.0000001E-303);
506: }
507:
508: public void testStringProperty() throws JMSException {
509: Message msg = new MessageImpl();
510: msg.setStringProperty("string", "123");
511: assertTrue(msg.propertyExists("string"));
512: assertEquals(msg.getStringProperty("string"), "123");
513: assertEquals(msg.getByteProperty("string"), Byte.valueOf("123")
514: .byteValue());
515: assertTrue(msg.getObjectProperty("string") instanceof String);
516: assertEquals(msg.getObjectProperty("string").toString(), "123");
517:
518: msg.setObjectProperty("string", "124");
519: assertTrue(msg.getObjectProperty("string") instanceof String);
520: assertEquals(msg.getObjectProperty("string").toString(), "124");
521:
522: assertEquals(msg.getShortProperty("string"), Short.valueOf(
523: "124").shortValue());
524:
525: assertEquals(msg.getIntProperty("string"), Integer.valueOf(
526: "124").intValue());
527:
528: assertEquals(msg.getLongProperty("string"), Long.valueOf("124")
529: .longValue());
530:
531: msg.setStringProperty("string", "123.55");
532: assertEquals(msg.getFloatProperty("string"), Float.valueOf(
533: "123.55").floatValue(), 0.01);
534: assertEquals(msg.getDoubleProperty("string"), Double.valueOf(
535: "123.55").doubleValue(), 0.01);
536:
537: msg.setStringProperty("string", Boolean.valueOf("true")
538: .toString());
539: assertTrue(msg.getBooleanProperty("string"));
540:
541: msg.setIntProperty("string", -5555);
542: assertEquals(msg.getIntProperty("string"), -5555);
543: assertEquals(msg.getStringProperty("string"), Integer
544: .toString(-5555));
545:
546: Message msg1 = new MessageImpl(msg);
547: assertTrue(msg1.propertyExists("string"));
548: assertEquals(msg1.getStringProperty("string"), Integer
549: .toString(-5555));
550: }
551: }
|