1 /*------------------------------------------------------------------------------
  2 Name:      TestCallbackConfig.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Login/logout test for xmlBlaster
  6 Version:   $Id: TestCallbackConfig.java 16172 2007-05-22 12:42:47Z ruff $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.qos;
  9 
 10 import java.util.logging.Logger;
 11 
 12 import junit.framework.Test;
 13 import junit.framework.TestCase;
 14 import junit.framework.TestSuite;
 15 
 16 import org.xmlBlaster.client.I_XmlBlasterAccess;
 17 import org.xmlBlaster.client.qos.ConnectQos;
 18 import org.xmlBlaster.client.qos.DisconnectQos;
 19 import org.xmlBlaster.client.qos.EraseReturnQos;
 20 import org.xmlBlaster.test.MsgInterceptor;
 21 import org.xmlBlaster.test.Util;
 22 import org.xmlBlaster.util.Global;
 23 import org.xmlBlaster.util.MsgUnit;
 24 import org.xmlBlaster.util.def.Constants;
 25 import org.xmlBlaster.util.qos.address.CallbackAddress;
 26 
 27 
 28 /**
 29  * This client does test different callback configuration settings. 
 30  * <p />
 31  * This client may be invoked multiple time on the same xmlBlaster server,
 32  * as it cleans up everything after his tests are done.
 33  */
 34 public class TestCallbackConfig extends TestCase
 35 {
 36    private static String ME = "TestCallbackConfig";
 37    private final Global glob;
 38    private static Logger log = Logger.getLogger(TestCallbackConfig.class.getName());
 39    private String name;
 40    private String passwd = "secret";
 41    private I_XmlBlasterAccess con = null;
 42    private String publishOid = null;
 43    private String cbSessionId = "topSecret";
 44    private MsgInterceptor updateInterceptor;
 45 
 46    /**
 47     * Constructs the TestCallbackConfig object.
 48     * <p />
 49     * @param testName   The name used in the test suite
 50     * @param name       The name to login to the xmlBlaster
 51     */
 52    public TestCallbackConfig(Global glob, String testName, String name) {
 53        super(testName);
 54        this.glob = glob;
 55 
 56        this.name = name;
 57    }
 58 
 59    /**
 60     * Sets up the fixture.
 61     * <p />
 62     * Connect to xmlBlaster and login as admin, subscribe to dead letters
 63     */
 64    protected void setUp() {
 65       Util.resetPorts();
 66       Util.resetPorts(glob);
 67       try {
 68          con = glob.getXmlBlasterAccess();
 69          ConnectQos qos = new ConnectQos(null, "admin", passwd);
 70          
 71          // We configure detailed how our callback is handled by xmlBlaster
 72          // In connect() a default callback server is created and its address is added to cbProps
 73          CallbackAddress cbProps = new CallbackAddress(new Global());
 74          cbProps.setCollectTime(0L); // dispatch/callback/burstMode/collectTime"
 75          cbProps.setSecretSessionId(cbSessionId);
 76          cbProps.setPingInterval(10000);
 77          cbProps.setRetries(1);
 78          cbProps.setDelay(1000);
 79          cbProps.setPtpAllowed(true);
 80          qos.addCallbackAddress(cbProps);
 81 
 82          this.updateInterceptor = new MsgInterceptor(this.glob, log, null);
 83          con.connect(qos, this.updateInterceptor); // Login to xmlBlaster and collect update messages with interceptor
 84       }
 85       catch (Exception e) {
 86          log.severe(e.toString() + " \n" + glob.getProperty().toXml() + " GLOBAL.INSTANCE:\n" + Global.instance().getProperty().toXml());
 87          e.printStackTrace();
 88          assertTrue(e.toString(), false);
 89       }
 90       this.updateInterceptor.clear();
 91    }
 92 
 93    /**
 94     * Tears down the fixture.
 95     * <p />
 96     * cleaning up .... erase() the previous message OID and logout
 97     */
 98    protected void tearDown() {
 99       try {
100          if (con != null) {
101             EraseReturnQos[] strArr = con.erase("<key oid='" + publishOid + "'/>", null);
102             if (strArr.length != 1) log.severe("ERROR: Erased " + strArr.length + " messages");
103             con.disconnect(new DisconnectQos(glob));
104          }
105       }
106       catch (Exception e) {
107          log.severe(e.toString());
108          assertTrue(e.toString(), false);
109       }
110    }
111 
112    /**
113     */
114    public void testCbSessionId() {
115       log.info("testCbSessionId() ...");
116       try {
117          con.subscribe("<key oid='testCallbackMsg'/>", null);
118 
119          publishOid = con.publish(new MsgUnit("<key oid='testCallbackMsg'/>", "Bla".getBytes(), null)).getKeyOid();
120 
121          log.info("Success: Publishing done, returned oid=" + publishOid);
122 
123          assertEquals("returned oid", "testCallbackMsg", publishOid);
124          assertEquals("numReceived after publishing", 1, this.updateInterceptor.waitOnUpdate(2000L, publishOid, Constants.STATE_OK));
125          assertEquals("", 1, this.updateInterceptor.getMsgs().length);
126          assertEquals("", this.cbSessionId, this.updateInterceptor.getMsgs()[0].getCbSessionId());
127       }
128       catch (Exception e) {
129          log.severe(e.toString());
130          assertTrue(e.toString(), false);
131       }
132       log.info("Success in testCbSessionId()");
133    }
134 
135    /**
136     * Method is used by TestRunner to load these tests
137     */
138    public static Test suite() {
139        TestSuite suite= new TestSuite();
140        String loginName = "Tim";
141        suite.addTest(new TestCallbackConfig(new Global(), "testCbSessionId", "Tim"));
142        return suite;
143    }
144 
145    /**
146     * Invoke:
147     * <pre>
148     *  java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestCallbackConfig
149     *
150     *  java org.xmlBlaster.test.qos.TestCallbackConfig
151     * </pre>
152     */
153    public static void main(String args[]) {
154       TestCallbackConfig testSub = new TestCallbackConfig(new Global(args), "TestCallbackConfig", "Tim");
155       testSub.setUp();
156       testSub.testCbSessionId();
157       testSub.tearDown();
158    }
159 }


syntax highlighted by Code2HTML, v. 0.9.1