-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestExecutor.java
More file actions
195 lines (160 loc) · 6.33 KB
/
TestExecutor.java
File metadata and controls
195 lines (160 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package ru.spbau.mit.alyokhina;
import ru.spbau.mit.alyokhina.annotation.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
/** Class for tests run */
public class TestExecutor {
/** Instance of class for calling tests */
private Object instance;
/** Class name for test runs */
private String testClassName;
/** Methods with annotation Before */
private List<Method> before = new ArrayList<>();
/** Methods with annotation BeforeClass */
private List<Method> beforeClass = new ArrayList<>();
/** Methods with annotation After */
private List<Method> after = new ArrayList<>();
/** Methods with annotation AfterClass */
private List<Method> afterClass = new ArrayList<>();
/** Methods with annotation Test */
private List<Method> tests = new ArrayList<>();
/**
* Constructor
* @param clazz class from which tests will be run
* @throws IllegalAccessException if newInstance threw IllegalAccessException
* @throws InstantiationException if newInstance threw InstantiationException
* @throws TestExecutorException if if wrong number methods with annotation
*/
public TestExecutor(Class<?> clazz) throws IllegalAccessException, InstantiationException, TestExecutorException {
getMethods(clazz);
instance = clazz.newInstance();
testClassName = clazz.getName();
}
/**
* Run tests
* @return list of results of each test
* @throws InvocationTargetException if we catch exception from the BeforeClass or the AfterClass
* @throws IllegalAccessException if invoke threw InvocationTargetException
*/
public List<TestResult> run() throws InvocationTargetException, IllegalAccessException {
List<TestResult> results = new ArrayList<>();
if (beforeClass.size() != 0) {
beforeClass.get(0).setAccessible(true);
beforeClass.get(0).invoke(instance);
}
for (Method method : tests) {
method.setAccessible(true);
results.add(invoke(method));
}
if (afterClass.size() != 0) {
afterClass.get(0).setAccessible(true);
afterClass.get(0).invoke(instance);
}
return results;
}
/**
*
* @param time test run time
* @param className class name for test
* @param testName test name
* @param isFail test failure
* @param causeOfIgnoring reason for which the test was ignored
* @param e Exception that was thrown by the test
* @return information about test in interface TestResult
*/
private TestResult getResult(final long time, final String className, final String testName,
final boolean isFail, final String causeOfIgnoring, final Exception e) {
return new TestResult() {
@Override
public long getTime() {
return time;
}
@Override
public String getClassName() {
return className;
}
@Override
public String getTestName() {
return testName;
}
@Override
public boolean isFail() {
return isFail;
}
@Override
public String causeOfIgnoring() {
return causeOfIgnoring;
}
@Override
public Exception getException() {
return e;
}
};
}
/**
* invoke method
* @param method method that will be called
* @return information about passing the test
* @throws IllegalAccessException if invoke threw IllegalAccessException
*/
private TestResult invoke(final Method method) throws IllegalAccessException {
final Test testAnnotation = method.getAnnotation(Test.class);
if (!testAnnotation.ignore().equals("")) {
return getResult(0, testClassName, method.getName(), false, testAnnotation.ignore(), null);
}
long startTimer = System.currentTimeMillis();
Exception exception = null;
try {
if (before.size() != 0) {
before.get(0).setAccessible(true);
before.get(0).invoke(instance);
}
method.invoke(instance);
if (after.size() != 0) {
after.get(0).setAccessible(true);
after.get(0).invoke(instance);
}
} catch (InvocationTargetException e) {
exception = (Exception) e.getCause();
}
long endTimer = System.currentTimeMillis();
if ((exception != null && !testAnnotation.expected().isInstance(exception)) ||
(exception == null && !testAnnotation.expected().equals(Test.IgnoredThrowable.class))) {
return getResult(endTimer - startTimer, testClassName, method.getName(), true, "", exception);
}
return getResult(endTimer - startTimer, testClassName, method.getName(), false, "", exception);
}
/**
* Group methods with annotation in class
* @param testClazz class for test
* @throws TestExecutorException if wrong number methods with annotation
*/
private void getMethods(Class<?> testClazz) throws TestExecutorException {
Class[] classes = {After.class, AfterClass.class, Before.class, BeforeClass.class, Test.class};
List<List<Method>> lists = new ArrayList<>();
lists.add(after);
lists.add(afterClass);
lists.add(before);
lists.add(beforeClass);
lists.add(tests);
for (Method method : testClazz.getDeclaredMethods()) {
boolean flag = false;
for (int i = 0; i < classes.length; i++) {
if (method.getAnnotation(classes[i]) != null) {
if (flag) {
throw new TestExecutorException("too much annotations for method " + method.getName());
}
lists.get(i).add(method);
flag = true;
}
}
}
for (int i = 0; i < lists.size() - 1; i++) {
if (lists.get(i).size() > 1) {
throw new TestExecutorException("too much methods with annotation " + classes[i].getName());
}
}
}
}