Source Code Cross Referenced for UtilTest.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » tests » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        // $Id: UtilTest.java,v 1.17.2.2 2007/03/14 16:36:45 belaban Exp $
002:
003:        package org.jgroups.tests;
004:
005:        import junit.framework.Test;
006:        import junit.framework.TestCase;
007:        import junit.framework.TestSuite;
008:        import org.jgroups.*;
009:        import org.jgroups.conf.ClassConfigurator;
010:        import org.jgroups.stack.IpAddress;
011:        import org.jgroups.util.Util;
012:
013:        import java.io.*;
014:        import java.util.ArrayList;
015:        import java.util.List;
016:        import java.util.Vector;
017:        import java.util.Properties;
018:
019:        public class UtilTest extends TestCase {
020:
021:            static {
022:                try {
023:                    ClassConfigurator.getInstance(true);
024:                } catch (ChannelException e) {
025:                    e.printStackTrace();
026:                }
027:            }
028:
029:            public UtilTest(String name) {
030:                super (name);
031:            }
032:
033:            public void testGetProperty() {
034:                Properties props = new Properties();
035:                props.setProperty("name", "Bela");
036:                props.setProperty("key", "val");
037:
038:                System.setProperty("name", "Michelle");
039:                System.setProperty("name2", "Nicole");
040:                String retval;
041:
042:                retval = Util.getProperty(new String[] { "name", "name2" },
043:                        props, "name", false, "Jeannette");
044:                assertEquals("Michelle", retval);
045:                props.setProperty("name", "Bela");
046:                props.setProperty("key", "val");
047:
048:                retval = Util.getProperty(new String[] { "name2", "name" },
049:                        props, "name", false, "Jeannette");
050:                assertEquals("Nicole", retval);
051:                props.setProperty("name", "Bela");
052:                props.setProperty("key", "val");
053:
054:                retval = Util.getProperty(new String[] { "name3", "name" },
055:                        props, "name", false, "Jeannette");
056:                assertEquals("Michelle", retval);
057:                props.setProperty("name", "Bela");
058:                props.setProperty("key", "val");
059:
060:                retval = Util.getProperty(new String[] { "name3", "name4" },
061:                        props, "name", false, "Jeannette");
062:                assertEquals("Bela", retval);
063:                props.setProperty("name", "Bela");
064:                props.setProperty("key", "val");
065:
066:                retval = Util.getProperty(new String[] { "name2", "name" },
067:                        props, "name", true, "Jeannette");
068:                assertEquals("Bela", retval);
069:                props.setProperty("name", "Bela");
070:                props.setProperty("key", "val");
071:
072:                retval = Util.getProperty(new String[] { "name2", "name" },
073:                        props, "name2", true, "Jeannette");
074:                assertEquals("Jeannette", retval);
075:                props.setProperty("name", "Bela");
076:                props.setProperty("key", "val");
077:
078:                retval = Util.getProperty(new String[] { "name2", "name" },
079:                        props, "name2", true, null);
080:                assertNull(retval);
081:                props.setProperty("name", "Bela");
082:                props.setProperty("key", "val");
083:            }
084:
085:            public void testIgnoreBindAddress() {
086:                boolean retval;
087:
088:                retval = Util.isBindAddressPropertyIgnored();
089:                assertFalse(retval);
090:
091:                System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "true");
092:                retval = Util.isBindAddressPropertyIgnored();
093:                assertTrue(retval);
094:
095:                System
096:                        .setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY,
097:                                "true2");
098:                retval = Util.isBindAddressPropertyIgnored();
099:                assertFalse(retval);
100:
101:                System
102:                        .setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY,
103:                                "false");
104:                retval = Util.isBindAddressPropertyIgnored();
105:                assertFalse(retval);
106:
107:                System.getProperties().remove(
108:                        Global.IGNORE_BIND_ADDRESS_PROPERTY);
109:                System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD,
110:                        "false");
111:                retval = Util.isBindAddressPropertyIgnored();
112:                assertFalse(retval);
113:
114:                System.getProperties().remove(
115:                        Global.IGNORE_BIND_ADDRESS_PROPERTY);
116:                System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD,
117:                        "true");
118:                retval = Util.isBindAddressPropertyIgnored();
119:                assertTrue(retval);
120:
121:                System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY, "true");
122:                System.setProperty(Global.IGNORE_BIND_ADDRESS_PROPERTY_OLD,
123:                        "true");
124:                retval = Util.isBindAddressPropertyIgnored();
125:                assertTrue(retval);
126:            }
127:
128:            public void testPrintBytes() {
129:                long num;
130:                String s;
131:
132:                num = 1;
133:                s = Util.printBytes(num);
134:                System.out.println(num + " is " + s);
135:                assertEquals("1b", s);
136:
137:                num = 999;
138:                s = Util.printBytes(num);
139:                System.out.println(num + " is " + s);
140:                assertEquals("999b", s);
141:
142:                num = 1000;
143:                s = Util.printBytes(num);
144:                System.out.println(num + " is " + s);
145:                assertEquals("1KB", s);
146:
147:                num = 1001;
148:                s = Util.printBytes(num);
149:                System.out.println(num + " is " + s);
150:                assertEquals("1KB", s);
151:
152:                num = 1010;
153:                s = Util.printBytes(num);
154:                System.out.println(num + " is " + s);
155:                assertEquals("1.01KB", s);
156:
157:                num = 1543;
158:                s = Util.printBytes(num);
159:                System.out.println(num + " is " + s);
160:                assertEquals("1.54KB", s);
161:
162:                num = 10000;
163:                s = Util.printBytes(num);
164:                System.out.println(num + " is " + s);
165:                assertEquals("10KB", s);
166:
167:                num = 150000;
168:                s = Util.printBytes(num);
169:                System.out.println(num + " is " + s);
170:                assertEquals("150KB", s);
171:
172:                num = 150023;
173:                s = Util.printBytes(num);
174:                System.out.println(num + " is " + s);
175:                assertEquals("150.02KB", s);
176:
177:                num = 1200000;
178:                s = Util.printBytes(num);
179:                System.out.println(num + " is " + s);
180:                assertEquals("1.2MB", s);
181:
182:                num = 150000000;
183:                s = Util.printBytes(num);
184:                System.out.println(num + " is " + s);
185:                assertEquals("150MB", s);
186:
187:                num = 150030000;
188:                s = Util.printBytes(num);
189:                System.out.println(num + " is " + s);
190:                assertEquals("150.03MB", s);
191:
192:                num = 1200000000;
193:                s = Util.printBytes(num);
194:                System.out.println(num + " is " + s);
195:                assertEquals("1.2GB", s);
196:            }
197:
198:            public void testObjectToFromByteBuffer() throws Exception {
199:                byte[] buf;
200:                IpAddress addr = new IpAddress("localhost", 5000), addr2;
201:                List list = new ArrayList(), list2;
202:                list.add("Bela");
203:                list.add("Jeannette");
204:
205:                buf = Util.objectToByteBuffer(addr);
206:                addr2 = (IpAddress) Util.objectFromByteBuffer(buf);
207:                System.out.println("addr=" + addr + ", addr2=" + addr2);
208:                assertEquals(addr, addr2);
209:
210:                buf = Util.objectToByteBuffer(list);
211:                list2 = (List) Util.objectFromByteBuffer(buf);
212:                System.out.println("list=" + list + ", list2=" + list2);
213:                assertEquals(list, list2);
214:
215:                Object obj = null;
216:                buf = Util.objectToByteBuffer(obj);
217:                assertNotNull(buf);
218:                assertTrue(buf.length > 0);
219:                obj = Util.objectFromByteBuffer(buf);
220:                assertNull(obj);
221:
222:                Object[] values = new Object[] { Boolean.TRUE, Boolean.FALSE,
223:                        new Byte((byte) 22), new Byte("2"), new Character('5'),
224:                        new Double(3.14), new Float(352.3), new Integer(100),
225:                        new Long(322649), new Short((short) 22), "Bela Ban" };
226:                for (int i = 0; i < values.length; i++) {
227:                    Object value = values[i];
228:                    marshal(value);
229:                }
230:            }
231:
232:            public void testObjectToByteArrayWithLargeString() throws Exception {
233:                marshalString(Short.MAX_VALUE);
234:            }
235:
236:            public void testObjectToByteArrayWithLargeString2()
237:                    throws Exception {
238:                marshalString(Short.MAX_VALUE - 100);
239:            }
240:
241:            public void testObjectToByteArrayWithLargeString3()
242:                    throws Exception {
243:                marshalString(Short.MAX_VALUE + 1);
244:            }
245:
246:            public void testObjectToByteArrayWithLargeString4()
247:                    throws Exception {
248:                marshalString(Short.MAX_VALUE + 100);
249:            }
250:
251:            public void testObjectToByteArrayWithLargeString5()
252:                    throws Exception {
253:                marshalString(Short.MAX_VALUE + 100000);
254:            }
255:
256:            private void marshalString(int size) throws Exception {
257:                byte[] tmp = new byte[size];
258:                String str = new String(tmp, 0, tmp.length);
259:                byte[] retval = Util.objectToByteBuffer(str);
260:                System.out.println("length=" + retval.length + " bytes");
261:                String obj = (String) Util.objectFromByteBuffer(retval);
262:                System.out.println("read " + obj.length() + " string");
263:            }
264:
265:            void marshal(Object obj) throws Exception {
266:                byte[] buf = Util.objectToByteBuffer(obj);
267:                assertNotNull(buf);
268:                assertTrue(buf.length > 0);
269:                Object obj2 = Util.objectFromByteBuffer(buf);
270:                System.out.println("obj=" + obj + ", obj2=" + obj2 + " (type="
271:                        + obj.getClass().getName() + ", length=" + buf.length
272:                        + " bytes)");
273:                assertEquals(obj, obj2);
274:            }
275:
276:            public void testWriteStreamable() throws IOException,
277:                    IllegalAccessException, InstantiationException {
278:                Message m = new Message(null, null, "Hello");
279:                ViewId vid = new ViewId(null, 12345);
280:                ViewId vid2 = new ViewId(new IpAddress("127.0.0.1", 5555),
281:                        35623);
282:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
283:                DataOutputStream dos = new DataOutputStream(outstream);
284:                Util.writeGenericStreamable(m, dos);
285:                Util.writeGenericStreamable(vid, dos);
286:                Util.writeGenericStreamable(vid2, dos);
287:                dos.close();
288:                byte[] buf = outstream.toByteArray();
289:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
290:                DataInputStream dis = new DataInputStream(instream);
291:                Message m2 = (Message) Util.readGenericStreamable(dis);
292:                ViewId v3 = (ViewId) Util.readGenericStreamable(dis);
293:                ViewId v4 = (ViewId) Util.readGenericStreamable(dis);
294:                assertNotNull(m2.getBuffer());
295:                assertEquals(m.getLength(), m2.getLength());
296:                assertNotNull(v3);
297:                assertEquals(vid, v3);
298:                assertNotNull(v4);
299:                assertEquals(vid2, v4);
300:            }
301:
302:            public void testWriteViewIdWithNullCoordinator()
303:                    throws IOException, IllegalAccessException,
304:                    InstantiationException {
305:                ViewId vid = new ViewId(null, 12345);
306:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
307:                DataOutputStream dos = new DataOutputStream(outstream);
308:                Util.writeGenericStreamable(vid, dos);
309:                dos.close();
310:                byte[] buf = outstream.toByteArray();
311:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
312:                DataInputStream dis = new DataInputStream(instream);
313:                ViewId v4 = (ViewId) Util.readGenericStreamable(dis);
314:                assertEquals(vid, v4);
315:            }
316:
317:            public void testWriteView() throws IOException,
318:                    IllegalAccessException, InstantiationException {
319:                ViewId vid = new ViewId(null, 12345);
320:                Vector members = new Vector();
321:                View v;
322:                IpAddress a1 = new IpAddress("localhost", 1234);
323:                IpAddress a2 = new IpAddress("127.0.0.1", 4444);
324:                IpAddress a4 = new IpAddress("www.google.com", 7777);
325:                members.add(a1);
326:                members.add(a2);
327:                members.add(a4);
328:                v = new View(vid, members);
329:
330:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
331:                DataOutputStream dos = new DataOutputStream(outstream);
332:                Util.writeGenericStreamable(v, dos);
333:                Util.writeStreamable(v, dos);
334:                dos.close();
335:                byte[] buf = outstream.toByteArray();
336:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
337:                DataInputStream dis = new DataInputStream(instream);
338:                View v2 = (View) Util.readGenericStreamable(dis);
339:                assertEquals(v, v2);
340:                v2 = (View) Util.readStreamable(View.class, dis);
341:                assertEquals(v, v2);
342:            }
343:
344:            public void testWriteString() throws IOException {
345:                String s1 = "Bela Ban", s2 = "Michelle Ban";
346:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
347:                DataOutputStream dos = new DataOutputStream(outstream);
348:                Util.writeString(s1, dos);
349:                Util.writeString(s2, dos);
350:                dos.close();
351:                byte[] buf = outstream.toByteArray();
352:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
353:                DataInputStream dis = new DataInputStream(instream);
354:                String s3 = Util.readString(dis);
355:                String s4 = Util.readString(dis);
356:                assertEquals(s1, s3);
357:                assertEquals(s2, s4);
358:            }
359:
360:            public void writeAddress() throws IOException,
361:                    IllegalAccessException, InstantiationException {
362:                IpAddress a1 = new IpAddress("localhost", 1234);
363:                IpAddress a2 = new IpAddress("127.0.0.1", 4444);
364:                IpAddress a3 = new IpAddress("thishostdoesnexist", 6666);
365:                IpAddress a4 = new IpAddress("www.google.com", 7777);
366:
367:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
368:                DataOutputStream dos = new DataOutputStream(outstream);
369:                Util.writeAddress(a1, dos);
370:                Util.writeAddress(a2, dos);
371:                Util.writeAddress(a3, dos);
372:                Util.writeAddress(a4, dos);
373:                dos.close();
374:                byte[] buf = outstream.toByteArray();
375:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
376:                DataInputStream dis = new DataInputStream(instream);
377:
378:                assertEquals(a1, Util.readAddress(dis));
379:                assertEquals(a2, Util.readAddress(dis));
380:                assertEquals(a3, Util.readAddress(dis));
381:                assertEquals(a4, Util.readAddress(dis));
382:            }
383:
384:            public void writeNullAddress() throws IOException,
385:                    IllegalAccessException, InstantiationException {
386:                IpAddress a1 = null;
387:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
388:                DataOutputStream dos = new DataOutputStream(outstream);
389:                Util.writeAddress(a1, dos);
390:                dos.close();
391:                byte[] buf = outstream.toByteArray();
392:                ByteArrayInputStream instream = new ByteArrayInputStream(buf);
393:                DataInputStream dis = new DataInputStream(instream);
394:                assertNull(Util.readAddress(dis));
395:            }
396:
397:            public void testWriteByteBuffer() throws IOException {
398:                byte[] buf = new byte[1024], tmp;
399:                for (int i = 0; i < buf.length; i++)
400:                    buf[i] = 0;
401:                ByteArrayOutputStream outstream = new ByteArrayOutputStream();
402:                DataOutputStream dos = new DataOutputStream(outstream);
403:                Util.writeByteBuffer(buf, dos);
404:                dos.close();
405:                tmp = outstream.toByteArray();
406:                ByteArrayInputStream instream = new ByteArrayInputStream(tmp);
407:                DataInputStream dis = new DataInputStream(instream);
408:                byte[] buf2 = Util.readByteBuffer(dis);
409:                assertNotNull(buf2);
410:                assertEquals(buf.length, buf2.length);
411:            }
412:
413:            public void testMatch() {
414:                long[] a = { 1, 2, 3 };
415:                long[] b = { 2, 3, 4 };
416:                long[] c = null;
417:                long[] d = { 1, 2, 3, 4 };
418:                long[] e = { 1, 2, 3 };
419:
420:                assertTrue(Util.match(a, a));
421:                assertFalse(Util.match(a, b));
422:                assertFalse(Util.match(a, c));
423:                assertFalse(Util.match(a, d));
424:                assertTrue(Util.match(a, e));
425:                assertTrue(Util.match(c, c));
426:                assertFalse(Util.match(c, a));
427:            }
428:
429:            public void testPickRandomElement() {
430:                Vector v = new Vector();
431:                for (int i = 0; i < 10; i++) {
432:                    v.add(new Integer(i));
433:                }
434:
435:                Integer el;
436:                for (int i = 0; i < 10000; i++) {
437:                    el = (Integer) Util.pickRandomElement(v);
438:                    assertTrue(el.intValue() >= 0 && el.intValue() < 10);
439:                }
440:            }
441:
442:            public void testAll() {
443:                ArrayList l = new ArrayList();
444:                l.add("one");
445:                l.add("two");
446:                l.add("one");
447:                System.out.println("-- list is " + l);
448:                assertFalse(Util.all(l, "one"));
449:                l.remove("two");
450:                System.out.println("-- list is " + l);
451:                assertTrue(Util.all(l, "one"));
452:            }
453:
454:            public void testParseCommaDelimitedString() {
455:                String input = "1,2,3,4,5,6,7,8,9,10 , 11, 12 ,13";
456:
457:                List list = Util.parseCommaDelimitedStrings(input);
458:                System.out.println("list: " + list);
459:                assertEquals(13, list.size());
460:                assertEquals("1", list.get(0));
461:                assertEquals("13", list.get(list.size() - 1));
462:            }
463:
464:            public void testParseSemicolonDelimitedString() {
465:                String input = "one;two ; three; four ; five;six";
466:                List list = Util.parseStringList(input, ";");
467:                System.out.println("list: " + list);
468:                assertEquals(6, list.size());
469:                assertEquals("one", list.get(0));
470:                assertEquals("six", list.get(list.size() - 1));
471:            }
472:
473:            public void testParseSemicolonDelimitedString2() {
474:                String input = "  myID1::subID1 ; myID2::mySubID2; myID3 ;myID4::blaSubID4";
475:                List list = Util.parseStringList(input, ";");
476:                System.out.println("list: " + list);
477:                assertEquals(4, list.size());
478:                assertEquals("myID1::subID1", list.get(0));
479:                assertEquals("myID4::blaSubID4", list.get(list.size() - 1));
480:            }
481:
482:            public void testVariableSubstitution() {
483:                String val = "hello world", replacement;
484:                replacement = Util.substituteVariable(val);
485:                assertEquals(val, replacement); // no substitution
486:
487:                val = "my name is ${user.name}";
488:                replacement = Util.substituteVariable(val);
489:                assertNotSame(val, replacement);
490:                assertFalse(val.equals(replacement));
491:
492:                val = "my name is ${user.name} and ${user.name}";
493:                replacement = Util.substituteVariable(val);
494:                assertFalse(val.equals(replacement));
495:                assertEquals(-1, replacement.indexOf("${"));
496:
497:                val = "my name is ${unknown.var:Bela Ban}";
498:                replacement = Util.substituteVariable(val);
499:                assertTrue(replacement.indexOf("Bela Ban") >= 0);
500:                assertEquals(-1, replacement.indexOf("${"));
501:
502:                val = "my name is ${unknown.var}";
503:                replacement = Util.substituteVariable(val);
504:                assertTrue(replacement.indexOf("${") >= 0);
505:
506:                val = "here is an invalid ${argument because it doesn't contains a closing bracket";
507:                try {
508:                    replacement = Util.substituteVariable(val);
509:                    fail("should be an IllegalArgumentException");
510:                } catch (Throwable t) {
511:                    assertEquals(IllegalArgumentException.class, t.getClass());
512:                }
513:            }
514:
515:            public static Test suite() {
516:                return new TestSuite(UtilTest.class);
517:            }
518:
519:            public static void main(String[] args) {
520:                junit.textui.TestRunner.run(suite());
521:            }
522:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.