001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.lang;
032:
033: import java.io.*;
034: import java.lang.ref.WeakReference;
035: import java.util.EnumSet;
036: import java.util.concurrent.Callable;
037: import net.sf.retrotranslator.tests.TestCaseBase;
038:
039: /**
040: * @author Taras Puchko
041: */
042: public class Enum_TestCase extends TestCaseBase {
043:
044: public void testName() throws Exception {
045: assertEquals("GREEN", getName(MyColor.GREEN));
046: assertEquals("EAST", getName(CardinalPoint.EAST));
047: }
048:
049: private static String getName(Enum anEnum) {
050: return anEnum.name();
051: }
052:
053: public void testOrdinal() throws Exception {
054: assertEquals(1, MyColor.GREEN.ordinal());
055: assertEquals(2, CardinalPoint.SOUTH.ordinal());
056: }
057:
058: public void testToString() throws Exception {
059: assertEquals("GREEN", MyColor.GREEN.toString());
060: assertEquals("SOUTH", CardinalPoint.SOUTH.toString());
061: }
062:
063: public void testEquals() throws Exception {
064: assertEquals(MyColor.GREEN, MyColor.GREEN);
065: assertFalse(MyColor.GREEN.equals(MyColor.RED));
066: assertEquals(CardinalPoint.SOUTH, CardinalPoint.SOUTH);
067: assertFalse(CardinalPoint.SOUTH.equals(CardinalPoint.NORTH));
068: assertFalse(MyColor.GREEN.equals(CardinalPoint.NORTH));
069: assertFalse(MyColor.GREEN.equals(null));
070: }
071:
072: public void testCompareTo() throws Exception {
073: assertTrue(MyColor.BLUE.compareTo(MyColor.RED) > 0);
074: assertTrue(MyColor.GREEN.compareTo(MyColor.GREEN) == 0);
075: assertTrue(MyColor.RED.compareTo(MyColor.GREEN) < 0);
076: assertTrue(CardinalPoint.WEST.compareTo(CardinalPoint.NORTH) > 0);
077: assertTrue(CardinalPoint.EAST.compareTo(CardinalPoint.EAST) == 0);
078: assertTrue(CardinalPoint.SOUTH.compareTo(CardinalPoint.WEST) < 0);
079: }
080:
081: public void testGetDeclaringClass() {
082: assertSame(MyColor.class, MyColor.GREEN.getClass());
083: assertSame(MyColor.class, MyColor.GREEN.getDeclaringClass());
084: assertNotSame(CardinalPoint.class, CardinalPoint.NORTH
085: .getClass());
086: assertSame(CardinalPoint.class, CardinalPoint.NORTH
087: .getDeclaringClass());
088: }
089:
090: public void testValueOf() throws Exception {
091: MyColor color = Enum.valueOf(MyColor.class, "GREEN");
092: assertEquals(MyColor.GREEN, color);
093: assertSame(MyColor.GREEN, MyColor.valueOf("GREEN"));
094: try {
095: MyColor.valueOf("WHITE");
096: fail("No such color!");
097: } catch (IllegalArgumentException e) {
098: //ok
099: }
100: CardinalPoint point = Enum.valueOf(CardinalPoint.class, "WEST");
101: assertEquals(CardinalPoint.WEST, point);
102: assertSame(CardinalPoint.WEST, CardinalPoint.valueOf("WEST"));
103: try {
104: CardinalPoint.valueOf("CENTER");
105: fail("No such point!");
106: } catch (IllegalArgumentException e) {
107: //ok
108: }
109: }
110:
111: public void testValues() {
112: checkColors(MyColor.values());
113: checkColors(MyColor.class.getEnumConstants());
114: checkPoints(CardinalPoint.values());
115: checkPoints(CardinalPoint.class.getEnumConstants());
116: }
117:
118: private void checkColors(MyColor[] colors) {
119: assertEquals(3, colors.length);
120: assertEquals(MyColor.RED, colors[0]);
121: assertEquals(MyColor.GREEN, colors[1]);
122: assertEquals(MyColor.BLUE, colors[2]);
123: }
124:
125: private void checkPoints(CardinalPoint[] points) {
126: assertEquals(4, points.length);
127: assertEquals(CardinalPoint.NORTH, points[0]);
128: assertEquals(CardinalPoint.EAST, points[1]);
129: assertEquals(CardinalPoint.SOUTH, points[2]);
130: assertEquals(CardinalPoint.WEST, points[3]);
131: }
132:
133: public void testReadResolve() throws Exception {
134: assertSame(MyColor.BLUE, pump(MyColor.BLUE));
135: assertSame(CardinalPoint.SOUTH, pump(CardinalPoint.SOUTH));
136: }
137:
138: static class MyClassLoader extends ClassLoader {
139:
140: public MyClassLoader(ClassLoader parent) {
141: super (parent);
142: }
143:
144: public Class defineClass(byte[] code) {
145: return defineClass(null, code, 0, code.length);
146: }
147: }
148:
149: enum Letter {
150: A, B, C;
151: public static Letter DEFAULT = Enum.valueOf(Letter.class, "B");
152: public static final EnumSet<Letter> SET = EnumSet
153: .complementOf(EnumSet.of(B));
154: }
155:
156: public void testInitOrder() throws Exception {
157: assertEquals("A", Letter.A.name());
158: assertEquals("B", Letter.B.name());
159: assertEquals("C", Letter.C.name());
160: assertEquals("B", Letter.DEFAULT.name());
161: assertEquals(2, Letter.SET.size());
162: assertTrue(Letter.SET.contains(Letter.A));
163: assertFalse(Letter.SET.contains(Letter.B));
164: assertTrue(Letter.SET.contains(Letter.C));
165: }
166:
167: public void testGarbageCollector() throws Exception {
168: ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
169: byte[] buffer = new byte[0x1000];
170: InputStream inputStream = MyColor.class.getResource(
171: MyColor.class.getSimpleName() + ".class").openStream();
172: int count;
173: while ((count = inputStream.read(buffer)) > 0) {
174: outputStream.write(buffer, 0, count);
175: }
176: inputStream.close();
177: MyClassLoader classLoader = new MyClassLoader(MyColor.class
178: .getClassLoader());
179: Class enumClass = classLoader.defineClass(outputStream
180: .toByteArray());
181: assertEquals(3, enumClass.getEnumConstants().length);
182: final WeakReference<ClassLoader> reference = new WeakReference<ClassLoader>(
183: classLoader);
184: classLoader = null;
185: System.gc();
186: assertSame(enumClass.getClassLoader(), reference.get());
187: enumClass = null;
188: gc(new Callable<Boolean>() {
189: public Boolean call() throws Exception {
190: return reference.get() != null;
191: }
192: });
193: assertNull(reference.get());
194: }
195:
196: enum MyEnum {
197: A, B, C, D;
198:
199: MyEnum() {
200: try {
201: Thread.sleep(500);
202: } catch (InterruptedException e) {
203: e.printStackTrace();
204: }
205: }
206: }
207:
208: public void testSlowInitialization() throws Exception {
209: new Thread() {
210: public void run() {
211: checkEnumConstantCount();
212: }
213: }.start();
214: Thread.sleep(500);
215: checkEnumConstantCount();
216: }
217:
218: private void checkEnumConstantCount() {
219: assertEquals(4, MyEnum.class.getEnumConstants().length);
220: }
221:
222: }
|