Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaa5f364

Browse files
customize annotations
1 parentc1818bc commitaa5f364

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+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
4+
publicclassAnotherSimpleTestCase {
5+
@SteveSunFirstCustomAnnotation(expected =ArithmeticException.class)
6+
publicvoidtest4() {
7+
inta =1/0;
8+
}
9+
10+
@SteveSunFirstCustomAnnotation(ignore =ArithmeticException.class)
11+
publicvoidtest5() {
12+
inta =1/0;
13+
System.out.println("a = " +a);
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
importjava.io.IOException;
4+
importjava.util.ArrayList;
5+
importjava.util.List;
6+
7+
publicclassMain {
8+
//TODO: it's partially working, or the majority part, but not completely. Make it all work.
9+
publicstaticvoidmain(String[]args)throwsIOException {
10+
MyTestRunnerrunner =newMyTestRunner();
11+
12+
runner.run(AnotherSimpleTestCase.class);
13+
14+
List<Class<?>>testCaseClasses =newArrayList<Class<?>>();
15+
testCaseClasses.add(SimpleTestCase.class);
16+
testCaseClasses.add(AnotherSimpleTestCase.class);
17+
runner.run(testCaseClasses);
18+
19+
}
20+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
importjava.lang.reflect.InvocationTargetException;
4+
importjava.lang.reflect.Method;
5+
importjava.util.List;
6+
7+
publicclassMyTestRunner {
8+
publicvoidrun(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+
publicvoidrun(List<Class<?>>testCaseClasses) {
16+
for (Class<?>testClass :testCaseClasses) {
17+
runTestClass(testClass);
18+
}
19+
}
20+
21+
privatevoidrunTestClass(Class<?>klass) {
22+
for (Methodmethod :klass.getMethods()) {
23+
SteveSunFirstCustomAnnotationannotation =method
24+
.getAnnotation(SteveSunFirstCustomAnnotation.class);
25+
if (annotation !=null)
26+
runTestMethod(klass,method,annotation);
27+
}
28+
}
29+
30+
privatevoidrunTestMethod(Class<?>klass,Methodmethod,
31+
SteveSunFirstCustomAnnotationannotation) {
32+
if (annotation.state() !=MyTestState.ACTIVE)
33+
return;
34+
try {
35+
System.out.println("Running test: "
36+
+getTestName(method,annotation));
37+
ObjecttestInstance =klass.newInstance();
38+
method.invoke(testInstance);
39+
System.out.println("SUCCESS");
40+
}catch (InstantiationExceptione) {
41+
System.err.println("FAILED: Failed to instantiate class "
42+
+klass.getName());
43+
}catch (IllegalAccessExceptione) {
44+
System.err.println("FAILED: Failed to call test method "
45+
+method.getName());
46+
}catch (InvocationTargetExceptione) {
47+
checkThrowable(annotation,e.getCause());
48+
}
49+
}
50+
51+
privatestaticStringgetTestName(Methodmethod,
52+
SteveSunFirstCustomAnnotationannotation) {
53+
return !annotation.name().isEmpty() ?annotation.name() :method
54+
.getName();
55+
}
56+
57+
privatevoidcheckThrowable(SteveSunFirstCustomAnnotationannotation,
58+
Throwableth) {
59+
if (annotation.expected() ==th.getClass())
60+
System.out.println("annotation.expected() executes: SUCCESS");
61+
elseif (annotation.ignore() ==th.getClass()) {
62+
System.out.println("annotation.ignore() executes: SUCCESS");
63+
}else {
64+
System.out.println("FAILED: " +th.getMessage());
65+
}
66+
}
67+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
publicenumMyTestState {
4+
ACTIVE,INACTIVE
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
importorg.junit.Assert;
4+
5+
publicclassSimpleTestCase {
6+
@SteveSunFirstCustomAnnotation(name ="test1WithCustomName",state =MyTestState.ACTIVE)
7+
publicvoidtest1() {
8+
Assert.assertEquals(2,1 +1);
9+
Assert.assertEquals(Integer.parseInt("-3"), -3);
10+
}
11+
12+
@SteveSunFirstCustomAnnotation(expected =NumberFormatException.class)
13+
publicvoidtest2() {
14+
Integer.parseInt("1.23ddd");
15+
}
16+
17+
@SteveSunFirstCustomAnnotation(state =MyTestState.INACTIVE)
18+
publicvoidtest3() {
19+
thrownewIllegalStateException("Test case is inactive");
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagesporadic.customize_annotations_generics_wildcards_examples;
2+
3+
importjava.lang.annotation.Retention;
4+
importjava.lang.annotation.Target;
5+
importjava.lang.annotation.RetentionPolicy;
6+
importjava.lang.annotation.ElementType;
7+
8+
@Retention(RetentionPolicy.RUNTIME)
9+
@Target(ElementType.METHOD)
10+
public @interfaceSteveSunFirstCustomAnnotation {
11+
Stringname()default"";
12+
13+
MyTestStatestate()defaultMyTestState.ACTIVE;
14+
15+
Class<?extendsThrowable>expected()defaultNone.class;
16+
17+
staticclassNoneextendsThrowable {}
18+
19+
Class<?extendsThrowable>ignore()defaultNone.class;
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp