1 /*------------------------------------------------------------------------------
2 Name: TestGet.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Testing publish()
6 Version: $Id: TestGet.java 14833 2006-03-06 21:38:58Z laghi $
7 ------------------------------------------------------------------------------*/
8 package org.xmlBlaster.test.qos;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.client.qos.ConnectQos;
14 import org.xmlBlaster.util.XmlBlasterException;
15 import org.xmlBlaster.client.qos.GetReturnQos;
16 import org.xmlBlaster.client.qos.EraseReturnQos;
17 import org.xmlBlaster.client.qos.PublishQos;
18 import org.xmlBlaster.client.I_XmlBlasterAccess;
19 import org.xmlBlaster.util.MsgUnit;
20
21 import junit.framework.*;
22
23
24 /**
25 * This client tests the synchronous method get() with its different qos variants.
26 * <p>
27 * This client may be invoked multiple time on the same xmlBlaster server,
28 * as it cleans up everything after his tests are done.
29 * <p>
30 * Invoke examples:<br />
31 * <pre>
32 * java junit.textui.TestRunner org.xmlBlaster.test.qos.TestGet
33 * java junit.swingui.TestRunner org.xmlBlaster.test.qos.TestGet
34 * </pre>
35 */
36 public class TestGet extends TestCase
37 {
38 private static String ME = "TestGet";
39 private final Global glob;
40 private static Logger log = Logger.getLogger(TestGet.class.getName());
41
42 private String publishOid = "TestGet";
43 private I_XmlBlasterAccess connection;
44 private String loginName;
45 private String senderContent = "A test message";
46
47 /**
48 * Constructs the TestGet object.
49 * <p />
50 * @param testName The name used in the test suite
51 * @param loginName The name to login to the xmlBlaster
52 */
53 public TestGet(Global glob, String testName, String loginName)
54 {
55 super(testName);
56 this.glob = glob;
57
58 this.loginName = loginName;
59 }
60
61
62 /**
63 * Sets up the fixture.
64 * <p />
65 * Connect to xmlBlaster and login
66 */
67 protected void setUp()
68 {
69 try {
70 connection = glob.getXmlBlasterAccess(); // Find orb
71 String passwd = "secret";
72 ConnectQos qos = new ConnectQos(glob, loginName, passwd); // == "<qos></qos>";
73 // Login to xmlBlaster, don't create a callback server
74 connection.connect(qos, null);
75 }
76 catch (Exception e) {
77 log.severe(e.toString());
78 e.printStackTrace();
79 }
80 }
81
82
83 /**
84 * Tears down the fixture.
85 * <p />
86 * cleaning up .... erase() the previous message OID and logout
87 */
88 protected void tearDown()
89 {
90 connection.disconnect(null);
91 // Give the server some millis to finish the iiop handshake ...
92 try { Thread.sleep(200); } catch (InterruptedException e) {}
93 log.info("Success, logged out");
94 }
95
96
97 /**
98 * TEST: Get an not existing and an existing message
99 * <p />
100 * The returned content is checked
101 */
102 public void testGet()
103 {
104 if (log.isLoggable(Level.FINE)) log.fine("1. Get a not existing message ...");
105 try {
106 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'></key>";
107 String qos = "<qos></qos>";
108 MsgUnit[] msgArr = connection.get(xmlKey, qos);
109 if (msgArr.length > 0) {
110 log.severe("Received " + msgArr.length + " unexpected messages");
111 for (int i=0; i<msgArr.length; i++) {
112 log.severe("Wrong message is " + msgArr[i].toXml());
113 }
114 assertTrue("get of not existing message is not possible", false);
115 }
116 else
117 log.info("Success, got zero messages when trying to get unknown message");
118 } catch(XmlBlasterException e) {
119 log.severe("get of not existing message should not throw an exception");
120 //System.exit(1);
121 assertTrue("get of not existing message should not throw an exception", false);
122 //log.info("Success, got XmlBlasterException for trying to get unknown message: " + e.getMessage());
123 }
124
125 if (log.isLoggable(Level.FINE)) log.fine("2. Publish a message ...");
126 try {
127 String xmlKey = "<key oid='" + publishOid + "' contentMime='text/plain'>\n</key>";
128 PublishQos qosWrapper = new PublishQos(glob); // the same as "<qos></qos>"
129 MsgUnit msgUnit = new MsgUnit(xmlKey, senderContent.getBytes(), qosWrapper.toXml());
130 connection.publish(msgUnit);
131 log.info("Success, published a message");
132 } catch(XmlBlasterException e) {
133 assertTrue("publish - XmlBlasterException: " + e.getMessage(), false);
134 }
135
136 if (log.isLoggable(Level.FINE)) log.fine("3. Get an existing message ...");
137 try {
138 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'></key>";
139 String qos = "<qos></qos>";
140 MsgUnit[] msgArr = connection.get(xmlKey, qos);
141
142 assertEquals("Got wrong number of messages", 1, msgArr.length);
143 log.info("Success, got the message '" + msgArr[0].getKey() + "'");
144 if (log.isLoggable(Level.FINEST)) log.finest(msgArr[0].toXml());
145
146 GetReturnQos getQos = new GetReturnQos(glob, msgArr[0].getQos());
147 assertEquals("Sender is corrupted", loginName, getQos.getSender().getLoginName());
148 log.info("Get success from sender " + getQos.getSender());
149
150 assertEquals("Corrupted content", senderContent, new String(msgArr[0].getContent()));
151 } catch(XmlBlasterException e) {
152 log.severe("XmlBlasterException for trying to get a message: " + e.getMessage());
153 assertTrue("Couldn't get() an existing message", false);
154 }
155
156 // TODO: We should use the helpers GetKey and GetQos:
157 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
158 "</key>";
159 String qos = "<qos></qos>";
160 try {
161 EraseReturnQos[] arr = connection.erase(xmlKey, qos);
162 if (arr.length != 1) {
163 log.severe("Erased " + arr.length + " messages:");
164 fail("Message " + publishOid + " was not erased");
165 }
166 log.info("Success, erased a message");
167 } catch(XmlBlasterException e) { log.severe("XmlBlasterException: " + e.getMessage()); }
168 }
169
170
171 /**
172 * LOAD TEST: get 200 times a not existing message
173 */
174 public void testGetMany()
175 {
176 int num = glob.getProperty().get("numTries", 5);
177 log.info("Get " + num + " not existing messages ...");
178 String xmlKey = "<key oid='NotExistingMessage' queryType='EXACT'></key>";
179 String qos = "<qos></qos>";
180 for (int ii=0; ii<num; ii++) {
181 try {
182 MsgUnit[] msgArr = connection.get(xmlKey, qos);
183 if (msgArr.length > 0)
184 assertTrue("get() of not existing message is not possible", false);
185 } catch(XmlBlasterException e) {
186 assertTrue("get() of not existing message should not throw an Exception: " + e.toString(), false);
187 // log.info("Success, got XmlBlasterException for trying to get unknown message: " + e.getMessage());
188 }
189 }
190 log.info("Get " + num + " not existing messages done");
191 }
192
193
194 /**
195 * Method is used by TestRunner to load these tests
196 */
197 public static Test suite()
198 {
199 TestSuite suite= new TestSuite();
200 String loginName = "Tim";
201 Global glob = new Global();
202 suite.addTest(new TestGet(glob, "testGet", loginName));
203 suite.addTest(new TestGet(glob, "testGetMany", loginName));
204 return suite;
205 }
206
207
208 /**
209 * Invoke: java org.xmlBlaster.test.qos.TestGet
210 * @deprecated Use the TestRunner from the testsuite to run it:<p />
211 * <pre> java -Djava.compiler= junit.textui.TestRunner org.xmlBlaster.test.qos.TestGet</pre>
212 */
213 public static void main(String args[])
214 {
215 Global glob = new Global();
216 if (glob.init(args) != 0) {
217 System.out.println(ME + " Init failed");
218 System.exit(1);
219 }
220 TestGet testSub = new TestGet(glob, "TestGet", "Tim");
221 testSub.setUp();
222 testSub.testGet();
223 testSub.testGetMany();
224 testSub.tearDown();
225 }
226 }
syntax highlighted by Code2HTML, v. 0.9.1