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.Constructor;
020: import java.lang.reflect.Modifier;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
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: EnumUtilsTest.java 437554 2006-08-28 06:21:41Z bayard $
034: */
035:
036: public final class EnumUtilsTest extends TestCase {
037:
038: public EnumUtilsTest(String name) {
039: super (name);
040: }
041:
042: public void setUp() {
043: }
044:
045: public static Test suite() {
046: TestSuite suite = new TestSuite(EnumUtilsTest.class);
047: suite.setName("EnumUtils Tests");
048: return suite;
049: }
050:
051: //-----------------------------------------------------------------------
052: public void testConstructor() {
053: assertNotNull(new EnumUtils());
054: Constructor[] cons = EnumUtils.class.getDeclaredConstructors();
055: assertEquals(1, cons.length);
056: assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
057: assertEquals(true, Modifier.isPublic(EnumUtils.class
058: .getModifiers()));
059: assertEquals(false, Modifier.isFinal(EnumUtils.class
060: .getModifiers()));
061: }
062:
063: //-----------------------------------------------------------------------
064: public void testIterator() {
065: Iterator it = EnumUtils.iterator(ColorEnum.class);
066: assertSame(ColorEnum.RED, it.next());
067: assertSame(ColorEnum.GREEN, it.next());
068: assertSame(ColorEnum.BLUE, it.next());
069: it = EnumUtils.iterator(DummyEnum.class);
070: assertEquals(false, it.hasNext());
071: }
072:
073: public void testIteratorEx() {
074: try {
075: EnumUtils.iterator(null);
076: fail();
077: } catch (IllegalArgumentException ex) {
078: }
079: try {
080: EnumUtils.iterator(Object.class);
081: fail();
082: } catch (IllegalArgumentException ex) {
083: }
084: }
085:
086: //-----------------------------------------------------------------------
087: public void testList() {
088: List list = EnumUtils.getEnumList(ColorEnum.class);
089: Iterator it = list.iterator();
090: assertSame(ColorEnum.RED, it.next());
091: assertSame(ColorEnum.GREEN, it.next());
092: assertSame(ColorEnum.BLUE, it.next());
093: list = EnumUtils.getEnumList(DummyEnum.class);
094: assertEquals(0, list.size());
095: }
096:
097: public void testListEx() {
098: try {
099: EnumUtils.getEnumList(null);
100: fail();
101: } catch (IllegalArgumentException ex) {
102: }
103: try {
104: EnumUtils.getEnumList(Object.class);
105: fail();
106: } catch (IllegalArgumentException ex) {
107: }
108: }
109:
110: //-----------------------------------------------------------------------
111: public void testMap() {
112: Map map = EnumUtils.getEnumMap(ColorEnum.class);
113: assertTrue(map.containsValue(ColorEnum.RED));
114: assertTrue(map.containsValue(ColorEnum.GREEN));
115: assertTrue(map.containsValue(ColorEnum.BLUE));
116: assertSame(ColorEnum.RED, map.get("Red"));
117: assertSame(ColorEnum.GREEN, map.get("Green"));
118: assertSame(ColorEnum.BLUE, map.get("Blue"));
119: map = EnumUtils.getEnumMap(DummyEnum.class);
120: assertEquals(0, map.size());
121: }
122:
123: public void testMapEx() {
124: try {
125: EnumUtils.getEnumMap(null);
126: fail();
127: } catch (IllegalArgumentException ex) {
128: }
129: try {
130: EnumUtils.getEnumMap(Object.class);
131: fail();
132: } catch (IllegalArgumentException ex) {
133: }
134: }
135:
136: //-----------------------------------------------------------------------
137: public void testGet() {
138: assertSame(ColorEnum.RED, EnumUtils.getEnum(ColorEnum.class,
139: "Red"));
140: assertSame(ColorEnum.GREEN, EnumUtils.getEnum(ColorEnum.class,
141: "Green"));
142: assertSame(ColorEnum.BLUE, EnumUtils.getEnum(ColorEnum.class,
143: "Blue"));
144: assertSame(null, EnumUtils.getEnum(ColorEnum.class, "Pink"));
145: assertSame(null, EnumUtils.getEnum(DummyEnum.class, "Pink"));
146: }
147:
148: public void testGetEx() {
149: try {
150: EnumUtils.getEnum(null, "");
151: fail();
152: } catch (IllegalArgumentException ex) {
153: }
154: try {
155: EnumUtils.getEnum(Object.class, "Red");
156: fail();
157: } catch (IllegalArgumentException ex) {
158: }
159: }
160:
161: //-----------------------------------------------------------------------
162: public void testGetValue() {
163: assertSame(ValuedColorEnum.RED, EnumUtils.getEnum(
164: ValuedColorEnum.class, 1));
165: assertSame(ValuedColorEnum.GREEN, EnumUtils.getEnum(
166: ValuedColorEnum.class, 2));
167: assertSame(ValuedColorEnum.BLUE, EnumUtils.getEnum(
168: ValuedColorEnum.class, 3));
169: assertSame(null, EnumUtils.getEnum(ValuedColorEnum.class, 4));
170: assertSame(null, EnumUtils.getEnum(DummyEnum.class, 5));
171: }
172:
173: public void testGetValueEx() {
174: try {
175: EnumUtils.getEnum(null, 0);
176: fail();
177: } catch (IllegalArgumentException ex) {
178: }
179: try {
180: EnumUtils.getEnum(Object.class, 2);
181: fail();
182: } catch (IllegalArgumentException ex) {
183: }
184: }
185:
186: }
|