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.tests;
032:
033: import java.io.*;
034: import java.lang.reflect.*;
035: import java.util.*;
036: import java.util.concurrent.Callable;
037: import junit.framework.TestCase;
038:
039: /**
040: * @author Taras Puchko
041: */
042: public abstract class TestCaseBase extends TestCase {
043:
044: protected static final Locale HINDI = new Locale("hi", "IN");
045:
046: protected TestCaseBase() {
047: }
048:
049: protected TestCaseBase(String string) {
050: super (string);
051: }
052:
053: protected static ParameterizedType getParameterizedType(Class aClass) {
054: return (ParameterizedType) getType(aClass);
055: }
056:
057: protected static Type getType(Class aClass) {
058: try {
059: return aClass.getField("f").getGenericType();
060: } catch (NoSuchFieldException e) {
061: throw new RuntimeException(e);
062: }
063: }
064:
065: protected static <E> E singleton(E[] array) {
066: assertEquals(1, array.length);
067: return array[0];
068: }
069:
070: public static void assertEqualElements(Object[] a, Object... b) {
071: Set set = new HashSet<Object>(Arrays.asList(a));
072: assertEquals(set.size(), a.length);
073: for (Object object : b) {
074: assertTrue(set.remove(object));
075: }
076: assertTrue(set.isEmpty());
077: }
078:
079: protected Object pump(Object o) throws Exception {
080: ByteArrayOutputStream stream = new ByteArrayOutputStream();
081: new ObjectOutputStream(stream).writeObject(o);
082: return new ObjectInputStream(new ByteArrayInputStream(stream
083: .toByteArray())).readObject();
084: }
085:
086: protected void assertFormat(String expected, String format,
087: Object... argument) {
088: assertFormat(Locale.FRANCE, expected, format, argument);
089: }
090:
091: protected void assertFormat(Locale locale, String expected,
092: String format, Object... argument) {
093: assertEquals(expected, new Formatter(Locale.GERMANY).format(
094: locale, format, argument).toString());
095: }
096:
097: protected void assertFormatException(
098: Class<? extends RuntimeException> expected, String format,
099: Object... argument) {
100: try {
101: Formatter formatter = new Formatter().format(Locale.FRANCE,
102: format, argument);
103: fail("Result: '" + formatter
104: + "', but expected exception: " + expected);
105: } catch (RuntimeException e) {
106: if (!expected.isInstance(e)) {
107: throw e;
108: }
109: }
110: }
111:
112: protected static String readLine(File file, String csn)
113: throws Exception {
114: FileInputStream stream = new FileInputStream(file);
115: BufferedReader reader = new BufferedReader(
116: csn == null ? new InputStreamReader(stream)
117: : new InputStreamReader(stream, csn));
118: try {
119: return reader.readLine();
120: } finally {
121: reader.close();
122: }
123: }
124:
125: protected void gc(Callable<Boolean> predicate) throws Exception {
126: System.gc();
127: try {
128: List<long[]> list = new ArrayList<long[]>();
129: while (predicate.call() && list.size() < Integer.MAX_VALUE) {
130: list.add(new long[1000000]);
131: }
132: } catch (OutOfMemoryError e) {
133: // ok
134: }
135: }
136:
137: protected static boolean isJava14AtLeast() {
138: return System.getProperty("java.version").compareTo("1.4") >= 0;
139: }
140:
141: protected static boolean isJava5AtLeast() {
142: return System.getProperty("java.version").compareTo("1.5") >= 0;
143: }
144: }
|