1 /*------------------------------------------------------------------------------
2 Name: XmlScriptInterpreterTest.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6
7 package org.xmlBlaster.test.classtest;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.InputStreamReader;
12 import java.util.HashMap;
13
14 import org.custommonkey.xmlunit.XMLTestCase;
15 import org.custommonkey.xmlunit.XMLUnit;
16 import java.util.logging.Logger;
17 import java.util.logging.Level;
18 import org.xmlBlaster.client.I_Callback;
19 import org.xmlBlaster.client.XmlBlasterAccess;
20 import org.xmlBlaster.client.qos.*;
21 import org.xmlBlaster.client.script.XmlScriptClient;
22 import org.xmlBlaster.client.script.XmlScriptInterpreter;
23 import org.xmlBlaster.util.Global;
24 import org.xmlBlaster.util.MsgUnit;
25 import org.xmlBlaster.util.XmlBlasterException;
26
27 /**
28 * Test ClientProperty.
29 * @author <a href="mailto:michele@laghi.eu">Michele Laghi</a>
30 * <p />
31 * All methods starting with 'test' and without arguments are invoked automatically
32 * <p />
33 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.XmlScriptInterpreterTest
34 * @see org.xmlBlaster.client.script.XmlScriptInterpreter
35 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.qos.xmlScript.html">The client.xmlScript requirement</a>
36 */
37 public class XmlScriptInterpreterTest extends XMLTestCase {
38 protected final static String ME = "XmlScriptInterpreterTest";
39 protected Global glob;
40 private static Logger log = Logger.getLogger(XmlScriptInterpreterTest.class.getName());
41 protected XmlScriptInterpreter interpreter;
42 private TestAccessor accessor;
43 private ByteArrayOutputStream out;
44 private HashMap attachments;
45
46
47 public class TestAccessor extends XmlBlasterAccess {
48
49 private boolean doRemoteCalls;
50 private String key, qos;
51 private byte[] content;
52
53 public TestAccessor(Global global, boolean doRemoteCalls) {
54 super(global);
55 this.doRemoteCalls = doRemoteCalls;
56 }
57
58 public String getQos() {
59 return this.qos;
60 }
61
62 public String getKey() {
63 return this.key;
64 }
65
66 public byte[] getContent() {
67 return this.content;
68 }
69
70 public ConnectReturnQos connect(ConnectQos qos, I_Callback callback)
71 throws XmlBlasterException {
72 this.qos = qos.toXml();
73 if (this.doRemoteCalls) return super.connect(qos, callback);
74 return null;
75 }
76
77 public SubscribeReturnQos subscribe(String key, String qos)
78 throws XmlBlasterException {
79 this.qos = qos;
80 this.key = key;
81 log.fine("subscribe: " + key + " " + qos);
82 if (this.doRemoteCalls) return super.subscribe(key, qos);
83 return null;
84 }
85
86 public UnSubscribeReturnQos[] unSubscribe(String key, String qos)
87 throws XmlBlasterException {
88 this.qos = qos;
89 this.key = key;
90 log.fine("unSubscribe: " + key + " " + qos);
91 if (this.doRemoteCalls) return super.unSubscribe(key, qos);
92 return null;
93 }
94
95 public PublishReturnQos publish(MsgUnit msgUnit)
96 throws XmlBlasterException {
97 this.qos = msgUnit.getQos();
98 this.key = msgUnit.getKey();
99 this.content = msgUnit.getContent();
100 log.fine("publish: " + key + " " + qos);
101 if (this.doRemoteCalls) return super.publish(msgUnit);
102 return null;
103 }
104
105 public PublishReturnQos[] publishArr(MsgUnit[] msgUnits) throws XmlBlasterException {
106 // currently only store the first ones ...
107 this.qos = msgUnits[0].getQos();
108 this.key = msgUnits[0].getKey();
109 this.content = msgUnits[0].getContent();
110 log.fine("publishArr: " + key + " " + qos);
111 if (this.doRemoteCalls) return super.publishArr(msgUnits);
112 return null;
113 }
114
115 public MsgUnit[] get(String key, String qos) throws XmlBlasterException {
116 this.qos = qos;
117 this.key = key;
118 log.fine("get: " + key + " " + qos);
119 if (this.doRemoteCalls) return super.get(key, qos);
120 return null;
121 }
122
123 public EraseReturnQos[] erase(String key, String qos) throws XmlBlasterException {
124 this.qos = qos;
125 this.key = key;
126 log.fine("erase: " + key + " " + qos);
127 if (this.doRemoteCalls) return super.erase(key, qos);
128 return null;
129 }
130
131 public boolean disconnect(DisconnectQos qos) {
132 this.qos = qos.toXml();
133 log.fine("disconnect: " + key + " " + qos);
134 if (this.doRemoteCalls) return super.disconnect(qos);
135 return false;
136 }
137 }
138
139 public XmlScriptInterpreterTest(String name) {
140 this(new Global(), name);
141 }
142
143 public XmlScriptInterpreterTest(Global global, String name) {
144 super(name);
145 boolean doRemoteCalls = false;
146 this.accessor = new TestAccessor(global, doRemoteCalls);
147 this.out = new ByteArrayOutputStream();
148 this.attachments = new HashMap();
149 String contentRef = "QmxhQmxhQmxh"; // this is used in testPublishArr
150 this.attachments.put("attachment1", contentRef);
151 this.interpreter = new XmlScriptClient(global, this.accessor, out, out, this.attachments);
152 XMLUnit.setIgnoreWhitespace(true);
153 }
154
155 protected void setUp() {
156 this.glob = Global.instance();
157
158 }
159
160 protected void testConnect() throws Exception {
161 String tmp = "<xmlBlaster>\n" +
162 " <connect>\n" +
163 " <qos>\n" +
164 " <securityService type='htpasswd' version='1.0'>\n" +
165 " <![CDATA[" +
166 " <user>michele</user>" +
167 " <passwd>secret</passwd>" +
168 " ]]>" +
169 " </securityService>\n" +
170 " <session name='client/joe/3' timeout='3600000' maxSessions='10'" +
171 " clearSessions='false' reconnectSameClientOnly='false'/>\n" +
172 " <ptp>true</ptp>\n" +
173 " <duplicateUpdates>false</duplicateUpdates>\n" +
174 " <queue relating='callback' maxEntries='1000' maxBytes='4000000'>\n" +
175 " <callback type='IOR' sessionId='4e56890ghdFzj0' pingInterval='10000'\n" +
176 " retries='-1' delay='10000' oneway='false' dispatchPlugin='undef'>\n" +
177 " IOR:10000010033200000099000010....\n" +
178 " <burstMode collectTime='400' />\n" +
179 " <compress type='gzip' minSize='3000'/>\n" +
180 " <ptp>true</ptp>\n" +
181 " </callback>\n" +
182 " </queue>\n" +
183 " <!-- a client specific property: here it could be the bean to invoke on updates -->\n" +
184 " <clientProperty name='onMessageDefault'>beanName</clientProperty>\n" +
185 " </qos>\n" +
186 " </connect>\n" +
187 "</xmlBlaster>\n";
188
189 ByteArrayInputStream in = new ByteArrayInputStream(tmp.getBytes());
190 this.interpreter.parse(new InputStreamReader(in));
191 String qos = this.accessor.getQos();
192
193 assertXpathExists("/qos/securityService[@type='htpasswd']", qos);
194 assertXpathExists("/qos/session", qos);
195 assertXpathExists("/qos/ptp", qos);
196 assertXpathExists("/qos/duplicateUpdates", qos);
197 assertXpathExists("/qos/queue/callback", qos);
198 assertXpathExists("/qos/clientProperty[@name='onMessageDefault']", qos);
199 assertXpathExists("/qos/duplicateUpdates", qos);
200 assertXpathExists("/qos/duplicateUpdates", qos);
201 }
202
203 protected void testSubscribe() throws Exception {
204 String qosRef = "" +
205 " <qos>\n" +
206 " <subscribe id='_subId:1'/>\n" +
207 " <erase forceDestroy='true'/>\n" +
208 " <meta>false</meta>\n" +
209 " <content>false</content>\n" +
210 " <multiSubscribe>false</multiSubscribe>\n" +
211 " <local>false</local>\n" +
212 " <initialUpdate>false</initialUpdate>\n" +
213 " <notify>false</notify>\n" +
214 " <filter type='GnuRegexFilter' version='1.0'>^H.*$</filter>\n" +
215 " <history numEntries='20'/>\n" +
216 " </qos>\n";
217 String keyRef = " <key oid='' queryType='XPATH'> /xmlBlaster/key[starts-with(@oid,'radar.')] </key>\n";
218
219 String cmd = "<xmlBlaster>\n <subscribe>\n" + qosRef + keyRef +
220 " </subscribe>\n" +
221 "</xmlBlaster>\n";
222
223 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
224 this.interpreter.parse(new InputStreamReader(in));
225 String qos = this.accessor.getQos();
226 String key = this.accessor.getKey();
227
228 log.info("testSubscribe: qos: '" + qos + "'");
229 log.info("testSubscribe: key: '" + key + "'");
230 assertXMLEqual(qosRef, qos);
231 assertXMLEqual(keyRef, key);
232 }
233
234 protected void testPublish() throws Exception {
235 String keyRef = "<key oid='MyMessageOid' contentMime='text/xml'><some><qos type='xxx'><content /></qos><key>xxx</key></some></key>";
236 // String keyRef = "<key oid='MyMessageOid' contentMime='text/xml'><some><qos type='xxx'><content /></qos>xxx</some></key>";
237 String qosRef = "<qos><priority>HIGH</priority></qos>";
238 String contentRef = "<some><content>Hello World</content><qos></qos><key><key><qos><qos></qos></qos></key></key></some>";
239
240 String cmd = "<xmlBlaster id='xxyyzz'><publish>" + keyRef + "<content>" + contentRef + "</content>" +
241 qosRef + "</publish></xmlBlaster>\n";
242
243 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
244
245 this.out.reset();
246 this.interpreter.parse(new InputStreamReader(in));
247 String qos = this.accessor.getQos();
248 String key = this.accessor.getKey();
249 String content = new String(this.accessor.getContent()).trim();
250 log.info("testPublish: qos: '" + qos + "' '" + qosRef + "'");
251 log.info("testPublish: key: '" + key + "' '" + keyRef + "'");
252 log.info("testPublish: content: '" + content + "' and should be '" + contentRef);
253 assertXMLEqual(keyRef, key);
254 assertXMLEqual(qosRef, qos);
255 assertEquals(contentRef, content);
256 String response = new String(this.out.toByteArray());
257 log.info("testPublish: response: '" + response + "'");
258 assertXpathExists("/xmlBlasterResponse[@id='xxyyzz']", response);
259 }
260
261 protected void testPublishArr() throws Exception {
262 String keyRef = "<key oid='MyMessageOid' contentMime='text/xml'/>";
263 String qosRef = "<qos><priority>HIGH</priority></qos>";
264 String contentRef = "QmxhQmxhQmxh"; // this is in the attachment
265 String contentShould = "BlaBlaBla";
266
267 String cmd = "<xmlBlaster><publishArr><message>" + keyRef + "<content link='attachment1' encoding='base64'>" + "</content>" +
268 qosRef + "</message></publishArr></xmlBlaster>\n";
269
270 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
271 this.interpreter.parse(new InputStreamReader(in));
272 String qos = this.accessor.getQos();
273 String key = this.accessor.getKey();
274 String content = new String(this.accessor.getContent()).trim();
275 log.info("testPublishArr: qos: '" + qos + "' '" + qosRef + "'");
276 log.info("testPublishArr: key: '" + key + "' '" + keyRef + "'");
277 log.info("testPublishArr: content: '" + content + "'");
278
279 assertXMLEqual(keyRef, key);
280 assertXMLEqual(qosRef, qos);
281 assertEquals(contentShould, content);
282 }
283
284 protected void testDisconnect() throws Exception {
285 String qosRef = "<qos><deleteSubjectQueue/><clearSessions>false</clearSessions></qos>";
286 String cmd = "<xmlBlaster><disconnect>" + qosRef + "</disconnect></xmlBlaster>";
287
288 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
289 this.interpreter.parse(new InputStreamReader(in));
290 String qos = this.accessor.getQos();
291 log.info("testDisconnect: qos: '" + qos + "' '" + qosRef + "'");
292 assertXMLEqual(qosRef, qos);
293 }
294
295 protected void testUnSubscribe() throws Exception {
296 String qosRef = "<qos/>";
297 String keyRef = "<key queryType='XPATH'>//key</key>";
298
299 String cmd = "<xmlBlaster>\n <unSubscribe>\n" + qosRef + keyRef +
300 " </unSubscribe>\n" +
301 "</xmlBlaster>\n";
302
303 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
304 this.interpreter.parse(new InputStreamReader(in));
305 String qos = this.accessor.getQos();
306 String key = this.accessor.getKey();
307
308 log.info("testUnSubscribe: qos: '" + qos + "'");
309 log.info("testUnSubscribe: key: '" + key + "'");
310 assertXMLEqual(qosRef, qos);
311 assertXMLEqual(keyRef, key);
312 }
313
314
315 protected void testErase() throws Exception {
316 String qosRef = "<qos><erase forceDestroy='false'/></qos>";
317 String keyRef = "<key oid='MyTopic'/>";
318
319 String cmd = "<xmlBlaster>\n <erase>\n" + qosRef + keyRef +
320 " </erase>\n" +
321 "</xmlBlaster>\n";
322
323 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
324 this.interpreter.parse(new InputStreamReader(in));
325 String qos = this.accessor.getQos();
326 String key = this.accessor.getKey();
327
328 log.info("testErase: qos: '" + qos + "'");
329 log.info("testErase: key: '" + key + "'");
330 assertXMLEqual(qosRef, qos);
331 assertXMLEqual(keyRef, key);
332 }
333
334 protected void testGet() throws Exception {
335 String qosRef = "<qos><content>false</content>" +
"<filter type='GnuRegexFilter' version='1.0'>^H.*$</filter>" +
336 "<history numEntries='20'/></qos>";
337 String keyRef = "<key oid='MyMessage' />";
338
339 String cmd = "<xmlBlaster>\n <get>\n" + qosRef + keyRef +
340 " </get>\n" +
341 "</xmlBlaster>\n";
342
343 ByteArrayInputStream in = new ByteArrayInputStream(cmd.getBytes());
344 this.interpreter.parse(new InputStreamReader(in));
345 String qos = this.accessor.getQos();
346 String key = this.accessor.getKey();
347
348 log.info("testGet: qos: '" + qos + "'");
349 log.info("testGet: key: '" + key + "'");
350 assertXMLEqual(qosRef, qos);
351 assertXMLEqual(keyRef, key);
352 }
353
354
355
356
357 /**
358 * <pre>
359 * java org.xmlBlaster.test.classtest.XmlScriptInterpreterTest
360 * </pre>
361 */
362 public static void main(String args[])
363 {
364 try {
365 Global global = new Global(args);
366 XmlScriptInterpreterTest test = new XmlScriptInterpreterTest(global, "XmlScriptInterpreterTest");
367 test.setUp();
368 test.testConnect();
369 test.testSubscribe();
370 test.testPublishArr();
371 test.testDisconnect();
372 test.testGet();
373 test.testErase();
374 test.testUnSubscribe();
375 test.testPublish();
376 // test.testWait();
377 //testSub.tearDown();
378 }
379 catch(Throwable e) {
380 e.printStackTrace();
381 //fail(e.toString());
382 }
383 }
384 }
syntax highlighted by Code2HTML, v. 0.9.1