KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > UtilTest


1 // $Id: UtilTest.java,v 1.1 2007/07/04 07:29:33 belaban Exp $
2

3 package org.jgroups.tests;
4
5 import junit.framework.Test;
6 import junit.framework.TestCase;
7 import junit.framework.TestSuite;
8 import org.jgroups.*;
9 import org.jgroups.conf.ClassConfigurator;
10 import org.jgroups.stack.IpAddress;
11 import org.jgroups.util.Util;
12
13 import java.io.*;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Vector JavaDoc;
17 import java.util.Properties JavaDoc;
18
19
20 public class UtilTest extends TestCase {
21
22     static {
23         try {
24             ClassConfigurator.getInstance(true);
25         }
26         catch(ChannelException e) {
27             e.printStackTrace();
28         }
29     }
30
31     public UtilTest(String JavaDoc name) {
32         super(name);
33     }
34
35
36     public void testGetProperty() {
37         Properties JavaDoc props=new Properties JavaDoc();
38         props.setProperty("name", "Bela");
39         props.setProperty("key", "val");
40
41         System.setProperty("name", "Michelle");
42         System.setProperty("name2", "Nicole");
43         String JavaDoc retval;
44
45         retval=Util.getProperty(new String JavaDoc[]{"name", "name2"}, props, "name", false, "Jeannette");
46         assertEquals("Michelle", retval);
47         props.setProperty("name", "Bela"); props.setProperty("key", "val");
48
49         retval=Util.getProperty(new String JavaDoc[]{"name2", "name"}, props, "name", false, "Jeannette");
50         assertEquals("Nicole", retval);
51         props.setProperty("name", "Bela"); props.setProperty("key", "val");
52
53         retval=Util.getProperty(new String JavaDoc[]{"name3", "name"}, props, "name", false, "Jeannette");
54         assertEquals("Michelle", retval);
55         props.setProperty("name", "Bela"); props.setProperty("key", "val");
56
57         retval=Util.getProperty(new String JavaDoc[]{"name3", "name4"}, props, "name", false, "Jeannette");
58         assertEquals("Bela", retval);
59         props.setProperty("name", "Bela"); props.setProperty("key", "val");
60
61         retval=Util.getProperty(new String JavaDoc[]{"name2", "name"}, props, "name", true, "Jeannette");
62         assertEquals("Bela", retval);
63         props.setProperty("name", "Bela"); props.setProperty("key", "val");
64
65         retval=Util.getProperty(new String JavaDoc[]{"name2", "name"}, props, "name2", true, "Jeannette");
66         assertEquals("Jeannette", retval);
67         props.setProperty("name", "Bela"); props.setProperty("key", "val");
68
69         retval=Util.getProperty(new String JavaDoc[]{"name2", "name"}, props, "name2", true, null);
70         assertNull(retval);
71         props.setProperty("name", "Bela"); props.setProperty("key", "val");
72     }
73
74
75     public void testIgnoreBindAddress() {
76         boolean retval;
77
78         retval=Util.isBindAddressPropertyIgnored();
79         assertFalse(retval);
80
81         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "true");
82         retval=Util.isBindAddressPropertyIgnored();
83         assertTrue(retval);
84
85         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "true2");
86         retval=Util.isBindAddressPropertyIgnored();
87         assertFalse(retval);
88
89         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "false");
90         retval=Util.isBindAddressPropertyIgnored();
91         assertFalse(retval);
92
93         System.getProperties().remove(Global.IGNORE_BIND_ADDRESS_PROPERTY);
94         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD, "false");
95         retval=Util.isBindAddressPropertyIgnored();
96         assertFalse(retval);
97
98         System.getProperties().remove(Global.IGNORE_BIND_ADDRESS_PROPERTY);
99         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD, "true");
100         retval=Util.isBindAddressPropertyIgnored();
101         assertTrue(retval);
102
103
104         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "true");
105         System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD, "true");
106         retval=Util.isBindAddressPropertyIgnored();
107         assertTrue(retval);
108     }
109
110
111     public void testPrintBytes() {
112         long num;
113         String JavaDoc s;
114
115         num=1;
116         s=Util.printBytes(num);
117         System.out.println(num + " is " + s);
118         assertEquals("1b", s);
119
120         num=999;
121         s=Util.printBytes(num);
122         System.out.println(num + " is " + s);
123         assertEquals("999b", s);
124
125         num=1000;
126         s=Util.printBytes(num);
127         System.out.println(num + " is " + s);
128         assertEquals("1KB", s);
129
130         num=1001;
131         s=Util.printBytes(num);
132         System.out.println(num + " is " + s);
133         assertEquals("1KB", s);
134
135         num=1010;
136         s=Util.printBytes(num);
137         System.out.println(num + " is " + s);
138         assertEquals("1.01KB", s);
139
140         num=1543;
141         s=Util.printBytes(num);
142         System.out.println(num + " is " + s);
143         assertEquals("1.54KB", s);
144
145         num=10000;
146         s=Util.printBytes(num);
147         System.out.println(num + " is " + s);
148         assertEquals("10KB", s);
149
150         num=150000;
151         s=Util.printBytes(num);
152         System.out.println(num + " is " + s);
153         assertEquals("150KB", s);
154
155         num=150023;
156         s=Util.printBytes(num);
157         System.out.println(num + " is " + s);
158         assertEquals("150.02KB", s);
159
160         num=1200000;
161         s=Util.printBytes(num);
162         System.out.println(num + " is " + s);
163         assertEquals("1.2MB", s);
164
165         num=150000000;
166         s=Util.printBytes(num);
167         System.out.println(num + " is " + s);
168         assertEquals("150MB", s);
169
170         num=150030000;
171         s=Util.printBytes(num);
172         System.out.println(num + " is " + s);
173         assertEquals("150.03MB", s);
174
175         num=1200000000;
176         s=Util.printBytes(num);
177         System.out.println(num + " is " + s);
178         assertEquals("1.2GB", s);
179     }
180
181
182     public void testObjectToFromByteBuffer() throws Exception JavaDoc {
183         byte[] buf;
184         IpAddress addr=new IpAddress("localhost", 5000), addr2;
185         List JavaDoc list=new ArrayList JavaDoc(), list2;
186         list.add("Bela");
187         list.add("Jeannette");
188
189         buf=Util.objectToByteBuffer(addr);
190         addr2=(IpAddress)Util.objectFromByteBuffer(buf);
191         System.out.println("addr=" + addr + ", addr2=" + addr2);
192         assertEquals(addr, addr2);
193
194         buf=Util.objectToByteBuffer(list);
195         list2=(List JavaDoc)Util.objectFromByteBuffer(buf);
196         System.out.println("list=" + list + ", list2=" + list2);
197         assertEquals(list, list2);
198
199         Object JavaDoc obj=null;
200         buf=Util.objectToByteBuffer(obj);
201         assertNotNull(buf);
202         assertTrue(buf.length > 0);
203         obj=Util.objectFromByteBuffer(buf);
204         assertNull(obj);
205
206         Object JavaDoc[] values=new Object JavaDoc[]{
207                 Boolean.TRUE,
208                 Boolean.FALSE,
209                 new Byte JavaDoc((byte)22),
210                 new Byte JavaDoc("2"),
211                 new Character JavaDoc('5'),
212                 new Double JavaDoc(3.14),
213                 new Float JavaDoc(352.3),
214                 new Integer JavaDoc(100),
215                 new Long JavaDoc(322649),
216                 new Short JavaDoc((short)22),
217                 "Bela Ban"
218         };
219         for(int i=0; i < values.length; i++) {
220             Object JavaDoc value=values[i];
221             marshal(value);
222         }
223     }
224
225
226     public void testObjectToByteArrayWithLargeString() throws Exception JavaDoc {
227         marshalString(Short.MAX_VALUE );
228     }
229
230
231      public void testObjectToByteArrayWithLargeString2() throws Exception JavaDoc {
232         marshalString(Short.MAX_VALUE - 100);
233     }
234
235
236     public void testObjectToByteArrayWithLargeString3() throws Exception JavaDoc {
237         marshalString(Short.MAX_VALUE + 1);
238     }
239
240     public void testObjectToByteArrayWithLargeString4() throws Exception JavaDoc {
241         marshalString(Short.MAX_VALUE + 100);
242     }
243
244      public void testObjectToByteArrayWithLargeString5() throws Exception JavaDoc {
245         marshalString(Short.MAX_VALUE + 100000);
246     }
247
248
249     private void marshalString(int size) throws Exception JavaDoc {
250         byte[] tmp=new byte[size];
251         String JavaDoc str=new String JavaDoc(tmp, 0, tmp.length);
252         byte[] retval=Util.objectToByteBuffer(str);
253         System.out.println("length=" + retval.length + " bytes");
254         String JavaDoc obj=(String JavaDoc)Util.objectFromByteBuffer(retval);
255         System.out.println("read " + obj.length() + " string");
256     }
257
258
259
260     void marshal(Object JavaDoc obj) throws Exception JavaDoc {
261         byte[] buf=Util.objectToByteBuffer(obj);
262         assertNotNull(buf);
263         assertTrue(buf.length > 0);
264         Object JavaDoc obj2=Util.objectFromByteBuffer(buf);
265         System.out.println("obj=" + obj + ", obj2=" + obj2 + " (type=" + obj.getClass().getName() + ", length=" + buf.length + " bytes)");
266         assertEquals(obj, obj2);
267     }
268
269
270     public void testWriteStreamable() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
271         Message m=new Message(null, null, "Hello");
272         ViewId vid=new ViewId(null, 12345);
273         ViewId vid2=new ViewId(new IpAddress("127.0.0.1", 5555), 35623);
274         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
275         DataOutputStream dos=new DataOutputStream(outstream);
276         Util.writeGenericStreamable(m, dos);
277         Util.writeGenericStreamable(vid, dos);
278         Util.writeGenericStreamable(vid2, dos);
279         dos.close();
280         byte[] buf=outstream.toByteArray();
281         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
282         DataInputStream dis=new DataInputStream(instream);
283         Message m2=(Message)Util.readGenericStreamable(dis);
284         ViewId v3=(ViewId)Util.readGenericStreamable(dis);
285         ViewId v4=(ViewId)Util.readGenericStreamable(dis);
286         assertNotNull(m2.getBuffer());
287         assertEquals(m.getLength(), m2.getLength());
288         assertNotNull(v3);
289         assertEquals(vid, v3);
290         assertNotNull(v4);
291         assertEquals(vid2, v4);
292     }
293
294     public void testWriteViewIdWithNullCoordinator() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
295         ViewId vid=new ViewId(null, 12345);
296         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
297         DataOutputStream dos=new DataOutputStream(outstream);
298         Util.writeGenericStreamable(vid, dos);
299         dos.close();
300         byte[] buf=outstream.toByteArray();
301         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
302         DataInputStream dis=new DataInputStream(instream);
303         ViewId v4=(ViewId)Util.readGenericStreamable(dis);
304         assertEquals(vid, v4);
305     }
306
307
308     public void testWriteView() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
309         ViewId vid=new ViewId(null, 12345);
310         Vector JavaDoc members=new Vector JavaDoc();
311         View v;
312         IpAddress a1=new IpAddress("localhost", 1234);
313         IpAddress a2=new IpAddress("127.0.0.1", 4444);
314         IpAddress a4=new IpAddress("www.google.com", 7777);
315         members.add(a1);
316         members.add(a2);
317         members.add(a4);
318         v=new View(vid, members);
319
320         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
321         DataOutputStream dos=new DataOutputStream(outstream);
322         Util.writeGenericStreamable(v, dos);
323         Util.writeStreamable(v, dos);
324         dos.close();
325         byte[] buf=outstream.toByteArray();
326         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
327         DataInputStream dis=new DataInputStream(instream);
328         View v2=(View)Util.readGenericStreamable(dis);
329         assertEquals(v, v2);
330         v2=(View)Util.readStreamable(View.class, dis);
331         assertEquals(v, v2);
332     }
333
334
335     public void testWriteString() throws IOException {
336         String JavaDoc s1="Bela Ban", s2="Michelle Ban";
337         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
338         DataOutputStream dos=new DataOutputStream(outstream);
339         Util.writeString(s1, dos);
340         Util.writeString(s2, dos);
341         dos.close();
342         byte[] buf=outstream.toByteArray();
343         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
344         DataInputStream dis=new DataInputStream(instream);
345         String JavaDoc s3=Util.readString(dis);
346         String JavaDoc s4=Util.readString(dis);
347         assertEquals(s1, s3);
348         assertEquals(s2, s4);
349     }
350
351     public void writeAddress() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
352         IpAddress a1=new IpAddress("localhost", 1234);
353         IpAddress a2=new IpAddress("127.0.0.1", 4444);
354         IpAddress a3=new IpAddress("thishostdoesnexist", 6666);
355         IpAddress a4=new IpAddress("www.google.com", 7777);
356
357         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
358         DataOutputStream dos=new DataOutputStream(outstream);
359         Util.writeAddress(a1, dos);
360         Util.writeAddress(a2, dos);
361         Util.writeAddress(a3, dos);
362         Util.writeAddress(a4, dos);
363         dos.close();
364         byte[] buf=outstream.toByteArray();
365         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
366         DataInputStream dis=new DataInputStream(instream);
367
368         assertEquals(a1, Util.readAddress(dis));
369         assertEquals(a2, Util.readAddress(dis));
370         assertEquals(a3, Util.readAddress(dis));
371         assertEquals(a4, Util.readAddress(dis));
372     }
373
374     public void writeNullAddress() throws IOException, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
375         IpAddress a1=null;
376         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
377         DataOutputStream dos=new DataOutputStream(outstream);
378         Util.writeAddress(a1, dos);
379         dos.close();
380         byte[] buf=outstream.toByteArray();
381         ByteArrayInputStream instream=new ByteArrayInputStream(buf);
382         DataInputStream dis=new DataInputStream(instream);
383         assertNull(Util.readAddress(dis));
384     }
385
386
387     public void testWriteByteBuffer() throws IOException {
388         byte[] buf=new byte[1024], tmp;
389         for(int i=0; i < buf.length; i++)
390             buf[i]=0;
391         ByteArrayOutputStream outstream=new ByteArrayOutputStream();
392         DataOutputStream dos=new DataOutputStream(outstream);
393         Util.writeByteBuffer(buf, dos);
394         dos.close();
395         tmp=outstream.toByteArray();
396         ByteArrayInputStream instream=new ByteArrayInputStream(tmp);
397         DataInputStream dis=new DataInputStream(instream);
398         byte[] buf2=Util.readByteBuffer(dis);
399         assertNotNull(buf2);
400         assertEquals(buf.length, buf2.length);
401     }
402
403
404     public void testMatch() {
405         long[] a={1,2,3};
406         long[] b={2,3,4};
407         long[] c=null;
408         long[] d={1,2,3,4};
409         long[] e={1,2,3};
410
411         assertTrue(Util.match(a,a));
412         assertFalse(Util.match(a,b));
413         assertFalse(Util.match(a,c));
414         assertFalse(Util.match(a,d));
415         assertTrue(Util.match(a,e));
416         assertTrue(Util.match(c,c));
417         assertFalse(Util.match(c, a));
418     }
419
420
421     public void testPickRandomElement() {
422         Vector JavaDoc v=new Vector JavaDoc();
423         for(int i=0; i < 10; i++) {
424             v.add(new Integer JavaDoc(i));
425         }
426
427         Integer JavaDoc el;
428         for(int i=0; i < 10000; i++) {
429             el=(Integer JavaDoc)Util.pickRandomElement(v);
430             assertTrue(el.intValue() >= 0 && el.intValue() < 10);
431         }
432     }
433
434     public void testAll() {
435         ArrayList JavaDoc l=new ArrayList JavaDoc();
436         l.add("one"); l.add("two"); l.add("one");
437         System.out.println("-- list is " + l);
438         assertFalse(Util.all(l, "one"));
439         l.remove("two");
440         System.out.println("-- list is " + l);
441         assertTrue(Util.all(l, "one"));
442     }
443
444
445     public void testParseCommaDelimitedString() {
446         String JavaDoc input="1,2,3,4,5,6,7,8,9,10 , 11, 12 ,13";
447
448         List JavaDoc list=Util.parseCommaDelimitedStrings(input);
449         System.out.println("list: " + list);
450         assertEquals(13, list.size());
451         assertEquals("1", list.get(0));
452         assertEquals("13", list.get(list.size() -1));
453     }
454
455
456     public void testParseSemicolonDelimitedString() {
457         String JavaDoc input="one;two ; three; four ; five;six";
458         List JavaDoc list=Util.parseStringList(input, ";");
459         System.out.println("list: " + list);
460         assertEquals(6, list.size());
461         assertEquals("one", list.get(0));
462         assertEquals("six", list.get(list.size() -1));
463     }
464
465
466     public void testParseSemicolonDelimitedString2() {
467         String JavaDoc input=" myID1::subID1 ; myID2::mySubID2; myID3 ;myID4::blaSubID4";
468         List JavaDoc list=Util.parseStringList(input, ";");
469         System.out.println("list: " + list);
470         assertEquals(4, list.size());
471         assertEquals("myID1::subID1", list.get(0));
472         assertEquals("myID4::blaSubID4", list.get(list.size() -1));
473     }
474
475
476     public void testVariableSubstitution() {
477         String JavaDoc val="hello world", replacement;
478         replacement=Util.substituteVariable(val);
479         assertEquals(val, replacement); // no substitution
480

481         val="my name is ${user.name}";
482         replacement=Util.substituteVariable(val);
483         assertNotSame(val, replacement);
484         assertFalse(val.equals(replacement));
485
486         val="my name is ${user.name} and ${user.name}";
487         replacement=Util.substituteVariable(val);
488         assertFalse(val.equals(replacement));
489         assertEquals(-1, replacement.indexOf("${"));
490
491         val="my name is ${unknown.var:Bela Ban}";
492         replacement=Util.substituteVariable(val);
493         assertTrue(replacement.indexOf("Bela Ban") >= 0);
494         assertEquals(-1, replacement.indexOf("${"));
495
496         val="my name is ${unknown.var}";
497         replacement=Util.substituteVariable(val);
498         assertTrue(replacement.indexOf("${") >= 0);
499
500         val="here is an invalid ${argument because it doesn't contains a closing bracket";
501         try {
502             replacement=Util.substituteVariable(val);
503             fail("should be an IllegalArgumentException");
504         }
505         catch(Throwable JavaDoc t) {
506             assertEquals(IllegalArgumentException JavaDoc.class, t.getClass());
507         }
508     }
509
510
511
512     public static Test suite() {
513         return new TestSuite(UtilTest.class);
514     }
515
516     public static void main(String JavaDoc[] args) {
517         junit.textui.TestRunner.run(suite());
518     }
519 }
520
521
522
Popular Tags