Skip to content

Commit aa5f364

Browse files
customize annotations
1 parent c1818bc commit aa5f364

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
4+
public class AnotherSimpleTestCase {
5+
@SteveSunFirstCustomAnnotation(expected = ArithmeticException.class)
6+
public void test4() {
7+
int a = 1/0;
8+
}
9+
10+
@SteveSunFirstCustomAnnotation(ignore = ArithmeticException.class)
11+
public void test5() {
12+
int a = 1/0;
13+
System.out.println("a = " + a);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
import java.io.IOException;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class Main {
8+
//TODO: it's partially working, or the majority part, but not completely. Make it all work.
9+
public static void main(String[] args) throws IOException {
10+
MyTestRunner runner = new MyTestRunner();
11+
12+
runner.run(AnotherSimpleTestCase.class);
13+
14+
List<Class<?>> testCaseClasses = new ArrayList<Class<?>>();
15+
testCaseClasses.add(SimpleTestCase.class);
16+
testCaseClasses.add(AnotherSimpleTestCase.class);
17+
runner.run(testCaseClasses);
18+
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.util.List;
6+
7+
public class MyTestRunner {
8+
public void run(Class<?>... klasses) {
9+
for (Class<?> testClass : klasses) {
10+
runTestClass(testClass);
11+
}
12+
}
13+
14+
//an overloaded method to the above one, so that it could accept a list of classes, not just a simple array of classes
15+
public void run(List<Class<?>> testCaseClasses) {
16+
for (Class<?> testClass : testCaseClasses) {
17+
runTestClass(testClass);
18+
}
19+
}
20+
21+
private void runTestClass(Class<?> klass) {
22+
for (Method method : klass.getMethods()) {
23+
SteveSunFirstCustomAnnotation annotation = method
24+
.getAnnotation(SteveSunFirstCustomAnnotation.class);
25+
if (annotation != null)
26+
runTestMethod(klass, method, annotation);
27+
}
28+
}
29+
30+
private void runTestMethod(Class<?> klass, Method method,
31+
SteveSunFirstCustomAnnotation annotation) {
32+
if (annotation.state() != MyTestState.ACTIVE)
33+
return;
34+
try {
35+
System.out.println("Running test: "
36+
+ getTestName(method, annotation));
37+
Object testInstance = klass.newInstance();
38+
method.invoke(testInstance);
39+
System.out.println("SUCCESS");
40+
} catch (InstantiationException e) {
41+
System.err.println("FAILED: Failed to instantiate class "
42+
+ klass.getName());
43+
} catch (IllegalAccessException e) {
44+
System.err.println("FAILED: Failed to call test method "
45+
+ method.getName());
46+
} catch (InvocationTargetException e) {
47+
checkThrowable(annotation, e.getCause());
48+
}
49+
}
50+
51+
private static String getTestName(Method method,
52+
SteveSunFirstCustomAnnotation annotation) {
53+
return !annotation.name().isEmpty() ? annotation.name() : method
54+
.getName();
55+
}
56+
57+
private void checkThrowable(SteveSunFirstCustomAnnotation annotation,
58+
Throwable th) {
59+
if (annotation.expected() == th.getClass())
60+
System.out.println("annotation.expected() executes: SUCCESS");
61+
else if (annotation.ignore() == th.getClass()) {
62+
System.out.println("annotation.ignore() executes: SUCCESS");
63+
} else {
64+
System.out.println("FAILED: " + th.getMessage());
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
public enum MyTestState {
4+
ACTIVE, INACTIVE
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
import org.junit.Assert;
4+
5+
public class SimpleTestCase {
6+
@SteveSunFirstCustomAnnotation(name = "test1WithCustomName", state = MyTestState.ACTIVE)
7+
public void test1() {
8+
Assert.assertEquals(2, 1 + 1);
9+
Assert.assertEquals(Integer.parseInt("-3"), -3);
10+
}
11+
12+
@SteveSunFirstCustomAnnotation(expected = NumberFormatException.class)
13+
public void test2() {
14+
Integer.parseInt("1.23ddd");
15+
}
16+
17+
@SteveSunFirstCustomAnnotation(state = MyTestState.INACTIVE)
18+
public void test3() {
19+
throw new IllegalStateException("Test case is inactive");
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package sporadic.customize_annotations_generics_wildcards_examples;
2+
3+
import java.lang.annotation.Retention;
4+
import java.lang.annotation.Target;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.ElementType;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interface SteveSunFirstCustomAnnotation {
11+
String name() default "";
12+
13+
MyTestState state() default MyTestState.ACTIVE;
14+
15+
Class<? extends Throwable> expected() default None.class;
16+
17+
static class None extends Throwable {}
18+
19+
Class<? extends Throwable> ignore() default None.class;
20+
}

0 commit comments

Comments
 (0)