1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package vogar.target;
18
19import java.io.ByteArrayOutputStream;
20import java.io.PrintStream;
21import java.util.concurrent.atomic.AtomicReference;
22import junit.framework.TestCase;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import vogar.Result;
27import vogar.monitor.TargetMonitor;
28import vogar.target.junit.JUnitRunner;
29import vogar.target.junit3.FailTest;
30import vogar.target.junit3.LongTest;
31import vogar.target.junit3.LongTest2;
32import vogar.target.junit3.SimpleTest;
33import vogar.target.junit3.SimpleTest2;
34import vogar.target.junit3.SuiteTest;
35import vogar.target.junit3.WrongSuiteTest;
36
37// This test class is designed for both JUnit3 and newer than JUnit4.8.2
38// because it can work for original vogar which uses JUnit3 and new vogar which uses JUnit4.8.2
39// If you test it with JUnit4, please test with newer than JUnit4.8.2 in console because Eclipse Helios uses JUnit4.8.1
40
41// for JUnit 3: java -cp bin:lib/junit-3.8.2.jar:lib/mockito-all-1.8.5.jar junit.textui.TestRunner vogar.target.JUnitRunnerTest
42// for JUnit 4: java -cp bin:lib/junit-4.8.2.jar:lib/mockito-all-1.8.5.jar org.junit.runner.JUnitCore vogar.target.JUnitRunnerTest vogar.target.JUnit4RunnerTest
43public class JUnitRunnerTest extends TestCase {
44    private Runner runner;
45    private TargetMonitor monitor;
46    private TestEnvironment testEnvironment = new TestEnvironment();
47    private final AtomicReference<String> skipPastReference = new AtomicReference<String>();
48
49    public void setUp() {
50        runner = new JUnitRunner();
51        monitor = mock(TargetMonitor.class);
52    }
53
54    public void test_supports_should_judge_whether_Object_is_not_supported() {
55        assertEquals(false, runner.supports(Object.class));
56    }
57
58    public void test_supports_should_judge_whether_SimpleTest_which_inferits_from_TestCase_is_supported() {
59        assertEquals(true, runner.supports(SimpleTest.class));
60    }
61
62    public void test_supports_should_judge_whether_WrongSuiteTest_which_has_suite_non_static_method_is_not_supported() {
63        assertEquals(false, runner.supports(WrongSuiteTest.class));
64    }
65
66    public void test_supports_should_judge_whether_SuiteTest_which_has_suite_static_method_is_supported() {
67        assertEquals(true, runner.supports(SuiteTest.class));
68    }
69
70    public void test_init_and_run_for_SimpleTest_should_perform_test() {
71        Class<?> target = SimpleTest.class;
72        runner.init(monitor, "", null, target, skipPastReference, testEnvironment, 0, false);
73        runner.run("", null, null);
74
75        verify(monitor).outcomeStarted(runner,
76                target.getName() + "#testSimple", "");
77        verify(monitor).outcomeFinished(Result.SUCCESS);
78    }
79
80    public void test_init_and_run_for_SuiteTest_should_perform_tests() {
81        Class<?> target = SuiteTest.class;
82        runner.init(monitor, "", null, target, skipPastReference, testEnvironment, 0, false);
83        runner.run("", null, null);
84
85        verify(monitor).outcomeStarted(runner,
86                "vogar.target.junit3.SimpleTest#testSimple", "");
87        verify(monitor).outcomeStarted(runner,
88                "vogar.target.junit3.SimpleTest2#testSimple1", "");
89        verify(monitor).outcomeStarted(runner,
90                "vogar.target.junit3.SimpleTest2#testSimple2", "");
91        verify(monitor).outcomeStarted(runner,
92                "vogar.target.junit3.SimpleTest2#testSimple3", "");
93        verify(monitor, times(4)).outcomeFinished(Result.SUCCESS);
94    }
95
96    public void test_init_and_run_for_SimpleTest2_with_ActionName_should_perform_test() {
97        Class<?> target = SimpleTest2.class;
98        String actionName = "actionName";
99        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
100        runner.run("", null, null);
101
102        verify(monitor).outcomeStarted(runner,
103                target.getName() + "#testSimple1", actionName);
104        verify(monitor).outcomeStarted(runner,
105                target.getName() + "#testSimple2", actionName);
106        verify(monitor).outcomeStarted(runner,
107                target.getName() + "#testSimple3", actionName);
108        verify(monitor, times(3)).outcomeFinished(Result.SUCCESS);
109    }
110
111    public void test_init_and_run_for_SimpleTest2_limitting_to_1method_should_perform_test() {
112        Class<?> target = SimpleTest2.class;
113        String actionName = "actionName";
114        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
115        runner.run("", null, new String[] { "testSimple2" });
116
117        verify(monitor).outcomeStarted(runner,
118                target.getName() + "#testSimple2", actionName);
119        verify(monitor).outcomeFinished(Result.SUCCESS);
120    }
121
122    public void test_init_and_run_for_SimpleTest2_limitting_to_2methods_should_perform_test() {
123        Class<?> target = SimpleTest2.class;
124        String actionName = "actionName";
125        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
126        runner.run("", null, new String[] { "testSimple2", "testSimple3" });
127
128        verify(monitor).outcomeStarted(runner,
129                target.getName() + "#testSimple2", actionName);
130        verify(monitor).outcomeStarted(runner,
131                target.getName() + "#testSimple3", actionName);
132        verify(monitor, times(2)).outcomeFinished(Result.SUCCESS);
133    }
134
135    public void test_init_limitting_to_1method_and_run_for_SimpleTest2_should_perform_test() {
136        Class<?> target = SimpleTest2.class;
137        String actionName = "actionName";
138        runner.init(monitor, actionName, "testSimple2", target, skipPastReference, testEnvironment, 0, false);
139        runner.run("", null, null);
140
141        verify(monitor).outcomeStarted(runner,
142                target.getName() + "#testSimple2", actionName);
143        verify(monitor).outcomeFinished(Result.SUCCESS);
144    }
145
146    // JUnit3 can't perform test by indicating test method in test suite
147    public void test_init_limitting_to_1method_and_run_for_SuiteTest_should_throw_exception() {
148        Class<?> target = SuiteTest.class;
149
150        try {
151            runner.init(monitor, "", "testSimple", target, skipPastReference, testEnvironment, 0, false);
152            runner.run("", null, null);
153            fail("should throw ClassCastException.");
154        } catch (ClassCastException e) {
155        }
156    }
157
158    public void test_init_limitting_to_wrong_1method_and_run_for_SimpleTest2_should_fail_test() {
159        Class<?> target = SimpleTest2.class;
160        String actionName = "actionName";
161        ByteArrayOutputStream baos = new ByteArrayOutputStream();
162        System.setOut(new PrintStream(baos));
163
164        runner.init(monitor, actionName, "testSimple5", target, skipPastReference, testEnvironment, 0, false);
165        runner.run("", null, null);
166
167        verify(monitor).outcomeStarted(runner,
168                target.getName() + "#testSimple5", actionName);
169        verify(monitor).outcomeFinished(Result.EXEC_FAILED);
170
171        String outStr = baos.toString();
172        assertTrue(outStr
173                .contains("junit.framework.AssertionFailedError: Method " + '"'
174                        + "testSimple5" + '"' + " not found"));
175    }
176
177    public void test_init_and_run_for_SimpleTest2_limitting_to_1method_with_both_init_and_run_should_perform_test() {
178        Class<?> target = SimpleTest2.class;
179        String actionName = "actionName";
180        runner.init(monitor, actionName, "testSimple3", target, skipPastReference, testEnvironment, 0, false);
181        runner.run("", null, new String[] { "testSimple2" });
182
183        verify(monitor).outcomeStarted(runner,
184                target.getName() + "#testSimple2", actionName);
185        verify(monitor).outcomeFinished(Result.SUCCESS);
186    }
187
188    public void test_init_and_run_for_FailTest_should_perform_test() {
189        Class<?> target = FailTest.class;
190        String actionName = "actionName";
191
192        ByteArrayOutputStream baos = new ByteArrayOutputStream();
193        System.setOut(new PrintStream(baos));
194
195        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
196        runner.run("", null, null);
197
198        verify(monitor).outcomeStarted(runner,
199                target.getName() + "#testSuccess", actionName);
200        verify(monitor).outcomeStarted(runner, target.getName() + "#testFail",
201                actionName);
202        verify(monitor).outcomeStarted(runner,
203                target.getName() + "#testThrowException", actionName);
204        verify(monitor).outcomeFinished(Result.SUCCESS);
205        verify(monitor, times(2)).outcomeFinished(Result.EXEC_FAILED);
206
207        String outStr = baos.toString();
208        assertTrue(outStr
209                .contains("junit.framework.AssertionFailedError: failed."));
210        assertTrue(outStr.contains("java.lang.RuntimeException: exceptrion"));
211    }
212
213    public void test_init_and_run_for_LongTest_with_time_limit_should_report_time_out() {
214        Class<?> target = LongTest.class;
215        String actionName = "actionName";
216
217        ByteArrayOutputStream baos = new ByteArrayOutputStream();
218        System.setOut(new PrintStream(baos));
219
220        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
221        runner.run("", null, null);
222
223        verify(monitor).outcomeStarted(runner, target.getName() + "#test",
224                actionName);
225        verify(monitor).outcomeFinished(Result.EXEC_FAILED);
226
227        String outStr = baos.toString();
228        assertTrue(outStr.contains("java.util.concurrent.TimeoutException"));
229    }
230
231    public void test_init_and_run_for_LongTest2_with_time_limit_should_not_report_time_out() {
232        Class<?> target = LongTest2.class;
233        String actionName = "actionName";
234
235        runner.init(monitor, actionName, null, target, skipPastReference, testEnvironment, 0, false);
236        runner.run("", null, null);
237
238        verify(monitor, times(8)).outcomeFinished(Result.SUCCESS);
239    }
240}
241