1 16 package org.apache.commons.lang.enum; 17 18 import java.util.Iterator; 19 import java.util.List; 20 import java.util.Map; 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 33 34 public final class ValuedEnumTest extends TestCase { 35 36 public ValuedEnumTest(String 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 toString = ValuedColorEnum.RED.toString(); 74 assertEquals("ValuedColorEnum[Red=1]", toString); 75 assertSame(toString, ValuedColorEnum.RED.toString()); 76 } 77 78 public void testIterator() { 79 Iterator 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 list = ValuedColorEnum.getEnumList(); 87 88 assertNotNull(list); 89 90 assertEquals( list.size(), 91 ValuedColorEnum.getEnumMap().keySet().size()); 92 93 Iterator 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 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 |