📄 testtopiclifecycle.java
字号:
/*------------------------------------------------------------------------------Name: TestTopicLifeCycle.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Testing some topic state transitions------------------------------------------------------------------------------*/package org.xmlBlaster.test.topic;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.util.Global;import org.xmlBlaster.client.qos.ConnectQos;import org.xmlBlaster.util.FileLocator;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.MsgUnit;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.qos.TopicProperty;import org.xmlBlaster.client.I_Callback;import org.xmlBlaster.client.key.UpdateKey;import org.xmlBlaster.client.key.PublishKey;import org.xmlBlaster.client.key.GetKey;import org.xmlBlaster.client.key.SubscribeKey;import org.xmlBlaster.client.key.UnSubscribeKey;import org.xmlBlaster.client.key.EraseKey;import org.xmlBlaster.client.qos.GetQos;import org.xmlBlaster.client.qos.PublishQos;import org.xmlBlaster.client.qos.PublishReturnQos;import org.xmlBlaster.client.qos.UpdateQos;import org.xmlBlaster.client.qos.SubscribeQos;import org.xmlBlaster.client.qos.SubscribeReturnQos;import org.xmlBlaster.client.qos.EraseQos;import org.xmlBlaster.client.qos.EraseReturnQos;import org.xmlBlaster.client.qos.UnSubscribeQos;import org.xmlBlaster.client.I_XmlBlasterAccess;import org.xmlBlaster.util.EmbeddedXmlBlaster;import org.xmlBlaster.test.Util;import org.xmlBlaster.test.MsgInterceptor;import junit.framework.Test;import junit.framework.TestSuite;import org.custommonkey.xmlunit.XMLTestCase;/** * Here we test some state transitions of a topic. * <p> * We traverse the possible transitions of a topic (TopicHandler.java) * as described in requirement engine.message.lifecycle by sending some expiring messages (see * state transition brackets in requirement)<br /> * Please see individual test for a description * </p> * <p> * Invoke examples: * </p> * <pre> * java junit.textui.TestRunner org.xmlBlaster.test.topic.TestTopicLifeCycle * * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.topic.TestTopicLifeCycle * </pre> * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/engine.message.lifecycle.html">The engine.message.lifecycle requirement</a> * @see org.xmlBlaster.engine.TopicHandler */public class TestTopicLifeCycle extends XMLTestCase implements I_Callback { private Global glob; private static Logger log = Logger.getLogger(TestTopicLifeCycle.class.getName()); private I_XmlBlasterAccess con = null; private String senderContent = "Some message content"; private String publishOid = "TestTopicLifeCycleMsg"; private final String xpathTag = "<something/>"; private final String xpath = "//something"; private SubscribeReturnQos subscribeReturnQos; private long blockUpdateTime = 0L; private EmbeddedXmlBlaster serverThread; private int serverPort = 9566; private boolean startEmbedded = true; private MsgInterceptor updateInterceptor; /** * Constructs the TestTopicLifeCycle object. * <p /> * @param testName The name used in the test suite * @param loginName The name to login to the xmlBlaster */ public TestTopicLifeCycle(Global glob, String testName) { super(testName); this.glob = glob; } /** * Sets up the fixture. * <p /> * Creates a CORBA connection and does a login.<br /> * - One connection for the sender client<br /> */ protected void setUp() { this.startEmbedded = glob.getProperty().get("startEmbedded", this.startEmbedded); if (this.startEmbedded) { glob.init(Util.getOtherServerPorts(serverPort)); String[] args = { }; glob.init(args); serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob); log.info("XmlBlaster is ready for testing the priority dispatch plugin"); } try { con = glob.getXmlBlasterAccess(); ConnectQos qos = new ConnectQos(glob); // == "<qos></qos>"; this.updateInterceptor = new MsgInterceptor(this.glob, log, this); con.connect(qos, this.updateInterceptor); } catch (Exception e) { log.severe(e.toString()); e.printStackTrace(); } this.updateInterceptor.clear(); } /** * Tears down the fixture. * <p /> * cleaning up .... logout */ protected void tearDown() { try { Thread.sleep(200L); } catch( InterruptedException i) {} // Wait 200 milli seconds, until all updates are processed ... String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'/>"; try { EraseReturnQos[] arr = con.erase(xmlKey, "<qos/>"); if (arr.length != 0) { log.severe("Erased " + arr.length + " messages instead of 0"); } assertEquals("Erase", 0, arr.length); // The volatile message schould not exist !! } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); } con.disconnect(null); con=null; if (this.startEmbedded) { try { Thread.sleep(500L); } catch( InterruptedException i) {} // Wait some time EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread); this.serverThread = null; } // reset to default server port (necessary if other tests follow in the same JVM). Util.resetPorts(); this.glob = null; } public EraseReturnQos[] sendErase(boolean forceDestroy) { log.info("Erasing a topic forceDestroy=" + forceDestroy); try { EraseQos eq = new EraseQos(glob); eq.setForceDestroy(forceDestroy); EraseKey ek = new EraseKey(glob, this.publishOid); EraseReturnQos[] er = con.erase(ek.toXml(), eq.toXml()); return er; } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); } return null; } /** * Publish an almost volatile message. */ public void sendExpiringMsg(boolean initializeTopic, long topicDestroyDelay, long msgLifeTime) { log.info("Sending a message initializeTopic=" + initializeTopic + " topicDestroyDelay=" + topicDestroyDelay + " msgLifeTime=" + msgLifeTime); try { // Publish a volatile message PublishKey pk = new PublishKey(glob, publishOid, "text/xml", "1.0"); PublishQos pq = new PublishQos(glob); pq.setLifeTime(msgLifeTime); pq.setForceDestroy(false); if (initializeTopic) { // Configure the topic to our needs TopicProperty topicProperty = new TopicProperty(glob); topicProperty.setDestroyDelay(topicDestroyDelay); topicProperty.setCreateDomEntry(false); pq.setTopicProperty(topicProperty); } MsgUnit msgUnit = new MsgUnit(pk, senderContent, pq); PublishReturnQos publishReturnQos = con.publish(msgUnit); assertEquals("Retunred oid is invalid", publishOid, publishReturnQos.getKeyOid()); log.info("Sending of '" + senderContent + "' done, returned oid=" + publishOid + " " + msgUnit.toXml()); } catch(XmlBlasterException e) { log.severe("publish() XmlBlasterException: " + e.getMessage()); assertTrue("publish - XmlBlasterException: " + e.getMessage(), false); } } /** * Publish an almost volatile XPATH message. * @return publishOid */ public String sendExpiringXPathMsg(long topicDestroyDelay, long msgLifeTime) { log.info("Sending a XPath message topicDestroyDelay=" + topicDestroyDelay + " msgLifeTime=" + msgLifeTime); try { // Publish a volatile message PublishKey pk = new PublishKey(glob, "", "text/xml", "1.0"); pk.setClientTags(xpathTag); PublishQos pq = new PublishQos(glob); pq.setLifeTime(msgLifeTime); pq.setForceDestroy(false); // Configure the topic to our needs TopicProperty topicProperty = new TopicProperty(glob); topicProperty.setDestroyDelay(topicDestroyDelay); topicProperty.setCreateDomEntry(true); pq.setTopicProperty(topicProperty); MsgUnit msgUnit = new MsgUnit(pk, senderContent, pq); PublishReturnQos publishReturnQos = con.publish(msgUnit); log.info("Sending of '" + senderContent + "' done, returned oid=" + publishReturnQos.getKeyOid() + " " + msgUnit.toXml()); return publishReturnQos.getKeyOid(); } catch(XmlBlasterException e) { log.severe("publish() XmlBlasterException: " + e.getMessage()); assertTrue("publish - XmlBlasterException: " + e.getMessage(), false); return ""; // never reached } } /** * Subscribe a volatile message. */ public void subscribeMsg() { log.info("Subscribing message '" + publishOid + "'..."); try { // Subscribe for the volatile message SubscribeKey sk = new SubscribeKey(glob, publishOid); SubscribeQos sq = new SubscribeQos(glob); this.subscribeReturnQos = con.subscribe(sk.toXml(), sq.toXml()); log.info("Subscribing of '" + publishOid + "' done"); } catch(XmlBlasterException e) { log.severe("subscribe() XmlBlasterException: " + e.getMessage()); assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false); } } /** * Subscribe topics with XPATH. * @return The subscription id */ public String subscribeXPathMsg() { log.info("Subscribing message xpath='" + xpath + "'..."); try { // Subscribe for the volatile message SubscribeKey sk = new SubscribeKey(glob, xpath, Constants.XPATH); SubscribeQos sq = new SubscribeQos(glob); this.subscribeReturnQos = con.subscribe(sk.toXml(), sq.toXml()); log.info("Subscribing of '" + xpath + "' done"); return this.subscribeReturnQos.getSubscriptionId(); } catch(XmlBlasterException e) { log.severe("subscribe() XmlBlasterException: " + e.getMessage()); assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -