1 /*------------------------------------------------------------------------------
  2 Name:      TestHistoryZero.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.client;
  7 
  8 import java.util.logging.Logger;
  9 import org.xmlBlaster.util.Global;
 10 import org.xmlBlaster.client.key.EraseKey;
 11 import org.xmlBlaster.client.key.GetKey;
 12 import org.xmlBlaster.client.qos.ConnectQos;
 13 import org.xmlBlaster.util.XmlBlasterException;
 14 import org.xmlBlaster.util.def.Constants;
 15 import org.xmlBlaster.util.property.PropString;
 16 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 17 import org.xmlBlaster.client.qos.EraseQos;
 18 import org.xmlBlaster.client.qos.PublishQos;
 19 import org.xmlBlaster.client.qos.GetQos;
 20 import org.xmlBlaster.client.I_XmlBlasterAccess;
 21 import org.xmlBlaster.util.qos.HistoryQos;
 22 import org.xmlBlaster.util.qos.TopicProperty;
 23 import org.xmlBlaster.util.qos.storage.HistoryQueueProperty;
 24 import org.xmlBlaster.util.MsgUnit;
 25 
 26 import org.xmlBlaster.test.Util;
 27 import org.xmlBlaster.test.MsgInterceptor;
 28 import junit.framework.*;
 29 
 30 
 31 /**
 32  * Tests the setting of history queue to zero.
 33  * <p>
 34  * Invoke examples:<br />
 35  * <pre>
 36  *   java junit.textui.TestRunner -noloading org.xmlBlaster.test.client.TestHistoryZero
 37  *   java junit.swingui.TestRunner -noloading org.xmlBlaster.test.client.TestHistoryZero
 38  * </pre>
 39  * @see org.xmlBlaster.client.I_XmlBlasterAccess
 40  */
 41 public class TestHistoryZero extends TestCase {
 42    private static String ME = "TestHistoryZero";
 43    
 44    private Global glob;
 45    private Global serverGlobal;
 46    private static Logger log = Logger.getLogger(TestHistoryZero.class.getName());
 47 
 48    private int serverPort = 7604;
 49    private EmbeddedXmlBlaster serverThread;
 50 
 51    private MsgInterceptor updateInterceptor;
 52    private String senderName;
 53 
 54    private final String contentMime = "text/plain";
 55 
 56    public TestHistoryZero(String testName) {
 57       this(null, testName);
 58    }
 59 
 60    public TestHistoryZero(Global glob, String testName) {
 61       super(testName);
 62       this.glob = glob;
 63       this.senderName = testName;
 64    }
 65 
 66    /**
 67     * Sets up the fixture.
 68     * <p />
 69     * Connect to xmlBlaster and login
 70     */
 71    protected void setUp() {
 72       this.glob = (this.glob == null) ? Global.instance() : this.glob;
 73 
 74       glob.init(Util.getOtherServerPorts(serverPort));
 75 
 76       String[] args = new String[] {"-queue/history/maxEntriesCache", "0",
 77                                     "-queue/history/maxEntries","0",
 78                                    };
 79 
 80       this.serverGlobal = this.glob.getClone(args);
 81       
 82       serverThread = EmbeddedXmlBlaster.startXmlBlaster(this.serverGlobal);
 83       log.info("XmlBlaster is ready for testing on bootstrapPort " + serverPort);
 84       try {
 85          I_XmlBlasterAccess con = glob.getXmlBlasterAccess(); // Find orb
 86 
 87          String passwd = "secret";
 88          ConnectQos connectQos = new ConnectQos(glob, senderName, passwd); // == "<qos>...</qos>";
 89          // set the persistent connection 
 90          connectQos.setPersistent(false);
 91          this.updateInterceptor = new MsgInterceptor(this.glob, log, null); // Collect received msgs
 92          con.connect(connectQos, this.updateInterceptor);  // Login to xmlBlaster, register for updates
 93       }
 94       catch (XmlBlasterException e) {
 95           log.warning("setUp() - login failed: " + e.getMessage());
 96           fail("setUp() - login fail: " + e.getMessage());
 97       }
 98       catch (Exception e) {
 99           log.severe("setUp() - login failed: " + e.toString());
100           e.printStackTrace();
101           fail("setUp() - login fail: " + e.toString());
102       }
103    }
104 
105    /**
106     * Tears down the fixture.
107     * <p />
108     * cleaning up .... erase() the previous message OID and logout
109     */
110    protected void tearDown() {
111       log.info("Entering tearDown(), test is finished");
112       String xmlKey = "<key oid='' queryType='XPATH'>\n" +
113                       "   //TestHistoryZero-AGENT" +
114                       "</key>";
115 
116       String qos = "<qos><forceDestroy>true</forceDestroy></qos>";
117       I_XmlBlasterAccess con = this.glob.getXmlBlasterAccess();
118       try {
119          con.erase(xmlKey, qos);
120 
121          PropString defaultPlugin = new PropString("CACHE,1.0");
122          String propName = defaultPlugin.setFromEnv(this.glob, glob.getStrippedId(), null, "persistence", Constants.RELATING_TOPICSTORE, "defaultPlugin");
123          log.info("Lookup of propName=" + propName + " defaultValue=" + defaultPlugin.getValue());
124       }
125       catch(XmlBlasterException e) {
126          log.severe("XmlBlasterException: " + e.getMessage());
127       }
128       finally {
129          con.disconnect(null);
130          EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
131          this.serverThread = null;
132          // reset to default server bootstrapPort (necessary if other tests follow in the same JVM).
133          Util.resetPorts(this.serverGlobal);
134          Util.resetPorts(glob);
135          this.glob = null;
136          con = null;
137          Global.instance().shutdown();
138       }
139    }
140 
141    /**
142     * TEST: Construct a message and publish it.
143     * <p />
144     */
145    public void doPublish(String oid, int counter, long numHistory) throws XmlBlasterException {
146       log.info("Publishing a message " + oid + " ...");
147       String xmlKey = "<key oid='" + oid + "' contentMime='" + contentMime + "'>\n" +
148                       "   <TestHistoryZero-AGENT id='192.168.124.10' subId='1' type='generic'>" +
149                       "   </TestHistoryZero-AGENT>" +
150                       "</key>";
151       String content = "" + counter;
152       PublishQos qosWrapper = new PublishQos(glob); // == "<qos></qos>"
153       if (numHistory > -1) {
154          TopicProperty topicProp = new TopicProperty(this.glob);
155          HistoryQueueProperty historyQueueProp = topicProp.getHistoryQueueProperty();
156          historyQueueProp.setMaxEntries(numHistory);
157          // TODO TEST THE maxEntriesCache != entriesCache. First specify the required behaviour.
158          historyQueueProp.setMaxEntriesCache(numHistory);
159          qosWrapper.setTopicProperty(topicProp);               
160       }
161       
162       MsgUnit msgUnit = new MsgUnit(xmlKey, content.getBytes(), qosWrapper.toXml());
163 
164       this.glob.getXmlBlasterAccess().publish(msgUnit);
165       log.info("Success: Publishing of " + oid + " done");
166    }
167 
168    private void doGet(String oid, int expect) {
169       GetKey key = new GetKey(this.glob, oid);
170       GetQos qos = new GetQos(this.glob);
171       HistoryQos histQos = new HistoryQos(this.glob, 2);
172       qos.setHistoryQos(histQos);
173       try {
174          MsgUnit[] msg = this.glob.getXmlBlasterAccess().get(key, qos);
175          assertNotNull("should not be null", msg);
176          assertEquals("the number of returned values is wrong", expect, msg.length);
177       }
178       catch (XmlBlasterException ex) {
179          ex.printStackTrace();
180          assertTrue("no exception should take place here", false);
181       }
182    }
183       
184 
185    /**
186     * TEST: <br />
187     */
188    public void testHistoryZero() {
189 
190       int numPublish = 5;
191       for (int i=0; i<numPublish; i++) {
192          try {
193             doPublish("historyZeroA", i+1, -1);
194             doPublish("historyZeroB", i+1, 0);
195             doPublish("historyZeroC", i+1, 1);
196             doPublish("historyZeroD", i+1, 2);
197             doPublish("historyZeroE", i+1, 5);
198          }
199          catch (XmlBlasterException ex) {
200             ex.printStackTrace();
201             assertTrue("an exception should not occur here", false);
202          }   
203       }
204       doGet("historyZeroA", 0);
205       doGet("historyZeroB", 0);
206       doGet("historyZeroC", 1);
207       doGet("historyZeroD", 2);
208       doGet("historyZeroE", 2);
209       try {
210          EraseKey eraseKey = new EraseKey(this.glob, "//TestHistoryZero-AGENT", "XPATH");
211          EraseQos eraseQos = new EraseQos(this.glob);
212          this.glob.getXmlBlasterAccess().erase(eraseKey, eraseQos);
213       }
214       catch (XmlBlasterException e) {
215          e.printStackTrace();
216          assertTrue("exception should not occur here " + e.getMessage(), false);
217       }
218    }
219 
220    /**
221     * Invoke: java org.xmlBlaster.test.client.TestHistoryZero
222     * <p />
223     * @deprecated Use the TestRunner from the testsuite to run it:<p />
224     * <pre>   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.client.TestHistoryZero</pre>
225     */
226    public static void main(String args[])
227    {
228       Global glob = new Global();
229       if (glob.init(args) != 0) {
230          System.out.println(ME + ": Init failed");
231          System.exit(1);
232       }
233 
234       TestHistoryZero test = new TestHistoryZero(glob, "TestHistoryZero/1");
235       
236       test.setUp();
237       test.testHistoryZero();
238       test.tearDown();
239    }
240 }


syntax highlighted by Code2HTML, v. 0.9.1