1 /*------------------------------------------------------------------------------
 2 Name:      StopXmlBlaster.java
 3 Project:   xmlBlaster.org
 4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 5 Comment:   Stop xmlBlaster
 6 ------------------------------------------------------------------------------*/
 7 package org.xmlBlaster.test;
 8 
 9 import org.xmlBlaster.client.qos.ConnectQos;
10 import org.xmlBlaster.client.qos.DisconnectQos;
11 import org.xmlBlaster.client.I_XmlBlasterAccess;
12 import org.xmlBlaster.util.MsgUnit;
13 import org.xmlBlaster.util.Global;
14 
15 import junit.framework.*;
16 
17 /**
18  * This client connects to xmlBlaster and stops it with a command message.
19  * <p />
20  * Invoke: java org.xmlBlaster.test.StopXmlBlaster
21  */
22 public class StopXmlBlaster extends TestCase
23 {
24    Global glob;
25 
26    public StopXmlBlaster(String name) { // For Junit invoke
27       super(name);
28       this.glob = Global.instance();
29    }
30 
31    public StopXmlBlaster(String[] args, String name) { // Used by our main
32       super(name);
33       this.glob = new Global(args);
34       testStop();
35    }
36 
37    /** Stop xmlBlaster server (invoked by junit automatically as name starts with 'test') */
38    public void testStop() {
39       try {
40          I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
41 
42          ConnectQos qos = new ConnectQos(glob, "joe", "secret");
43          con.connect(qos, null);
44 
45          con.publish(new MsgUnit("<key oid='__cmd:?exit=0'/>", "".getBytes(), "<qos/>"));
46 
47          con.disconnect(null);
48 
49          // xmlBlaster shuts down 2 sec later + time to process shutdown
50          try { Thread.sleep(4000L); } catch( InterruptedException i) {}
51 
52          try {
53             Global glob2 = this.glob.getClone(null);
54             I_XmlBlasterAccess con2 = glob2.getXmlBlasterAccess();
55             ConnectQos connectQos = new ConnectQos(glob2, "joe", "secret");
56             con2.connect(connectQos, null);
57             fail("No connection expected");
58          }
59          catch(org.xmlBlaster.util.XmlBlasterException e) {
60             System.err.println("Success, connection not possible any more");
61          }
62       }
63       catch (Exception e) {
64          System.err.println(e.toString());
65          fail(e.toString());
66       }
67    }
68 
69    public static void main(String args[]) {
70       new StopXmlBlaster(args, "StopXmlBlaster");
71    }
72 }


syntax highlighted by Code2HTML, v. 0.9.1