1 /*------------------------------------------------------------------------------
2 Name: TestSubOneway.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Demo code for a client using xmlBlaster
6 ------------------------------------------------------------------------------*/
7 package org.xmlBlaster.test.qos;
8
9 import java.util.logging.Logger;
10 import java.util.logging.Level;
11
12 import org.xmlBlaster.test.Msg;
13 import org.xmlBlaster.test.MsgInterceptor;
14 import org.xmlBlaster.util.Global;
15 import org.xmlBlaster.client.qos.ConnectQos;
16 import org.xmlBlaster.util.XmlBlasterException;
17 import org.xmlBlaster.client.I_XmlBlasterAccess;
18 import org.xmlBlaster.client.key.PublishKey;
19 import org.xmlBlaster.client.key.SubscribeKey;
20 import org.xmlBlaster.client.key.UnSubscribeKey;
21 import org.xmlBlaster.client.qos.PublishQos;
22 import org.xmlBlaster.client.qos.SubscribeQos;
23 import org.xmlBlaster.client.qos.EraseReturnQos;
24 import org.xmlBlaster.client.qos.UnSubscribeQos;
25 import org.xmlBlaster.util.MsgUnit;
26 import org.xmlBlaster.util.def.Constants;
27
28 import junit.framework.*;
29
30
31 /**
32 * This client tests the oneway callback.
33 * <br />
34 * This client may be invoked multiple time on the same xmlBlaster server,
35 * as it cleans up everything after his tests are done.
36 * <p>
37 * Invoke examples:<br />
38 * <pre>
39 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestSubOneway
40 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestSubOneway
41 * </pre>
42 */
43 public class TestSubOneway extends TestCase
44 {
45 private static String ME = "TestSubOneway";
46 private final Global glob;
47 private static Logger log = Logger.getLogger(TestSubOneway.class.getName());
48
49 private String oidExact = "OnewayMessage";
50 private I_XmlBlasterAccess senderConnection;
51 private String senderName;
52
53 private MsgInterceptor msgInterceptor;
54
55 /**
56 * Constructs the TestSubOneway object.
57 * <p />
58 * @param testName The name used in the test suite
59 * @param loginName The name to login to the xmlBlaster
60 */
61 public TestSubOneway(Global glob, String testName, String loginName) {
62 super(testName);
63 this.glob = glob;
64
65 this.senderName = loginName;
66 }
67
68
69 /**
70 * Connect to xmlBlaster and login
71 */
72 protected void setUp() {
73 try {
74 this.msgInterceptor = new MsgInterceptor(glob, log, null);
75 this.senderConnection = glob.getXmlBlasterAccess();
76 String passwd = "secret";
77 ConnectQos qos = new ConnectQos(glob, senderName, passwd);
78 this.senderConnection.connect(qos, this.msgInterceptor);
79 }
80 catch (Exception e) {
81 log.severe("Login failed: " + e.toString());
82 fail("Login failed: " + e.toString());
83 }
84 }
85
86
87 /**
88 * cleaning up .... erase() the previous message OID and logout
89 */
90 protected void tearDown() {
91 try {
92 UnSubscribeKey sk = new UnSubscribeKey(glob, this.oidExact);
93 UnSubscribeQos sq = new UnSubscribeQos(glob);
94 this.senderConnection.unSubscribe(sk, sq);
95
96 EraseReturnQos[] arr = this.senderConnection.erase("<key oid='" + this.oidExact + "'/>", "<qos/>");
97 assertEquals("Erase", 1, arr.length);
98 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
99
100 this.senderConnection.disconnect(null);
101 }
102
103
104 /**
105 * Subscribe oneway.
106 */
107 public void testOneway() throws Exception {
108 if (log.isLoggable(Level.FINE)) log.fine("Subscribing using EXACT syntax ...");
109
110 SubscribeKey sk = new SubscribeKey(glob, this.oidExact);
111 SubscribeQos sq = new SubscribeQos(glob);
112 sq.setWantUpdateOneway(true);
113 this.senderConnection.subscribe(sk, sq);
114 log.info("Success: Subscribe done");
115
116 PublishKey pk = new PublishKey(glob, this.oidExact);
117 PublishQos pq = new PublishQos(glob);
118 MsgUnit msgUnit = new MsgUnit(pk, "Hello oneway", pq);
119 this.senderConnection.publish(msgUnit);
120
121 this.msgInterceptor.waitOnUpdate(2000L, this.oidExact, Constants.STATE_OK, 1);
122 Msg[] msgs = this.msgInterceptor.getMsgs();
123 assertEquals(1, msgs.length);
124 }
125
126 /**
127 * Method is used by TestRunner to load these tests
128 */
129 public static Test suite()
130 {
131 TestSuite suite= new TestSuite();
132 suite.addTest(new TestSubOneway(new Global(), "testOneway", "joe"));
133 return suite;
134 }
135
136
137 /**
138 * Invoke: java org.xmlBlaster.test.qos.TestSubOneway
139 */
140 public static void main(String args[]) throws Exception
141 {
142 Global glob = new Global();
143 if (glob.init(args) != 0) {
144 System.err.println(ME + ": Init failed");
145 System.exit(1);
146 }
147 TestSubOneway testSub = new TestSubOneway(glob, "TestSubOneway", "joe");
148 testSub.setUp();
149 testSub.testOneway();
150 testSub.tearDown();
151 }
152 }
syntax highlighted by Code2HTML, v. 0.9.1