1 package org.xmlBlaster.test.classtest;
  2 
  3 import java.util.Properties;
  4 
  5 import java.util.logging.Logger;
  6 import org.xmlBlaster.util.Global;
  7 import org.xmlBlaster.util.XmlBlasterException;
  8 
  9 import junit.framework.*;
 10 import org.xmlBlaster.engine.runlevel.RunLevelActionSaxFactory;
 11 import org.xmlBlaster.engine.runlevel.RunLevelAction;
 12 import org.xmlBlaster.engine.runlevel.PluginConfig;
 13 import org.xmlBlaster.util.def.ErrorCode;
 14 import org.xmlBlaster.util.qos.MsgQosData;
 15 import org.xmlBlaster.util.qos.MsgQosSaxFactory;
 16 import org.xmlBlaster.engine.runlevel.PluginConfigSaxFactory;
 17 import org.xmlBlaster.engine.runlevel.PluginHolderSaxFactory;
 18 import org.xmlBlaster.engine.runlevel.PluginHolder;
 19 import org.xmlBlaster.engine.runlevel.PluginConfigComparator;
 20 
 21 
 22 /**
 23  * Test ConnectQos. 
 24  * <p />
 25  * All methods starting with 'test' and without arguments are invoked automatically
 26  * <p />
 27  * TODO: http://xmlunit.sourceforge.net/
 28  * <p />
 29  * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.RunLevelTest
 30  * @see org.xmlBlaster.util.qos.ConnectQosData
 31  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.connect.html" target="others">the interface.connect requirement</a>
 32  */
 33 public class RunLevelTest extends TestCase {
 34    final static String ME = "RunLevelTest";
 35    protected Global glob;
 36    private static Logger log = Logger.getLogger(RunLevelTest.class.getName());
 37    int counter = 0;
 38 
 39 
 40    public RunLevelTest(String name, String[] args) {
 41       super(name);
 42       this.glob = Global.instance();
 43       this.glob.init(args);
 44 
 45    }
 46 
 47    public RunLevelTest(String name) {
 48       super(name);
 49       this.glob = Global.instance();
 50 
 51    }
 52 
 53    protected void setUp() {
 54    }
 55 
 56    protected void tearDown() {
 57    }
 58 
 59    public void testAction() {
 60       String me = ME + "-testAction";
 61       log.info("start");
 62 
 63       try {
 64          String xml = "<action do='LOAD'\n" +
 65                       "        onStartupRunlevel='3'\n" +
 66                       "        sequence='5'\n" +
 67                       "        onFail='resource.configuration.pluginFailed'/>";
 68    
 69          RunLevelActionSaxFactory factory = new RunLevelActionSaxFactory(this.glob);
 70          RunLevelAction action = factory.readObject(xml);
 71 
 72          for (int i=0; i < 2; i++) {
 73             assertEquals(me + " checking do attribute", "LOAD", action.getDo());
 74             assertEquals(me + " checking onFail attribute", "resource.configuration.pluginFailed", action.getOnFail().getErrorCode());
 75             assertEquals(me + " checking onShutdownLevel attribute", -1, action.getOnShutdownRunlevel());
 76             assertEquals(me + " checking onStartupLevel attribute", 3, action.getOnStartupRunlevel());
 77             assertEquals(me + " checking sequence attribute", 5, action.getSequence());
 78             assertEquals(me + " checking hasOnFail", true, action.hasOnFail());
 79             assertEquals(me + " checking isOnShutdownLevel", false, action.isOnShutdownRunlevel());
 80             assertEquals(me + " checking isOnStartupLevel", true, action.isOnStartupRunlevel());
 81             xml = action.toXml();
 82             action = factory.readObject(xml);
 83             log.info("going to test the second time ...");
 84          }
 85 
 86          // now test a null string
 87 
 88          try {
 89             xml = null;
 90             action = factory.readObject(xml);
 91             assertTrue(me + " a null string is not allowed here. Should have thrown an exception", true);
 92          }
 93          catch (XmlBlasterException ex) {
 94             log.info("the exception is allowed here since a null string is not allowed here." + ex.getMessage());
 95          }
 96          try {
 97             xml = "";
 98             action = factory.readObject(xml);
 99             assertTrue(me + " an empty string is not allowed here. Should have thrown an exception", true);
100          }
101          catch (XmlBlasterException ex) {
102             log.info("the exception is allowed here since an empty string is not allowed here." + ex.getMessage());
103          }
104          try {
105             xml = "xyz";
106             action = factory.readObject(xml);
107             assertTrue(me + " a non-xml string is not allowed here. Should have thrown an exception", true);
108          }
109          catch (XmlBlasterException ex) {
110             log.info("the exception is allowed here since a non-xml string is not allowed here." + ex.getMessage());
111          }
112          try {
113             xml = "<xmlBlaster></xmlBlaster>";
114             action = factory.readObject(xml);
115             assertTrue(me + " a wrong tag name is not allowed here. Should have thrown an exception", true);
116          }
117          catch (XmlBlasterException ex) {
118             log.info("the exception is allowed here since  a wrong tag name is not allowed here." + ex.getMessage());
119          }
120 
121          // this is allowed ...
122          xml = "<action/>";
123          action = factory.readObject(xml);
124 
125       }
126       catch (XmlBlasterException e) {
127          fail(ME+ " failed: " + e.toString());
128       }
129       log.info("successfully ended");
130    }
131 
132    public void testPluginConfig() {
133       String me = ME + "-testPluginConfig";
134       try {
135          log.info("start");
136          PluginConfig config = new PluginConfig(this.glob, "queueJDBC", true, "org.xmlBlaster.util.queue.jdbc.JDBCQueueCommonTablePlugin");
137          config.addAttribute("url", "jdbc:oracle:thin:@localhost:1521:noty");
138          config.addAttribute("user", "joe");
139          config.addAttribute("password", "secret");
140          config.addAttribute("connectionBusyTimeout", "90000");
141          config.addAttribute("maxWaitingThreads", "300");
142          RunLevelAction action = new RunLevelAction(this.glob, "LOAD", 3, -1, ErrorCode.toErrorCode("internal.unknown"), 5);
143          config.addAction(action);
144          action = new RunLevelAction(this.glob, "STOP", -1, 2, null, 4);
145          config.addAction(action);
146      
147          String xml = config.toXml();
148          log.info(xml);
149      
150          PluginConfigSaxFactory factory = new PluginConfigSaxFactory(this.glob);
151          config = factory.readObject(xml);
152          RunLevelAction[] actions = config.getActions();
153          assertEquals(me + " number of actions", 2, actions.length);
154       }
155       catch (XmlBlasterException e) {
156          fail(ME + " failed: " + e.toString());
157       }
158       log.info("successfully ended");
159    }
160 
161    private MsgQosData getQosData(String attrVal) throws XmlBlasterException {
162       PluginConfigSaxFactory factory = new PluginConfigSaxFactory(this.glob);
163       String xml = "<plugin id='FilePollerPlugin' className='org.xmlBlaster.client.filepoller.FilePollerPlugin'>\n" +
164                    "  <attribute id='qosTest'>" + attrVal + "</attribute>\n" + 
165                    "  <action do='LOAD' onStartupRunlevel='9' sequence='6' onFail='resource.configuration.pluginFailed'/>\n" +
166                    "  <action do='STOP' onShutdownRunlevel='6' sequence='5'/>\n" + 
167                    "</plugin>\n";
168       PluginConfig config = factory.readObject(xml);
169       Properties prop = config.getPluginInfo().getParameters();
170       String txt = prop.getProperty("qosTest", null);
171       if (txt == null) {
172          prop.list(System.err);
173          assertTrue("the qosTest is null when it should not", false);
174       }
175       MsgQosSaxFactory msgFactory = new MsgQosSaxFactory(this.glob);
176       return msgFactory.readObject(txt);
177    }
178 
179    public void testPluginConfigParser() {
180       log.info("start");
181       String xml = "<![CDATA[<qos><expiration lifeTime='4000'/></qos>]]>";
182       try {
183          MsgQosData data = getQosData(xml);
184          assertEquals("Wrong lifetime", 4000L, data.getLifeTime()); 
185       }
186       catch (XmlBlasterException e) {
187          assertTrue(ME + " parsing failed for '" + xml + "'", false);
188       }
189       /*
190       xml = "&lt;![CDATA[<qos><expiration lifeTime='4000'/></qos>]]&gt;";
191       try {
192          MsgQosData data = getQosData(xml);
193          assertEquals("Wrong lifetime", 4000L, data.getLifeTime()); 
194       }
195       catch (XmlBlasterException e) {
196          assertTrue(ME + " parsing failed for '" + xml + "'", false);
197       }
198       */
199 
200       xml = "<qos><expiration lifeTime='4000'/></qos>";
201       try {
202          MsgQosData data = getQosData(xml);
203          assertEquals("Wrong lifetime", 4000L, data.getLifeTime()); 
204       }
205       catch (XmlBlasterException e) {
206          assertTrue(ME + " parsing failed for '" + xml + "'", false);
207       }
208 
209       xml = "<qos><![CDATA[<expiration lifeTime='4000'/>]]></qos>";
210       try {
211          MsgQosData data = getQosData(xml);
212          // unless you change the parsing in MsgQosData
213          assertEquals("Wrong lifetime", -1L, data.getLifeTime()); 
214       }
215       catch (XmlBlasterException e) {
216          assertTrue(ME + " parsing failed for '" + xml + "'", false);
217       }
218       
219    }
220 
221 
222    public void testPluginHolder() {
223       String me = ME + "-testPluginHolder";
224       try {
225          log.info("start");
226 
227          PluginHolder holder = new PluginHolder(this.glob);
228          
229          PluginConfig tmp = new PluginConfig(this.glob, "queueJDBC", true, "org.xmlBlaster.util.queue.jdbc.JDBCQueueCommonTablePlugin");
230          holder.addDefaultPluginConfig(tmp);
231          tmp = new PluginConfig(this.glob, "queueRAM", true, "org.xmlBlaster.util.queue.ram.RAMQueuePlugin");
232          holder.addPluginConfig("avalon", tmp);
233 
234          tmp = holder.getPluginConfig("avalon", "queueRAM");
235          if (tmp == null) assertTrue(me + " getting 'avalon queueRAM'", false);
236          log.info(tmp.toXml());
237 
238          tmp = holder.getPluginConfig("avalon", "queueJDBC");
239          if (tmp == null) assertTrue(me + " getting 'avalon queueJDBC'", false);
240          log.info(tmp.toXml());
241 
242          PluginConfig[] help = holder.getAllPluginConfig("avalon");
243          assertEquals(me + " get all plugins for avalon", 2, help.length);
244 
245 
246          String xml = new String();
247          xml += "<xmlBlaster>\n" +
248                 "   <!-- A typical plugin which is loaded by client request -->\n" +
249                 "   <plugin id='dispatchPriority'\n" +
250                 "           className='org.xmlBlaster.util.dispatch.plugins.prio.PriorizedDispatchPlugin'\n" +
251                 "           jar='/tmp/my.jar'>\n" +
252                 "      <attribute id='config'>\n" +
253                 "         <![CDATA[\n" +
254                 "         <msgDispatch defaultStatus='64k' defaultAction='send'>\n" +
255                 "         <onStatus oid='_bandwidth.status' content='64k' defaultAction='destroy'>\n" +
256                 "            <action do='send'  ifPriority='7-9'/>\n" +
257                 "            <action do='queue'  ifPriority='2-6'/>\n" +
258                 "         </onStatus>\n" +
259                 "         <onStatus oid='_bandwidth.status' content='2M'>\n" +
260                 "            <action do='send'  ifPriority='0-9'/>\n" +
261                 "         </onStatus>\n" +
262                 "         </msgDispatch>\n" +
263                 "         ]]>\n" +
264                 "      </attribute>\n" +
265                 "   </plugin>\n" +
266                 "\n" +
267                 "   <plugin id='queueCACHE' className='org.xmlBlaster.util.queue.cache.CacheQueueInterceptorPlugin'>\n" +
268                 "      <attribute id='transientQueue'>queueRAM</attribute>\n" +
269                 "      <attribute id='persistentQueue'>queueJDBC</attribute>\n" +
270                 "   </plugin>\n" +
271                 "   \n" +
272                 "  <plugin id='queueRAM' className='org.xmlBlaster.util.queue.ram.RamQueuePlugin'/>\n" +
273                 "\n" +
274                 "   <plugin id='storage:CACHE' className='org.xmlBlaster.engine.msgstore.cache.PersistenceCachePlugin'>\n" +
275                 "      <attribute id='transientQueue'>storage:RAM</attribute>\n" +
276                 "      <attribute id='persistentQueue'>storage:JDBC</attribute>\n" +
277                 "   </plugin>\n" +
278                 "   \n" +
279                 "   <plugin id='storage:RAM' className='org.xmlBlaster.engine.msgstore.ram.MapPlugin'/>\n" +
280                 "   \n" +
281                 "   <!-- and here the declarations which are specific to the given nodes -->\n" +
282                 "   <node id='heron'>\n" +
283                 "      <plugin id='protocol:SOCKET:admin' \n" +
284                 "              className='org.xmlBlaster.protocol.socket.SocketDriver'>\n" +
285                 "         <attribute id='port'>69000</attribute>\n" +
286                 "      </plugin>\n" +
287                 "     \n" +
288                 "      <!-- /node/heron/plugin/protocol:SOCKET:users/attribute/port=6901 -->\n" +
289                 "      <!-- /node/heron/plugin/protocol:SOCKET:users/action/LOAD/onStartupRunlevel=3 -->\n" +
290                 "      <!-- /node/heron/plugin/protocol:SOCKET:users/action/LOAD/sequence=5 -->\n" +
291                 "      <plugin id='protocol:SOCKET:users' className='org.xmlBlaster.protocol.socket.SocketDriver'>\n" +
292                 "         <attribute id='port'>6901</attribute>\n" +
293                 "         <action do='LOAD' onStartupRunlevel='3' sequence='5' onFail='resource.configuration.pluginFailed'/>\n" +
294                 "         <action do='STOP' onShutdownRunlevel='2' sequence='4'/>\n" +
295                 "      </plugin>\n" +
296                 "     \n" +
297                 "      <plugin id='queueJDBC' className='org.xmlBlaster.util.queue.jdbc.JDBCQueueCommonTablePlugin'>\n" +
298                 "         <attribute id='url'>jdbc:oracle:thin:@localhost:1521:noty</attribute>\n" +
299                 "         <attribute id='user'>joe</attribute>\n" +
300                 "         <attribute id='password'>secret</attribute>\n" +
301                 "         <attribute id='connectionBusyTimeout'>90000</attribute>\n" +
302                 "         <attribute id='maxWaitingThreads'>300</attribute>\n" +
303                 "      </plugin>\n" +
304                 "     \n" +
305                 "      <plugin id='storage:JDBC' className='org.xmlBlaster.engine.msgstore.cache.PersistenceCachePlugin'>\n" +
306                 "         <attribute id='url'>jdbc:oracle:thin:@localhost:1521:noty</attribute>\n" +
307                 "         <attribute id='user'>joe</attribute>\n" +
308                 "         <attribute id='password'>secret</attribute>\n" +
309                 "         <attribute id='connectionBusyTimeout'>90000</attribute>\n" +
310                 "         <attribute id='maxWaitingThreads'>300</attribute>\n" +
311                 "      </plugin>\n" +
312                 "    </node> <!-- heron -->\n" +
313                 " \n" +
314                 "    <node id='avalon'>\n" +
315                 "       ...\n" +
316                 "      <plugin id='queueJDBC' className='org.xmlBlaster.util.queue.jdbc.JDBCQueueCommonTablePlugin'>\n" +
317                 "         <attribute id='url'>jdbc:oracle:thin:@localhost:1521:noty</attribute>\n" +
318                 "         <attribute id='user'>joe</attribute>\n" +
319                 "         <attribute id='password'>secret</attribute>\n" +
320                 "         <attribute id='connectionBusyTimeout'>90000</attribute>\n" +
321                 "         <attribute id='maxWaitingThreads'>300</attribute>\n" +
322                 "        <attribute id='tableNamePrefix'>AVALON_</attribute>\n" +
323                 "      </plugin>\n" +
324                 "      ...\n" +
325                 "    </node>\n" +
326                 "</xmlBlaster>\n";  
327 
328          PluginHolderSaxFactory factory = new PluginHolderSaxFactory(this.glob);
329          PluginHolder pluginHolder = null;
330          for (int i=0; i < 2; i++) {
331             log.info("looping through the loop. sweep '" + i + "'");
332             pluginHolder = factory.readObject(xml);
333             PluginConfig[] plugins = pluginHolder.getAllPluginConfig("avalon");
334             assertEquals(me + " number of plugins for 'avalon' in plugin holder", 6, plugins.length);
335 
336             PluginConfig pluginConfig = null;
337             pluginConfig = pluginHolder.getPluginConfig("avalon","dispatchPriority");
338             if (pluginConfig == null) 
339                assertTrue(me + " getting plugin 'dispatchPriority' for avalon gives null", false);
340             String id = pluginConfig.getId();
341             assertEquals(me + " id for avalon/dispatchPriority", "dispatchPriority", id);
342             String className = pluginConfig.getClassName();
343             assertEquals(me + " className for avalon/dispatchPriority", "org.xmlBlaster.util.dispatch.plugins.prio.PriorizedDispatchPlugin", className);
344 
345             pluginConfig = pluginHolder.getPluginConfig("avalon","queueCACHE");
346             pluginConfig = pluginHolder.getPluginConfig("avalon","queueRAM");
347             pluginConfig = pluginHolder.getPluginConfig("avalon","queueJDBC");
348             pluginConfig = pluginHolder.getPluginConfig("avalon","storage:CACHE");
349             pluginConfig = pluginHolder.getPluginConfig("avalon","storage:RAM");
350 
351             //should not exist
352             pluginConfig = pluginHolder.getPluginConfig("avalon","storage:JDBC");
353 
354             //should be the individual of heron (not from xmlBlaster)
355             pluginConfig = pluginHolder.getPluginConfig("heron","queueJDBC");
356 
357             xml = pluginHolder.toXml();
358             log.info(xml);
359          }
360       }
361       catch (XmlBlasterException e) {
362          fail(me + " failed: " + e.toString());
363       }
364       log.info("successfully ended");
365    }
366 
367 
368    public void testPluginConfigComparator() {
369       String me = ME + "-testPluginConfigConparator";
370       log.info("start");
371 
372       PluginConfigComparator upComparator = new PluginConfigComparator(this.glob, true);
373       PluginConfigComparator downComparator = new PluginConfigComparator(this.glob, false);
374 
375       PluginConfig config1 = new PluginConfig(this.glob, "test:PLUGIN1", true, "org.universe.Plugin1");
376       RunLevelAction action = new RunLevelAction(this.glob, "LOAD", 3, -1, null, 5);
377       config1.addAction(action);
378       action = new RunLevelAction(this.glob, "STOP", -1, 2, null, 4);
379       config1.addAction(action);
380      
381       PluginConfig config2 = new PluginConfig(this.glob, "test:PLUGIN2", true, "org.universe.Plugin2");
382       action = new RunLevelAction(this.glob, "LOAD", 3, -1, null, 5);
383       config2.addAction(action);
384       action = new RunLevelAction(this.glob, "STOP", -1, 2, null, 4);
385       config2.addAction(action);
386      
387       int cmp = upComparator.compare(config1, config2);
388       assertTrue(me + " number of actions", cmp < 0);
389 
390       cmp = downComparator.compare(config1, config2);
391       assertTrue(me + " number of actions", cmp > 0);
392 
393       PluginConfig config3 = new PluginConfig(this.glob, "test:PLUGIN3", true, "org.universe.Plugin3");
394       action = new RunLevelAction(this.glob, "LOAD", 2, -1, null, 3);
395       config3.addAction(action);
396       action = new RunLevelAction(this.glob, "STOP", -1, 1, null, 3);
397       config3.addAction(action);
398       cmp = upComparator.compare(config1, config3);
399       assertTrue(me + " number of actions", cmp > 0);
400       cmp = downComparator.compare(config1, config3);
401       assertTrue(me + " number of actions", cmp < 0);
402 
403       PluginConfig config4 = new PluginConfig(this.glob, "test:PLUGIN4", true, "org.universe.Plugin4");
404       action = new RunLevelAction(this.glob, "LOAD", 2, -1, null, 4);
405       config4.addAction(action);
406       action = new RunLevelAction(this.glob, "STOP", -1, 1, null, 4);
407       config4.addAction(action);
408       cmp = upComparator.compare(config3, config4);
409       assertTrue(me + " number of actions", cmp < 0);
410       cmp = downComparator.compare(config3, config4);
411       assertTrue(me + " number of actions", cmp < 0);
412 
413       try {
414          cmp = upComparator.compare(config3, (PluginConfig)null);
415          assertTrue(me + " number of actions", true);
416       }
417       catch (ClassCastException ex) {
418          log.info("exception is OK and expected in this context");
419       }
420       try {
421          cmp = upComparator.compare(config3, (PluginConfig)null);
422          assertTrue(me + " number of actions", true);
423       }
424       catch (ClassCastException ex) {
425          log.info("exception is OK and expected in this context");
426       }
427       try {
428          cmp = upComparator.compare((PluginConfig)null, config4);
429          assertTrue(me + " number of actions", true);
430       }
431       catch (ClassCastException ex) {
432          log.info("exception is OK and expected in this context");
433       }
434       try {
435          cmp = downComparator.compare((PluginConfig)null, config4);
436          assertTrue(me + " number of actions", true);
437       }
438       catch (ClassCastException ex) {
439          log.info("exception is OK and expected in this context");
440       }
441 
442       log.info("successfully ended");
443    }
444 
445 
446 
447    /**
448     * <pre>
449     *  java org.xmlBlaster.test.classtest.RunLevelTest
450     * </pre>
451     */
452    public static void main(String args[])
453    {
454       RunLevelTest testSub = new RunLevelTest("RunLevelTest", args);
455 
456       testSub.setUp();
457       testSub.testAction();
458       testSub.tearDown();
459 
460       testSub.setUp();
461       testSub.testPluginConfig();
462       testSub.tearDown();
463 
464       testSub.setUp();
465       testSub.testPluginConfigParser();
466       testSub.tearDown();
467 
468       testSub.setUp();
469       testSub.testPluginHolder();
470       testSub.tearDown();
471 
472       testSub.setUp();
473       testSub.testPluginConfigComparator();
474       testSub.tearDown();
475 
476    }
477 }


syntax highlighted by Code2HTML, v. 0.9.1