Source Code Cross Referenced for EnumEqualsTest.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.net.URLClassLoader;
020:
021:        import junit.framework.Test;
022:        import junit.framework.TestCase;
023:        import junit.framework.TestSuite;
024:
025:        /**
026:         * Test cases for the {@link Enum} class equals method.
027:         *
028:         * @author Matthias Eichel
029:         * @author Stephen Colebourne
030:         * @version $Id: EnumEqualsTest.java 437554 2006-08-28 06:21:41Z bayard $
031:         */
032:        public final class EnumEqualsTest extends TestCase {
033:
034:            public EnumEqualsTest(String name) {
035:                super (name);
036:            }
037:
038:            public void setUp() {
039:            }
040:
041:            public static Test suite() {
042:                TestSuite suite = new TestSuite(EnumEqualsTest.class);
043:                suite.setName("Enum equals Tests");
044:                return suite;
045:            }
046:
047:            //-----------------------------------------------------------------------
048:            static final class CarColorEnum extends Enum {
049:                public static final CarColorEnum BLACK = new CarColorEnum(
050:                        "black");
051:                public static final CarColorEnum BROWN = new CarColorEnum(
052:                        "brown");
053:                public static final CarColorEnum YELLOW = new CarColorEnum(
054:                        "yellow");
055:                public static final CarColorEnum BLUE = new CarColorEnum("blue");
056:                public static final CarColorEnum RED = new CarColorEnum("red");
057:
058:                private CarColorEnum(String enumAsString) {
059:                    super (enumAsString);
060:                }
061:            }
062:
063:            static final class TrafficlightColorEnum extends Enum {
064:                public static final TrafficlightColorEnum RED = new TrafficlightColorEnum(
065:                        "red");
066:                public static final TrafficlightColorEnum YELLOW = new TrafficlightColorEnum(
067:                        "yellow");
068:                public static final TrafficlightColorEnum GREEN = new TrafficlightColorEnum(
069:                        "green");
070:
071:                private TrafficlightColorEnum(String enumAsString) {
072:                    super (enumAsString);
073:                }
074:            }
075:
076:            static class TotallyUnrelatedClass {
077:                private final String name;
078:
079:                public TotallyUnrelatedClass(final String name) {
080:                    this .name = name;
081:                }
082:
083:                public String getName() {
084:                    return name;
085:                }
086:            }
087:
088:            //-----------------------------------------------------------------------
089:            public void testEquals() {
090:                assertEquals(false, CarColorEnum.RED
091:                        .equals(TrafficlightColorEnum.RED));
092:                assertEquals(false, CarColorEnum.YELLOW
093:                        .equals(TrafficlightColorEnum.YELLOW));
094:
095:                assertEquals(false, TrafficlightColorEnum.RED
096:                        .equals(new TotallyUnrelatedClass("red")));
097:                assertEquals(false, CarColorEnum.RED
098:                        .equals(new TotallyUnrelatedClass("red")));
099:
100:                assertEquals(false, TrafficlightColorEnum.RED
101:                        .equals(new TotallyUnrelatedClass("some")));
102:                assertEquals(false, CarColorEnum.RED
103:                        .equals(new TotallyUnrelatedClass("some")));
104:            }
105:
106:            public void testEquals_classloader_equal() throws Exception {
107:                ClassLoader cl = ColorEnum.class.getClassLoader();
108:                if (cl instanceof  URLClassLoader) {
109:                    URLClassLoader urlCL = (URLClassLoader) cl;
110:                    URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
111:                            null);
112:                    URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
113:                            null);
114:                    Class otherEnumClass1 = urlCL1
115:                            .loadClass("org.apache.commons.lang.enums.ColorEnum");
116:                    Class otherEnumClass2 = urlCL2
117:                            .loadClass("org.apache.commons.lang.enums.ColorEnum");
118:                    Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
119:                            .get(null);
120:                    Object blue2 = otherEnumClass2.getDeclaredField("BLUE")
121:                            .get(null);
122:                    assertEquals(true, blue1.equals(blue2));
123:                }
124:            }
125:
126:            public void testEquals_classloader_different() throws Exception {
127:                ClassLoader cl = ColorEnum.class.getClassLoader();
128:                if (cl instanceof  URLClassLoader) {
129:                    URLClassLoader urlCL = (URLClassLoader) cl;
130:                    URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
131:                            null);
132:                    URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
133:                            null);
134:                    Class otherEnumClass1 = urlCL1
135:                            .loadClass("org.apache.commons.lang.enums.ColorEnum");
136:                    Class otherEnumClass2 = urlCL2
137:                            .loadClass("org.apache.commons.lang.enums.ColorEnum");
138:                    Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
139:                            .get(null);
140:                    Object blue2 = otherEnumClass2.getDeclaredField("RED").get(
141:                            null);
142:                    assertEquals(false, blue1.equals(blue2));
143:                }
144:            }
145:
146:            //-----------------------------------------------------------------------
147:            public void testCompareTo() {
148:                try {
149:                    CarColorEnum.RED.compareTo(TrafficlightColorEnum.RED);
150:                    fail();
151:                } catch (ClassCastException ex) {
152:                }
153:                try {
154:                    CarColorEnum.YELLOW.compareTo(TrafficlightColorEnum.YELLOW);
155:                    fail();
156:                } catch (ClassCastException ex) {
157:                }
158:                try {
159:                    TrafficlightColorEnum.RED
160:                            .compareTo(new TotallyUnrelatedClass("red"));
161:                    fail();
162:                } catch (ClassCastException ex) {
163:                }
164:                try {
165:                    CarColorEnum.RED
166:                            .compareTo(new TotallyUnrelatedClass("red"));
167:                    fail();
168:                } catch (ClassCastException ex) {
169:                }
170:                try {
171:                    TrafficlightColorEnum.RED
172:                            .compareTo(new TotallyUnrelatedClass("some"));
173:                    fail();
174:                } catch (ClassCastException ex) {
175:                }
176:                try {
177:                    CarColorEnum.RED
178:                            .compareTo(new TotallyUnrelatedClass("some"));
179:                    fail();
180:                } catch (ClassCastException ex) {
181:                }
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.