1 package org.xmlBlaster.test.classtest;
  2 
  3 import java.util.logging.Logger;
  4 import java.util.logging.Level;
  5 import org.xmlBlaster.util.StopWatch;
  6 import org.xmlBlaster.client.qos.GetQos;
  7 import org.xmlBlaster.engine.ServerScope;
  8 import org.xmlBlaster.util.XmlBlasterException;
  9 import org.xmlBlaster.util.def.MethodName;
 10 import org.xmlBlaster.util.key.QueryKeyData;
 11 import org.xmlBlaster.util.qos.ClientProperty;
 12 import org.xmlBlaster.util.qos.QueryQosData;
 13 import org.xmlBlaster.engine.admin.CommandWrapper;
 14 
 15 import junit.framework.*;
 16 
 17 /**
 18  * Test CommandWrapper class. 
 19  * <p />
 20  * Invoke: java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.classtest.CommandWrapperTest
 21  *
 22  * @see org.xmlBlaster.engine.admin.CommandWrapper
 23  */
 24 public class CommandWrapperTest extends TestCase {
 25    private String ME = "CommandWrapperTest";
 26    protected ServerScope glob;
 27    private static Logger log = Logger.getLogger(CommandWrapperTest.class.getName());
 28    private StopWatch stopWatch = new StopWatch();
 29 
 30    public CommandWrapperTest(String name) {
 31       super(name);
 32    }
 33 
 34    protected void setUp() {
 35       glob = new ServerScope();
 36 
 37       glob.setId("heron");
 38    }
 39 
 40    protected void tearDown() {
 41    }
 42 
 43    public void testBasic() {
 44       assertEquals("Wrong node id", "heron", glob.getId());
 45       String cmd = null;
 46       CommandWrapper w = null;
 47 
 48       try {
 49          cmd = "/node/heron/client/joe/?sessionList";
 50          w = new CommandWrapper(glob, cmd);
 51          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 52          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 53          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 54          assertEquals("Command '" + cmd + "' wrong parsed", "joe/?sessionList", w.getTail());
 55       
 56          cmd = "/node/heron/client/joe/";
 57          w = new CommandWrapper(glob, cmd);
 58          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 59          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 60          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 61          assertEquals("Command '" + cmd + "' wrong parsed", "joe/", w.getTail());
 62 
 63          cmd = "/node/heron/client/joe";
 64          w = new CommandWrapper(glob, cmd);
 65          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 66          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 67          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 68          assertEquals("Command '" + cmd + "' wrong parsed", "joe", w.getTail());
 69 
 70          cmd = "/node/heron/client/";
 71          w = new CommandWrapper(glob, cmd);
 72          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 73          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 74          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 75          assertEquals("Command '" + cmd + "' wrong parsed", null, w.getTail());
 76 
 77          cmd = "/node/heron/client";
 78          w = new CommandWrapper(glob, cmd);
 79          assertEquals("Command '" + cmd + "' wrong parsed", "/node/heron/client", w.getCommand());
 80          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 81          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 82          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 83          assertEquals("Command '" + cmd + "' wrong parsed", null, w.getTail());
 84 
 85          cmd = "client/joe/?sessionList";
 86          w = new CommandWrapper(glob, cmd);
 87          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 88          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 89          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 90          assertEquals("Command '" + cmd + "' wrong parsed", "joe/?sessionList", w.getTail());
 91       
 92          cmd = "client";
 93          w = new CommandWrapper(glob, cmd);
 94          assertEquals("Command '" + cmd + "' wrong parsed", "/node/heron/client", w.getCommand());
 95          assertEquals("Command '" + cmd + "' wrong parsed", "node", w.getRoot());
 96          assertEquals("Command '" + cmd + "' wrong parsed", "heron", w.getClusterNodeId());
 97          assertEquals("Command '" + cmd + "' wrong parsed", "client", w.getThirdLevel());
 98          assertEquals("Command '" + cmd + "' wrong parsed", null, w.getTail());
 99       }
100       catch(XmlBlasterException e) {
101          fail("Failed: " + e.toString());
102       }
103    }
104 
105    public void testInvalid() {
106       assertEquals("Wrong node id", "heron", glob.getId());
107       String cmd = null;
108       CommandWrapper w = null;
109 
110       try {
111          cmd = "/node/heron/";
112          w = new CommandWrapper(glob, cmd);
113          fail("Failed, expected exception for '" + cmd + "'");
114       }
115       catch(XmlBlasterException e) {
116          System.out.println("OK - expected Exception: " + e.toString());
117       }
118 
119       try {
120          cmd = "/node/foeignNode/client/?joe";
121          w = new CommandWrapper(glob, cmd);
122          fail("Failed, expected exception for '" + cmd + "'");
123       }
124       catch(XmlBlasterException e) {
125          System.out.println("OK - expected Exception: " + e.toString());
126       }
127 
128       try {
129          cmd = "/strangeNode/heron/client/?joe";
130          w = new CommandWrapper(glob, cmd);
131          fail("Failed, expected exception for '" + cmd + "'");
132       }
133       catch(XmlBlasterException e) {
134          System.out.println("OK - expected Exception: " + e.toString());
135       }
136 
137       try {
138          cmd = "/node/";
139          w = new CommandWrapper(glob, cmd);
140          fail("Failed, expected exception for '" + cmd + "'");
141       }
142       catch(XmlBlasterException e) {
143          System.out.println("OK - expected Exception: " + e.toString());
144       }
145 
146       try {
147          cmd = "/";
148          w = new CommandWrapper(glob, cmd);
149          fail("Failed, expected exception for '" + cmd + "'");
150       }
151       catch(XmlBlasterException e) {
152          System.out.println("OK - expected Exception: " + e.toString());
153       }
154 
155       try {
156          cmd = "";
157          w = new CommandWrapper(glob, cmd);
158          fail("Failed, expected exception for '" + cmd + "'");
159       }
160       catch(XmlBlasterException e) {
161          System.out.println("OK - expected Exception: " + e.toString());
162       }
163 
164       try {
165          cmd = null;
166          w = new CommandWrapper(glob, cmd);
167          fail("Failed, expected exception for '" + cmd + "'");
168       }
169       catch(XmlBlasterException e) {
170          System.out.println("OK - expected Exception: " + e.toString());
171       }
172 
173       try {
174          cmd = "/////";
175          w = new CommandWrapper(glob, cmd);
176          fail("Failed, expected exception for '" + cmd + "'");
177       }
178       catch(XmlBlasterException e) {
179          System.out.println("OK - expected Exception: " + e.toString());
180       }
181    }
182 
183 /*
184    public void testQosData() {
185       String cmd = null;
186 
187       GetQos getQos = new GetQos(this.glob);
188       getQos.addClientProperty("_one", "1");
189       getQos.addClientProperty("_two", "2");
190       
191       cmd = "client/joe/1/?queue&xmlBlaster.qos=" + getQos.toXml();
192       QueryKeyData keyData = new QueryKeyData(this.glob);
193       keyData.setOid("__cmd:" + cmd);
194       
195       try {
196          CommandWrapper w = new CommandWrapper(glob, keyData);
197          QueryQosData qos = w.getQueryQosData();
198          log.info(qos.toXml());
199          ClientProperty clp = qos.getClientProperty("_one");
200          assertNotNull("should not be null", clp);
201          assertEquals("wrong value for this property", "1", clp.getStringValue());
202          clp = qos.getClientProperty("_two");
203          assertNotNull("should not be null", clp);
204          assertEquals("wrong value for this property", "2", clp.getStringValue());
205          clp = qos.getClientProperty("_three");
206          assertNull("should be null", clp);
207       }
208       catch(XmlBlasterException e) {
209          e.printStackTrace();
210          assertTrue("exception should not occur here: " + e.getMessage(), false);
211       }
212    }
213 */
214    /**
215     * Invoke: java org.xmlBlaster.test.client.TestCommandWrapperTest
216     * <p />
217     * @deprecated Use the TestRunner from the testsuite to run it:<p />
218     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.client.TestActivateDispatcher</pre>
219     */
220    public static void main(String args[])
221    {
222       ServerScope glob = new ServerScope();
223       if (glob.init(args) != 0) {
224          System.out.println("Init failed");
225          System.exit(1);
226       }
227 
228       CommandWrapperTest test = new CommandWrapperTest("CommandWrapperTest");
229       test.setUp();
230       test.testInvalid();
231       test.tearDown();
232    }
233 
234 
235 }


syntax highlighted by Code2HTML, v. 0.9.1