1 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 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 string) { 50 super(string); 51 } 52 53 protected static ParameterizedType getParameterizedType(Class aClass) { 54 return (ParameterizedType) getType(aClass); 55 } 56 57 protected static Type getType(Class aClass) { 58 try { 59 return aClass.getField("f").getGenericType(); 60 } catch (NoSuchFieldException e) { 61 throw new RuntimeException (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 [] a, Object ... b) { 71 Set set = new HashSet<Object >(Arrays.asList(a)); 72 assertEquals(set.size(), a.length); 73 for (Object object : b) { 74 assertTrue(set.remove(object)); 75 } 76 assertTrue(set.isEmpty()); 77 } 78 79 protected Object pump(Object o) throws Exception { 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 expected, String format, Object ... argument) { 86 assertFormat(Locale.FRANCE, expected, format, argument); 87 } 88 89 protected void assertFormat(Locale locale, String expected, String format, Object ... argument) { 90 assertEquals(expected, new Formatter(Locale.GERMANY).format(locale, format, argument).toString()); 91 } 92 93 protected void assertFormatException(Class <? extends RuntimeException > expected, String format, Object ... argument) { 94 try { 95 Formatter formatter = new Formatter().format(Locale.FRANCE, format, argument); 96 fail("Result: '" + formatter + "', but expected exception: " + expected); 97 } catch (RuntimeException e) { 98 if (!expected.isInstance(e)) { 99 throw e; 100 } 101 } 102 } 103 104 protected static String readLine(File file, String csn) throws Exception { 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 |