]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/Tester.java
1 package org
.argeo
.cms
.util
;
3 import java
.lang
.reflect
.Method
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Collections
;
8 import java
.util
.TreeMap
;
10 /** A generic tester based on Java assertions and functional programming. */
12 private Map
<String
, TesterStatus
> results
= Collections
.synchronizedSortedMap(new TreeMap
<>());
14 private ClassLoader classLoader
;
16 /** Use {@link Thread#getContextClassLoader()} by default. */
18 this(Thread
.currentThread().getContextClassLoader());
21 public Tester(ClassLoader classLoader
) {
22 this.classLoader
= classLoader
;
25 public void execute(String className
) {
28 clss
= classLoader
.loadClass(className
);
29 boolean assertionsEnabled
= clss
.desiredAssertionStatus();
30 if (!assertionsEnabled
)
31 throw new IllegalStateException("Test runner " + getClass().getName()
32 + " requires Java assertions to be enabled. Call the JVM with the -ea argument.");
33 } catch (Exception e1
) {
34 throw new IllegalArgumentException("Cannot initalise test for " + className
, e1
);
37 List
<Method
> methods
= findMethods(clss
);
38 if (methods
.size() == 0)
39 throw new IllegalArgumentException("No test method found in " + clss
);
40 // TODO make order more predictable?
41 for (Method method
: methods
) {
42 String uid
= method
.getDeclaringClass().getName() + "#" + method
.getName();
43 TesterStatus testStatus
= new TesterStatus(uid
);
46 beforeTest(uid
, method
);
47 obj
= clss
.getDeclaredConstructor().newInstance();
49 testStatus
.setPassed();
50 afterTestPassed(uid
, method
, obj
);
51 } catch (Exception e
) {
52 testStatus
.setFailed(e
);
53 afterTestFailed(uid
, method
, obj
, e
);
55 results
.put(uid
, testStatus
);
60 protected void beforeTest(String uid
, Method method
) {
61 // System.out.println(uid + ": STARTING");
64 protected void afterTestPassed(String uid
, Method method
, Object obj
) {
65 System
.out
.println(uid
+ ": PASSED");
68 protected void afterTestFailed(String uid
, Method method
, Object obj
, Throwable e
) {
69 System
.out
.println(uid
+ ": FAILED");
73 protected List
<Method
> findMethods(Class
<?
> clss
) {
74 List
<Method
> methods
= new ArrayList
<Method
>();
75 // Method call = getMethod(clss, "call");
79 for (Method method
: clss
.getMethods()) {
80 if (method
.getName().startsWith("test")) {
87 protected Method
getMethod(Class
<?
> clss
, String name
, Class
<?
>... parameterTypes
) {
89 return clss
.getMethod(name
, parameterTypes
);
90 } catch (NoSuchMethodException e
) {
92 } catch (SecurityException e
) {
93 throw new IllegalStateException(e
);
97 public static void main(String
[] args
) {
98 // deal with arguments
100 if (args
.length
< 1) {
101 System
.err
.println(usage());
103 throw new IllegalArgumentException();
108 Tester test
= new Tester();
110 test
.execute(className
);
111 } catch (Throwable e
) {
115 Map
<String
, TesterStatus
> r
= test
.results
;
116 for (String uid
: r
.keySet()) {
117 TesterStatus testStatus
= r
.get(uid
);
118 System
.out
.println(testStatus
);
122 public static String
usage() {
123 return "java " + Tester
.class.getName() + " [test class name]";