1 /*------------------------------------------------------------------------------
  2 Name:      TestGetSql92Filter.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Login/logout test for xmlBlaster
  6 Version:   $Id: TestGetSql92Filter.java 14813 2006-03-04 23:02:48Z laghi $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.test.mime;
  9 
 10 import java.util.logging.Logger;
 11 import java.util.logging.Level;
 12 import org.xmlBlaster.util.Global;
 13 import org.xmlBlaster.util.XmlBlasterException;
 14 import org.xmlBlaster.client.key.PublishKey;
 15 import org.xmlBlaster.client.qos.ConnectQos;
 16 import org.xmlBlaster.client.I_XmlBlasterAccess;
 17 import org.xmlBlaster.client.qos.GetQos;
 18 import org.xmlBlaster.client.qos.EraseReturnQos;
 19 import org.xmlBlaster.client.qos.PublishQos;
 20 import org.xmlBlaster.util.MsgUnit;
 21 import org.xmlBlaster.util.qos.AccessFilterQos;
 22 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 23 
 24 import org.xmlBlaster.test.Util;
 25 import junit.framework.*;
 26 
 27 
 28 /**
 29  * This client tests the <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/mime.plugin.accessfilter.html">
 30  * access filter plugin framework</a> on get() invocations. 
 31  * <p />
 32  * Here we test the regular expression plugin which allows us to select messages
 33  * when the message content matches a given regular expression.
 34  * <p />
 35  * We start our own xmlBlaster server in a thread.
 36  * This client may be invoked multiple time on the same xmlBlaster server,
 37  * as it cleans up everything after his tests are done.
 38  * <p>
 39  * Invoke examples:<br />
 40  * <pre>
 41  *    java junit.textui.TestRunner org.xmlBlaster.test.mime.TestGetSql92Filter
 42  *    java junit.swingui.TestRunner org.xmlBlaster.test.mime.TestGetSql92Filter
 43  * </pre>
 44  */
 45 public class TestGetSql92Filter extends TestCase
 46 {
 47    private static String ME = "TestGetSql92Filter";
 48    private final Global glob;
 49    private static Logger log = Logger.getLogger(TestGetSql92Filter.class.getName());
 50 
 51    private I_XmlBlasterAccess con = null;
 52    private String name;
 53    private String passwd = "secret";
 54    private EmbeddedXmlBlaster serverThread;
 55    private int serverPort = 7604;
 56 
 57    /**
 58     * Constructs the TestGetSql92Filter object.
 59     * <p />
 60     * @param testName   The name used in the test suite
 61     * @param name       The name to login to the xmlBlaster
 62     */
 63    public TestGetSql92Filter(Global glob, String testName, String name)
 64    {
 65       super(testName);
 66       this.glob = glob;
 67 
 68       this.name = name;
 69    }
 70 
 71    /**
 72     * Sets up the fixture.
 73     * <p />
 74     * We start an own xmlBlaster server in a separate thread,
 75     * it is configured to load our simple demo MIME filter plugin.
 76     * <p />
 77     * Then we connect as a client
 78     */
 79    protected void setUp()
 80    {
 81       // We register here the demo plugin with xmlBlaster server, supplying an argument to the plugin
 82       String[] args = new String[10];
 83       args[0] = "-bootstrapPort";        // For all protocol we may use set an alternate server port
 84       args[1] = "" + serverPort;
 85       args[2] = "-plugin/socket/port";
 86       args[3] = "" + (serverPort-1);
 87       args[4] = "-plugin/rmi/registryPort";
 88       args[5] = "" + (serverPort-2);
 89       args[6] = "-plugin/xmlrpc/port";
 90       args[7] = "" + (serverPort-3);
 91       args[8] = "-MimeAccessPlugin[Sql92Filter][1.0]";
 92       args[9] = "org.xmlBlaster.engine.mime.sql92.Sql92Filter";
 93       glob.init(args);
 94 
 95       serverThread = EmbeddedXmlBlaster.startXmlBlaster(args);
 96       log.info("XmlBlaster is ready for testing subscribe MIME filter");
 97 
 98       try {
 99          log.info("Connecting ...");
100          con = glob.getXmlBlasterAccess();
101          ConnectQos qos = new ConnectQos(glob, name, passwd);
102          con.connect(qos, null); // Login to xmlBlaster
103       }
104       catch (Exception e) {
105          Thread.dumpStack();
106          log.severe("Can't connect to xmlBlaster: " + e.toString());
107       }
108    }
109 
110    /**
111     * Tears down the fixture.
112     * <p />
113     * cleaning up .... erase() the previous message OID and logout
114     */
115    protected void tearDown()
116    {
117       try { Thread.sleep(200L); } catch( InterruptedException i) {}   // Wait 200 milli seconds, until all updates are processed ...
118 
119       try {
120          EraseReturnQos[] arr = con.erase("<key oid='MSG'/>", null);
121          assertEquals("Erased", 1, arr.length);
122       } catch(XmlBlasterException e) { fail("XmlBlasterException: " + e.getMessage()); }
123 
124       con.disconnect(null);
125       con = null;
126 
127       try { Thread.sleep(500L); } catch( InterruptedException i) {}    // Wait some time
128       EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
129       this.serverThread = null;
130 
131       // reset to default server port (necessary if other tests follow in the same JVM).
132       Util.resetPorts();
133    }
134 
135    /**
136     * First we send a message which matches our regular expression and should be returned to us,
137     * then we send a message with a different content which should be filtered
138     */
139    public void testFilter()
140    {
141       String filter = "posX BETWEEN 0 AND 200 AND posY BETWEEN 0 AND 200";
142       String content = "space-shuttle";
143       log.info("TEST 2: The published message does match the pattern");
144       try {
145          PublishQos pubQos = new PublishQos(this.glob);
146          pubQos.addClientProperty("posX", 50);
147          pubQos.addClientProperty("posY", 150);
148          MsgUnit msgUnit = new MsgUnit(new PublishKey(this.glob, "MSG"), content, pubQos);
149          con.publish(msgUnit);
150       } catch(XmlBlasterException e) {
151          log.warning("XmlBlasterException: " + e.getMessage());
152          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
153       }
154 
155       try {
156          GetQos qos = new GetQos(glob);
157          qos.addAccessFilter(new AccessFilterQos(glob, "Sql92Filter", "1.0", filter));
158 
159          MsgUnit[] msgUnits = con.get("<key oid='MSG'/>", qos.toXml());
160          assertTrue("Expected one returned message", msgUnits != null);
161          assertTrue("Expected exactly one returned message", msgUnits.length==1);
162          assertTrue("Message content in corrupted '" + new String(msgUnits[0].getContent()) + "' versus '" + content + "'",
163                 msgUnits[0].getContent().length == content.length());
164          log.info("Success: Got one message.");
165       } catch(XmlBlasterException e) {
166          log.warning("XmlBlasterException: " + e.getMessage());
167          assertTrue("get - XmlBlasterException: " + e.getMessage(), false);
168       }
169 
170 
171       log.info("TEST 2: The published message does NOT match the pattern");
172       try {
173          PublishQos pubQos = new PublishQos(this.glob);
174          pubQos.addClientProperty("posX", 250);
175          pubQos.addClientProperty("posY", 150);
176          MsgUnit msgUnit = new MsgUnit(new PublishKey(this.glob, "MSG"), content, pubQos);
177          con.publish(msgUnit);
178       } catch(XmlBlasterException e) {
179          log.warning("XmlBlasterException: " + e.getMessage());
180          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
181       }
182 
183       try {
184          GetQos qos = new GetQos(glob);
185          qos.addAccessFilter(new AccessFilterQos(glob, "Sql92Filter", "1.0", filter));
186 
187          MsgUnit[] msgUnits = con.get("<key oid='MSG'/>", qos.toXml());
188          if (msgUnits.length > 0) log.info(msgUnits[0].toXml());
189          assertTrue("Expected zero returned message", msgUnits!=null);
190          assertEquals("Expected zero returned message", 0, msgUnits.length);
191          log.info("Success: Got no message.");
192       } catch(XmlBlasterException e) {
193          log.warning("XmlBlasterException: " + e.getMessage());
194          assertTrue("get - XmlBlasterException: " + e.getMessage(), false);
195       }
196 
197       log.info("Success in testFilter()");
198    }
199 
200    /**
201     * Method is used by TestRunner to load these tests
202     */
203    public static Test suite()
204    {
205        TestSuite suite= new TestSuite();
206        String loginName = "Tim";
207        suite.addTest(new TestGetSql92Filter(new Global(), "testFilter", "sql92"));
208        return suite;
209    }
210 
211    /**
212     * Invoke: 
213     * <pre>
214     *   java org.xmlBlaster.test.mime.TestGetSql92Filter
215     *   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.mime.TestGetSql92Filter
216     * <pre>
217     */
218    public static void main(String args[])
219    {
220       Global glob = new Global();
221       if (glob.init(args) != 0) {
222          System.err.println(ME + ": Init failed");
223          System.exit(1);
224       }
225       TestGetSql92Filter testSub = new TestGetSql92Filter(glob, "TestGetSql92Filter", "sql92");
226       testSub.setUp();
227       testSub.testFilter();
228    }
229 }


syntax highlighted by Code2HTML, v. 0.9.1