1 package org.xmlBlaster.test.classtest;
2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.ByteArrayOutputStream;
6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.io.InputStreamReader;
9 import java.io.OutputStream;
10 import java.io.PrintStream;
11 import java.net.InetAddress;
12 import java.net.ServerSocket;
13 import java.net.Socket;
14 import java.util.Hashtable;
15 import java.util.Vector;
16
17 import java.util.logging.Logger;
18 import java.util.logging.Level;
19 import org.xmlBlaster.client.protocol.http.common.BufferedInputStreamMicro;
20 import org.xmlBlaster.client.protocol.http.common.Msg;
21 import org.xmlBlaster.client.protocol.http.common.MsgHolder;
22 import org.xmlBlaster.client.protocol.http.common.ObjectInputStreamMicro;
23 import org.xmlBlaster.client.protocol.http.common.ObjectOutputStreamMicro;
24 import org.xmlBlaster.util.Global;
25
26 import junit.framework.*;
27
28 /**
29 * Test ConnectQos.
30 * <p />
31 * All methods starting with 'test' and without arguments are invoked automatically
32 * <p />
33 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.MicroEditionTest
34 */
35 public class MicroEditionTest extends TestCase {
36
37 public class Sender extends Thread {
38
39 private int port;
40 private byte[] content;
41 private long delay;
42 private InetAddress addr;
43
44 public Sender(InetAddress addr, int port, byte[] content, long delay) {
45 super();
46 this.port = port;
47 this.content = content;
48 this.delay = delay;
49 this.addr = addr;
50 start();
51 }
52
53 public void run() {
54 try {
55 Socket sock = new Socket(this.addr, this.port);
56 OutputStream out = sock.getOutputStream();
57 for (int i=0; i < this.content.length; i++) {
58 out.write(this.content[i]);
59 Thread.sleep(this.delay);
60 }
61 out.flush();
62 sock.close();
63 }
64 catch (Exception ex) {
65 ex.printStackTrace();
66 }
67 }
68
69 }
70
71 public class Receiver extends Thread {
72
73 private int port;
74 private Vector lines = new Vector();
75 private ServerSocket serverSocket;
76 private boolean isMicro;
77 private boolean finished;
78
79 public Receiver(int port, boolean isMicro) throws IOException {
80 super();
81 this.port = port;
82 this.serverSocket = new ServerSocket(this.port);
83 }
84
85 public InetAddress startListener() {
86 start();
87 return this.serverSocket.getInetAddress();
88 }
89
90 public String[] getLines() {
91 return (String[])this.lines.toArray(new String[this.lines.size()]);
92 }
93
94 public boolean isFinished() {
95 return this.finished;
96 }
97
98 public void run() {
99 System.out.println("starting thread");
100 try {
101 Socket sock = this.serverSocket.accept();
102 InputStream in = sock.getInputStream();
103
104 if (this.isMicro) {
105 BufferedInputStreamMicro br = new BufferedInputStreamMicro(in);
106 while (true) {
107 String line = br.readLine();
108 if (line != null) this.lines.add(line);
109 else break;
110 }
111 }
112 else {
113 BufferedReader br = new BufferedReader(new InputStreamReader(in));
114 while (true) {
115 String line = br.readLine();
116 if (line != null) this.lines.add(line);
117 else break;
118 }
119 }
120 sock.close();
121 }
122 catch (Exception ex) {
123 ex.printStackTrace();
124 }
125 this.finished = true;
126 }
127
128 }
129
130 final static String ME = "MicroEditionTest";
131 protected Global glob;
132 private static Logger log = Logger.getLogger(MicroEditionTest.class.getName());
133 int counter = 0;
134
135 public MicroEditionTest(String name, String[] args) {
136 super(name);
137 this.glob = Global.instance();
138 this.glob.init(args);
139
140 }
141
142 public MicroEditionTest(String name) {
143 super(name);
144 this.glob = Global.instance();
145
146 }
147
148 protected void setUp() {
149 }
150
151 protected void tearDown() {
152 }
153
154 private void assertHashtableContent(String key, Hashtable ref, Hashtable is) {
155 Object val1 = ref.get(key);
156 Object val2 = is.get(key);
157 assertEquals("wrong value for key '" + key + "'", val1, val2);
158 }
159
160 public void testVectorIO() {
161 try {
162 ByteArrayOutputStream baos = new ByteArrayOutputStream();
163 ObjectOutputStreamMicro oosm = new ObjectOutputStreamMicro(baos);
164
165 Hashtable key = new Hashtable();
166 key.put("one", "1");
167 key.put("two", "2");
168 key.put("three", "3");
169 key.put("four", "4");
170
171 Hashtable qos = new Hashtable();
172 qos.put("one-qos", "1");
173 qos.put("two-qos", "2");
174 qos.put("three-qos", "3");
175
176 byte[] content = "This is the content".getBytes();
177 Msg msg = new Msg(key, content, qos);
178 Vector inVec = new Vector();
179 inVec.add(key);
180 inVec.add(qos);
181 inVec.add(content);
182 inVec.add(key);
183 inVec.add(qos);
184 inVec.add(content);
185 oosm.writeObject(inVec);
186
187 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
188 ObjectInputStreamMicro oism = new ObjectInputStreamMicro(bais);
189 Object obj = oism.readObject();
190 assertTrue("hashtable is not of type 'Vector', it is " + obj.getClass().getName(), obj instanceof Vector);
191 Vector vec = (Vector)obj;
192 assertEquals("wrong number of entries in vector", 6, vec.size());
193 Hashtable keyOut = (Hashtable)vec.get(0);
194 Hashtable qosOut = (Hashtable)vec.get(1);
195 byte[] contentOut = (byte[])vec.get(2);
196 assertHashtableContent("one", key, keyOut);
197 assertHashtableContent("two", key, keyOut);
198 assertHashtableContent("three", key, keyOut);
199 assertHashtableContent("four", key, keyOut);
200 assertHashtableContent("one-qos", qos, qosOut);
201 assertHashtableContent("two-qos", qos, qosOut);
202 assertHashtableContent("three-qos", qos, qosOut);
203
204 keyOut = (Hashtable)vec.get(3);
205 qosOut = (Hashtable)vec.get(4);
206 contentOut = (byte[])vec.get(5);
207 assertHashtableContent("one", key, keyOut);
208 assertHashtableContent("two", key, keyOut);
209 assertHashtableContent("three", key, keyOut);
210 assertHashtableContent("four", key, keyOut);
211 assertHashtableContent("one-qos", qos, qosOut);
212 assertHashtableContent("two-qos", qos, qosOut);
213 assertHashtableContent("three-qos", qos, qosOut);
214 log.info("testVectorIO successfully completed");
215 }
216 catch (Exception ex) {
217 ex.printStackTrace();
218 assertTrue("exception occured: " + ex.getMessage(), false);
219 }
220
221 }
222
223 public void testHashtableIO() {
224 try {
225 ByteArrayOutputStream baos = new ByteArrayOutputStream();
226 ObjectOutputStreamMicro oosm = new ObjectOutputStreamMicro(baos);
227
228 Hashtable hashtable = new Hashtable();
229 hashtable.put("one", "1");
230 hashtable.put("two", "2");
231 hashtable.put("three", "3");
232 hashtable.put("four", "4");
233
234 oosm.writeObject(hashtable);
235 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
236 ObjectInputStreamMicro oism = new ObjectInputStreamMicro(bais);
237 Object obj = oism.readObject();
238 assertTrue("hashtable is not of type 'Hashtable', it is " + obj.getClass().getName(), obj instanceof Hashtable);
239 Hashtable table = (Hashtable)obj;
240 assertEquals("wrong number of entries in hashtable", hashtable.size(), table.size());
241 assertHashtableContent("one", hashtable, table);
242 assertHashtableContent("two", hashtable, table);
243 assertHashtableContent("three", hashtable, table);
244 assertHashtableContent("four", hashtable, table);
245 log.info("testHashtableIO successfully completed");
246 }
247 catch (Exception ex) {
248 ex.printStackTrace();
249 assertTrue("exception occured: " + ex.getMessage(), false);
250 }
251
252 }
253
254 public void testStringIO() {
255 try {
256 ByteArrayOutputStream baos = new ByteArrayOutputStream();
257 ObjectOutputStreamMicro oosm = new ObjectOutputStreamMicro(baos);
258
259 String testString = "this is\n\na simple\nmultiline string\n";
260 oosm.writeObject(testString);
261 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
262 ObjectInputStreamMicro oism = new ObjectInputStreamMicro(bais);
263 Object obj = oism.readObject();
264 assertTrue("string is not of type 'String', it is " + obj.getClass().getName(), obj instanceof String);
265 String response = (String)obj;
266 assertEquals("wrong content for the string", testString, response);
267 log.info("testStringIO successfully completed");
268 }
269 catch (Exception ex) {
270 ex.printStackTrace();
271 assertTrue("exception occured: " + ex.getMessage(), false);
272 }
273 }
274
275 public void testReadLine() {
276 try {
277 ByteArrayOutputStream baos = new ByteArrayOutputStream();
278 PrintStream ps = new PrintStream(baos);
279 ps.println("first line");
280 ps.println("");
281 ps.println("");
282 ps.println("");
283 ps.println("second line");
284 ps.println("third line");
285 ps.println("");
286 ps.println("");
287 ps.close();
288 byte[] buf = baos.toByteArray();
289 byte[] buf1 = new byte[buf.length];
290 for (int i=0; i < buf.length; i++) buf1[i] = buf[i];
291
292 BufferedInputStreamMicro bism = new BufferedInputStreamMicro(new ByteArrayInputStream(buf));
293 BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(buf1)));
294 while (true) {
295 String referenceLine = br.readLine(); // of the normal reader
296 if (referenceLine == null)
297 break;
298 String lineToCheck = bism.readLine(); // must be the same as the corresponding reference since we expect same behaviour
299 if (lineToCheck == null)
300 assertTrue("the line to be checked is unexpectedly null", false);
301 assertEquals("wrong content", referenceLine, lineToCheck);
302 }
303
304 String lineToCheck = bism.readLine(); // must be the same as the corresponding reference since we expect same behaviour
305 assertTrue("The line to check must also be null", lineToCheck == null);
306
307 log.info("testReadLine successfully completed");
308 }
309 catch (Exception ex) {
310 ex.printStackTrace();
311 assertTrue("exception occured: " + ex.getMessage(), false);
312 }
313 }
314
315 public void testReadLineDelayed() {
316 try {
317 ByteArrayOutputStream baos = new ByteArrayOutputStream();
318 PrintStream ps = new PrintStream(baos);
319 ps.println("first line");
320 ps.println("");
321 ps.println("");
322 ps.println("");
323 ps.println("second line");
324 ps.println("third line");
325 ps.println("");
326 ps.println("");
327 ps.close();
328 byte[] buf = baos.toByteArray();
329
330 int port1 = 26666;
331 int port2 = 26667;
332 Receiver receiver1 = new Receiver(port1, false);
333 Receiver receiver2 = new Receiver(port2, true);
334
335 log.info("testReadLineDelayed create sender 1");
336 Sender sender1 = new Sender(receiver1.startListener(), port1, buf, 1L);
337 log.info("testReadLineDelayed create sender 2");
338 Sender sender2 = new Sender(receiver2.startListener(), port2, buf, 10L);
339
340 while (!receiver1.isFinished() || !receiver2.isFinished()) {
341 Thread.sleep(200L);
342 }
343
344 String[] resp1 = receiver1.getLines();
345 String[] resp2 = receiver2.getLines();
346
347 assertEquals("wrong number of lines returned", resp1.length, resp2.length);
348 for (int i=0; i < resp1.length; i++) {
349 log.info(".testReadLineDelayed '" + resp1[i] + "' '" + resp2[i] + "'");
350 }
351 for (int i=0; i < resp1.length; i++) {
352 assertEquals("wrong content of line " + i, resp1[i], resp2[i]);
353 }
354
355 }
356 catch (Exception ex) {
357 ex.printStackTrace();
358 assertTrue("exception occured: " + ex.getMessage(), false);
359 }
360 }
361
362 public void testMessageIO() {
363 try {
364 ByteArrayOutputStream baos = new ByteArrayOutputStream();
365 ObjectOutputStreamMicro oosm = new ObjectOutputStreamMicro(baos);
366
367 String oid = "someOid";
368 String key = "<key oid='100'></key>";
369 String qos = "<qos><persistent/></qos>";
370 byte[] content = "This is the content".getBytes();
371
372 int length = ObjectOutputStreamMicro.writeMessage(baos, oid, key, qos, content);
373 assertEquals("wrong length returned", oid.length() + key.length() + qos.length() + content.length + 3, length);
374 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
375 ObjectInputStreamMicro oism = new ObjectInputStreamMicro(bais);
376
377 int size = bais.available();
378 assertEquals("wrong length of bytes available", length, size);
379 byte[] msg = new byte[size];
380 bais.read(msg);
381 MsgHolder msgHolder = ObjectInputStreamMicro.readMessage(msg);
382
383 assertEquals("wrong content for the oid", oid, msgHolder.getOid());
384 assertEquals("wrong content for the key", key, msgHolder.getKey());
385 assertEquals("wrong content for the qos", qos, msgHolder.getQos());
386 assertEquals("wrong content for the content", new String(content), new String(msgHolder.getContent()));
387
388 log.info("testMessageIO successfully completed");
389 }
390 catch (Exception ex) {
391 ex.printStackTrace();
392 assertTrue("exception occured: " + ex.getMessage(), false);
393 }
394 }
395
396 /**
397 * <pre>
398 * java org.xmlBlaster.test.classtest.MicroEditionTest
399 * </pre>
400 */
401 public static void main(String args[])
402 {
403 MicroEditionTest test = new MicroEditionTest("MicroEditionTest", args);
404
405 test.setUp();
406 test.testStringIO();
407 test.tearDown();
408
409 test.setUp();
410 test.testHashtableIO();
411 test.tearDown();
412
413 test.setUp();
414 test.testVectorIO();
415 test.tearDown();
416
417 test.setUp();
418 test.testReadLine();
419 test.tearDown();
420
421 test.setUp();
422 test.testReadLineDelayed();
423 test.tearDown();
424
425 test.setUp();
426 test.testMessageIO();
427 test.tearDown();
428 }
429 }
syntax highlighted by Code2HTML, v. 0.9.1