1 package org.junit.tests.junit3compatibility;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNull;
5 import static org.junit.Assert.assertNotNull;
6
7 import java.lang.annotation.ElementType;
8 import java.lang.annotation.Retention;
9 import java.lang.annotation.RetentionPolicy;
10 import java.lang.annotation.Target;
11 import junit.extensions.TestDecorator;
12 import junit.framework.JUnit4TestAdapter;
13 import junit.framework.TestCase;
14 import junit.framework.TestSuite;
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.junit.internal.runners.JUnit38ClassRunner;
18 import org.junit.runner.Description;
19 import org.junit.runner.JUnitCore;
20 import org.junit.runner.Result;
21 import org.junit.runner.notification.Failure;
22 import org.junit.runner.notification.RunListener;
23 import org.junit.runner.manipulation.Filter;
24 import org.junit.runner.manipulation.NoTestsRemainException;
25
26 public class JUnit38ClassRunnerTest {
27 public static class MyTest extends TestCase {
28 public void testA() {
29
30 }
31 }
32
33 @Test
34 public void plansDecoratorCorrectly() {
35 JUnit38ClassRunner runner = new JUnit38ClassRunner(new TestDecorator(new TestSuite(MyTest.class)));
36 assertEquals(1, runner.testCount());
37 }
38
39 public static class AnnotatedTest {
40 @Test
41 public void foo() {
42 Assert.fail();
43 }
44 }
45
46 @Test
47 public void canUnadaptAnAdapter() {
48 JUnit38ClassRunner runner = new JUnit38ClassRunner(new JUnit4TestAdapter(AnnotatedTest.class));
49 Result result = new JUnitCore().run(runner);
50 Failure failure = result.getFailures().get(0);
51 assertEquals(Description.createTestDescription(AnnotatedTest.class, "foo"), failure.getDescription());
52 }
53
54 static int count;
55
56 static public class OneTest extends TestCase {
57 public void testOne() {
58 }
59 }
60
61 @Test
62 public void testListener() throws Exception {
63 JUnitCore runner = new JUnitCore();
64 RunListener listener = new RunListener() {
65 @Override
66 public void testStarted(Description description) {
67 assertEquals(Description.createTestDescription(OneTest.class, "testOne"),
68 description);
69 count++;
70 }
71 };
72
73 runner.addListener(listener);
74 count = 0;
75 Result result = runner.run(OneTest.class);
76 assertEquals(1, count);
77 assertEquals(1, result.getRunCount());
78 }
79
80 public static class ClassWithInvalidMethod extends TestCase {
81 @SuppressWarnings("unused")
82 private void testInvalid() {
83 }
84 }
85
86 @Test
87 public void invalidTestMethodReportedCorrectly() {
88 Result result = JUnitCore.runClasses(ClassWithInvalidMethod.class);
89 Failure failure = result.getFailures().get(0);
90 assertEquals("warning", failure.getDescription().getMethodName());
91 assertEquals("junit.framework.TestSuite$1", failure.getDescription().getClassName());
92 }
93
94 @Retention(RetentionPolicy.RUNTIME)
95 @Target(ElementType.METHOD)
96 public static @interface MyAnnotation {
97 }
98
99 public static class JUnit3ClassWithAnnotatedMethod extends TestCase {
100 @MyAnnotation
101 public void testAnnotated() {
102 }
103
104 public void testNotAnnotated() {
105 }
106 }
107
108 public static class DerivedAnnotatedMethod extends JUnit3ClassWithAnnotatedMethod {
109 }
110
111 @Test
112 public void getDescriptionWithAnnotation() {
113 JUnit38ClassRunner runner = new JUnit38ClassRunner(JUnit3ClassWithAnnotatedMethod.class);
114 assertAnnotationFiltering(runner);
115 }
116
117 @Test
118 public void getDescriptionWithAnnotationInSuper() {
119 JUnit38ClassRunner runner = new JUnit38ClassRunner(DerivedAnnotatedMethod.class);
120 assertAnnotationFiltering(runner);
121 }
122
123 private void assertAnnotationFiltering(JUnit38ClassRunner runner) {
124 Description d = runner.getDescription();
125 assertEquals(2, d.testCount());
126 for (Description methodDesc : d.getChildren()) {
127 if (methodDesc.getMethodName().equals("testAnnotated")) {
128 assertNotNull(methodDesc.getAnnotation(MyAnnotation.class));
129 } else {
130 assertNull(methodDesc.getAnnotation(MyAnnotation.class));
131 }
132 }
133 }
134
135 public static class RejectAllTestsFilter extends Filter {
136 @Override
137 public boolean shouldRun(Description description) {
138 return description.isSuite();
139 }
140
141 @Override
142 public String describe() {
143 return "filter all";
144 }
145 }
146
147
148
149
150 @Test(expected = NoTestsRemainException.class)
151 public void filterNoTestsRemain() throws NoTestsRemainException {
152 JUnit38ClassRunner runner = new JUnit38ClassRunner(OneTest.class);
153 runner.filter(new RejectAllTestsFilter());
154 }
155 }