1 /*
2 * Copyright (c) 2003 Peter Antman, Teknik i Media <peter.antman@tim.se>
3 *
4 * $Id: TestJ2eeServices.java 14833 2006-03-06 21:38:58Z laghi $
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package org.xmlBlaster.test.j2ee;
21 import java.util.logging.Logger;
22 import java.util.logging.Level;
23 import org.xmlBlaster.util.Global;
24 import org.xmlBlaster.util.XmlBlasterException;
25 import org.xmlBlaster.client.qos.ConnectQos;
26 import org.xmlBlaster.client.qos.DisconnectQos;
27 import org.xmlBlaster.client.I_XmlBlasterAccess;
28 import org.xmlBlaster.client.I_Callback;
29 import org.xmlBlaster.client.key.UpdateKey;
30 import org.xmlBlaster.client.qos.UpdateQos;
31 import org.xmlBlaster.client.qos.EraseReturnQos;
32 import org.xmlBlaster.client.qos.SubscribeQos;
33 import org.xmlBlaster.protocol.corba.serverIdl.Server;
34 import org.xmlBlaster.util.MsgUnit;
35 import org.xmlBlaster.util.EmbeddedXmlBlaster;
36 import org.xmlBlaster.j2ee.util.GlobalUtil;
37 import org.xmlBlaster.test.Util;
38
39 import org.xmlBlaster.j2ee.jmx.XmlBlasterService;
40 import org.xmlBlaster.j2ee.k2.BlasterManagedConnectionFactory;
41 import org.xmlBlaster.j2ee.k2.client.BlasterConnectionFactory;
42 import org.xmlBlaster.j2ee.k2.client.BlasterConnection;
43
44 import junit.framework.*;
45
46 import java.util.HashMap;
47 import javax.naming.Context;
48 /**
49 * Test the j2ee services in combination.
50 *
51 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
52 * @version $Revision: 1.2 $
53 */
54
55 public class TestJ2eeServices extends TestCase implements I_Callback {
56 private static String ME = "TestJ2eeServices";
57 private final Global glob;
58 private static Logger log = Logger.getLogger(TestJ2eeServices.class.getName());
59
60 private I_XmlBlasterAccess con = null;
61 private String propertyFileName = "org/xmlBlaster/test/j2ee/blasterServer.properties";
62 private String clientPropertyFileName = "org/xmlBlaster/test/j2ee/blasterClient.properties";
63 private XmlBlasterService server;
64 private int serverPort = 8624;
65 private BlasterManagedConnectionFactory mcf;
66 private BlasterConnectionFactory factory;
67 private HashMap subscriberTable = new HashMap();
68 private int[] subRec = new int[2];
69 private String subscribeOid;
70 private String subscribeOid2;
71 private String name ="testsub";
72 private String passwd = "secret";
73 private int numReceived = 0; // error checking
74
75 public TestJ2eeServices(){
76 this(null, "TestJ2eeServices");
77 }
78
79 public TestJ2eeServices(Global glob, String testName){
80 super(testName);
81 this.glob = (glob == null) ? Global.instance() : glob;
82
83
84 }
85
86 protected void setUpServer() throws Exception {
87 System.setProperty(Context.INITIAL_CONTEXT_FACTORY ,"org.xmlBlaster.test.j2ee.MemContext");
88 server = new XmlBlasterService();
89 server.setPropertyFileName(propertyFileName);
90 server.setJNDIName("xmlBlaster/globalUtil");
91 server.setPort(""+serverPort);
92 server.start();
93 }
94
95 /**
96 * we just skip anny pooling and goes directly?
97 */
98 protected void setUpK2() throws Exception {
99 mcf = new BlasterManagedConnectionFactory();
100 mcf.setUserName("test");
101 mcf.setPassword("test");
102 mcf.setClientProtocol("LOCAL");
103 mcf.setIorPort(""+serverPort);
104 mcf.setPropertyFileName(clientPropertyFileName);
105 mcf.setJNDIName("xmlBlaster/globalUtil");
106
107 factory = (BlasterConnectionFactory)mcf.createConnectionFactory();
108 }
109
110 protected void setUp() throws Exception {
111 setUpServer();
112 setUpK2();
113
114 String[] args = {
115 "-bootstrapPort", // For all protocol we may use set an alternate server port
116 "" + serverPort
117 };
118 glob.init(args);
119 // Set up a subscriber
120 try {
121 log.info("Connecting ...");
122 con = glob.getXmlBlasterAccess();
123 ConnectQos qos = new ConnectQos(glob, name, passwd);
124 con.connect(qos, this); // Login to xmlBlaster
125
126 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
127 "//TestLocalProtocol-AGENT" +
128 "</key>";
129 String sqos = "<qos><notify>false</notify></qos>"; // send no erase events
130
131 subscribeOid = con.subscribe(xmlKey, sqos).getSubscriptionId() ;
132 log.info("Success: Subscribe on subscriptionId=" + subscribeOid + " done");
133 assertTrue("returned null subscriptionId", subscribeOid != null);
134
135 subscriberTable.put(subscribeOid, new Integer(0));
136
137
138 } catch(XmlBlasterException e) {
139 log.warning("XmlBlasterException: " + e.getMessage());
140 assertTrue("subscribe - XmlBlasterException: " + e.getMessage(), false);
141 }
142 }
143 protected void tearDown() throws Exception
144 {
145 log.info("TEST: tearing down");
146
147 // FIXME: how do we destroy the managed connections?
148
149
150 try { Thread.sleep(200L); } catch( InterruptedException i) {} // Wait 200 milli seconds, until all updates are processed ...
151
152 try {
153 con.unSubscribe("<key oid='"+subscribeOid+"'/>",
154 "<qos/>");
155 EraseReturnQos[] arr = con.erase("<key oid='' queryType='XPATH'>\n" +
156 " /xmlBlaster/key/TestLocalProtocol-AGENT" +
157 "</key>", "<qos/>");
158 assertEquals("Erase", 5, arr.length);
159 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
160
161 con.disconnect(null);
162 con=null;
163
164 try { Thread.sleep(500L); } catch( InterruptedException i) {} // Wait some time
165 server.stop();
166
167
168 }
169
170 public void testPublish()throws Exception {
171
172
173
174
175 // Publish 5 messages
176 // 5 for first sub
177 // 1 for second sub
178 BlasterConnection conn = null;
179 for ( int i = 0; i<5;i++) {
180 try {
181 conn = factory.getConnection();
182 String c = "<content>"+i+"</content>";
183 String k = "<key oid='"+i+"' contentMime='text/xml'><TestLocalProtocol-AGENT id='"+i+"' type='generic'/></key>";
184 log.info("Key: " +k);
185 conn.publish(new MsgUnit(k, c.getBytes(), null));
186 } catch(XmlBlasterException e) {
187 log.warning("XmlBlasterException: " + e.getMessage());
188 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
189 }finally {
190
191 if ( conn != null) {
192 conn.close();
193 } // end of if ()
194
195 } // end of finally
196
197 }
198
199 waitOnUpdate(subscribeOid,10000L, 5);
200 }
201 /**
202 * This is the callback method invoked from xmlBlaster
203 * delivering us a new asynchronous message.
204 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
205 */
206 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
207 {
208 log.info("Receiving update of a message " + updateKey.getOid() + " for subId: " + updateQos.getSubscriptionId() );
209 int ii = ((Integer)subscriberTable.get(updateQos.getSubscriptionId())).intValue();
210 log.fine("Got message " + new String(content));
211 subRec[ii]++;
212 numReceived++;
213 return "";
214 }
215
216 /**
217 * Little helper, waits until the wanted number of messages are arrived
218 * or returns when the given timeout occurs.
219 * <p />
220 * @param timeout in milliseconds
221 * @param numWait how many messages to wait
222 */
223 private void waitOnUpdate(String subId,final long timeout, final int numWait)
224 {
225 long pollingInterval = 50L; // check every 0.05 seconds
226 if (timeout < 50) pollingInterval = timeout / 10L;
227 long sum = 0L;
228 int ii = ((Integer)subscriberTable.get(subId)).intValue();
229 // check if too few are arriving
230 while (subRec[ii] < numWait) {
231 try { Thread.sleep(pollingInterval); } catch( InterruptedException i) {}
232 sum += pollingInterval;
233 assertTrue("Timeout of " + timeout + " occurred without update", sum <= timeout);
234 }
235
236 // check if too many are arriving
237 try { Thread.sleep(timeout); } catch( InterruptedException i) {}
238 assertEquals("Wrong number of messages arrived", numWait, subRec[ii]);
239 log.info("Found correct rec messages for: " + subId);
240 subRec[ii]= 0;
241 }
242
243 /**
244 * Method is used by TestRunner to load these tests
245 */
246 public static Test suite()
247 {
248 TestSuite suite= new TestSuite();
249 String loginName = "Tim";
250 suite.addTest(new TestJ2eeServices(new Global(), "testPublish"));
251 return suite;
252 }
253
254 /**
255 * Invoke:
256 * <pre>
257 * java org.xmlBlaster.test.mime.TestXPathSubscribeFilter
258 * java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.mime.TestXPathSubscribeFilter
259 * <pre>
260 */
261 public static void main(String args[])
262 {
263 Global glob = new Global();
264 if (glob.init(args) != 0) {
265 System.err.println(ME + ": Init failed");
266 System.exit(1);
267 }
268 TestJ2eeServices testSub = new TestJ2eeServices(glob, "TestLocalProtocol"); try {
269 testSub.setUp();
270
271 testSub.testPublish();
272
273
274 testSub.tearDown();
275 } catch (Throwable e) {
276 e.printStackTrace();
277 } // end of try-catch
278 }
279 }// TestJ2eeServices
syntax highlighted by Code2HTML, v. 0.9.1