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.util.Iterator;
020:import java.util.List;
021:import java.util.Map;
022:
023:import junit.framework.Test;
024:import junit.framework.TestCase;
025:import junit.framework.TestSuite;
026:import org.apache.commons.lang.SerializationUtils;
027:
028:/**
029: * Test cases for the {@link Enum} class.
030: *
031: * @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
032: * @version $Id: ValuedEnumTest.java 437554 2006-08-28 06:21:41Z bayard $
033: */
034:
035:public final class ValuedEnumTest extends TestCase {
036:
037: public ValuedEnumTest(String name) {
038: super (name);
039: }
040:
041: public void setUp() {
042: }
043:
044: public static Test suite() {
045: TestSuite suite = new TestSuite(ValuedEnumTest.class);
046: suite.setName("ValuedEnum Tests");
047: return suite;
048: }
049:
050: public void testName() {
051: assertEquals("Red", ValuedColorEnum.RED.getName());
052: assertEquals("Green", ValuedColorEnum.GREEN.getName());
053: assertEquals("Blue", ValuedColorEnum.BLUE.getName());
054: }
055:
056: public void testValue() {
057: assertEquals(1, ValuedColorEnum.RED.getValue());
058: assertEquals(2, ValuedColorEnum.GREEN.getValue());
059: assertEquals(3, ValuedColorEnum.BLUE.getValue());
060: }
061:
062: public void testCompareTo() {
063: assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.BLUE) == 0);
064: assertTrue(ValuedColorEnum.RED.compareTo(ValuedColorEnum.BLUE) < 0);
065: assertTrue(ValuedColorEnum.BLUE.compareTo(ValuedColorEnum.RED) > 0);
066: }
067:
068: public void testEquals() {
069: assertSame(ValuedColorEnum.RED, ValuedColorEnum.RED);
070: assertSame(ValuedColorEnum.getEnum("Red"), ValuedColorEnum.RED);
071: }
072:
073: public void testToString() {
074: String toString = ValuedColorEnum.RED.toString();
075: assertEquals("ValuedColorEnum[Red=1]", toString);
076: assertSame(toString, ValuedColorEnum.RED.toString());
077: }
078:
079: public void testIterator() {
080: Iterator it = ValuedColorEnum.iterator();
081: assertSame(ValuedColorEnum.RED, it.next());
082: assertSame(ValuedColorEnum.GREEN, it.next());
083: assertSame(ValuedColorEnum.BLUE, it.next());
084: }
085:
086: public void testList() {
087: List list = ValuedColorEnum.getEnumList();
088:
089: assertNotNull(list);
090:
091: assertEquals( list.size(),
092: ValuedColorEnum.getEnumMap().keySet().size());
093:
094: Iterator it = list.iterator();
095: assertSame(ValuedColorEnum.RED, it.next());
096: assertSame(ValuedColorEnum.GREEN, it.next());
097: assertSame(ValuedColorEnum.BLUE, it.next());
098: }
099:
100: public void testMap() {
101: Map map = ValuedColorEnum.getEnumMap();
102:
103: assertNotNull(map);
104:
105: assertEquals( map.keySet().size(),
106: ValuedColorEnum.getEnumList().size());
107:
108: assertTrue(map.containsValue(ValuedColorEnum.RED));
109: assertTrue(map.containsValue(ValuedColorEnum.GREEN));
110: assertTrue(map.containsValue(ValuedColorEnum.BLUE));
111: assertSame(ValuedColorEnum.RED, map.get("Red"));
112: assertSame(ValuedColorEnum.GREEN, map.get("Green"));
113: assertSame(ValuedColorEnum.BLUE, map.get("Blue"));
114: }
115:
116: public void testGet() {
117: assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum("Red"));
118: assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum("Green"));
119: assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum("Blue"));
120: assertSame(null, ValuedColorEnum.getEnum("Pink"));
121: }
122:
123: public void testGetValue() {
124: assertSame(ValuedColorEnum.RED, ValuedColorEnum.getEnum(1));
125: assertSame(ValuedColorEnum.GREEN, ValuedColorEnum.getEnum(2));
126: assertSame(ValuedColorEnum.BLUE, ValuedColorEnum.getEnum(3));
127: assertSame(null, ValuedColorEnum.getEnum(4));
128: }
129:
130: public void testSerialization() {
131: assertSame(ValuedColorEnum.RED, SerializationUtils.clone(ValuedColorEnum.RED));
132: assertSame(ValuedColorEnum.GREEN, SerializationUtils.clone(ValuedColorEnum.GREEN));
133: assertSame(ValuedColorEnum.BLUE, SerializationUtils.clone(ValuedColorEnum.BLUE));
134: }
135:
136:}
|