EnumTest.java revision 2ad60cfc28e14ee8f0bb038720836a4696c478ad
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 java.util.HashMap;
20
21import junit.framework.TestCase;
22
23import org.apache.harmony.testframework.serialization.SerializationTest;
24
25import tests.util.SerializationTester;
26
27public 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    /**
49     * @tests java.lang.Enum#compareTo(java.lang.Enum)
50     */
51    public void test_compareToLjava_lang_Enum() {
52        assertTrue(0 < Sample.MOE.compareTo(Sample.LARRY));
53        assertEquals(0, Sample.MOE.compareTo(Sample.MOE));
54        assertTrue(0 > Sample.MOE.compareTo(Sample.CURLY));
55        try {
56            Sample.MOE.compareTo((Sample)null);
57            fail("Should throw NullPointerException");
58        } catch (NullPointerException e) {
59            // Expected
60        }
61    }
62
63    /**
64     * @tests java.lang.Enum#equals(Object)
65     */
66    public void test_equalsLjava_lang_Object() {
67        assertFalse(moe.equals("bob"));
68        assertTrue(moe.equals(Sample.MOE));
69        assertFalse(Sample.LARRY.equals(Sample.CURLY));
70        assertTrue(Sample.LARRY.equals(larry));
71        assertFalse(Sample.CURLY.equals(null));
72    }
73
74    /**
75     * @tests java.lang.Enum#getDeclaringClass()
76     */
77    public void test_getDeclaringClass() {
78        assertEquals(Sample.class, moe.getDeclaringClass());
79    }
80
81    /**
82     * @tests java.lang.Enum#hashCode()
83     */
84    public void test_hashCode() {
85        assertEquals (moe.hashCode(), moe.hashCode());
86    }
87
88    /**
89     * @tests java.lang.Enum#name()
90     */
91    public void test_name() {
92        assertEquals("MOE", moe.name());
93    }
94
95    /**
96     * @tests java.lang.Enum#ordinal()
97     */
98    public void test_ordinal() {
99        assertEquals(0, larry.ordinal());
100        assertEquals(1, moe.ordinal());
101        assertEquals(2, Sample.CURLY.ordinal());
102    }
103
104    /**
105     * @tests java.lang.Enum#toString()
106     */
107    public void test_toString() {
108        assertTrue(moe.toString().equals("MOE"));
109    }
110
111    /**
112     * @tests java.lang.Enum#valueOf(Class, String)
113     */
114    public void test_valueOfLjava_lang_String() {
115        assertSame(Sample.CURLY, Sample.valueOf("CURLY"));
116        assertSame(Sample.LARRY, Sample.valueOf("LARRY"));
117        assertSame(moe, Sample.valueOf("MOE"));
118        try {
119            Sample.valueOf("non-existant");
120            fail("Expected an exception");
121        } catch (IllegalArgumentException e){
122            // Expected
123        }
124        try {
125            Sample.valueOf(null);
126            fail("Should throw NullPointerException");
127        } catch (NullPointerException e) {
128            // May be caused by some compilers' code
129        } catch (IllegalArgumentException e) {
130            // other compilers will throw this
131        }
132
133
134        Sample s = Enum.valueOf(Sample.class, "CURLY");
135        assertSame(s, Sample.CURLY);
136        s = Enum.valueOf(Sample.class, "LARRY");
137        assertSame(larry, s);
138        s = Enum.valueOf(Sample.class, "MOE");
139        assertSame(s, moe);
140        try {
141            Enum.valueOf(Bogus.class, "MOE");
142            fail("Expected IllegalArgumentException");
143        } catch (IllegalArgumentException e) {
144            // Expected
145        }
146        try {
147            Enum.valueOf((Class<Sample>)null, "a string");
148            fail("Expected an exception");
149        } catch (NullPointerException e) {
150            // May be caused by some compilers' code
151        } catch (IllegalArgumentException e) {
152            // other compilers will throw this
153        }
154        try {
155            Enum.valueOf(Sample.class, null);
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((Class<Sample>)null, (String)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    }
171
172    /**
173     * @tests java.lang.Enum#values
174     */
175    public void test_values() {
176        Sample[] myValues = Sample.values();
177        assertEquals(3, myValues.length);
178
179        assertEquals(Sample.LARRY, myValues[0]);
180        assertEquals(Sample.MOE, myValues[1]);
181        assertEquals(Sample.CURLY, myValues[2]);
182
183        assertEquals(0, Empty.values().length);
184    }
185
186    /**
187     * @test Serialization/deserilazation compatibility with Harmony.
188     */
189    public void test_compatibilitySerialization_inClass_Complex_Harmony() throws Exception{
190        // TODO migrate to the new testing framework
191        assertTrue(SerializationTester.assertCompabilityEquals(new MockEnum2(),
192            "serialization/org/apache/harmony/luni/tests/java/lang/EnumTest.harmony.ser"));
193    }
194
195    /**
196     * @tests serialization/deserialization compatibility.
197     */
198    public void testSerializationSelf() throws Exception {
199
200        // test a map class that has enums.
201        // regression test for Harmony-1163
202        HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
203        enumColorMap.put(Color.Red, 1);
204        enumColorMap.put(Color.Blue, 3);
205
206        Object[] testCases = { enumColorMap, Sample.CURLY };
207
208        SerializationTest.verifySelf(testCases);
209
210        // test a class that has enums as its fields.
211        MockEnum mock = new MockEnum();
212        MockEnum test = (MockEnum) SerializationTest.copySerializable(mock);
213        assertEquals(mock.i, test.i);
214        assertEquals(mock.str, test.str);
215        assertEquals(mock.samEnum, test.samEnum);
216
217        // test a class that has enums and a string of same name as its fields.
218        MockEnum2 mock2 = new MockEnum2();
219        MockEnum2 test2 = (MockEnum2) SerializationTest.copySerializable(mock2);
220        assertEquals(mock2.i, test2.i);
221        assertEquals(mock2.str, test2.str);
222        assertEquals(mock2.samEnum, test2.samEnum);
223    }
224
225    /**
226     * @tests serialization/deserialization compatibility with RI.
227     */
228    public void testSerializationCompatibility() throws Exception {
229
230        // regression test for Harmony-1163
231        HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
232        enumColorMap.put(Color.Red, 1);
233        enumColorMap.put(Color.Blue, 3);
234
235        Object[] testCases = { Sample.CURLY, new MockEnum(),
236        // test a class that has enums and a string of same name as its fields.
237                new MockEnum2(),
238                // test a map class that has enums.
239                enumColorMap, };
240
241        SerializationTest.verifyGolden(this, testCases);
242    }
243}
244