1 /*------------------------------------------------------------------------------
2 Name: TestMessages.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.test.jms;
7
8 import java.io.ByteArrayOutputStream;
9 import java.lang.reflect.Method;
10
11 import javax.jms.JMSException;
12 import javax.jms.MessageNotWriteableException;
13
14 import java.util.logging.Logger;
15
16 import org.xmlBlaster.jms.MessageHelper;
17 import org.xmlBlaster.jms.XBBytesMessage;
18 import org.xmlBlaster.jms.XBDestination;
19 import org.xmlBlaster.jms.XBMessage;
20 import org.xmlBlaster.jms.XBTextMessage;
21 import org.xmlBlaster.util.Global;
22 import org.xmlBlaster.util.MsgUnit;
23
24 import junit.framework.*;
25
26 /**
27 * Test Messages.
28 * <p />
29 * All methods starting with 'test' and without arguments are invoked automatically
30 * <p />
31 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.TestJmsSubscribe
32 * @see org.xmlBlaster.util.qos.ConnectQosData
33 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/jms.html" target="others">the jms requirement</a>
34 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
35 */
36 public class TestMessages extends TestCase {
37 protected Global glob;
38 private static Logger log = Logger.getLogger(TestMessages.class.getName());
39 int counter = 0, nmax;
40
41 public TestMessages(String name) {
42 super(name);
43 }
44
45 public void prepare(String[] args) {
46 this.glob = new Global(args);
47 // this.glob.init(args);
48 }
49
50 protected void setUp() {
51 this.glob = Global.instance();
52
53 }
54
55 protected void tearDown() {
56
57 }
58
59 private void checkIfAllowed(XBMessage msg, String setter, String getter, Object val, boolean allowed) {
60 try {
61 String methodName = "get" + getter + "Property";
62 Class[] argTypes = new Class[] {String.class };
63 Object[] args = new Object[] { setter };
64 Method method = XBMessage.class.getMethod(methodName, argTypes);
65 try {
66 log.info("checkIfAllowed: setter='" + setter + "', getter='" + getter + "' expected='" + allowed + "'");
67 method.invoke(msg, args);
68 assertTrue("the combination set" + setter + "Property / get" + getter + "Property should NOT be allowed", allowed);
69 }
70 catch (Exception ex) {
71 if (ex instanceof JMSException) {
72 if (allowed) ex.printStackTrace();
73 assertTrue("the combination set" + setter + "Property / get" + getter + "Property should be allowed: " + ex.getMessage(), !allowed);
74 }
75 }
76 }
77 catch (Exception ex) {
78 ex.printStackTrace();
79 assertTrue("Something is probably wrong with this test", false);
80 }
81 }
82
83 private void checkSetter(XBMessage msg, String name, Object obj) {
84 try {
85 Object obj1 = msg.getObjectProperty(name).getClass();
86 assertEquals("Type of setter is incorrect ", obj, obj1);
87 }
88 catch (Exception ex) {
89 assertTrue("exception should not occur when testing setter for '" + name + "' ", false);
90 }
91 }
92
93 /**
94 * Specified in the jms 1.1 spec Cap. 3.5.4
95 * Tests the setting and getting of properties of all possible types
96 * according to the specification for the javax.jms.Message
97 * @see http://java.sun.com/j2ee/1.4/docs/api/javax.jms.Message.html
98 */
99 public void testPropertyValueConversion() {
100 try {
101 byte[] content = null;
102 int type = XBMessage.TEXT;
103 XBMessage msg = new XBMessage(null, content, type);
104
105 String[] keys = new String[] {"Boolean", "Byte", "Short", "Int", "Long", "Float", "Double", "String"};
106 Object[] values = new Object[] { new Boolean(false), new Byte((byte)1), new Short((short)2), new Integer(3), new Long(4), new Float(5.01), new Double(6.02), new String("7 String") };
107
108 msg.setBooleanProperty(keys[0], false);
109 msg.setByteProperty(keys[1], (byte)1);
110 msg.setShortProperty(keys[2], (short)2);
111 msg.setIntProperty(keys[3], (int)3);
112 msg.setLongProperty(keys[4], (long)4);
113 msg.setFloatProperty(keys[5], (float)5.01);
114 msg.setDoubleProperty(keys[6], (double)6.02);
115 msg.setStringProperty(keys[7], "7 (String)");
116
117 // prepare the result matrix (all others should be false)
118 boolean[][] allowed = new boolean[8][8];
119 for (int j=0; j < 8; j++) {
120 for (int i=0; i < 8; i++) allowed[j][i] = false;
121 }
122
123 for (int i=0; i < 8; i++) allowed[7][i] = true;
124 for (int i=0; i < 8; i++) allowed[i][7] = true;
125 allowed[0][0] = true;
126 allowed[1][1] = true;
127 allowed[1][2] = true;
128 allowed[2][2] = true;
129 allowed[1][3] = true;
130 allowed[2][3] = true;
131 allowed[3][3] = true;
132 allowed[1][4] = true;
133 allowed[2][4] = true;
134 allowed[3][4] = true;
135 allowed[4][4] = true;
136 allowed[5][5] = true;
137 allowed[5][6] = true;
138 allowed[6][6] = true;
139
140 // test first if all types are set correctly
141 checkSetter(msg, "Boolean", Boolean.class);
142 checkSetter(msg, "Byte", Byte.class);
143 checkSetter(msg, "Short", Short.class);
144 checkSetter(msg, "Int", Integer.class);
145 checkSetter(msg, "Long", Long.class);
146 checkSetter(msg, "Float", Float.class);
147 checkSetter(msg, "Double", Double.class);
148 checkSetter(msg, "String", String.class);
149
150 for (int j=0; j < 8; j++) {
151 for (int i=0; i < 8; i++) {
152 checkIfAllowed(msg, keys[j], keys[i], values[j], allowed[j][i]);
153 }
154 }
155 }
156 catch (Exception ex) {
157 ex.printStackTrace();
158 assertTrue("exception occured ", false);
159 }
160 }
161
162 public void testTextMessage() {
163 try {
164 { // 1. key, content and qos all null in constructor
165 byte[] content = null;
166 XBTextMessage msg = new XBTextMessage(null, content);
167
168 String txt1 = "funny Things happen";
169 msg.setText(txt1);
170 String txt2 = msg.getText();
171 assertEquals("normal text comparison", txt1, txt2);
172
173 txt1 = null;
174 msg.setText(txt1);
175 txt2 = msg.getText();
176 assertEquals("normal text comparison", txt1, txt2);
177 }
178
179 { // 2. content not null in constructor
180 XBTextMessage msg = new XBTextMessage(null, "oh I am a text msg".getBytes());
181
182 String txt1 = "funny Things happen";
183 msg.setText(txt1);
184 String txt2 = msg.getText();
185 assertEquals("normal text comparison", txt1, txt2);
186
187 txt1 = null;
188 msg.setText(txt1);
189 txt2 = msg.getText();
190 assertEquals("normal text comparison", txt1, txt2);
191 }
192 }
193 catch (Exception ex) {
194 ex.printStackTrace();
195 assertTrue("exception occured ", false);
196 }
197 }
198
199
200 public void testBytesMessage() {
201 try {
202 {
203 ByteArrayOutputStream baos = new ByteArrayOutputStream();
204 byte[] buf = new byte[100];
205 baos.write(buf);
206 byte[] buf1 = baos.toByteArray();
207 assertEquals("Wrong size of output buffer", buf.length, buf1.length);
208 }
209 { // 1. key, content and qos all null in constructor
210 byte[] content = null;
211 XBBytesMessage msg = new XBBytesMessage(null, content);
212
213 content = new byte[256];
214 for (int i=0; i < 256; i++) {
215 content[i] = (byte)i;
216 }
217
218 msg.writeBytes(content, 0, content.length);
219 XBDestination dest = new XBDestination("someTopic", null);
220 msg.setJMSDestination(dest);
221 msg.reset();
222
223 byte[] content2 = new byte[256];
224 msg.readBytes(content2);
225 for (int i=0; i < content.length; i++)
226 assertEquals("byte nr. '" + i + "' is wrong", content[i], content2[i]);
227 }
228 { // 1. key, content and qos all null in constructor
229 byte[] content = null;
230 XBBytesMessage msg = new XBBytesMessage(null, content);
231
232 content = new byte[256];
233 for (int i=0; i < 256; i++) {
234 content[i] = (byte)i;
235 }
236
237 msg.writeBytes(content, 0, content.length);
238 XBDestination dest = new XBDestination("someTopic", null);
239 msg.setJMSDestination(dest);
240 msg.reset();
241 MsgUnit msgUnit = MessageHelper.convertToMessageUnit(new Global(), msg);
242
243 byte[] content2 = msgUnit.getContent();
244 assertEquals("Wrong length", content.length, content2.length);
245 msg.readBytes(content2);
246 for (int i=0; i < content.length; i++)
247 assertEquals("byte nr. '" + i + "' is wrong", content[i], content2[i]);
248 }
249 }
250 catch (Exception ex) {
251 ex.printStackTrace();
252 assertTrue("exception occured ", false);
253 }
254 }
255
256 /**
257 * <pre>
258 * java org.xmlBlaster.test.classtest.TestMessages
259 * </pre>
260 */
261 public static void main(String args[])
262 {
263 TestMessages test = new TestMessages("TestMessages");
264 test.prepare(args);
265
266 test.setUp();
267 test.testBytesMessage();
268 test.tearDown();
269
270 test.setUp();
271 test.testPropertyValueConversion();
272 test.tearDown();
273
274 test.setUp();
275 test.testTextMessage();
276 test.tearDown();
277 }
278 }
syntax highlighted by Code2HTML, v. 0.9.1