1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. 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 17 package org.apache.harmony.tests.java.lang; 18 19 import java.util.HashMap; 20 21 import junit.framework.TestCase; 22 23 import org.apache.harmony.testframework.serialization.SerializationTest; 24 25 import tests.util.SerializationTester; 26 27 public class EnumTest extends TestCase { 28 29 enum Sample { 30 LARRY, MOE, CURLY 31 } 32 33 Sample larry = Sample.LARRY; 34 35 Sample moe = Sample.MOE; 36 37 enum Empty { 38 } 39 40 enum Bogus { 41 UNUSED 42 } 43 44 enum Color { 45 Red, Green, Blue {}; 46 } 47 48 enum MockCloneEnum { 49 ONE; 50 callClone()51 public void callClone() throws CloneNotSupportedException { 52 super.clone(); 53 } 54 } 55 56 /** 57 * java.lang.Enum#compareTo(java.lang.Enum) 58 */ test_compareToLjava_lang_Enum()59 public void test_compareToLjava_lang_Enum() { 60 assertTrue(0 < Sample.MOE.compareTo(Sample.LARRY)); 61 assertEquals(0, Sample.MOE.compareTo(Sample.MOE)); 62 assertTrue(0 > Sample.MOE.compareTo(Sample.CURLY)); 63 try { 64 Sample.MOE.compareTo((Sample) null); 65 fail("Should throw NullPointerException"); 66 } catch (NullPointerException e) { 67 // Expected 68 } 69 } 70 71 /** 72 * java.lang.Enum#equals(Object) 73 */ test_equalsLjava_lang_Object()74 public void test_equalsLjava_lang_Object() { 75 assertFalse(moe.equals("bob")); 76 assertTrue(moe.equals(Sample.MOE)); 77 assertFalse(Sample.LARRY.equals(Sample.CURLY)); 78 assertTrue(Sample.LARRY.equals(larry)); 79 assertFalse(Sample.CURLY.equals(null)); 80 } 81 82 /** 83 * java.lang.Enum#getDeclaringClass() 84 */ test_getDeclaringClass()85 public void test_getDeclaringClass() { 86 assertEquals(Sample.class, moe.getDeclaringClass()); 87 } 88 89 /** 90 * java.lang.Enum#hashCode() 91 */ test_hashCode()92 public void test_hashCode() { 93 assertEquals(moe.hashCode(), moe.hashCode()); 94 } 95 96 /** 97 * java.lang.Enum#name() 98 */ test_name()99 public void test_name() { 100 assertEquals("MOE", moe.name()); 101 } 102 103 /** 104 * java.lang.Enum#ordinal() 105 */ test_ordinal()106 public void test_ordinal() { 107 assertEquals(0, larry.ordinal()); 108 assertEquals(1, moe.ordinal()); 109 assertEquals(2, Sample.CURLY.ordinal()); 110 } 111 112 /** 113 * java.lang.Enum#toString() 114 */ test_toString()115 public void test_toString() { 116 assertTrue(moe.toString().equals("MOE")); 117 } 118 119 /** 120 * java.lang.Enum#valueOf(Class, String) 121 */ test_valueOfLjava_lang_String()122 public void test_valueOfLjava_lang_String() { 123 assertSame(Sample.CURLY, Sample.valueOf("CURLY")); 124 assertSame(Sample.LARRY, Sample.valueOf("LARRY")); 125 assertSame(moe, Sample.valueOf("MOE")); 126 try { 127 Sample.valueOf("non-existant"); 128 fail("Expected an exception"); 129 } catch (IllegalArgumentException e) { 130 // Expected 131 } 132 try { 133 Sample.valueOf(null); 134 fail("Should throw NullPointerException"); 135 } catch (NullPointerException e) { 136 // May be caused by some compilers' code 137 } catch (IllegalArgumentException e) { 138 // other compilers will throw this 139 } 140 141 142 Sample s = Enum.valueOf(Sample.class, "CURLY"); 143 assertSame(s, Sample.CURLY); 144 s = Enum.valueOf(Sample.class, "LARRY"); 145 assertSame(larry, s); 146 s = Enum.valueOf(Sample.class, "MOE"); 147 assertSame(s, moe); 148 try { 149 Enum.valueOf(Bogus.class, "MOE"); 150 fail("Expected IllegalArgumentException"); 151 } catch (IllegalArgumentException e) { 152 // Expected 153 } 154 try { 155 Enum.valueOf((Class<Sample>) null, "a string"); 156 fail("Expected an exception"); 157 } catch (NullPointerException e) { 158 // May be caused by some compilers' code 159 } catch (IllegalArgumentException e) { 160 // other compilers will throw this 161 } 162 try { 163 Enum.valueOf(Sample.class, null); 164 fail("Expected an exception"); 165 } catch (NullPointerException e) { 166 // May be caused by some compilers' code 167 } catch (IllegalArgumentException e) { 168 // other compilers will throw this 169 } 170 try { 171 Enum.valueOf((Class<Sample>) null, (String) null); 172 fail("Expected an exception"); 173 } catch (NullPointerException e) { 174 // May be caused by some compilers' code 175 } catch (IllegalArgumentException e) { 176 // other compilers will throw this 177 } 178 } 179 180 /** 181 * java.lang.Enum#values 182 */ test_values()183 public void test_values() { 184 Sample[] myValues = Sample.values(); 185 assertEquals(3, myValues.length); 186 187 assertEquals(Sample.LARRY, myValues[0]); 188 assertEquals(Sample.MOE, myValues[1]); 189 assertEquals(Sample.CURLY, myValues[2]); 190 191 assertEquals(0, Empty.values().length); 192 } 193 194 /** 195 * java.lang.Enum#clone() 196 */ test_clone()197 public void test_clone() { 198 try { 199 MockCloneEnum.ONE.callClone(); 200 fail("Should throw CloneNotSupprotedException"); 201 } catch (CloneNotSupportedException e1) { 202 // expected 203 } 204 205 } 206 test_compatibilitySerialization_inClass_Complex_Harmony()207 public void test_compatibilitySerialization_inClass_Complex_Harmony() throws Exception { 208 // TODO migrate to the new testing framework 209 assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum2(), 210 "serialization/org/apache/harmony/tests/java/lang/EnumTest.harmony.ser")); 211 } 212 213 /** 214 * serialization/deserialization compatibility. 215 */ testSerializationSelf()216 public void testSerializationSelf() throws Exception { 217 218 // test a map class that has enums. 219 // regression test for Harmony-1163 220 HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>(); 221 enumColorMap.put(Color.Red, 1); 222 enumColorMap.put(Color.Blue, 3); 223 224 Object[] testCases = { enumColorMap, Sample.CURLY }; 225 226 SerializationTest.verifySelf(testCases); 227 228 // test a class that has enums as its fields. 229 MockEnum mock = new MockEnum(); 230 MockEnum test = (MockEnum) SerializationTest.copySerializable(mock); 231 assertEquals(mock.i, test.i); 232 assertEquals(mock.str, test.str); 233 assertEquals(mock.samEnum, test.samEnum); 234 235 // test a class that has enums and a string of same name as its fields. 236 MockEnum2 mock2 = new MockEnum2(); 237 MockEnum2 test2 = (MockEnum2) SerializationTest.copySerializable(mock2); 238 assertEquals(mock2.i, test2.i); 239 assertEquals(mock2.str, test2.str); 240 assertEquals(mock2.samEnum, test2.samEnum); 241 } 242 243 /** 244 * serialization/deserialization compatibility with RI. 245 */ testSerializationCompatibility()246 public void testSerializationCompatibility() throws Exception { 247 248 // regression test for Harmony-1163 249 HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>(); 250 enumColorMap.put(Color.Red, 1); 251 enumColorMap.put(Color.Blue, 3); 252 253 Object[] testCases = { Sample.CURLY, new MockEnum(), 254 // test a class that has enums and a string of same name as its fields. 255 new MockEnum2(), 256 // test a map class that has enums. 257 enumColorMap, }; 258 259 SerializationTest.verifyGolden(this, testCases); 260 } 261 } 262