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.enum;
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.getModifiers()));
058: assertEquals(false, Modifier.isFinal(EnumUtils.class.getModifiers()));
059: }
060:
061: //-----------------------------------------------------------------------
062: public void testIterator() {
063: Iterator it = EnumUtils.iterator(ColorEnum.class);
064: assertSame(ColorEnum.RED, it.next());
065: assertSame(ColorEnum.GREEN, it.next());
066: assertSame(ColorEnum.BLUE, it.next());
067: it = EnumUtils.iterator(DummyEnum.class);
068: assertEquals(false, it.hasNext());
069: }
070:
071: public void testIteratorEx() {
072: try {
073: EnumUtils.iterator(null);
074: fail();
075: } catch (IllegalArgumentException ex) {}
076: try {
077: EnumUtils.iterator(Object.class);
078: fail();
079: } catch (IllegalArgumentException ex) {}
080: }
081:
082: //-----------------------------------------------------------------------
083: public void testList() {
084: List list = EnumUtils.getEnumList(ColorEnum.class);
085: Iterator it = list.iterator();
086: assertSame(ColorEnum.RED, it.next());
087: assertSame(ColorEnum.GREEN, it.next());
088: assertSame(ColorEnum.BLUE, it.next());
089: list = EnumUtils.getEnumList(DummyEnum.class);
090: assertEquals(0, list.size());
091: }
092:
093: public void testListEx() {
094: try {
095: EnumUtils.getEnumList(null);
096: fail();
097: } catch (IllegalArgumentException ex) {}
098: try {
099: EnumUtils.getEnumList(Object.class);
100: fail();
101: } catch (IllegalArgumentException ex) {}
102: }
103:
104: //-----------------------------------------------------------------------
105: public void testMap() {
106: Map map = EnumUtils.getEnumMap(ColorEnum.class);
107: assertTrue(map.containsValue(ColorEnum.RED));
108: assertTrue(map.containsValue(ColorEnum.GREEN));
109: assertTrue(map.containsValue(ColorEnum.BLUE));
110: assertSame(ColorEnum.RED, map.get("Red"));
111: assertSame(ColorEnum.GREEN, map.get("Green"));
112: assertSame(ColorEnum.BLUE, map.get("Blue"));
113: map = EnumUtils.getEnumMap(DummyEnum.class);
114: assertEquals(0, map.size());
115: }
116:
117: public void testMapEx() {
118: try {
119: EnumUtils.getEnumMap(null);
120: fail();
121: } catch (IllegalArgumentException ex) {}
122: try {
123: EnumUtils.getEnumMap(Object.class);
124: fail();
125: } catch (IllegalArgumentException ex) {}
126: }
127:
128: //-----------------------------------------------------------------------
129: public void testGet() {
130: assertSame(ColorEnum.RED, EnumUtils.getEnum(ColorEnum.class, "Red"));
131: assertSame(ColorEnum.GREEN, EnumUtils.getEnum(ColorEnum.class, "Green"));
132: assertSame(ColorEnum.BLUE, EnumUtils.getEnum(ColorEnum.class, "Blue"));
133: assertSame(null, EnumUtils.getEnum(ColorEnum.class, "Pink"));
134: assertSame(null, EnumUtils.getEnum(DummyEnum.class, "Pink"));
135: }
136:
137: public void testGetEx() {
138: try {
139: EnumUtils.getEnum(null, "");
140: fail();
141: } catch (IllegalArgumentException ex) {}
142: try {
143: EnumUtils.getEnum(Object.class, "Red");
144: fail();
145: } catch (IllegalArgumentException ex) {}
146: }
147:
148: //-----------------------------------------------------------------------
149: public void testGetValue() {
150: assertSame(ValuedColorEnum.RED, EnumUtils.getEnum(ValuedColorEnum.class, 1));
151: assertSame(ValuedColorEnum.GREEN, EnumUtils.getEnum(ValuedColorEnum.class, 2));
152: assertSame(ValuedColorEnum.BLUE, EnumUtils.getEnum(ValuedColorEnum.class, 3));
153: assertSame(null, EnumUtils.getEnum(ValuedColorEnum.class, 4));
154: assertSame(null, EnumUtils.getEnum(DummyEnum.class, 5));
155: }
156:
157: public void testGetValueEx() {
158: try {
159: EnumUtils.getEnum(null, 0);
160: fail();
161: } catch (IllegalArgumentException ex) {}
162: try {
163: EnumUtils.getEnum(Object.class, 2);
164: fail();
165: } catch (IllegalArgumentException ex) {}
166: }
167:
168:}
|