...3import junit.framework.JUnit4TestAdapter;4import junit.framework.TestCase;5import junit.framework.TestFailure;6import junit.framework.TestListener;7import junit.framework.TestResult;8import org.junit.After;9import org.junit.AfterClass;10import org.junit.Before;11import org.junit.BeforeClass;12import org.junit.Test;13import org.junit.runner.Description;14import org.junit.runner.RunWith;15import org.junit.runner.Runner;16import org.junit.runner.notification.RunNotifier;17public class ForwardCompatibilityTest extends TestCase {18 static String fLog;19 static public class NewTest {20 @Before21 public void before() {22 fLog += "before ";23 }24 @After25 public void after() {26 fLog += "after ";27 }28 @Test29 public void test() {30 fLog += "test ";31 }32 }33 public void testCompatibility() {34 fLog = "";35 TestResult result = new TestResult();36 junit.framework.Test adapter = new JUnit4TestAdapter(NewTest.class);37 adapter.run(result);38 assertEquals("before test after ", fLog);39 }40 public void testToString() {41 JUnit4TestAdapter adapter = new JUnit4TestAdapter(NewTest.class);42 junit.framework.Test test = adapter.getTests().get(0);43 assertEquals(String.format("test(%s)", NewTest.class.getName()), test.toString());44 }45 public void testUseGlobalCache() {46 JUnit4TestAdapter adapter1 = new JUnit4TestAdapter(NewTest.class);47 JUnit4TestAdapter adapter2 = new JUnit4TestAdapter(NewTest.class);48 assertSame(adapter1.getTests().get(0), adapter2.getTests().get(0));49 }50 static Exception exception = new Exception();51 public static class ErrorTest {52 @Test53 public void error() throws Exception {54 throw exception;55 }56 }57 public void testException() {58 TestResult result = new TestResult();59 junit.framework.Test adapter = new JUnit4TestAdapter(ErrorTest.class);60 adapter.run(result);61 assertEquals(exception, result.errors().nextElement().thrownException());62 }63 public void testNotifyResult() {64 JUnit4TestAdapter adapter = new JUnit4TestAdapter(ErrorTest.class);65 TestResult result = new TestResult();66 final StringBuffer log = new StringBuffer();67 result.addListener(new TestListener() {68 public void startTest(junit.framework.Test test) {69 log.append(" start " + test);70 }71 public void endTest(junit.framework.Test test) {72 log.append(" end " + test);73 }74 public void addFailure(junit.framework.Test test, AssertionFailedError t) {75 log.append(" failure " + test);76 }77 public void addError(junit.framework.Test test, Throwable t) {78 log.append(" error " + test);79 }80 });81 adapter.run(result);82 String testName = String.format("error(%s)", ErrorTest.class.getName());83 assertEquals(String.format(" start %s error %s end %s", testName, testName, testName), log.toString());84 }85 public static class NoExceptionTest {86 @Test(expected = Exception.class)87 public void succeed() throws Exception {88 }89 }90 public void testNoException() {91 TestResult result = new TestResult();92 junit.framework.Test adapter = new JUnit4TestAdapter(NoExceptionTest.class);93 adapter.run(result);94 assertFalse(result.wasSuccessful());95 }96 public static class ExpectedTest {97 @Test(expected = Exception.class)98 public void expected() throws Exception {99 throw new Exception();100 }101 }102 public void testExpected() {103 TestResult result = new TestResult();104 junit.framework.Test adapter = new JUnit4TestAdapter(ExpectedTest.class);105 adapter.run(result);106 assertTrue(result.wasSuccessful());107 }108 public static class UnExpectedExceptionTest {109 @Test(expected = Exception.class)110 public void expected() throws Exception {111 throw new Error();112 }113 }114 static String log;115 public static class BeforeClassTest {116 @BeforeClass117 public static void beforeClass() {118 log += "before class ";119 }120 @Before121 public void before() {122 log += "before ";123 }124 @Test125 public void one() {126 log += "test ";127 }128 @Test129 public void two() {130 log += "test ";131 }132 @After133 public void after() {134 log += "after ";135 }136 @AfterClass137 public static void afterClass() {138 log += "after class ";139 }140 }141 public void testBeforeAndAfterClass() {142 log = "";143 TestResult result = new TestResult();144 junit.framework.Test adapter = new JUnit4TestAdapter(BeforeClassTest.class);145 adapter.run(result);146 assertEquals("before class before test after before test after after class ", log);147 }148 public static class ExceptionInBeforeTest {149 @Before150 public void error() {151 throw new Error();152 }153 @Test154 public void nothing() {155 }156 }157 public void testExceptionInBefore() {158 TestResult result = new TestResult();159 junit.framework.Test adapter = new JUnit4TestAdapter(ExceptionInBeforeTest.class);160 adapter.run(result);161 assertEquals(1, result.errorCount());162 }163 public static class InvalidMethodTest {164 @BeforeClass165 public void shouldBeStatic() {166 }167 @Test168 public void aTest() {169 }170 }171 public void testInvalidMethod() {172 TestResult result = new TestResult();173 junit.framework.Test adapter = new JUnit4TestAdapter(InvalidMethodTest.class);174 adapter.run(result);175 assertEquals(1, result.errorCount());176 TestFailure failure = result.errors().nextElement();177 assertTrue(failure.exceptionMessage().contains("Method shouldBeStatic() should be static"));178 }179 private static boolean wasRun = false;180 public static class MarkerRunner extends Runner {181 public MarkerRunner(Class<?> klass) {182 }183 @Override184 public void run(RunNotifier notifier) {185 wasRun = true;186 }187 @Override188 public int testCount() {189 return 0;190 }191 @Override192 public Description getDescription() {193 return Description.EMPTY;194 }195 }196 @RunWith(MarkerRunner.class)197 public static class NoTests {198 }199 public void testRunWithClass() {200 wasRun = false;201 TestResult result = new TestResult();202 junit.framework.Test adapter = new JUnit4TestAdapter(NoTests.class);203 adapter.run(result);204 assertTrue(wasRun);205 }206 public void testToStringSuite() {207 junit.framework.Test adapter = new JUnit4TestAdapter(NoTests.class);208 assertEquals(NoTests.class.getName(), adapter.toString());209 }210}...