-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestManager.java
More file actions
138 lines (125 loc) · 5.32 KB
/
TestManager.java
File metadata and controls
138 lines (125 loc) · 5.32 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
package lesson7.test_framework;
import lesson7.TestEx;
import lesson7.classes_for_tests.TestExJU4TestCase;
import lesson7.test_framework.my_annotations.AfterSuite;
import lesson7.test_framework.my_annotations.BeforeSuite;
import lesson7.test_framework.my_annotations.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class TestManager {
public static void main(String[] args) {
Class clazz = TestExJU4TestCase.class;
start(clazz);
}
public static void start(Class clazz) {
doIt(clazz);
}
public static void start(String className) {
Class clazz = null;
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
doIt(clazz);
}
private static void doIt(Class clazz) {
Method[] methods = clazz.getDeclaredMethods();
checkAnnotations(methods);
Annotation[] annotations = clazz.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
startMethodWithBeforeSuiteAnnotation(clazz, methods);
startMethodWithTestAnnotation(clazz, methods);
startMethodWithAfterSuiteAnnotation(clazz, methods);
}
private static void checkAnnotations(Method[] methods) {
int beforeCount = 0;
int afterCount = 0;
for (Method method : methods) {
// BeforeSuite annotation = method.getAnnotation(BeforeSuite.class);
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
// System.out.println("method " + method + " annotation: " + annotation);
if (annotation instanceof BeforeSuite) {
beforeCount++;
}
if (annotation instanceof AfterSuite) {
afterCount++;
}
}
if (beforeCount > 1 || afterCount > 1) {
throw new RuntimeException();
}
}
}
private static void startMethodWithAfterSuiteAnnotation(Class clazz, Method[] methods) {
for (Method method : methods) {
AfterSuite asAnnotaion = method.getAnnotation(AfterSuite.class);
if (asAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
private static void startMethodWithTestAnnotation(Class clazz, Method[] methods) {
for (Method method : methods) {
Test testAnnotaion = method.getAnnotation(Test.class);
if (testAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
private static void startMethodWithBeforeSuiteAnnotation(Class clazz, Method[] methods) {
for (Method method : methods) {
BeforeSuite bsAnnotaion = method.getAnnotation(BeforeSuite.class);
if (bsAnnotaion != null) {
try {
// Constructor[] declaredConstructors = clazz.getDeclaredConstructors();
Constructor<TestExJU4TestCase> defaultConstructor = clazz.getDeclaredConstructor();
TestExJU4TestCase testClass = defaultConstructor.newInstance();
method.invoke(testClass);
// method.invoke(clazz.newInstance());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
}