1 /*------------------------------------------------------------------------------
  2 Name:      TestPubBurstMode.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Testing publish()
  6 Version:   $Id: TestPubBurstMode.java 14846 2006-03-07 17:14:22Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.qos;
  9 
 10 import org.xmlBlaster.util.StopWatch;
 11 
 12 import java.util.logging.Logger;
 13 import java.util.logging.Level;
 14 import org.xmlBlaster.util.Global;
 15 import org.xmlBlaster.client.qos.ConnectQos;
 16 import org.xmlBlaster.util.XmlBlasterException;
 17 import org.xmlBlaster.client.I_Callback;
 18 import org.xmlBlaster.client.key.UpdateKey;
 19 import org.xmlBlaster.client.qos.UpdateQos;
 20 import org.xmlBlaster.client.qos.PublishReturnQos;
 21 import org.xmlBlaster.client.qos.EraseReturnQos;
 22 import org.xmlBlaster.client.qos.PublishQos;
 23 import org.xmlBlaster.client.I_XmlBlasterAccess;
 24 import org.xmlBlaster.util.MsgUnit;
 25 
 26 import junit.framework.*;
 27 
 28 
 29 /**
 30  * This client tests the method publish() in burst mode, messages are
 31  * collected and sent in one method call. 
 32  * <p>
 33  * This client may be invoked multiple time on the same xmlBlaster server,
 34  * as it cleans up everything after his tests are done.
 35  * <p>
 36  * Invoke examples:<br />
 37  * <pre>
 38  *  java org.xmlBlaster.test.qos.TestPubBurstMode -numPublish 10000 -dispatch/connection/protocol RMI -warn false
 39  *  java junit.textui.TestRunner org.xmlBlaster.test.qos.TestPubBurstMode
 40  *  java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestPubBurstMode
 41  * </pre>
 42  */
 43 public class TestPubBurstMode extends TestCase
 44 {
 45    private static String ME = "TestPubBurstMode";
 46    private final Global glob;
 47    private static Logger log = Logger.getLogger(TestPubBurstMode.class.getName());
 48 
 49    private boolean messageArrived = false;
 50 
 51    private String subscribeOid;
 52    private String publishOid = "AMessage";
 53    private I_XmlBlasterAccess senderConnection;
 54    private String senderName;
 55 
 56    private int numPublish;
 57    private final String contentMime = "text/xml";
 58    private final String contentMimeExtended = "1.0";
 59    
 60    private StopWatch stopWatch = new StopWatch();
 61 
 62 
 63    /**
 64     * Constructs the TestPubBurstMode object.
 65     * <p />
 66     * @param testName  The name used in the test suite
 67     * @param loginName The name to login to the xmlBlaster
 68     */
 69    public TestPubBurstMode(Global glob, String testName, String loginName)
 70    {
 71       super(testName);
 72       this.glob = glob;
 73 
 74       this.senderName = loginName;
 75    }
 76 
 77 
 78    /**
 79     * Sets up the fixture.
 80     * <p />
 81     * Connect to xmlBlaster and login
 82     */
 83    protected void setUp()
 84    {
 85       try {
 86          numPublish = glob.getProperty().get("numPublish", 100);
 87          senderConnection = glob.getXmlBlasterAccess();   // Find orb
 88          String passwd = "secret";
 89          ConnectQos connectQos = new ConnectQos(glob, senderName, passwd);
 90          senderConnection.connect(connectQos, null); // Login to xmlBlaster
 91       }
 92       catch (Exception e) {
 93           log.severe(e.toString());
 94           e.printStackTrace();
 95       }
 96    }
 97 
 98 
 99    /**
100     * Tears down the fixture.
101     * <p />
102     * cleaning up .... erase() the previous message OID and logout
103     */
104    protected void tearDown()
105    {
106       String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
107                       "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
108                       "</key>";
109       String qos = "<qos></qos>";
110       try {
111          EraseReturnQos[] arr = senderConnection.erase(xmlKey, qos);
112          assertEquals("Erase", 1, arr.length);
113       } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
114 
115       senderConnection.disconnect(null);
116    }
117 
118 
119    /**
120     * TEST: Construct a message and publish it.
121     */
122    public void testPublish()
123    {
124       if (log.isLoggable(Level.FINE)) log.fine("Publishing messages ...");
125 
126       String xmlKey = "<key oid='" + publishOid + "' contentMime='" + contentMime + "' contentMimeExtended='" + contentMimeExtended + "'>\n" +
127                       "   <TestPubBurstMode-AGENT id='192.168.124.10' subId='1' type='generic'>" +
128                       "   </TestPubBurstMode-AGENT>" +
129                       "</key>";
130       PublishQos qosWrapper = new PublishQos(glob);
131       String qos = qosWrapper.toXml(); // == "<qos></qos>"
132 
133       MsgUnit[] msgUnitArr = new MsgUnit[numPublish];
134       try {
135          for (int ii=0; ii<numPublish; ii++) {
136             String senderContent = ME + ii; // different content forcing xmlBlaster to store
137             msgUnitArr[ii] = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
138          }
139       }
140       catch (XmlBlasterException e) {
141          fail(e.getMessage());
142       }
143 
144       PublishReturnQos[] publishOidArr = null;
145       try {
146          log.info("Publishing " + numPublish + " messages in burst mode ...");
147          stopWatch = new StopWatch();
148          publishOidArr = senderConnection.publishArr(msgUnitArr);
149          double elapsed = (double)stopWatch.elapsed()/1000.; // msec -> sec
150          log.info("Published " + numPublish + " messages in burst mode in " + elapsed + " sec: " +
151                        (long)(numPublish/elapsed) + " messages/sec");
152       } catch(XmlBlasterException e) {
153          log.warning("XmlBlasterException: " + e.getMessage());
154          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
155       }
156       assertTrue("returned publishOidArr == null", publishOidArr != null);
157       assertEquals("returned publishOidArr", numPublish, publishOidArr.length);
158    }
159 
160 
161    /**
162     * TEST: Construct a message and publish it,<br />
163     * the previous XPath subscription should match and send an update.
164     */
165    public void testPublishMany()
166    {
167       testPublish();
168       System.out.println("");
169       testPublish();
170       System.out.println("");
171       testPublish();
172    }
173 
174 
175    /**
176     * Method is used by TestRunner to load these tests
177     */
178    public static Test suite()
179    {
180        TestSuite suite= new TestSuite();
181        String loginName = "Tim";
182        suite.addTest(new TestPubBurstMode(new Global(), "testPublishMany", loginName));
183        return suite;
184    }
185 
186 
187    /**
188     * Invoke: java org.xmlBlaster.test.qos.TestPubBurstMode
189     * @deprecated Use the TestRunner from the testsuite to run it:<p />
190     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestPubBurstMode</pre>
191     */
192    public static void main(String args[])
193    {
194       Global glob = new Global();
195       if (glob.init(args) != 0) {
196          System.err.println(ME + ": Init failed");
197          System.exit(1);
198       }
199       TestPubBurstMode testPub = new TestPubBurstMode(glob, "TestPubBurstMode", "Tim");
200       testPub.setUp();
201       testPub.testPublishMany();
202       testPub.tearDown();
203    }
204 }


syntax highlighted by Code2HTML, v. 0.9.1