PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/src/sys/java/fanx/test/Test.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 338 lines | 225 code | 44 blank | 69 comment | 45 complexity | a8cd99cf5d5878d18d76b6a61e5353ab MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 3 Sep 05 Brian Frank Creation
  7. //
  8. package fanx.test;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import fanx.util.*;
  12. import fan.sys.*;
  13. /**
  14. * Test is the base class of test cases as well as the
  15. * main entry for test harness.
  16. */
  17. public abstract class Test
  18. {
  19. //////////////////////////////////////////////////////////////////////////
  20. // Test Case List
  21. //////////////////////////////////////////////////////////////////////////
  22. public static String[] tests =
  23. {
  24. "CharsetTest",
  25. "EmitTest",
  26. "FileUtilTest",
  27. "StrBufTest",
  28. "StrUtilTest",
  29. "TokenizerTest",
  30. "DateTimeTest",
  31. };
  32. //////////////////////////////////////////////////////////////////////////
  33. // Test Cases
  34. //////////////////////////////////////////////////////////////////////////
  35. /**
  36. * Get the test name.
  37. */
  38. public final String testName()
  39. {
  40. return testName;
  41. }
  42. /**
  43. * Return true if we should skip this test.
  44. */
  45. public boolean skip()
  46. {
  47. return false;
  48. }
  49. /**
  50. * Run the test case - any exception thrown
  51. * is considered a failed test.
  52. */
  53. public abstract void run()
  54. throws Exception;
  55. //////////////////////////////////////////////////////////////////////////
  56. // Reflection Utils
  57. //////////////////////////////////////////////////////////////////////////
  58. public Object newInstance(Class cls, Object[] args) throws Exception
  59. {
  60. return cls.getConstructor(argsToParams(args)).newInstance(args);
  61. }
  62. public Object make(Class cls) throws Exception { return make(cls, new Object[0]); }
  63. public Object make(Class cls, Object[] args) throws Exception
  64. {
  65. return findMethod(cls, "make", args.length).invoke(null, args);
  66. }
  67. public Object invoke(Class cls, String name) throws Exception { return invoke(cls, name, new Object[0]); }
  68. public Object invoke(Class cls, String name, Object[] args) throws Exception
  69. {
  70. try
  71. {
  72. int paramCount = (args == null) ? 0 : args.length;
  73. return findMethod(cls, name, paramCount).invoke(null, args);
  74. }
  75. catch (java.lang.reflect.InvocationTargetException e)
  76. {
  77. throw (Exception)e.getCause();
  78. }
  79. }
  80. public Object invoke(Object instance, String name) throws Exception { return invoke(instance, name, new Object[0]); }
  81. public Object invoke(Object instance, String name, Object[] args) throws Exception
  82. {
  83. try
  84. {
  85. int paramCount = (args == null) ? 0 : args.length;
  86. return findMethod(instance.getClass(), name, paramCount).invoke(instance, args);
  87. }
  88. catch (java.lang.reflect.InvocationTargetException e)
  89. {
  90. throw (Exception)e.getCause();
  91. }
  92. }
  93. public java.lang.reflect.Method findMethod(Class cls, String name) throws Exception
  94. {
  95. return findMethod(cls, name, -1);
  96. }
  97. public java.lang.reflect.Method findMethod(Class cls, String name, int paramCount) throws Exception
  98. {
  99. java.lang.reflect.Method method;
  100. method = findMethod(cls, name, paramCount, cls.getMethods()); if (method != null) return method;
  101. method = findMethod(cls, name, paramCount, cls.getDeclaredMethods()); if (method != null) return method;
  102. throw new IllegalStateException("No method " + name);
  103. }
  104. public java.lang.reflect.Method findMethod(Class cls, String name, int paramCount, java.lang.reflect.Method[] methods) throws Exception
  105. {
  106. for (int i=0; i<methods.length; ++i)
  107. if (methods[i].getName().equals(name))
  108. {
  109. if (paramCount != -1 && methods[i].getParameterTypes().length != paramCount) continue;
  110. return methods[i];
  111. }
  112. return null;
  113. }
  114. public Object get(Class cls, String name) throws Exception
  115. {
  116. return cls.getField(name).get(null);
  117. }
  118. public Object get(Object instance, String name) throws Exception
  119. {
  120. if (instance instanceof Class)
  121. return ((Class)instance).getField(name).get(null);
  122. else
  123. return instance.getClass().getField(name).get(instance);
  124. }
  125. public void set(Class cls, String name, Object val) throws Exception
  126. {
  127. cls.getField(name).set(null, val);
  128. }
  129. public void set(Object instance, String name, Object val) throws Exception
  130. {
  131. if (instance instanceof Class)
  132. ((Class)instance).getField(name).set(null, val);
  133. else
  134. instance.getClass().getField(name).set(instance, val);
  135. }
  136. public Class[] argsToParams(Object[] args)
  137. {
  138. Class[] params = new Class[args.length];
  139. for (int i=0; i<params.length; ++i)
  140. params[i] = argToParam(args[i]);
  141. return params;
  142. }
  143. public Class argToParam(Object arg)
  144. {
  145. Class cls = arg.getClass();
  146. if (cls == Boolean.class) return boolean.class;
  147. if (cls == Integer.class) return int.class;
  148. if (cls == Long.class) return long.class;
  149. if (cls == Float.class) return float.class;
  150. if (cls == Double.class) return double.class;
  151. return cls;
  152. }
  153. //////////////////////////////////////////////////////////////////////////
  154. // Verify Utils
  155. //////////////////////////////////////////////////////////////////////////
  156. /**
  157. * Verify the condition is true, otherwise throw an exception.
  158. */
  159. public void verify(boolean b)
  160. {
  161. if (!b) throw new RuntimeException("Test failed");
  162. verified++;
  163. totalVerified++;
  164. }
  165. /**
  166. * Verify a and b are equal.
  167. */
  168. public void verifyEq(boolean a, boolean b)
  169. {
  170. if (a != b) throw new RuntimeException("Test failed " + a + " != " + b);
  171. verified++;
  172. totalVerified++;
  173. }
  174. /**
  175. * Verify a and b are equal.
  176. */
  177. public void verifyEq(long a, long b)
  178. {
  179. if (a != b) throw new RuntimeException("Test failed " + a + " != " + b);
  180. verified++;
  181. totalVerified++;
  182. }
  183. /**
  184. * Verify a and b are equal taking into account nulls.
  185. */
  186. public void verify(Object a, Object b)
  187. {
  188. try
  189. {
  190. verify(equals(a, b));
  191. }
  192. catch (RuntimeException e)
  193. {
  194. if (!e.getMessage().equals("Test failed")) throw e;
  195. throw new RuntimeException("Test failed " + a + " != " + b);
  196. }
  197. }
  198. /**
  199. * Equals taking into account nulls.
  200. */
  201. public static boolean equals(Object a, Object b)
  202. {
  203. if (a == null) return b == null;
  204. else if (b == null) return false;
  205. else return a.equals(b);
  206. }
  207. /**
  208. * Force test failure
  209. */
  210. public void fail()
  211. {
  212. verify(false);
  213. }
  214. //////////////////////////////////////////////////////////////////////////
  215. // Misc Utils
  216. //////////////////////////////////////////////////////////////////////////
  217. /**
  218. * Print a line to standard out.
  219. */
  220. public static void println(Object s)
  221. {
  222. System.out.println(s);
  223. }
  224. /**
  225. * Print a line to standard out if in verbose mode.
  226. */
  227. public static void verbose(Object s)
  228. {
  229. if (verbose) System.out.println(s);
  230. }
  231. /**
  232. * Get the temporary directory to use for tests.
  233. */
  234. public static File temp()
  235. {
  236. temp.mkdirs();
  237. return temp;
  238. }
  239. //////////////////////////////////////////////////////////////////////////
  240. // Main
  241. //////////////////////////////////////////////////////////////////////////
  242. static boolean runTest(String testName)
  243. {
  244. try
  245. {
  246. Class cls = Class.forName("fanx.test." + testName);
  247. Test test = (Test)cls.newInstance();
  248. test.testName = testName;
  249. if (test.skip())
  250. {
  251. println("-- Skip: sys::" + testName + " [skip]");
  252. }
  253. else
  254. {
  255. println("-- Run: sys::" + testName + "...");
  256. test.run();
  257. println(" Pass: sys::" + testName + " [" + test.verified + "]");
  258. }
  259. return true;
  260. }
  261. catch (Throwable e)
  262. {
  263. println("### Failed: " + testName);
  264. e.printStackTrace();
  265. return false;
  266. }
  267. }
  268. public static boolean test(String pattern)
  269. {
  270. temp = new File("temp");
  271. if (pattern == null) pattern = "";
  272. boolean allPassed = true;
  273. int testCount = 0;
  274. for (int i=0; i<tests.length; ++i)
  275. {
  276. if (tests[i].startsWith(pattern))
  277. {
  278. testCount++;
  279. if (!runTest(tests[i]))
  280. allPassed = false;
  281. }
  282. }
  283. try { FileUtil.delete(temp); } catch (IOException e) { e.printStackTrace(); }
  284. return allPassed;
  285. }
  286. //////////////////////////////////////////////////////////////////////////
  287. // Fields
  288. //////////////////////////////////////////////////////////////////////////
  289. public static boolean verbose;
  290. public static int totalVerified;
  291. private static File temp;
  292. private int verified;
  293. private String testName;
  294. }