KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > enums > ValuedEnumTest


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

33
34 public final class ValuedEnumTest extends TestCase {
35
36     public ValuedEnumTest(String JavaDoc name) {
37         super(name);
38     }
39
40     public void setUp() {
41     }
42
43     public static Test suite() {
44         TestSuite suite = new TestSuite(ValuedEnumTest.class);
45         suite.setName("ValuedEnum Tests");
46         return suite;
47     }
48
49     public void testName() {
50         assertEquals("Red", ValuedColorEnum.RED.getName());
51         assertEquals("Green", ValuedColorEnum.GREEN.getName());
52         assertEquals("Blue", ValuedColorEnum.BLUE.getName());
53     }
54
55     public void testValue() {
56         assertEquals(1, ValuedColorEnum.RED.getValue());
57         assertEquals(2, ValuedColorEnum.GREEN.getValue());
58         assertEquals(3, ValuedColorEnum.BLUE.getValue());
59     }
60
61     public void testCompareTo() {
62         assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.BLUE) == 0);
63         assertTrue(ValuedColorEnum.RED.compareTo(ValuedColorEnum.BLUE) < 0);
64         assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.RED) > 0);
65     }
66
67     public void testEquals() {
68         assertSame(ValuedColorEnum.RED, ValuedColorEnum.RED);
69         assertSame(ValuedColorEnum.getEnum("Red"), ValuedColorEnum.RED);
70     }
71
72     public void testToString() {
73         String JavaDoc toString = ValuedColorEnum.RED.toString();
74         assertEquals("ValuedColorEnum[Red=1]", toString);
75         assertSame(toString, ValuedColorEnum.RED.toString());
76     }
77
78     public void testIterator() {
79         Iterator JavaDoc it = ValuedColorEnum.iterator();
80         assertSame(ValuedColorEnum.RED, it.next());
81         assertSame(ValuedColorEnum.GREEN, it.next());
82         assertSame(ValuedColorEnum.BLUE, it.next());
83     }
84
85     public void testList() {
86         List JavaDoc list = ValuedColorEnum.getEnumList();
87         
88         assertNotNull(list);
89         
90         assertEquals( list.size(),
91                      ValuedColorEnum.getEnumMap().keySet().size());
92         
93         Iterator JavaDoc it = list.iterator();
94         assertSame(ValuedColorEnum.RED, it.next());
95         assertSame(ValuedColorEnum.GREEN, it.next());
96         assertSame(ValuedColorEnum.BLUE, it.next());
97     }
98
99     public void testMap() {
100         Map JavaDoc map = ValuedColorEnum.getEnumMap();
101         
102         assertNotNull(map);
103         
104         assertEquals( map.keySet().size(),
105                      ValuedColorEnum.getEnumList().size());
106                      
107         assertTrue(map.containsValue(ValuedColorEnum.RED));
108         assertTrue(map.containsValue(ValuedColorEnum.GREEN));
109         assertTrue(map.containsValue(ValuedColorEnum.BLUE));
110         assertSame(ValuedColorEnum.RED, map.get("Red"));
111         assertSame(ValuedColorEnum.GREEN, map.get("Green"));
112         assertSame(ValuedColorEnum.BLUE, map.get("Blue"));
113     }
114
115     public void testGet() {
116         assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum("Red"));
117         assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum("Green"));
118         assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum("Blue"));
119         assertSame(null, ValuedColorEnum.getEnum("Pink"));
120     }
121
122     public void testGetValue() {
123         assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum(1));
124         assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum(2));
125         assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum(3));
126         assertSame(null, ValuedColorEnum.getEnum(4));
127     }
128
129     public void testSerialization() {
130         assertSame(ValuedColorEnum.RED, SerializationUtils.clone(ValuedColorEnum.RED));
131         assertSame(ValuedColorEnum.GREEN, SerializationUtils.clone(ValuedColorEnum.GREEN));
132         assertSame(ValuedColorEnum.BLUE, SerializationUtils.clone(ValuedColorEnum.BLUE));
133     }
134
135 }
136
Popular Tags