EnumTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 17package org.apache.harmony.luni.tests.java.lang; 18 19import dalvik.annotation.TestInfo; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTarget; 22import dalvik.annotation.TestTargetClass; 23 24import junit.framework.TestCase; 25 26import java.util.HashMap; 27 28import org.apache.harmony.testframework.serialization.SerializationTest; 29 30import tests.util.SerializationTester; 31 32@TestTargetClass(Enum.class) 33public class EnumTest extends TestCase { 34 35 enum Sample { 36 LARRY, MOE, CURLY 37 } 38 39 Sample larry = Sample.LARRY; 40 41 Sample moe = Sample.MOE; 42 43 enum Empty { 44 } 45 46 enum Bogus { 47 UNUSED 48 } 49 50 enum Color { 51 Red, Green, Blue {}; 52 } 53 54 /** 55 * @tests java.lang.Enum#compareTo(java.lang.Enum) 56 */ 57 @TestInfo( 58 level = TestLevel.COMPLETE, 59 purpose = "", 60 targets = { 61 @TestTarget( 62 methodName = "compareTo", 63 methodArgs = {java.lang.Enum.class} 64 ) 65 }) 66 public void test_compareToLjava_lang_Enum() { 67 assertTrue(0 < Sample.MOE.compareTo(Sample.LARRY)); 68 assertEquals(0, Sample.MOE.compareTo(Sample.MOE)); 69 assertTrue(0 > Sample.MOE.compareTo(Sample.CURLY)); 70 try { 71 Sample.MOE.compareTo((Sample)null); 72 fail("Should throw NullPointerException"); 73 } catch (NullPointerException e) { 74 // Expected 75 } 76 } 77 78 /** 79 * @tests java.lang.Enum#equals(Object) 80 */ 81 @TestInfo( 82 level = TestLevel.COMPLETE, 83 purpose = "", 84 targets = { 85 @TestTarget( 86 methodName = "equals", 87 methodArgs = {java.lang.Object.class} 88 ) 89 }) 90 public void test_equalsLjava_lang_Object() { 91 assertFalse(moe.equals("bob")); 92 assertTrue(moe.equals(Sample.MOE)); 93 assertFalse(Sample.LARRY.equals(Sample.CURLY)); 94 assertTrue(Sample.LARRY.equals(larry)); 95 assertFalse(Sample.CURLY.equals(null)); 96 } 97 98 /** 99 * @tests java.lang.Enum#getDeclaringClass() 100 */ 101 @TestInfo( 102 level = TestLevel.COMPLETE, 103 purpose = "", 104 targets = { 105 @TestTarget( 106 methodName = "getDeclaringClass", 107 methodArgs = {} 108 ) 109 }) 110 public void test_getDeclaringClass() { 111 assertEquals(Sample.class, moe.getDeclaringClass()); 112 } 113 114 /** 115 * @tests java.lang.Enum#hashCode() 116 */ 117 @TestInfo( 118 level = TestLevel.PARTIAL, 119 purpose = "Doesn't check hash code of different objects,equal objects.", 120 targets = { 121 @TestTarget( 122 methodName = "hashCode", 123 methodArgs = {} 124 ) 125 }) 126 public void test_hashCode() { 127 assertEquals (moe.hashCode(), moe.hashCode()); 128 } 129 130 /** 131 * @tests java.lang.Enum#name() 132 */ 133 @TestInfo( 134 level = TestLevel.COMPLETE, 135 purpose = "", 136 targets = { 137 @TestTarget( 138 methodName = "name", 139 methodArgs = {} 140 ) 141 }) 142 public void test_name() { 143 assertEquals("MOE", moe.name()); 144 } 145 146 /** 147 * @tests java.lang.Enum#ordinal() 148 */ 149 @TestInfo( 150 level = TestLevel.COMPLETE, 151 purpose = "", 152 targets = { 153 @TestTarget( 154 methodName = "ordinal", 155 methodArgs = {} 156 ) 157 }) 158 public void test_ordinal() { 159 assertEquals(0, larry.ordinal()); 160 assertEquals(1, moe.ordinal()); 161 assertEquals(2, Sample.CURLY.ordinal()); 162 } 163 164 /** 165 * @tests java.lang.Enum#toString() 166 */ 167 @TestInfo( 168 level = TestLevel.COMPLETE, 169 purpose = "", 170 targets = { 171 @TestTarget( 172 methodName = "toString", 173 methodArgs = {} 174 ) 175 }) 176 public void test_toString() { 177 assertTrue(moe.toString().equals("MOE")); 178 } 179 180 /** 181 * @tests java.lang.Enum#valueOf(Class, String) 182 */ 183 @TestInfo( 184 level = TestLevel.COMPLETE, 185 purpose = "", 186 targets = { 187 @TestTarget( 188 methodName = "valueOf", 189 methodArgs = {java.lang.Class.class, java.lang.String.class} 190 ) 191 }) 192 public void test_valueOfLjava_lang_String() { 193 assertSame(Sample.CURLY, Sample.valueOf("CURLY")); 194 assertSame(Sample.LARRY, Sample.valueOf("LARRY")); 195 assertSame(moe, Sample.valueOf("MOE")); 196 try { 197 Sample.valueOf("non-existant"); 198 fail("Expected an exception"); 199 } catch (IllegalArgumentException e){ 200 // Expected 201 } 202 try { 203 Sample.valueOf(null); 204 fail("Should throw NullPointerException"); 205 } catch (NullPointerException e) { 206 // May be caused by some compilers' code 207 } catch (IllegalArgumentException e) { 208 // other compilers will throw this 209 } 210 211 212 Sample s = Enum.valueOf(Sample.class, "CURLY"); 213 assertSame(s, Sample.CURLY); 214 s = Enum.valueOf(Sample.class, "LARRY"); 215 assertSame(larry, s); 216 s = Enum.valueOf(Sample.class, "MOE"); 217 assertSame(s, moe); 218 try { 219 Enum.valueOf(Bogus.class, "MOE"); 220 fail("Expected IllegalArgumentException"); 221 } catch (IllegalArgumentException e) { 222 // Expected 223 } 224 try { 225 Enum.valueOf((Class<Sample>)null, "a string"); 226 fail("Expected an exception"); 227 } catch (NullPointerException e) { 228 // May be caused by some compilers' code 229 } catch (IllegalArgumentException e) { 230 // other compilers will throw this 231 } 232 try { 233 Enum.valueOf(Sample.class, null); 234 fail("Expected an exception"); 235 } catch (NullPointerException e) { 236 // May be caused by some compilers' code 237 } catch (IllegalArgumentException e) { 238 // other compilers will throw this 239 } 240 try { 241 Enum.valueOf((Class<Sample>)null, (String)null); 242 fail("Expected an exception"); 243 } catch (NullPointerException e) { 244 // May be caused by some compilers' code 245 } catch (IllegalArgumentException e) { 246 // other compilers will throw this 247 } 248 } 249 250 /** 251 * @tests java.lang.Enum#values 252 */ 253 @TestInfo( 254 level = TestLevel.TODO, 255 purpose = "There is no such method in the specification.", 256 targets = { 257 @TestTarget( 258 methodName = "values", 259 methodArgs = {} 260 ) 261 }) 262 public void test_values() { 263 Sample[] myValues = Sample.values(); 264 assertEquals(3, myValues.length); 265 266 assertEquals(Sample.LARRY, myValues[0]); 267 assertEquals(Sample.MOE, myValues[1]); 268 assertEquals(Sample.CURLY, myValues[2]); 269 270 assertEquals(0, Empty.values().length); 271 } 272 273 /** 274 * @test Serialization/deserilazation compatibility with Harmony. 275 */ 276 @TestInfo( 277 level = TestLevel.PARTIAL_OK, 278 purpose = "Serialization/deserilazation compatibility.", 279 targets = { 280 @TestTarget( 281 methodName = "!SerializationGolden", 282 methodArgs = {} 283 ) 284 }) 285 public void _test_compatibilitySerialization_inClass_Complex_Harmony() throws Exception{ 286 // TODO migrate to the new testing framework 287 assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum2(), 288 "serialization/org/apache/harmony/luni/tests/java/lang/EnumTest.harmony.ser")); 289 } 290 291 /** 292 * @tests serialization/deserialization compatibility. 293 */ 294 @TestInfo( 295 level = TestLevel.COMPLETE, 296 purpose = "Verifies serialization/deserialization compatibility.", 297 targets = { 298 @TestTarget( 299 methodName = "!SerializationSelf", 300 methodArgs = {} 301 ) 302 }) 303 public void testSerializationSelf() throws Exception { 304 305 // test a map class that has enums. 306 // regression test for Harmony-1163 307 HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>(); 308 enumColorMap.put(Color.Red, 1); 309 enumColorMap.put(Color.Blue, 3); 310 311 Object[] testCases = { enumColorMap, Sample.CURLY }; 312 313 SerializationTest.verifySelf(testCases); 314 315 // test a class that has enums as its fields. 316 MockEnum mock = new MockEnum(); 317 MockEnum test = (MockEnum) SerializationTest.copySerializable(mock); 318 assertEquals(mock.i, test.i); 319 assertEquals(mock.str, test.str); 320 assertEquals(mock.samEnum, test.samEnum); 321 322 // test a class that has enums and a string of same name as its fields. 323 MockEnum2 mock2 = new MockEnum2(); 324 MockEnum2 test2 = (MockEnum2) SerializationTest.copySerializable(mock2); 325 assertEquals(mock2.i, test2.i); 326 assertEquals(mock2.str, test2.str); 327 assertEquals(mock2.samEnum, test2.samEnum); 328 } 329 330 /** 331 * @tests serialization/deserialization compatibility with RI. 332 */ 333 @TestInfo( 334 level = TestLevel.PARTIAL_OK, 335 purpose = "Verifies serialization/deserialization compatibility.", 336 targets = { 337 @TestTarget( 338 methodName = "!SerializationGolden", 339 methodArgs = {} 340 ) 341 }) 342 public void testSerializationCompatibility() throws Exception { 343 344 // regression test for Harmony-1163 345 HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>(); 346 enumColorMap.put(Color.Red, 1); 347 enumColorMap.put(Color.Blue, 3); 348 349 Object[] testCases = { Sample.CURLY, new MockEnum(), 350 // test a class that has enums and a string of same name as its fields. 351 new MockEnum2(), 352 // test a map class that has enums. 353 enumColorMap, }; 354 355 SerializationTest.verifyGolden(this, testCases); 356 } 357} 358