001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.lang;
018:
019: import java.util.HashMap;
020:
021: import junit.framework.TestCase;
022:
023: import org.apache.harmony.testframework.serialization.SerializationTest;
024:
025: import tests.util.SerializationTester;
026:
027: public class EnumTest extends TestCase {
028:
029: enum Sample {
030: LARRY, MOE, CURLY
031: }
032:
033: Sample larry = Sample.LARRY;
034:
035: Sample moe = Sample.MOE;
036:
037: enum Empty {
038: }
039:
040: enum Bogus {
041: UNUSED
042: }
043:
044: enum Color {
045: Red, Green, Blue {
046: };
047: }
048:
049: /**
050: * @tests java.lang.Enum#compareTo(java.lang.Enum)
051: */
052: public void test_compareToLjava_lang_Enum() {
053: assertTrue(0 < Sample.MOE.compareTo(Sample.LARRY));
054: assertEquals(0, Sample.MOE.compareTo(Sample.MOE));
055: assertTrue(0 > Sample.MOE.compareTo(Sample.CURLY));
056: try {
057: Sample.MOE.compareTo((Sample) null);
058: fail("Should throw NullPointerException");
059: } catch (NullPointerException e) {
060: // Expected
061: }
062: }
063:
064: /**
065: * @tests java.lang.Enum#equals(Object)
066: */
067: public void test_equalsLjava_lang_Object() {
068: assertFalse(moe.equals("bob"));
069: assertTrue(moe.equals(Sample.MOE));
070: assertFalse(Sample.LARRY.equals(Sample.CURLY));
071: assertTrue(Sample.LARRY.equals(larry));
072: assertFalse(Sample.CURLY.equals(null));
073: }
074:
075: /**
076: * @tests java.lang.Enum#getDeclaringClass()
077: */
078: public void test_getDeclaringClass() {
079: assertEquals(Sample.class, moe.getDeclaringClass());
080: }
081:
082: /**
083: * @tests java.lang.Enum#hashCode()
084: */
085: public void test_hashCode() {
086: assertEquals(moe.hashCode(), moe.hashCode());
087: }
088:
089: /**
090: * @tests java.lang.Enum#name()
091: */
092: public void test_name() {
093: assertEquals("MOE", moe.name());
094: }
095:
096: /**
097: * @tests java.lang.Enum#ordinal()
098: */
099: public void test_ordinal() {
100: assertEquals(0, larry.ordinal());
101: assertEquals(1, moe.ordinal());
102: assertEquals(2, Sample.CURLY.ordinal());
103: }
104:
105: /**
106: * @tests java.lang.Enum#toString()
107: */
108: public void test_toString() {
109: assertTrue(moe.toString().equals("MOE"));
110: }
111:
112: /**
113: * @tests java.lang.Enum#valueOf(Class, String)
114: */
115: public void test_valueOfLjava_lang_String() {
116: assertSame(Sample.CURLY, Sample.valueOf("CURLY"));
117: assertSame(Sample.LARRY, Sample.valueOf("LARRY"));
118: assertSame(moe, Sample.valueOf("MOE"));
119: try {
120: Sample.valueOf("non-existant");
121: fail("Expected an exception");
122: } catch (IllegalArgumentException e) {
123: // Expected
124: }
125: try {
126: Sample.valueOf(null);
127: fail("Should throw NullPointerException");
128: } catch (NullPointerException e) {
129: // May be caused by some compilers' code
130: } catch (IllegalArgumentException e) {
131: // other compilers will throw this
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()
190: throws Exception {
191: // TODO migrate to the new testing framework
192: assertTrue(SerializationTester
193: .assertCompabilityEquals(new MockEnum2(),
194: "serialization/org/apache/harmony/luni/tests/java/lang/EnumTest.harmony.ser"));
195: }
196:
197: /**
198: * @tests serialization/deserialization compatibility.
199: */
200: public void testSerializationSelf() throws Exception {
201:
202: // test a map class that has enums.
203: // regression test for Harmony-1163
204: HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
205: enumColorMap.put(Color.Red, 1);
206: enumColorMap.put(Color.Blue, 3);
207:
208: Object[] testCases = { enumColorMap, Sample.CURLY };
209:
210: SerializationTest.verifySelf(testCases);
211:
212: // test a class that has enums as its fields.
213: MockEnum mock = new MockEnum();
214: MockEnum test = (MockEnum) SerializationTest
215: .copySerializable(mock);
216: assertEquals(mock.i, test.i);
217: assertEquals(mock.str, test.str);
218: assertEquals(mock.samEnum, test.samEnum);
219:
220: // test a class that has enums and a string of same name as its fields.
221: MockEnum2 mock2 = new MockEnum2();
222: MockEnum2 test2 = (MockEnum2) SerializationTest
223: .copySerializable(mock2);
224: assertEquals(mock2.i, test2.i);
225: assertEquals(mock2.str, test2.str);
226: assertEquals(mock2.samEnum, test2.samEnum);
227: }
228:
229: /**
230: * @tests serialization/deserialization compatibility with RI.
231: */
232: public void testSerializationCompatibility() throws Exception {
233:
234: // regression test for Harmony-1163
235: HashMap<Color, Integer> enumColorMap = new HashMap<Color, Integer>();
236: enumColorMap.put(Color.Red, 1);
237: enumColorMap.put(Color.Blue, 3);
238:
239: Object[] testCases = { Sample.CURLY, new MockEnum(),
240: // test a class that has enums and a string of same name as its fields.
241: new MockEnum2(),
242: // test a map class that has enums.
243: enumColorMap, };
244:
245: SerializationTest.verifyGolden(this, testCases);
246: }
247: }
|