001: package com.mockrunner.test.jms;
002:
003: import java.util.Arrays;
004:
005: import javax.jms.MessageFormatException;
006: import javax.jms.MessageNotWriteableException;
007:
008: import junit.framework.TestCase;
009:
010: import com.mockrunner.mock.jms.MockMapMessage;
011:
012: public class MockMapMessageTest extends TestCase {
013: public void testReadWrite() throws Exception {
014: MockMapMessage message = new MockMapMessage();
015: message.setBoolean("boolean1", true);
016: assertTrue(message.itemExists("boolean1"));
017: assertEquals("true", message.getString("boolean1"));
018: assertEquals(true, message.getBoolean("boolean1"));
019: message.setString("string1", "12.3");
020: assertEquals(12.3, message.getFloat("string1"), 0.01);
021: assertEquals(12.3, message.getDouble("string1"), 0.01);
022: assertEquals("12.3", message.getString("string1"));
023: assertEquals("12.3", message.getObject("string1"));
024: try {
025: message.getLong("string1");
026: fail();
027: } catch (NumberFormatException exc) {
028: //should throw exception
029: }
030: message.setObject("byteObject1", new Byte((byte) 1));
031: assertEquals("1", message.getString("byteObject1"));
032: assertEquals(1, message.getLong("byteObject1"));
033: assertEquals(1, message.getInt("byteObject1"));
034: assertEquals(1, message.getShort("byteObject1"));
035: assertEquals(1, message.getByte("byteObject1"));
036: try {
037: message.getDouble("byteObject1");
038: fail();
039: } catch (MessageFormatException exc) {
040: //should throw exception
041: }
042: message.setInt("int1", 123);
043: assertEquals(123, message.getLong("int1"));
044: try {
045: message.getShort("int1");
046: fail();
047: } catch (MessageFormatException exc) {
048: //should throw exception
049: }
050: byte[] data = new byte[] { 1, 2, 3 };
051: message.setBytes("bytes1", data);
052: assertTrue(Arrays.equals(message.getBytes("bytes1"),
053: new byte[] { 1, 2, 3 }));
054: message.setBytes("bytes2", data, 1, 2);
055: assertTrue(Arrays.equals(message.getBytes("bytes2"),
056: new byte[] { 2, 3 }));
057: try {
058: message.getString("bytes1");
059: fail();
060: } catch (MessageFormatException exc) {
061: //should throw exception
062: }
063: assertTrue(Arrays.equals((byte[]) message.getObject("bytes1"),
064: new byte[] { 1, 2, 3 }));
065: message.setObject("null", null);
066: assertFalse(message.itemExists("null"));
067: try {
068: message.getInt("null");
069: fail();
070: } catch (NumberFormatException exc) {
071: //should throw exception
072: }
073: assertNull(message.getString("null"));
074: }
075:
076: public void testReadOnly() throws Exception {
077: MockMapMessage message = new MockMapMessage();
078: message.setString("test", "test");
079: message.setBoolean("boolean", true);
080: message.setReadOnly(true);
081: try {
082: message.setString("test", "anothertest");
083: fail();
084: } catch (MessageNotWriteableException exc) {
085: //should throw exception
086: }
087: try {
088: message.setBoolean("boolean", false);
089: fail();
090: } catch (MessageNotWriteableException exc) {
091: //should throw exception
092: }
093: try {
094: message.setInt("int", 1);
095: fail();
096: } catch (MessageNotWriteableException exc) {
097: //should throw exception
098: }
099: assertEquals("test", message.getString("test"));
100: assertTrue(message.getBoolean("boolean"));
101: assertFalse(message.itemExists("int"));
102: message.clearBody();
103: message.setInt("int", 1);
104: assertTrue(message.itemExists("int"));
105: }
106:
107: public void testNullName() throws Exception {
108: MockMapMessage message = new MockMapMessage();
109: try {
110: message.setDouble(null, 123.4);
111: fail();
112: } catch (IllegalArgumentException exc) {
113: //should throw exception
114: }
115: try {
116: message.setBytes("", new byte[0]);
117: fail();
118: } catch (IllegalArgumentException exc) {
119: //should throw exception
120: }
121: try {
122: message.setString(null, "test");
123: fail();
124: } catch (IllegalArgumentException exc) {
125: //should throw exception
126: }
127: }
128:
129: public void testEquals() throws Exception {
130: MockMapMessage message1 = new MockMapMessage();
131: message1.setInt("name1", 1);
132: message1.setString("name2", "text");
133: message1.setBytes("name3", new byte[] { 1, 2, 3 });
134: assertTrue(message1.equals(message1));
135: MockMapMessage message2 = null;
136: assertFalse(message1.equals(message2));
137: message2 = new MockMapMessage();
138: assertFalse(message1.equals(message2));
139: assertTrue(message2.equals(new MockMapMessage()));
140: assertEquals(message2.hashCode(), new MockMapMessage()
141: .hashCode());
142: message2.setInt("name1", 1);
143: message2.setString("name2", "text");
144: message2.setBytes("name3", new byte[] { 1, 2, 1 });
145: assertFalse(message1.equals(message2));
146: assertFalse(message2.equals(message1));
147: message2.setBytes("name3", new byte[] { 1, 2, 3 });
148: assertTrue(message1.equals(message2));
149: assertTrue(message2.equals(message1));
150: assertEquals(message1.hashCode(), message2.hashCode());
151: message2.setString("name4", "text");
152: assertFalse(message1.equals(message2));
153: message2.setString("name4", null);
154: assertFalse(message1.equals(message2));
155: assertFalse(message2.equals(message1));
156: message1.setString("name4", "text");
157: assertTrue(message1.equals(message2));
158: assertTrue(message2.equals(message1));
159: assertEquals(message1.hashCode(), message2.hashCode());
160: message2.setBytes("name3", new byte[] {});
161: assertFalse(message1.equals(message2));
162: assertFalse(message2.equals(message1));
163: }
164:
165: public void testGetMap() throws Exception {
166: MockMapMessage message = new MockMapMessage();
167: assertTrue(message.getMap().isEmpty());
168: message.setString("1", "value1");
169: message.setInt("2", 2);
170: assertEquals(2, message.getMap().size());
171: assertEquals("value1", message.getMap().get("1"));
172: assertEquals(new Integer(2), message.getMap().get("2"));
173: message.getMap().put("3", "3");
174: assertEquals(2, message.getMap().size());
175: assertEquals("value1", message.getMap().get("1"));
176: assertEquals(new Integer(2), message.getMap().get("2"));
177: MockMapMessage otherMessage = new MockMapMessage();
178: otherMessage.setString("1", "value1");
179: otherMessage.setInt("2", 2);
180: assertTrue(message.equals(otherMessage));
181: }
182:
183: public void testClone() throws Exception {
184: MockMapMessage message = new MockMapMessage();
185: MockMapMessage newMessage = (MockMapMessage) message.clone();
186: assertNotSame(message, newMessage);
187: assertEquals(message, newMessage);
188: message.setFloat("float", 2.0f);
189: message.setString("string", "text");
190: byte[] myArray = new byte[] { 1, 2, 3 };
191: message.setBytes("bytes", myArray);
192: newMessage = (MockMapMessage) message.clone();
193: assertNotSame(message, newMessage);
194: assertEquals(message, newMessage);
195: assertEquals(2.0f, message.getFloat("float"), 0);
196: assertEquals("text", message.getString("string"));
197: assertNotSame(myArray, message.getBytes("bytes"));
198: assertTrue(Arrays.equals(myArray, message.getBytes("bytes")));
199: }
200:
201: public void testToString() throws Exception {
202: MockMapMessage message = new MockMapMessage();
203: assertEquals(MockMapMessage.class.getName() + ": {}", message
204: .toString());
205: message.setInt("name", 3);
206: assertEquals(MockMapMessage.class.getName() + ": {name=3}",
207: message.toString());
208: }
209: }
|