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: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Map;
023:
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import org.apache.commons.lang.SerializationUtils;
028:
029: /**
030: * Test cases for the {@link Enum} class.
031: *
032: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
033: * @version $Id: ValuedEnumTest.java 437554 2006-08-28 06:21:41Z bayard $
034: */
035:
036: public final class ValuedEnumTest extends TestCase {
037:
038: public ValuedEnumTest(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(ValuedEnumTest.class);
047: suite.setName("ValuedEnum Tests");
048: return suite;
049: }
050:
051: public void testName() {
052: assertEquals("Red", ValuedColorEnum.RED.getName());
053: assertEquals("Green", ValuedColorEnum.GREEN.getName());
054: assertEquals("Blue", ValuedColorEnum.BLUE.getName());
055: }
056:
057: public void testValue() {
058: assertEquals(1, ValuedColorEnum.RED.getValue());
059: assertEquals(2, ValuedColorEnum.GREEN.getValue());
060: assertEquals(3, ValuedColorEnum.BLUE.getValue());
061: }
062:
063: public void testCompareTo() {
064: assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.BLUE) == 0);
065: assertTrue(ValuedColorEnum.RED.compareTo(ValuedColorEnum.BLUE) < 0);
066: assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.RED) > 0);
067: }
068:
069: public void testCompareTo_classloader_equal() throws Exception {
070: ClassLoader cl = ValuedColorEnum.class.getClassLoader();
071: if (cl instanceof URLClassLoader) {
072: URLClassLoader urlCL = (URLClassLoader) cl;
073: URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
074: null);
075: URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
076: null);
077: Class otherEnumClass1 = urlCL1
078: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
079: Class otherEnumClass2 = urlCL2
080: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
081: Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
082: .get(null);
083: Object blue2 = otherEnumClass2.getDeclaredField("BLUE")
084: .get(null);
085: assertTrue(((Comparable) blue1).compareTo(blue2) == 0);
086: }
087: }
088:
089: public void testCompareTo_classloader_different() throws Exception {
090: ClassLoader cl = ValuedColorEnum.class.getClassLoader();
091: if (cl instanceof URLClassLoader) {
092: URLClassLoader urlCL = (URLClassLoader) cl;
093: URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
094: null);
095: URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
096: null);
097: Class otherEnumClass1 = urlCL1
098: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
099: Class otherEnumClass2 = urlCL2
100: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
101: Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
102: .get(null);
103: Object blue2 = otherEnumClass2.getDeclaredField("RED").get(
104: null);
105: assertTrue(((Comparable) blue1).compareTo(blue2) != 0);
106: }
107: }
108:
109: public void testCompareTo_nonEnumType() {
110: try {
111: ValuedColorEnum.BLUE.compareTo(new TotallyUnrelatedClass(
112: ValuedColorEnum.BLUE.getValue()));
113: fail();
114: } catch (ClassCastException ex) {
115: // expected
116: }
117: }
118:
119: public void testCompareTo_otherEnumType() {
120: try {
121: ValuedColorEnum.BLUE.compareTo(ValuedLanguageEnum.ENGLISH);
122: fail();
123: } catch (ClassCastException ex) {
124: // expected
125: }
126: }
127:
128: public void testCompareTo_otherType() {
129: try {
130: ValuedColorEnum.BLUE.compareTo("Blue");
131: fail();
132: } catch (ClassCastException ex) {
133: // expected
134: }
135: }
136:
137: public void testCompareTo_null() {
138: try {
139: ValuedColorEnum.BLUE.compareTo(null);
140: fail();
141: } catch (NullPointerException ex) {
142: // expected
143: }
144: }
145:
146: public void testEquals() {
147: assertSame(ValuedColorEnum.RED, ValuedColorEnum.RED);
148: assertSame(ValuedColorEnum.getEnum("Red"), ValuedColorEnum.RED);
149: }
150:
151: public void testEquals_classloader_equal() throws Exception {
152: ClassLoader cl = ValuedColorEnum.class.getClassLoader();
153: if (cl instanceof URLClassLoader) {
154: URLClassLoader urlCL = (URLClassLoader) cl;
155: URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
156: null);
157: URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
158: null);
159: Class otherEnumClass1 = urlCL1
160: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
161: Class otherEnumClass2 = urlCL2
162: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
163: Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
164: .get(null);
165: Object blue2 = otherEnumClass2.getDeclaredField("BLUE")
166: .get(null);
167: assertEquals(true, blue1.equals(blue2));
168: }
169: }
170:
171: public void testEquals_classloader_different() throws Exception {
172: ClassLoader cl = ValuedColorEnum.class.getClassLoader();
173: if (cl instanceof URLClassLoader) {
174: URLClassLoader urlCL = (URLClassLoader) cl;
175: URLClassLoader urlCL1 = new URLClassLoader(urlCL.getURLs(),
176: null);
177: URLClassLoader urlCL2 = new URLClassLoader(urlCL.getURLs(),
178: null);
179: Class otherEnumClass1 = urlCL1
180: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
181: Class otherEnumClass2 = urlCL2
182: .loadClass("org.apache.commons.lang.enums.ValuedColorEnum");
183: Object blue1 = otherEnumClass1.getDeclaredField("BLUE")
184: .get(null);
185: Object blue2 = otherEnumClass2.getDeclaredField("RED").get(
186: null);
187: assertEquals(false, blue1.equals(blue2));
188: }
189: }
190:
191: public void testToString() {
192: String toString = ValuedColorEnum.RED.toString();
193: assertEquals("ValuedColorEnum[Red=1]", toString);
194: assertSame(toString, ValuedColorEnum.RED.toString());
195: }
196:
197: public void testIterator() {
198: Iterator it = ValuedColorEnum.iterator();
199: assertSame(ValuedColorEnum.RED, it.next());
200: assertSame(ValuedColorEnum.GREEN, it.next());
201: assertSame(ValuedColorEnum.BLUE, it.next());
202: }
203:
204: public void testList() {
205: List list = ValuedColorEnum.getEnumList();
206:
207: assertNotNull(list);
208:
209: assertEquals(list.size(), ValuedColorEnum.getEnumMap().keySet()
210: .size());
211:
212: Iterator it = list.iterator();
213: assertSame(ValuedColorEnum.RED, it.next());
214: assertSame(ValuedColorEnum.GREEN, it.next());
215: assertSame(ValuedColorEnum.BLUE, it.next());
216: }
217:
218: public void testMap() {
219: Map map = ValuedColorEnum.getEnumMap();
220:
221: assertNotNull(map);
222:
223: assertEquals(map.keySet().size(), ValuedColorEnum.getEnumList()
224: .size());
225:
226: assertTrue(map.containsValue(ValuedColorEnum.RED));
227: assertTrue(map.containsValue(ValuedColorEnum.GREEN));
228: assertTrue(map.containsValue(ValuedColorEnum.BLUE));
229: assertSame(ValuedColorEnum.RED, map.get("Red"));
230: assertSame(ValuedColorEnum.GREEN, map.get("Green"));
231: assertSame(ValuedColorEnum.BLUE, map.get("Blue"));
232: }
233:
234: public void testGet() {
235: assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum("Red"));
236: assertSame(ValuedColorEnum.GREEN, ValuedColorEnum
237: .getEnum("Green"));
238: assertSame(ValuedColorEnum.BLUE, ValuedColorEnum
239: .getEnum("Blue"));
240: assertSame(null, ValuedColorEnum.getEnum("Pink"));
241: }
242:
243: public void testGetValue() {
244: assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum(1));
245: assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum(2));
246: assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum(3));
247: assertSame(null, ValuedColorEnum.getEnum(4));
248: }
249:
250: public void testSerialization() {
251: assertSame(ValuedColorEnum.RED, SerializationUtils
252: .clone(ValuedColorEnum.RED));
253: assertSame(ValuedColorEnum.GREEN, SerializationUtils
254: .clone(ValuedColorEnum.GREEN));
255: assertSame(ValuedColorEnum.BLUE, SerializationUtils
256: .clone(ValuedColorEnum.BLUE));
257: }
258:
259: //-----------------------------------------------------------------------s
260: static class TotallyUnrelatedClass {
261: private final int value;
262:
263: public TotallyUnrelatedClass(final int value) {
264: this .value = value;
265: }
266:
267: public int getValue() {
268: return value;
269: }
270: }
271:
272: }
|