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: }
|