001: package org.mockejb.jms.test;
002:
003: import junit.framework.TestCase;
004: import java.util.Enumeration;
005: import javax.jms.*;
006:
007: import org.mockejb.jms.MockQueue;
008:
009: /**
010: * Common class for testing messages.
011: * Sets header and properties and "later" checks if
012: * values are still the same.
013: * Also tests if arbitrary message has header and properties
014: * matching the test values.
015: * @author Dimitar Gospodinov
016: */
017: class MessageTester extends TestCase {
018:
019: protected Message message;
020:
021: private String jmsCorrelationId;
022: private int jmsDeliveryMode;
023: private Destination jmsDestination;
024: private Destination jmsReplyTo;
025: private long jmsExpiration;
026: private String jmsMessageId;
027: private int jmsPriority;
028: private long jmsTimestamp;
029: private String jmsType;
030:
031: protected MessageTester(String name) {
032: super (name);
033: }
034:
035: protected void tearDown() throws Exception {
036: message = null;
037: jmsCorrelationId = null;
038: jmsDeliveryMode = 0;
039: jmsDestination = null;
040: jmsReplyTo = null;
041: jmsExpiration = 0;
042: jmsMessageId = null;
043: jmsPriority = 0;
044: jmsTimestamp = 0;
045: jmsType = null;
046: }
047:
048: protected void setUp() throws Exception {
049: jmsCorrelationId = "xxx::CorrID";
050: jmsDeliveryMode = DeliveryMode.NON_PERSISTENT;
051: jmsDestination = new MockQueue("xxx::Destination");
052: jmsReplyTo = new MockQueue("xxx::ReplyToDestination");
053: jmsExpiration = 778899;
054: jmsMessageId = "xxx::messageID";
055: jmsPriority = 7;
056: jmsTimestamp = 88889999;
057: jmsType = "xxx::Type";
058:
059: message.setJMSCorrelationID(jmsCorrelationId);
060: message.setJMSDeliveryMode(jmsDeliveryMode);
061: message.setJMSDestination(jmsDestination);
062: message.setJMSReplyTo(jmsReplyTo);
063: message.setJMSExpiration(jmsExpiration);
064: message.setJMSMessageID(jmsMessageId);
065: message.setJMSPriority(jmsPriority);
066: message.setJMSTimestamp(jmsTimestamp);
067: message.setJMSType(jmsType);
068:
069: message.setBooleanProperty("boolean", false);
070: message.setByteProperty("byte", (byte) 64);
071: message.setShortProperty("short", (short) 1024);
072: message.setIntProperty("int", 65540);
073: message.setLongProperty("long", 58889999655511L);
074: message.setFloatProperty("float", (float) 8822.8);
075: message.setDoubleProperty("double", -1.88881E-221);
076: message.setStringProperty("string", "xxx::str_val");
077: Object obj = Integer.valueOf("70000");
078: message.setObjectProperty("obj", obj);
079: message.setBooleanProperty("boolean1", true);
080: }
081:
082: protected void checkMessageAttributes() throws JMSException {
083: checkMessageAttributes(message);
084: }
085:
086: protected void checkMessageAttributes(Message message)
087: throws JMSException {
088: assertEquals(jmsCorrelationId, message.getJMSCorrelationID());
089: assertEquals(jmsDeliveryMode, message.getJMSDeliveryMode());
090: assertSame(jmsDestination, message.getJMSDestination());
091: assertEquals(jmsExpiration, message.getJMSExpiration());
092: assertEquals(jmsMessageId, message.getJMSMessageID());
093: assertEquals(jmsPriority, message.getJMSPriority());
094: assertSame(jmsReplyTo, message.getJMSReplyTo());
095: assertEquals(jmsTimestamp, message.getJMSTimestamp());
096: assertEquals(jmsType, message.getJMSType());
097: assertFalse(message.getJMSRedelivered());
098:
099: Enumeration e = message.getPropertyNames();
100: int i = 0;
101: while (e.hasMoreElements()) {
102: String name = (String) e.nextElement();
103: if (name.equals("boolean")) {
104: assertFalse(message.getBooleanProperty("boolean"));
105: } else if (name.equals("byte")) {
106: assertEquals(message.getByteProperty("byte"), (byte) 64);
107: } else if (name.equals("short")) {
108: assertEquals(message.getShortProperty("short"),
109: (short) 1024);
110: } else if (name.equals("int")) {
111: assertEquals(message.getIntProperty("int"), 65540);
112: } else if (name.equals("long")) {
113: assertEquals(message.getLongProperty("long"),
114: 58889999655511L);
115: } else if (name.equals("float")) {
116: assertEquals(message.getFloatProperty("float"),
117: (float) 8822.8, (float) 0.1);
118: } else if (name.equals("double")) {
119: assertEquals(message.getDoubleProperty("double"),
120: -1.88881E-221, 0.00001E-221);
121: } else if (name.equals("string")) {
122: assertEquals(message.getStringProperty("string"),
123: "xxx::str_val");
124: } else if (name.equals("obj")) {
125: assertTrue(message.getObjectProperty("obj") instanceof Integer);
126: assertEquals(((Integer) message
127: .getObjectProperty("obj")).intValue(), 70000);
128: } else if (name.equals("boolean1")) {
129: assertTrue(message.getBooleanProperty("boolean1"));
130: } else {
131: fail();
132: }
133: i++;
134: }
135: assertEquals(i, 10);
136: }
137: }
|