Source Code Cross Referenced for EnumTest.java in  » Library » Apache-common-lang » org » apache » commons » lang » enums » 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 » Library » Apache common lang » org.apache.commons.lang.enums 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.lang.enums;
018:
019:        import java.lang.reflect.InvocationTargetException;
020:        import java.lang.reflect.Method;
021:        import java.net.URL;
022:        import java.net.URLClassLoader;
023:        import java.util.ArrayList;
024:        import java.util.HashMap;
025:        import java.util.Iterator;
026:        import java.util.List;
027:        import java.util.Map;
028:
029:        import junit.framework.AssertionFailedError;
030:        import junit.framework.Test;
031:        import junit.framework.TestCase;
032:        import junit.framework.TestSuite;
033:        import org.apache.commons.lang.SerializationUtils;
034:
035:        /**
036:         * Test cases for the {@link Enum} class.
037:         *
038:         * @author Stephen Colebourne
039:         * @author Gary D. Gregory
040:         * @version $Id: EnumTest.java 501606 2007-01-30 22:26:38Z bayard $
041:         */
042:
043:        public final class EnumTest extends TestCase {
044:
045:            private static final String ENUMS_CLASS_NAME = "org.apache.commons.lang.enums.ColorEnum";
046:
047:            public EnumTest(String name) {
048:                super (name);
049:            }
050:
051:            public void setUp() {
052:            }
053:
054:            public static Test suite() {
055:                TestSuite suite = new TestSuite(EnumTest.class);
056:                suite.setName("Enum Tests");
057:                return suite;
058:            }
059:
060:            public void testName() {
061:                assertEquals("Red", ColorEnum.RED.getName());
062:                assertEquals("Green", ColorEnum.GREEN.getName());
063:                assertEquals("Blue", ColorEnum.BLUE.getName());
064:            }
065:
066:            public void testCompareTo() {
067:                assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.BLUE) == 0);
068:                assertTrue(ColorEnum.RED.compareTo(ColorEnum.BLUE) > 0);
069:                assertTrue(ColorEnum.BLUE.compareTo(ColorEnum.RED) < 0);
070:                try {
071:                    ColorEnum.RED.compareTo(null);
072:                    fail();
073:                } catch (NullPointerException ex) {
074:                }
075:                try {
076:                    ColorEnum.RED.compareTo(new Object());
077:                    fail();
078:                } catch (ClassCastException ex) {
079:                }
080:            }
081:
082:            public void testEquals() {
083:                assertSame(ColorEnum.RED, ColorEnum.RED);
084:                assertSame(ColorEnum.getEnum("Red"), ColorEnum.RED);
085:                assertEquals(false, ColorEnum.RED.equals(null));
086:                assertEquals(true, ColorEnum.RED.equals(ColorEnum.RED));
087:                assertEquals(true, ColorEnum.RED.equals(ColorEnum
088:                        .getEnum("Red")));
089:            }
090:
091:            public void testHashCode() {
092:                assertEquals(ColorEnum.RED.hashCode(), ColorEnum.RED.hashCode());
093:                assertEquals(7 + ColorEnum.class.hashCode() + 3
094:                        * "Red".hashCode(), ColorEnum.RED.hashCode());
095:            }
096:
097:            public void testToString() {
098:                String toString = ColorEnum.RED.toString();
099:                assertEquals("ColorEnum[Red]", toString);
100:                assertSame(toString, ColorEnum.RED.toString());
101:            }
102:
103:            public void testIterator() {
104:                Iterator it = ColorEnum.iterator();
105:                assertSame(ColorEnum.RED, it.next());
106:                assertSame(ColorEnum.GREEN, it.next());
107:                assertSame(ColorEnum.BLUE, it.next());
108:            }
109:
110:            public void testList() {
111:                List list = new ArrayList(ColorEnum.getEnumList());
112:
113:                assertNotNull(list);
114:
115:                assertEquals(list.size(), ColorEnum.getEnumMap().keySet()
116:                        .size());
117:
118:                Iterator it = list.iterator();
119:                assertSame(ColorEnum.RED, it.next());
120:                assertSame(ColorEnum.GREEN, it.next());
121:                assertSame(ColorEnum.BLUE, it.next());
122:            }
123:
124:            public void testMap() {
125:                Map map = new HashMap(ColorEnum.getEnumMap());
126:
127:                assertNotNull(map);
128:                assertTrue(map.containsValue(ColorEnum.RED));
129:                assertTrue(map.containsValue(ColorEnum.GREEN));
130:                assertTrue(map.containsValue(ColorEnum.BLUE));
131:                assertSame(ColorEnum.RED, map.get("Red"));
132:                assertSame(ColorEnum.GREEN, map.get("Green"));
133:                assertSame(ColorEnum.BLUE, map.get("Blue"));
134:                assertEquals(map.keySet().size(), ColorEnum.getEnumList()
135:                        .size());
136:            }
137:
138:            public void testGet() {
139:                assertSame(ColorEnum.RED, ColorEnum.getEnum("Red"));
140:                assertSame(ColorEnum.GREEN, ColorEnum.getEnum("Green"));
141:                assertSame(ColorEnum.BLUE, ColorEnum.getEnum("Blue"));
142:                assertSame(null, ColorEnum.getEnum("Pink"));
143:            }
144:
145:            public void testSerialization() {
146:                int hashCode = ColorEnum.RED.hashCode();
147:                assertSame(ColorEnum.RED, SerializationUtils
148:                        .clone(ColorEnum.RED));
149:                assertEquals(hashCode, SerializationUtils.clone(ColorEnum.RED)
150:                        .hashCode());
151:                assertSame(ColorEnum.GREEN, SerializationUtils
152:                        .clone(ColorEnum.GREEN));
153:                assertSame(ColorEnum.BLUE, SerializationUtils
154:                        .clone(ColorEnum.BLUE));
155:            }
156:
157:            public void testBroken1() {
158:                try {
159:                    Broken1Enum.RED.getName();
160:                    fail();
161:                } catch (ExceptionInInitializerError ex) {
162:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
163:                }
164:            }
165:
166:            public void testBroken2() {
167:                try {
168:                    Broken2Enum.RED.getName();
169:                    fail();
170:                } catch (ExceptionInInitializerError ex) {
171:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
172:                }
173:            }
174:
175:            public void testBroken3() {
176:                try {
177:                    Broken3Enum.RED.getName();
178:                    fail();
179:                } catch (ExceptionInInitializerError ex) {
180:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
181:                }
182:            }
183:
184:            public void testBroken1Operation() {
185:                try {
186:                    Broken1OperationEnum.PLUS.getName();
187:                    fail();
188:                } catch (ExceptionInInitializerError ex) {
189:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
190:                }
191:            }
192:
193:            public void testBroken2Operation() {
194:                try {
195:                    Broken2OperationEnum.PLUS.getName();
196:                    fail();
197:                } catch (ExceptionInInitializerError ex) {
198:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
199:                }
200:            }
201:
202:            public void testBroken3Operation() {
203:                try {
204:                    Broken3OperationEnum.PLUS.getName();
205:                    fail();
206:                } catch (ExceptionInInitializerError ex) {
207:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
208:                }
209:            }
210:
211:            public void testBroken4Operation() {
212:                try {
213:                    Broken4OperationEnum.PLUS.getName();
214:                    fail();
215:                } catch (ExceptionInInitializerError ex) {
216:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
217:                }
218:            }
219:
220:            public void testBroken5Operation() {
221:                try {
222:                    Broken5OperationEnum.PLUS.getName();
223:                    fail();
224:                } catch (ExceptionInInitializerError ex) {
225:                    assertTrue(ex.getException() instanceof  IllegalArgumentException);
226:                }
227:            }
228:
229:            public void testOperationGet() {
230:                assertSame(OperationEnum.PLUS, OperationEnum.getEnum("Plus"));
231:                assertSame(OperationEnum.MINUS, OperationEnum.getEnum("Minus"));
232:                assertSame(null, OperationEnum.getEnum("Pink"));
233:            }
234:
235:            public void testOperationSerialization() {
236:                assertSame(OperationEnum.PLUS, SerializationUtils
237:                        .clone(OperationEnum.PLUS));
238:                assertSame(OperationEnum.MINUS, SerializationUtils
239:                        .clone(OperationEnum.MINUS));
240:            }
241:
242:            public void testOperationToString() {
243:                assertEquals("OperationEnum[Plus]", OperationEnum.PLUS
244:                        .toString());
245:            }
246:
247:            public void testOperationList() {
248:                List list = OperationEnum.getEnumList();
249:                assertNotNull(list);
250:                assertEquals(2, list.size());
251:                assertEquals(list.size(), OperationEnum.getEnumMap().keySet()
252:                        .size());
253:
254:                Iterator it = list.iterator();
255:                assertSame(OperationEnum.PLUS, it.next());
256:                assertSame(OperationEnum.MINUS, it.next());
257:            }
258:
259:            public void testOperationMap() {
260:                Map map = OperationEnum.getEnumMap();
261:                assertNotNull(map);
262:                assertEquals(map.keySet().size(), OperationEnum.getEnumList()
263:                        .size());
264:
265:                assertTrue(map.containsValue(OperationEnum.PLUS));
266:                assertTrue(map.containsValue(OperationEnum.MINUS));
267:                assertSame(OperationEnum.PLUS, map.get("Plus"));
268:                assertSame(OperationEnum.MINUS, map.get("Minus"));
269:            }
270:
271:            public void testOperationCalculation() {
272:                assertEquals(3, OperationEnum.PLUS.eval(1, 2));
273:                assertEquals(-1, OperationEnum.MINUS.eval(1, 2));
274:            }
275:
276:            //-----------------------------------------------------------------------
277:            public void testExtended1Get() {
278:                assertSame(Extended1Enum.ALPHA, Extended1Enum.getEnum("Alpha"));
279:                assertSame(Extended1Enum.BETA, Extended1Enum.getEnum("Beta"));
280:                assertSame(null, Extended1Enum.getEnum("Gamma"));
281:                assertSame(null, Extended1Enum.getEnum("Delta"));
282:            }
283:
284:            public void testExtended2Get() {
285:                assertSame(Extended1Enum.ALPHA, Extended2Enum.ALPHA);
286:                assertSame(Extended1Enum.BETA, Extended2Enum.BETA);
287:
288:                assertSame(Extended2Enum.ALPHA, Extended2Enum.getEnum("Alpha"));
289:                assertSame(Extended2Enum.BETA, Extended2Enum.getEnum("Beta"));
290:                assertSame(Extended2Enum.GAMMA, Extended2Enum.getEnum("Gamma"));
291:                assertSame(null, Extended2Enum.getEnum("Delta"));
292:            }
293:
294:            public void testExtended3Get() {
295:                assertSame(Extended2Enum.ALPHA, Extended3Enum.ALPHA);
296:                assertSame(Extended2Enum.BETA, Extended3Enum.BETA);
297:                assertSame(Extended2Enum.GAMMA, Extended3Enum.GAMMA);
298:
299:                assertSame(Extended3Enum.ALPHA, Extended3Enum.getEnum("Alpha"));
300:                assertSame(Extended3Enum.BETA, Extended3Enum.getEnum("Beta"));
301:                assertSame(Extended3Enum.GAMMA, Extended3Enum.getEnum("Gamma"));
302:                assertSame(Extended3Enum.DELTA, Extended3Enum.getEnum("Delta"));
303:            }
304:
305:            public void testExtendedSerialization() {
306:                assertSame(Extended1Enum.ALPHA, SerializationUtils
307:                        .clone(Extended1Enum.ALPHA));
308:                assertSame(Extended1Enum.BETA, SerializationUtils
309:                        .clone(Extended1Enum.BETA));
310:                assertSame(Extended2Enum.GAMMA, SerializationUtils
311:                        .clone(Extended2Enum.GAMMA));
312:                assertSame(Extended3Enum.DELTA, SerializationUtils
313:                        .clone(Extended3Enum.DELTA));
314:            }
315:
316:            public void testExtendedToString() {
317:                assertEquals("Extended1Enum[Alpha]", Extended1Enum.ALPHA
318:                        .toString());
319:                assertEquals("Extended1Enum[Beta]", Extended1Enum.BETA
320:                        .toString());
321:
322:                assertEquals("Extended1Enum[Alpha]", Extended2Enum.ALPHA
323:                        .toString());
324:                assertEquals("Extended1Enum[Beta]", Extended2Enum.BETA
325:                        .toString());
326:                assertEquals("Extended2Enum[Gamma]", Extended2Enum.GAMMA
327:                        .toString());
328:
329:                assertEquals("Extended1Enum[Alpha]", Extended3Enum.ALPHA
330:                        .toString());
331:                assertEquals("Extended1Enum[Beta]", Extended3Enum.BETA
332:                        .toString());
333:                assertEquals("Extended2Enum[Gamma]", Extended3Enum.GAMMA
334:                        .toString());
335:                assertEquals("Extended3Enum[Delta]", Extended3Enum.DELTA
336:                        .toString());
337:            }
338:
339:            public void testExtended1List() {
340:                List list = Extended1Enum.getEnumList();
341:                assertNotNull(list);
342:                assertEquals(2, list.size());
343:                assertEquals(list.size(), Extended1Enum.getEnumMap().keySet()
344:                        .size());
345:
346:                Iterator it = list.iterator();
347:                assertSame(Extended1Enum.ALPHA, it.next());
348:                assertSame(Extended1Enum.BETA, it.next());
349:            }
350:
351:            public void testExtended2List() {
352:                List list = Extended2Enum.getEnumList();
353:                assertNotNull(list);
354:                assertEquals(3, list.size());
355:                assertEquals(list.size(), Extended2Enum.getEnumMap().keySet()
356:                        .size());
357:
358:                Iterator it = list.iterator();
359:                assertSame(Extended2Enum.ALPHA, it.next());
360:                assertSame(Extended2Enum.BETA, it.next());
361:                assertSame(Extended2Enum.GAMMA, it.next());
362:            }
363:
364:            public void testExtended3List() {
365:                List list = Extended3Enum.getEnumList();
366:                assertNotNull(list);
367:                assertEquals(4, list.size());
368:                assertEquals(list.size(), Extended3Enum.getEnumMap().keySet()
369:                        .size());
370:
371:                Iterator it = list.iterator();
372:                assertSame(Extended3Enum.ALPHA, it.next());
373:                assertSame(Extended3Enum.BETA, it.next());
374:                assertSame(Extended3Enum.GAMMA, it.next());
375:                assertSame(Extended3Enum.DELTA, it.next());
376:            }
377:
378:            public void testExtended1Map() {
379:                Map map = Extended1Enum.getEnumMap();
380:                assertNotNull(map);
381:                assertEquals(map.keySet().size(), Extended1Enum.getEnumList()
382:                        .size());
383:
384:                assertTrue(map.containsValue(Extended1Enum.ALPHA));
385:                assertTrue(map.containsValue(Extended1Enum.BETA));
386:                assertSame(Extended1Enum.ALPHA, map.get("Alpha"));
387:                assertSame(Extended1Enum.BETA, map.get("Beta"));
388:            }
389:
390:            public void testExtended2Map() {
391:                Map map = Extended2Enum.getEnumMap();
392:                assertNotNull(map);
393:                assertEquals(map.keySet().size(), Extended2Enum.getEnumList()
394:                        .size());
395:
396:                assertTrue(map.containsValue(Extended2Enum.ALPHA));
397:                assertTrue(map.containsValue(Extended2Enum.BETA));
398:                assertTrue(map.containsValue(Extended2Enum.GAMMA));
399:                assertSame(Extended2Enum.ALPHA, map.get("Alpha"));
400:                assertSame(Extended2Enum.BETA, map.get("Beta"));
401:                assertSame(Extended2Enum.GAMMA, map.get("Gamma"));
402:            }
403:
404:            public void testExtended3Map() {
405:                Map map = Extended3Enum.getEnumMap();
406:                assertNotNull(map);
407:                assertEquals(map.keySet().size(), Extended3Enum.getEnumList()
408:                        .size());
409:
410:                assertTrue(map.containsValue(Extended3Enum.ALPHA));
411:                assertTrue(map.containsValue(Extended3Enum.BETA));
412:                assertTrue(map.containsValue(Extended3Enum.GAMMA));
413:                assertTrue(map.containsValue(Extended3Enum.DELTA));
414:                assertSame(Extended3Enum.ALPHA, map.get("Alpha"));
415:                assertSame(Extended3Enum.BETA, map.get("Beta"));
416:                assertSame(Extended3Enum.GAMMA, map.get("Gamma"));
417:                assertSame(Extended3Enum.DELTA, map.get("Delta"));
418:            }
419:
420:            //-----------------------------------------------------------------------
421:            public void testNested() {
422:                List list = new ArrayList(Nest.ColorEnum.getEnumList());
423:                assertEquals(3, list.size()); // all is well
424:                Iterator it = list.iterator();
425:                assertSame(Nest.ColorEnum.RED, it.next());
426:                assertSame(Nest.ColorEnum.GREEN, it.next());
427:                assertSame(Nest.ColorEnum.BLUE, it.next());
428:                // This nesting works because the enum constants are defined in the SAME
429:                // class as the getEnumList(). It just acts as a normal enum.
430:            }
431:
432:            public void testNestedBroken() {
433:                List list = new ArrayList(NestBroken.ColorEnum.getEnumList());
434:                try {
435:                    assertEquals(0, list.size()); // no enums!!! 
436:                    // this is BROKEN because the enum constants are defined in a DIFFERENT
437:                    // class from getEnumList(). Once NestBroken class is referenced,
438:                    // and thus class loaded with its enum constants, the getEnumList works:
439:                } catch (AssertionFailedError ex) {
440:                    // this actually works and isn't broken on Linux SunJDK1.4.1, so...
441:                    assertEquals(3, list.size());
442:                }
443:                new NestBroken();
444:                list = new ArrayList(NestBroken.ColorEnum.getEnumList());
445:                assertEquals(3, list.size()); // all is well!!!
446:                Iterator it = list.iterator();
447:                assertSame(NestBroken.RED, it.next());
448:                assertSame(NestBroken.GREEN, it.next());
449:                assertSame(NestBroken.BLUE, it.next());
450:            }
451:
452:            public void testNestedLinked() {
453:                List list = new ArrayList(NestLinked.ColorEnum.getEnumList());
454:                assertEquals(3, list.size()); // all is well
455:                Iterator it = list.iterator();
456:                assertSame(NestLinked.RED, it.next());
457:                assertSame(NestLinked.GREEN, it.next());
458:                assertSame(NestLinked.BLUE, it.next());
459:                // This nesting works because a static block in the enum class forces a
460:                // class load of the outer class which defines the enum constants.
461:            }
462:
463:            public void testNestedReferenced() {
464:                List list = new ArrayList(NestReferenced.ColorEnum
465:                        .getEnumList());
466:                assertEquals(3, list.size()); // all is well
467:                Iterator it = list.iterator();
468:                assertSame(NestReferenced.RED, it.next());
469:                assertSame(NestReferenced.GREEN, it.next());
470:                assertSame(NestReferenced.BLUE, it.next());
471:                // This nesting works because the enum constants are actually defined in
472:                // the SAME class as the getEnumList(). The references in the outer class
473:                // are just extra references.
474:            }
475:
476:            public void testColorEnumEqualsWithDifferentClassLoaders()
477:                    throws SecurityException, IllegalArgumentException,
478:                    ClassNotFoundException, NoSuchMethodException,
479:                    IllegalAccessException, InvocationTargetException {
480:                this .testWithDifferentClassLoaders(ColorEnum.BLUE);
481:                this .testWithDifferentClassLoaders(ColorEnum.GREEN);
482:                this .testWithDifferentClassLoaders(ColorEnum.RED);
483:            }
484:
485:            void testWithDifferentClassLoaders(ColorEnum colorEnum)
486:                    throws ClassNotFoundException, SecurityException,
487:                    NoSuchMethodException, IllegalArgumentException,
488:                    IllegalAccessException, InvocationTargetException {
489:                // Sanity checks:
490:                assertTrue(colorEnum.equals(colorEnum));
491:                assertNotNull(ColorEnum.class.getClassLoader());
492:                // set up:
493:                ClassLoader myClassLoader = EnumTest.class.getClassLoader();
494:                if (!(myClassLoader instanceof  URLClassLoader)) {
495:                    fail("EnumTest ClassLoader = "
496:                            + (myClassLoader == null ? null : myClassLoader
497:                                    .getClass().getName()));
498:                }
499:                ClassLoader classLoader = URLClassLoader.newInstance(
500:                        ((URLClassLoader) myClassLoader).getURLs(), null);
501:                Object enumObjectFromOtherClassLoader = this .getColorEnum(
502:                        classLoader, colorEnum.getName());
503:
504:                // the real test, part 1.
505:                try {
506:                    ColorEnum testCase = (ColorEnum) enumObjectFromOtherClassLoader;
507:                    fail("Should have thrown a ClassCastException for "
508:                            + testCase);
509:                } catch (ClassCastException e) {
510:                    // normal.
511:                }
512:
513:                // the real test, part 2.
514:                assertEquals(
515:                        "The two objects should match even though they are from different class loaders",
516:                        colorEnum, enumObjectFromOtherClassLoader);
517:
518:                // the real test, part 3 - testing equals(Object)
519:                int falseCount = 0;
520:                for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
521:                    ColorEnum element = (ColorEnum) iter.next();
522:                    if (!colorEnum.equals(element)) {
523:                        falseCount++;
524:                        assertFalse(enumObjectFromOtherClassLoader
525:                                .equals(element));
526:                    }
527:                }
528:                assertEquals(ColorEnum.getEnumList().size() - 1, falseCount);
529:
530:                // the real test, part 4 - testing compareTo(Object) == 0
531:                falseCount = 0;
532:                for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
533:                    ColorEnum element = (ColorEnum) iter.next();
534:                    if (!colorEnum.equals(element)) {
535:                        falseCount++;
536:                        assertFalse(((Comparable) enumObjectFromOtherClassLoader)
537:                                .compareTo(element) == 0);
538:                    }
539:                }
540:                assertEquals(ColorEnum.getEnumList().size() - 1, falseCount);
541:            }
542:
543:            Object getColorEnum(ClassLoader classLoader, String color)
544:                    throws ClassNotFoundException, SecurityException,
545:                    NoSuchMethodException, IllegalArgumentException,
546:                    IllegalAccessException, InvocationTargetException {
547:                // Sanity check:
548:                ColorEnum.RED.equals(ColorEnum.RED);
549:                assertNotNull(ColorEnum.class.getClassLoader());
550:                // set up:
551:                assertNotNull(classLoader);
552:                assertFalse(classLoader
553:                        .equals(ColorEnum.class.getClassLoader()));
554:                Class otherColorEnumClass = null;
555:                try {
556:                    otherColorEnumClass = classLoader
557:                            .loadClass(ENUMS_CLASS_NAME);
558:                } catch (ClassNotFoundException e) {
559:                    // Dump some information to help debug class loader issues under different JREs, Ant, Eclipse.
560:                    System.err.println("Could not load " + ENUMS_CLASS_NAME
561:                            + " from the class loader " + classLoader);
562:                    URLClassLoader urlCl = (URLClassLoader) classLoader;
563:                    URL[] urls = urlCl.getURLs();
564:                    System.err.println("Class loader has " + urls.length
565:                            + " URLs:");
566:                    for (int i = 0; i < urls.length; i++) {
567:                        System.err.println("URL[" + i + "] = " + urls[i]);
568:                    }
569:                    e.printStackTrace();
570:                    throw e;
571:                }
572:                assertNotNull(otherColorEnumClass);
573:                assertNotNull(otherColorEnumClass.getClassLoader());
574:                assertTrue(classLoader.equals(otherColorEnumClass
575:                        .getClassLoader()));
576:                assertFalse(otherColorEnumClass.getClassLoader().equals(
577:                        ColorEnum.class.getClassLoader()));
578:                Method method = otherColorEnumClass.getMethod("getEnum",
579:                        new Class[] { String.class });
580:                Object enumObject = method.invoke(otherColorEnumClass,
581:                        new Object[] { color });
582:                assertNotNull(enumObject);
583:                assertFalse(ColorEnum.class.equals(enumObject.getClass()));
584:                assertFalse(ColorEnum.class == enumObject.getClass());
585:                return enumObject;
586:            }
587:
588:            public void testEqualsToWrongInstance() {
589:                for (Iterator iter = ColorEnum.iterator(); iter.hasNext();) {
590:                    ColorEnum element = (ColorEnum) iter.next();
591:                    this .testEqualsToWrongInstance(element);
592:                }
593:            }
594:
595:            void testEqualsToWrongInstance(ColorEnum colorEnum) {
596:                assertEquals(false, colorEnum.equals("test"));
597:                assertEquals(false, colorEnum.equals(new Integer(1)));
598:                assertEquals(false, colorEnum.equals(new Boolean(true)));
599:                assertEquals(false, colorEnum.equals(new StringBuffer("test")));
600:                assertEquals(false, colorEnum.equals(new Object()));
601:                assertEquals(false, colorEnum.equals(null));
602:                assertEquals(false, colorEnum.equals(""));
603:                assertEquals(false, colorEnum.equals(ColorEnum.getEnum(null)));
604:                assertEquals(false, colorEnum.equals(ColorEnum.getEnum("")));
605:                assertEquals(false, colorEnum.equals(ColorEnum
606:                        .getEnum("This ColorEnum does not exist.")));
607:            }
608:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.