1 /*------------------------------------------------------------------------------
2 Name: TestPersistence.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Testing persistent messages
6 Version: $Id: TestPersistence.java 14846 2006-03-07 17:14:22Z ruff $
7 ------------------------------------------------------------------------------*/
8 package org.xmlBlaster.test.persistence;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12
13 import org.xmlBlaster.util.FileLocator;
14 import org.xmlBlaster.util.Global;
15
16 import org.xmlBlaster.client.I_XmlBlasterAccess;
17 import org.xmlBlaster.client.qos.ConnectQos;
18 import org.xmlBlaster.client.I_Callback;
19 import org.xmlBlaster.client.key.UpdateKey;
20 import org.xmlBlaster.client.qos.UpdateQos;
21 import org.xmlBlaster.client.qos.EraseReturnQos;
22 import org.xmlBlaster.util.XmlBlasterException;
23 import org.xmlBlaster.util.MsgUnit;
24
25 import junit.framework.*;
26
27
28 /**
29 * This client tests the persistence driver, the $lt;persistent/> flag.
30 * <p>
31 * Invoke examples:<br />
32 * <pre>
33 * java junit.textui.TestRunner org.xmlBlaster.test.persistence.TestPersistence
34 *
35 * java junit.swingui.TestRunner -noloading org.xmlBlaster.test.persistence.TestPersistence
36 * </pre>
37 */
38 public class TestPersistence extends TestCase implements I_Callback
39 {
40 private final static String ME = "TestPersistence";
41 private Global glob = null;
42 private static Logger log = Logger.getLogger(TestPersistence.class.getName());
43
44 private final String senderName = "Gesa";
45 private String publishOid = "HelloPersistent";
46 private I_XmlBlasterAccess senderConnection = null;
47 private String senderContent = "Some persistent content";
48
49 private int numReceived = 0;
50
51
52 /**
53 * Constructs the TestPersistence object.
54 * <p />
55 * @param testName The name used in the test suite
56 * @param loginName The name to login to the xmlBlaster
57 */
58 public TestPersistence(String testName)
59 {
60 super(testName);
61 }
62
63
64 /**
65 * Sets up the fixture.
66 * <p />
67 * Creates a CORBA connection and does a login.<br />
68 * - One connection for the sender client<br />
69 */
70 protected void setUp()
71 {
72 if (this.glob == null) this.glob = new Global();
73
74 try {
75 String passwd = "secret";
76 senderConnection = glob.getXmlBlasterAccess();
77 ConnectQos qos = new ConnectQos(glob, senderName, passwd); // == "<qos></qos>";
78 senderConnection.connect(qos, this);
79 }
80 catch (Exception e) {
81 log.severe(e.toString());
82 e.printStackTrace();
83 }
84 }
85
86
87 /**
88 * Tears down the fixture.
89 * <p />
90 * cleaning up .... logout
91 */
92 protected void tearDown()
93 {
94 try { Thread.sleep(200L); } catch( InterruptedException i) {} // Wait 200 milli seconds, until all updates are processed ...
95
96 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'>\n</key>";
97 String qos = "<qos></qos>";
98 try {
99 EraseReturnQos[] arr = senderConnection.erase(xmlKey, qos);
100 assertEquals("Erase", 1, arr.length);
101 } catch(XmlBlasterException e) { fail("Erase XmlBlasterException: " + e.getMessage()); }
102 checkContent(false);
103
104 senderConnection.disconnect(null);
105 }
106
107
108 /**
109 * Publish a persistent message.
110 * <p />
111 */
112 public void sendPersistent()
113 {
114 if (log.isLoggable(Level.FINE)) log.fine("Testing a persistent message ...");
115
116 String xmlKey = "<key oid='" + publishOid + "' contentMime='text/plain' contentMimeExtended='2.0' domain='RUGBY'>\n" +
117 "</key>";
118
119 String qos = "<qos>" +
120 " <persistent />" +
121 "</qos>";
122
123 try {
124 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qos);
125 String returnedOid = senderConnection.publish(msgUnit).getKeyOid();
126 assertEquals("Retunred oid is invalid", publishOid, returnedOid);
127 log.info("Sending of '" + senderContent + "' done, returned oid=" + publishOid);
128 } catch(XmlBlasterException e) {
129 log.severe("publish() XmlBlasterException: " + e.getMessage());
130 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
131 }
132
133 waitOnUpdate(1000L, 0);
134 assertEquals("numReceived after sending", 0, numReceived); // no message arrived?
135 numReceived = 0;
136 }
137
138
139 /**
140 * TEST: Publish a persistent message.
141 * <p />
142 */
143 public void testPersistent()
144 {
145 sendPersistent();
146 checkContent(true);
147 senderContent = senderContent + " again";
148 sendPersistent();
149 checkContent(true);
150 }
151
152
153 /**
154 * If the FileDriver is used, check if the correct content is written.
155 */
156 void checkContent(boolean checkContent)
157 {
158 String driverType = glob.getProperty().get("Persistence.Driver.Type", (String)null);
159 if (driverType == null || !driverType.equals("filestore")) {
160 log.info("Sorry, can't check persistence store, only checks for FileDriver is implemented");
161 return;
162 }
163
164 String path = glob.getProperty().get("Persistence.Path", (String)null);
165 if (path == null) {
166 log.info("Sorry, xmlBlaster is running memory based only, no checks possible");
167 return;
168 }
169
170 if (checkContent) {
171 try {
172 String persistenceContent = FileLocator.readAsciiFile(path, publishOid);
173 assertEquals("Written content is corrupted", senderContent, persistenceContent);
174 }
175 catch (Exception e) {
176 assertTrue("Couldn't read file " + FileLocator.concatPath(path, publishOid), false);
177 }
178 }
179 else { // Check if erased
180 java.io.File f = new java.io.File(path, publishOid);
181 if (f.exists())
182 assertTrue("File " + FileLocator.concatPath(path, publishOid) + " is not erased properly", false);
183 }
184 }
185
186 /**
187 * This is the callback method invoked from xmlBlaster
188 * delivering us a new asynchronous message.
189 * @see org.xmlBlaster.client.I_Callback#update(String, UpdateKey, byte[], UpdateQos)
190 */
191 public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
192 {
193 log.info("Receiving update of a message, checking ...");
194
195 numReceived += 1;
196
197 assertEquals("Wrong sender", senderName, updateQos.getSender().getLoginName());
198 assertEquals("Wrong oid of message returned", publishOid, updateKey.getOid());
199 assertEquals("Wrong mime of message returned", "text/plain", updateKey.getContentMime());
200 assertEquals("Wrong extended mime of message returned", "2.0", updateKey.getContentMimeExtended());
201 assertEquals("Wrong domain of message returned", "RUGBY", updateKey.getDomain());
202 assertEquals("Message content is corrupted", new String(senderContent), new String(content));
203 return "";
204 }
205
206 /**
207 * Little helper, waits until the wanted number of messages are arrived
208 * or returns when the given timeout occurs.
209 * <p />
210 * @param timeout in milliseconds
211 * @param numWait how many messages to wait
212 */
213 private void waitOnUpdate(final long timeout, final int numWait)
214 {
215 long pollingInterval = 50L; // check every 0.05 seconds
216 if (timeout < 50) pollingInterval = timeout / 10L;
217 long sum = 0L;
218 while (numReceived < numWait) {
219 try {
220 Thread.sleep(pollingInterval);
221 }
222 catch( InterruptedException i)
223 {}
224 sum += pollingInterval;
225 if (sum > timeout) {
226 log.warning("Timeout of " + timeout + " occurred");
227 break;
228 }
229 }
230 }
231
232
233 /**
234 * Method is used by TestRunner to load these tests
235 */
236 public static Test suite()
237 {
238 TestSuite suite= new TestSuite();
239 suite.addTest(new TestPersistence("testPersistent"));
240 return suite;
241 }
242
243
244 /**
245 * Invoke: java org.xmlBlaster.test.persistence.TestPersistence
246 * @deprecated Use the TestRunner from the testsuite to run it:<p />
247 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.persistence.TestPersistence</pre>
248 */
249 public static void main(String args[])
250 {
251 Global glob = new Global();
252 if (glob.init(args) != 0) {
253 System.err.println(ME + ": Init failed");
254 System.exit(1);
255 }
256 TestPersistence testSub = new TestPersistence("TestPersistence");
257 testSub.setUp();
258 testSub.testPersistent();
259 testSub.tearDown();
260 }
261 }
syntax highlighted by Code2HTML, v. 0.9.1