KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > tests > BaseTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.tests;
33
34 import java.io.*;
35 import java.lang.reflect.*;
36 import java.util.*;
37 import junit.framework.TestCase;
38
39 /**
40  * @author Taras Puchko
41  */

42 public abstract class BaseTestCase extends TestCase {
43
44     protected static final Locale HINDI = new Locale("hi", "IN");
45
46     protected BaseTestCase() {
47     }
48
49     protected BaseTestCase(String JavaDoc string) {
50         super(string);
51     }
52
53     protected static ParameterizedType getParameterizedType(Class JavaDoc aClass) {
54         return (ParameterizedType) getType(aClass);
55     }
56
57     protected static Type getType(Class JavaDoc aClass) {
58         try {
59             return aClass.getField("f").getGenericType();
60         } catch (NoSuchFieldException JavaDoc e) {
61             throw new RuntimeException JavaDoc(e);
62         }
63     }
64
65     protected static <E> E singleton(E[] array) {
66         assertEquals(1, array.length);
67         return array[0];
68     }
69
70     public static void assertEqualElements(Object JavaDoc[] a, Object JavaDoc... b) {
71         Set set = new HashSet<Object JavaDoc>(Arrays.asList(a));
72         assertEquals(set.size(), a.length);
73         for (Object JavaDoc object : b) {
74             assertTrue(set.remove(object));
75         }
76         assertTrue(set.isEmpty());
77     }
78
79     protected Object JavaDoc pump(Object JavaDoc o) throws Exception JavaDoc {
80         ByteArrayOutputStream stream = new ByteArrayOutputStream();
81         new ObjectOutputStream(stream).writeObject(o);
82         return new ObjectInputStream(new ByteArrayInputStream(stream.toByteArray())).readObject();
83     }
84
85     protected void assertFormat(String JavaDoc expected, String JavaDoc format, Object JavaDoc... argument) {
86         assertFormat(Locale.FRANCE, expected, format, argument);
87     }
88
89     protected void assertFormat(Locale locale, String JavaDoc expected, String JavaDoc format, Object JavaDoc... argument) {
90         assertEquals(expected, new Formatter(Locale.GERMANY).format(locale, format, argument).toString());
91     }
92
93     protected void assertFormatException(Class JavaDoc<? extends RuntimeException JavaDoc> expected, String JavaDoc format, Object JavaDoc... argument) {
94         try {
95             Formatter formatter = new Formatter().format(Locale.FRANCE, format, argument);
96             fail("Result: '" + formatter + "', but expected exception: " + expected);
97         } catch (RuntimeException JavaDoc e) {
98             if (!expected.isInstance(e)) {
99                 throw e;
100             }
101         }
102     }
103
104     protected static String JavaDoc readLine(File file, String JavaDoc csn) throws Exception JavaDoc {
105         FileInputStream stream = new FileInputStream(file);
106         BufferedReader reader = new BufferedReader(csn == null
107                 ? new InputStreamReader(stream) : new InputStreamReader(stream, csn) );
108         try {
109             return reader.readLine();
110         } finally {
111             reader.close();
112         }
113     }
114
115 }
116
Popular Tags