1 /*------------------------------------------------------------------------------
  2 Name:      TestPersistenceXMLDB.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing persistent messages using dbXMLDriver Persistence
  6 Version:   $Id: TestPersistenceXMLDB.java 16172 2007-05-22 12:42:47Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.persistence;
  9 
 10 import java.util.logging.Level;
 11 import java.util.logging.Logger;
 12 
 13 import junit.framework.Test;
 14 import junit.framework.TestCase;
 15 import junit.framework.TestSuite;
 16 
 17 import org.xmlBlaster.client.I_Callback;
 18 import org.xmlBlaster.client.I_XmlBlasterAccess;
 19 import org.xmlBlaster.client.key.UpdateKey;
 20 import org.xmlBlaster.client.qos.ConnectQos;
 21 import org.xmlBlaster.client.qos.UpdateQos;
 22 import org.xmlBlaster.test.Util;
 23 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 24 import org.xmlBlaster.util.Global;
 25 import org.xmlBlaster.util.MsgUnit;
 26 import org.xmlBlaster.util.XmlBlasterException;
 27 
 28 
 29 /**
 30  * This client tests the persistence driver, the $lt;persistent> flag.
 31  * <p>
 32  * Invoke examples:<br />
 33  * <pre>
 34  *    java junit.textui.TestRunner org.xmlBlaster.test.persistence.TestPersistenceXMLDB
 35  *
 36  *    java junit.swingui.TestRunner org.xmlBlaster.test.persistence.TestPersistenceXMLDB
 37  * </pre>
 38  */
 39 public class TestPersistenceXMLDB extends TestCase implements I_Callback {
 40    private final static String ME = "TestPersistenceXMLDB";
 41    private Global glob = null;
 42    private static Logger log = Logger.getLogger(TestPersistenceXMLDB.class.getName());
 43 
 44    private final String senderName = "Benedikt";
 45    private final String senderPasswd = "secret";
 46    private String publishOid = "amIpersistent";
 47    private String subscribeString = "subscribeMe";
 48    private I_XmlBlasterAccess senderConnection = null;
 49    private String senderContent = "Smoked < Ham"; // not well formed XML on purpose
 50    // private String sendetContent = "<description>Smoked Ham</description>";
 51 
 52    private int numReceived = 0;
 53 
 54    private EmbeddedXmlBlaster st;
 55    private EmbeddedXmlBlaster serverThread1;
 56    private EmbeddedXmlBlaster serverThread2;
 57    private int serverPort = 7604;
 58 
 59    /**
 60     * Constructs the TestPersistenceXMLDB object.
 61     * <p />
 62     * @param glob                Keeps global args and parameters.
 63     * @param testName The name used in the test suite.
 64     */
 65    public TestPersistenceXMLDB(Global glob, String testName)
 66    {
 67       super(testName);
 68       this.glob = glob;
 69 
 70    }
 71 
 72    /**
 73     * Starts a xmlBlaster serverthread.
 74     * @return the server thread.
 75     */
 76    protected EmbeddedXmlBlaster startServer() {
 77       EmbeddedXmlBlaster st;
 78       glob.init(Util.getOtherServerPorts(serverPort));
 79       /*
 80       How to get the PersistenceDriver switched on fromrunning xmlBlaster embedded?
 81       Example:
 82 
 83        String[] args = {
 84                          "-isRelease", "false",
 85                          "-logConsole", "true",
 86                          "-bootstrapHostname", "localhost",
 87                          "-socket.subscriptions", "ATD,VDM",
 88                          "-mom.username", "shInt",
 89                          "-mom.password", "xx",
 90                          "-appServ.username", "momusr",
 91                          "-appServ.password", "xx"
 92                        };
 93        glob.init(args);
 94        serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 95       */
 96 
 97       st = EmbeddedXmlBlaster.startXmlBlaster(Util.getOtherServerPorts(serverPort));
 98       log.info("XmlBlaster is ready for testing on bootstrapPort " + serverPort);
 99       return st;
100    } // end of startServer
101 
102 
103    /**
104     * Stops a xmlBlaster serverthread.
105     * @param st keeps the server thread
106     */
107    protected void stopServer(EmbeddedXmlBlaster st) {
108          EmbeddedXmlBlaster.stopXmlBlaster(st);
109          log.info("Xmlblaster stopped");
110          st = null;
111    } // end of stopServer
112 
113 
114    /**
115     * Connects a client at the server.
116     * @param name               The loginname.
117     * @param passwd     The loginpassword.
118     * @return The sender connection.
119     */
120    protected I_XmlBlasterAccess connectClient(String name, String passwd) {
121       log.info("connect to client: name='" + name + "' passwd='" + passwd + "'");
122          I_XmlBlasterAccess sc = null;
123       try {
124          sc = glob.getXmlBlasterAccess();
125          ConnectQos qos = new ConnectQos(glob, name, passwd); // == "<qos></qos>";
126          sc.connect(qos, this);
127          log.info(name + " connected" );
128       } catch (Exception e) {
129           log.severe(e.toString());
130           e.printStackTrace();
131       }
132       return sc;
133    } // end of connectClient
134 
135 
136    /**
137     * Disconnects a client from the server.
138     * @param sc A connection of a client to xmlBlaster.
139     */
140    protected void disconnectClient(I_XmlBlasterAccess sc) {
141       try {
142          sc.disconnect(null);
143          sc = null;
144       } catch (Exception e) {
145           log.severe(e.toString());
146           e.printStackTrace();
147       }
148    } // end of disconnectClient
149 
150 
151    /**
152     * Sets up the fixture.
153     * Sends a persistent message to be stored in persistence driver.
154     * <p />
155     * Starts the server, creates a connection and does a login.<br />
156     * Sends a persistent message and disconnects from server.<br />
157     * Shuts the server down.<br />
158     */
159    protected void setUp() {
160       serverThread1 = startServer();
161       senderConnection = connectClient(senderName, senderPasswd);
162 
163       sendPersistent(senderConnection);
164 
165       disconnectClient(senderConnection);
166       stopServer(serverThread1);
167 
168       serverThread1 = null;
169       senderConnection = null;
170    } // end of setUp
171 
172    protected void tearDown()
173    {
174       // reset to default server bootstrapPort (necessary if other tests follow in the same JVM).
175       Util.resetPorts();
176    }
177 
178    /**
179     * Publish a persistent message.
180     * @param sc A connection of a client to xmlBlaster.
181     */
182    public void sendPersistent(I_XmlBlasterAccess sc) {
183         if (log.isLoggable(Level.FINER)) log.finer("sendPersistent");
184       if (log.isLoggable(Level.FINE)) log.fine("Testing a persistent message ...");
185 
186       String xmlKey = "<key oid='" + publishOid + "' contentMime='text/plain'>\n" +
187                       "   <" + subscribeString + "/>\n" +
188                       "</key>";
189 
190       String qos = "<qos>" +
191                    "   <persistent />" +
192                    "</qos>";
193 
194       try {
195          MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
196          String returnedOid = sc.publish(msgUnit).getKeyOid();
197          assertEquals("Returned oid is invalid", publishOid, returnedOid);
198          log.info("Sending of '" + senderContent + "' done, returned oid '" + publishOid + "'");
199       } catch(XmlBlasterException e) {
200          log.severe("publish() XmlBlasterException: " + e.getMessage());
201          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
202       }
203    } // end of sendPersistent
204 
205 
206    /**
207     * Subscribes to publishOid at the given connection.
208     * @param sc A connection of a client to xmlBlaster.
209     */
210    protected void subscribe(I_XmlBlasterAccess sc) {
211         if (log.isLoggable(Level.FINER)) log.finer("subscribe");
212 
213       String xmlKeySub = "<key oid='' queryType='XPATH'>\n" + "/xmlBlaster/key/" + subscribeString + " </key>";
214       log.info("Subscribe to '" + xmlKeySub + "' ...");
215 
216       try {
217          sc.subscribe(xmlKeySub, "<qos></qos>");
218       } catch(XmlBlasterException e2) {
219          log.warning("XmlBlasterException: " + e2.getMessage());
220       }
221       //log.fine("Subscribed to '" + xmlKeySub + "' ...");
222    } // end of subscribe
223 
224 
225    /**
226     * TEST: Subscribes to the message with the given key per XPATH.
227     * <p />
228     * Starts the server, creates a connection and does a login.<br />
229     * Subscribes to a persistent message waits a while and disconnects from server.<br />
230     * Shuts the server down.<br />
231     */
232    public void testPersistent() {
233 
234       serverThread2 = startServer();
235       senderConnection = connectClient(senderName, senderPasswd);
236 
237       subscribe(senderConnection);
238       try { Thread.sleep(2000L); } catch( InterruptedException i) {}   // Wait 200 milli seconds, until all updates are processed ...
239 
240       disconnectClient(senderConnection);
241       stopServer(serverThread2);
242 
243       serverThread2 = null;
244       senderConnection = null;
245    } // end of testPersistent
246 
247 
248    /**
249     * This is the callback method invoked from xmlBlaster
250     * delivering us a new asynchronous message.
251     * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
252     */
253    public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
254       //log.info("Receiving update of a message ...");
255       if (log.isLoggable(Level.FINER)) log.finer("Receiving update of a message ...");
256 
257       numReceived += 1;
258 
259       System.out.println(updateKey.toXml());
260       System.out.println((new String(content)).toString());
261       System.out.println(updateQos.toXml());
262 
263       assertEquals("Wrong sender", senderName, updateQos.getSender().getLoginName());
264       assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
265       assertEquals("Message content is corrupted", new String(senderContent), new String(content));
266       return "";
267    }
268 
269 
270    /**
271     * Method is used by TestRunner to load these tests
272     */
273    public static Test suite() {
274        TestSuite suite= new TestSuite();
275        suite.addTest(new TestPersistenceXMLDB(new Global(), "testPersistent"));
276        return suite;
277    }
278 
279    /**
280     * Invoke: java org.xmlBlaster.test.persistence.TestPersistenceXMLDB
281     * @deprecated Use the TestRunner from the testsuite to run it:<p />
282     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.persistence.TestPersistenceXMLDB</pre>
283     */
284    public static void main(String args[]) {
285       Global glob = new Global();
286 
287       if (glob.init(args) != 0) {
288          System.err.println(ME + ": Init failed");
289          System.exit(1);
290       }
291 
292       TestPersistenceXMLDB testSub = new TestPersistenceXMLDB(glob, "TestPersistenceXMLDB");
293       testSub.setUp();
294       testSub.testPersistent();
295    }
296 }


syntax highlighted by Code2HTML, v. 0.9.1