KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > enum > EnumUtilsTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.enum;
17
18 import java.lang.reflect.Constructor;
19 import java.lang.reflect.Modifier;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 /**
29  * Test cases for the {@link Enum} class.
30  *
31  * @author <a HREF="mailto:scolebourne@joda.org">Stephen Colebourne</a>
32  * @version $Id: EnumUtilsTest.java 161244 2005-04-14 06:16:36Z ggregory $
33  */

34
35 public final class EnumUtilsTest extends TestCase {
36
37     public EnumUtilsTest(String name) {
38         super(name);
39     }
40
41     public void setUp() {
42     }
43
44     public static Test suite() {
45         TestSuite suite = new TestSuite(EnumUtilsTest.class);
46         suite.setName("EnumUtils Tests");
47         return suite;
48     }
49
50     //-----------------------------------------------------------------------
51
public void testConstructor() {
52         assertNotNull(new EnumUtils());
53         Constructor[] cons = EnumUtils.class.getDeclaredConstructors();
54         assertEquals(1, cons.length);
55         assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
56         assertEquals(true, Modifier.isPublic(EnumUtils.class.getModifiers()));
57         assertEquals(false, Modifier.isFinal(EnumUtils.class.getModifiers()));
58     }
59     
60     //-----------------------------------------------------------------------
61
public void testIterator() {
62         Iterator it = EnumUtils.iterator(ColorEnum.class);
63         assertSame(ColorEnum.RED, it.next());
64         assertSame(ColorEnum.GREEN, it.next());
65         assertSame(ColorEnum.BLUE, it.next());
66         it = EnumUtils.iterator(DummyEnum.class);
67         assertEquals(false, it.hasNext());
68     }
69
70     public void testIteratorEx() {
71         try {
72             EnumUtils.iterator(null);
73             fail();
74         } catch (IllegalArgumentException ex) {}
75         try {
76             EnumUtils.iterator(Object.class);
77             fail();
78         } catch (IllegalArgumentException ex) {}
79     }
80
81     //-----------------------------------------------------------------------
82
public void testList() {
83         List list = EnumUtils.getEnumList(ColorEnum.class);
84         Iterator it = list.iterator();
85         assertSame(ColorEnum.RED, it.next());
86         assertSame(ColorEnum.GREEN, it.next());
87         assertSame(ColorEnum.BLUE, it.next());
88         list = EnumUtils.getEnumList(DummyEnum.class);
89         assertEquals(0, list.size());
90     }
91
92     public void testListEx() {
93         try {
94             EnumUtils.getEnumList(null);
95             fail();
96         } catch (IllegalArgumentException ex) {}
97         try {
98             EnumUtils.getEnumList(Object.class);
99             fail();
100         } catch (IllegalArgumentException ex) {}
101     }
102
103     //-----------------------------------------------------------------------
104
public void testMap() {
105         Map map = EnumUtils.getEnumMap(ColorEnum.class);
106         assertTrue(map.containsValue(ColorEnum.RED));
107         assertTrue(map.containsValue(ColorEnum.GREEN));
108         assertTrue(map.containsValue(ColorEnum.BLUE));
109         assertSame(ColorEnum.RED, map.get("Red"));
110         assertSame(ColorEnum.GREEN, map.get("Green"));
111         assertSame(ColorEnum.BLUE, map.get("Blue"));
112         map = EnumUtils.getEnumMap(DummyEnum.class);
113         assertEquals(0, map.size());
114     }
115
116     public void testMapEx() {
117         try {
118             EnumUtils.getEnumMap(null);
119             fail();
120         } catch (IllegalArgumentException ex) {}
121         try {
122             EnumUtils.getEnumMap(Object.class);
123             fail();
124         } catch (IllegalArgumentException ex) {}
125     }
126
127     //-----------------------------------------------------------------------
128
public void testGet() {
129         assertSame(ColorEnum.RED, EnumUtils.getEnum(ColorEnum.class, "Red"));
130         assertSame(ColorEnum.GREEN, EnumUtils.getEnum(ColorEnum.class, "Green"));
131         assertSame(ColorEnum.BLUE, EnumUtils.getEnum(ColorEnum.class, "Blue"));
132         assertSame(null, EnumUtils.getEnum(ColorEnum.class, "Pink"));
133         assertSame(null, EnumUtils.getEnum(DummyEnum.class, "Pink"));
134     }
135
136     public void testGetEx() {
137         try {
138             EnumUtils.getEnum(null, "");
139             fail();
140         } catch (IllegalArgumentException ex) {}
141         try {
142             EnumUtils.getEnum(Object.class, "Red");
143             fail();
144         } catch (IllegalArgumentException ex) {}
145     }
146
147     //-----------------------------------------------------------------------
148
public void testGetValue() {
149         assertSame(ValuedColorEnum.RED, EnumUtils.getEnum(ValuedColorEnum.class, 1));
150         assertSame(ValuedColorEnum.GREEN, EnumUtils.getEnum(ValuedColorEnum.class, 2));
151         assertSame(ValuedColorEnum.BLUE, EnumUtils.getEnum(ValuedColorEnum.class, 3));
152         assertSame(null, EnumUtils.getEnum(ValuedColorEnum.class, 4));
153         assertSame(null, EnumUtils.getEnum(DummyEnum.class, 5));
154     }
155
156     public void testGetValueEx() {
157         try {
158             EnumUtils.getEnum(null, 0);
159             fail();
160         } catch (IllegalArgumentException ex) {}
161         try {
162             EnumUtils.getEnum(Object.class, 2);
163             fail();
164         } catch (IllegalArgumentException ex) {}
165     }
166
167 }
168
Popular Tags