1 /*------------------------------------------------------------------------------
  2 Name:      TestPublishFilter.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.test.mime;
  7 
  8 import java.util.logging.Logger;
  9 import java.util.logging.Level;
 10 import org.xmlBlaster.util.Global;
 11 import org.xmlBlaster.util.SessionName;
 12 import org.xmlBlaster.util.XmlBlasterException;
 13 import org.xmlBlaster.client.qos.ConnectQos;
 14 import org.xmlBlaster.client.qos.DisconnectQos;
 15 import org.xmlBlaster.client.I_XmlBlasterAccess;
 16 import org.xmlBlaster.client.qos.PublishQos;
 17 import org.xmlBlaster.client.qos.PublishReturnQos;
 18 import org.xmlBlaster.client.qos.EraseReturnQos;
 19 import org.xmlBlaster.client.key.UpdateKey;
 20 import org.xmlBlaster.client.qos.UpdateQos;
 21 import org.xmlBlaster.client.I_Callback;
 22 import org.xmlBlaster.util.MsgUnit;
 23 import org.xmlBlaster.util.def.Constants;
 24 import org.xmlBlaster.util.qos.address.Destination;
 25 import org.xmlBlaster.util.EmbeddedXmlBlaster;
 26 import org.xmlBlaster.test.Util;
 27 
 28 import junit.framework.*;
 29 
 30 
 31 /**
 32  * This client tests the <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/mime.plugin.publishfilter.html">
 33  * publish filter plugin framework</a> on publish() invocations. 
 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.TestPublishFilter
 42  *    java junit.swingui.TestRunner -noloading org.xmlBlaster.test.mime.TestPublishFilter
 43  * </pre>
 44  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/mime.plugin.publishfilter.html">The mime.plugin.publishfilter requirement</a>
 45  */
 46 public class TestPublishFilter extends TestCase
 47 {
 48    private static String ME = "TestPublishFilter";
 49    private final Global glob;
 50    private static Logger log = Logger.getLogger(TestPublishFilter.class.getName());
 51 
 52    private I_XmlBlasterAccess con = null;
 53    private String name;
 54    private String passwd = "secret";
 55    private EmbeddedXmlBlaster serverThread;
 56    private int serverPort = 7619;
 57    private int filterMessageContentBiggerAs = 10;
 58    private int numUpdated = 0;
 59 
 60    /**
 61     * Constructs the TestPublishFilter object.
 62     * <p />
 63     * @param testName   The name used in the test suite
 64     * @param name       The name to login to the xmlBlaster
 65     */
 66    public TestPublishFilter(Global glob, String testName, String name)
 67    {
 68       super(testName);
 69       this.glob = glob;
 70 
 71       this.name = name;
 72    }
 73 
 74    /**
 75     * Sets up the fixture.
 76     * <p />
 77     * We start an own xmlBlaster server in a separate thread,
 78     * it is configured to load our simple demo MIME filter plugin.
 79     * <p />
 80     * Then we connect as a client
 81     */
 82    protected void setUp()
 83    {
 84       // We register here the demo plugin with xmlBlaster server, supplying an argument to the plugin
 85       String[] args = new String[10];
 86       args[0] = "-bootstrapPort";        // For all protocol we may use set an alternate server port
 87       args[1] = "" + serverPort;
 88       args[2] = "-plugin/socket/port";
 89       args[3] = "" + (serverPort-1);
 90       args[4] = "-plugin/rmi/registryPort";
 91       args[5] = "" + (serverPort-2);
 92       args[6] = "-plugin/xmlrpc/port";
 93       args[7] = "" + (serverPort-3);
 94       args[8] = "-MimePublishPlugin[PublishLenChecker][1.0]";
 95       args[9] = "org.xmlBlaster.engine.mime.demo.PublishLenChecker,DEFAULT_MAX_LEN=10,THROW_EXCEPTION_FOR_LEN=3";
 96       glob.init(args);
 97 
 98       serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 99       log.info("XmlBlaster is ready for testing publish MIME filter");
100 
101       try {
102          log.info("Connecting ...");
103          con = glob.getXmlBlasterAccess();
104          ConnectQos qos = new ConnectQos(glob, name, passwd);
105          con.connect(qos, new I_Callback() {
106                public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) {
107                   log.info("Reveiving asynchronous message '" + updateKey.getOid() + "' in default handler");
108                   numUpdated++;
109                   return "";
110                }
111             });  // Login to xmlBlaster, default handler for updates
112       }
113       catch (Exception e) {
114          Thread.currentThread().dumpStack();
115          log.severe("Can't connect to xmlBlaster: " + e.toString());
116       }
117    }
118 
119    /**
120     * Tears down the fixture.
121     * <p />
122     * cleaning up .... erase() the previous message OID and logout
123     */
124    protected void tearDown()
125    {
126       try { Thread.sleep(200L); } catch( InterruptedException i) {} // Wait some time
127 
128       con.disconnect(null);
129       con=null;
130 
131       try { Thread.sleep(500L); } catch( InterruptedException i) {} // Wait some time
132       EmbeddedXmlBlaster.stopXmlBlaster(this.serverThread);
133       this.serverThread = null;
134 
135       // reset to default server port (necessary if other tests follow in the same JVM).
136       Util.resetPorts();
137    }
138 
139    /**
140     * First we send a message <= 10 content length which should be accepted by xmlBlaster
141     * and updated to us,
142     * then we send a message with 11 bytes in the content which should be filtered. 
143     * <p />
144     * The test is done in Publish/Subscribe mode
145     */
146    public void testFilter() {
147       log.info("testFilter() with filterMessageContentBiggerAs=" + filterMessageContentBiggerAs + " ...");
148 
149       log.info("TEST 1: Testing filtered message");
150       String content = "12345678901"; // content is too long, our plugin denies this message
151       try {
152          PublishReturnQos rq = con.publish(new MsgUnit("<key oid='MSG'/>", content.getBytes(), null));
153          log.info("TEST 1: SUCCESS returned state=" + rq.getState());
154          assertTrue("Return OK", !Constants.STATE_OK.equals(rq.getState()));
155       } catch(XmlBlasterException e) {
156          log.warning("XmlBlasterException: " + e.getMessage());
157          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
158       }
159       try {
160          MsgUnit[] msgUnits = con.get("<key oid='MSG'/>", null);
161          assertTrue("Invalid return", msgUnits!=null);
162          assertEquals("Expected no returned message", 0, msgUnits.length);
163       } catch(XmlBlasterException e) {
164          log.warning("get - XmlBlasterException: " + e.getMessage());
165          fail("get - XmlBlasterException: " + e.getMessage());
166       }
167 
168 
169       log.info("TEST 2: Testing unfiltered message");
170       content = "1234567890";
171       try {
172          PublishReturnQos rq = con.publish(new MsgUnit("<key oid='MSG'/>", content.getBytes(), null));
173          assertEquals("Return not OK", Constants.STATE_OK, rq.getState());
174          log.info("TEST 2: SUCCESS");
175       } catch(XmlBlasterException e) {
176          log.warning("XmlBlasterException: " + e.getMessage());
177          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
178       }
179       try {
180          MsgUnit[] msgUnits = con.get("<key oid='MSG'/>", null);
181          assertTrue("Expected one returned message", msgUnits!=null);
182          assertTrue("Expected exactly one returned message", msgUnits.length==1);
183          assertTrue("Message content in corrupted '" + new String(msgUnits[0].getContent()) + "' versus '" + content + "'",
184                 msgUnits[0].getContent().length == content.length());
185          log.info("Success: Got one message.");
186       } catch(XmlBlasterException e) {
187          log.warning("get - XmlBlasterException: " + e.getMessage());
188          fail("get - XmlBlasterException: " + e.getMessage());
189       }
190 
191       log.info("TEST 3: Test what happens if the plugin throws an exception");
192       try {   // see THROW_EXCEPTION_FOR_LEN=3
193          con.publish(new MsgUnit("<key oid='MSG'/>", "123".getBytes(), null));
194          fail("publish - expected an XmlBlasterException");
195       } catch(XmlBlasterException e) {
196          log.warning("TEST 3: SUCCESS XmlBlasterException: " + e.getMessage());
197       }
198 
199       try {
200          EraseReturnQos[] arr = con.erase("<key oid='MSG'/>", null);
201          assertEquals("Erased problem", 1, arr.length);
202       } catch(XmlBlasterException e) { fail(ME + " XmlBlasterException: " + e.getMessage()); }
203 
204       log.info("Success in testFilter()");
205    }
206 
207    /**
208     * First we send a message <= 10 content length which should be accepted by xmlBlaster
209     * and updated to us,
210     * then we send a message with 11 bytes in the content which should be filtered. 
211     * <p />
212     * The test is done in Point To Point mode
213     */
214    public void testPtPFilter() {
215       log.info("testPtPFilter() with filterMessageContentBiggerAs=" + filterMessageContentBiggerAs + " ...");
216 
217       numUpdated = 0;
218 
219       log.info("TEST 1: Testing filtered PtP message");
220       String content = "12345678901"; // content is too long, our plugin denies this message
221       try {
222          PublishQos pq = new PublishQos(glob);
223          pq.addDestination(new Destination(new SessionName(glob, name)));
224          PublishReturnQos rq = con.publish(new MsgUnit("<key oid='MSG'/>", content.getBytes(), pq.toXml()));
225          log.info("TEST 1: SUCCESS returned state=" + rq.getState());
226          assertTrue("Return OK", !Constants.STATE_OK.equals(rq.getState()));
227       } catch(XmlBlasterException e) {
228          log.warning("XmlBlasterException: " + e.getMessage());
229          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
230       }
231       try {
232          MsgUnit[] msgUnits = con.get("<key oid='MSG'/>", null);
233          assertTrue("Invalid return", msgUnits!=null);
234          assertEquals("Expected no returned message", 0, msgUnits.length);
235       } catch(XmlBlasterException e) {
236          log.warning("get - XmlBlasterException: " + e.getMessage());
237          fail("get - XmlBlasterException: " + e.getMessage());
238       }
239 
240       log.info("TEST 2: Testing unfiltered PtP message");
241       content = "1234567890";
242       try {
243          PublishQos pq = new PublishQos(glob);
244          pq.addDestination(new Destination(new SessionName(glob, name)));
245          PublishReturnQos rq = con.publish(new MsgUnit("<key oid='MSG'/>", content.getBytes(), pq.toXml()));
246          assertEquals("Return not OK", Constants.STATE_OK, rq.getState());
247          log.info("TEST 2: SUCCESS");
248       } catch(XmlBlasterException e) {
249          log.warning("XmlBlasterException: " + e.getMessage());
250          assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
251       }
252 
253       log.info("TEST 3: Test what happens if the plugin throws an exception");
254       try {   // see THROW_EXCEPTION_FOR_LEN=3
255          PublishQos pq = new PublishQos(glob);
256          pq.addDestination(new Destination(new SessionName(glob, name)));
257          con.publish(new MsgUnit("<key oid='MSG'/>", "123".getBytes(), pq.toXml()));
258          fail("publish - expected an XmlBlasterException");
259       } catch(XmlBlasterException e) {
260          log.warning("TEST 3: SUCCESS XmlBlasterException: " + e.getMessage());
261       }
262 
263       try { Thread.sleep(1000); } catch( InterruptedException i) {} // Wait some time
264       assertEquals("PtP updates is not one", 1, numUpdated);
265 
266       log.info("Success in testPtPFilter()");
267    }
268 
269    /**
270     * Method is used by TestRunner to load these tests
271     */
272    public static Test suite()
273    {
274        TestSuite suite= new TestSuite();
275        String loginName = "Tim";
276        suite.addTest(new TestPublishFilter(new Global(), "testFilter", "Tim"));
277        suite.addTest(new TestPublishFilter(new Global(), "testPtPFilter", "Tim"));
278        return suite;
279    }
280 
281    /**
282     * Invoke: 
283     * <pre>
284     *   java org.xmlBlaster.test.mime.TestPublishFilter
285     *   java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.mime.TestPublishFilter
286     * <pre>
287     */
288    public static void main(String args[])
289    {
290       Global glob = new Global();
291       if (glob.init(args) != 0) {
292          System.exit(0);
293       }
294       TestPublishFilter testSub = new TestPublishFilter(glob, "TestPublishFilter", "Tim");
295       testSub.setUp();
296       testSub.testFilter();
297       testSub.testPtPFilter();
298    }
299 }


syntax highlighted by Code2HTML, v. 0.9.1