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


syntax highlighted by Code2HTML, v. 0.9.1