1/*
2 * Copyright (C) 2015 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 org.junit.Test;
20import org.junit.runner.RunWith;
21import org.junit.runners.JUnit4;
22import vogar.target.junit3.WrongSuiteTest;
23import vogar.target.mixture.NonTestClass;
24import vogar.target.mixture.junit3.AbstractJUnit3Test;
25import vogar.target.mixture.junit3.JUnit3Test;
26import vogar.target.mixture.junit3.NotPublicConstructorTest;
27import vogar.target.mixture.junit3.TwoConstructorsTest;
28import vogar.target.mixture.junit4.AbstractJUnit4Test;
29import vogar.target.mixture.junit4.JUnit4Test;
30import vogar.target.mixture.main.MainApp;
31
32import static org.junit.Assert.assertEquals;
33
34/**
35 * Tests for {@link TestRunner}
36 */
37@RunWith(JUnit4.class)
38public class TestRunnerTest extends AbstractTestRunnerTest {
39
40    /**
41     * Make sure that the {@code --monitorPort <port>} command line option overrides the default
42     * specified in the properties.
43     */
44    @TestRunnerProperties(testClassOrPackage = "vogar.DummyTest", monitorPort = 2345)
45    @Test
46    public void testRunner_MonitorPortOverridden() throws Exception {
47        TestRunner runner = testRunnerRule.createTestRunner();
48        assertEquals(2345, (int) runner.monitorPort);
49
50        runner = testRunnerRule.createTestRunner("--monitorPort", "10");
51        assertEquals(10, (int) runner.monitorPort);
52    }
53
54    @TestRunnerProperties(testClass = Object.class)
55    @Test
56    public void testRunner_Object() throws Exception {
57        TestRunner runner = testRunnerRule.createTestRunner();
58        runner.run();
59
60        expectedResults()
61                .noRunner()
62                .completedNormally();
63    }
64
65    @TestRunnerProperties(testClass = WrongSuiteTest.class)
66    @Test
67    public void testRunner_WrongSuiteTest() throws Exception {
68        TestRunner runner = testRunnerRule.createTestRunner();
69        runner.run();
70
71        expectedResults()
72                .noRunner()
73                .completedNormally();
74    }
75
76    /**
77     * If this fails with a "No classes in package: vogar.target.mixture;" error then the tests are
78     * not being run from a JAR, add {@code -Dvogar-scan-directories-for-tests=true} to the
79     * command line to get it working properly. This is usually only a problem in IDEs.
80     */
81    @TestRunnerProperties(testClassOrPackage = "vogar.target.mixture")
82    @Test
83    public void testRunner_Mixture() throws Exception {
84        TestRunner runner = testRunnerRule.createTestRunner();
85        runner.run();
86
87        String notPublicClassOrConstructorTest
88                = "vogar.target.mixture.junit3.NotPublicClassOrConstructorTest";
89        String notPublicClassTest
90                = "vogar.target.mixture.junit3.NotPublicClassTest";
91        expectedResults()
92                .forTestClass(NonTestClass.class)
93                .unsupported()
94                .forTestClass(AbstractJUnit3Test.class)
95                .unsupported()
96                .forTestClass(JUnit3Test.class)
97                .success("testFoo")
98                .forTestClass(notPublicClassOrConstructorTest)
99                .failure("test1",
100                        "junit.framework.AssertionFailedError: Class "
101                                + notPublicClassOrConstructorTest
102                                + " has no public constructor TestCase(String name) or TestCase()\n")
103                .failure("test2",
104                        "junit.framework.AssertionFailedError: Class "
105                                + notPublicClassOrConstructorTest
106                                + " has no public constructor TestCase(String name) or TestCase()\n")
107                .forTestClass(notPublicClassTest)
108                .failure("test1",
109                        "junit.framework.AssertionFailedError: Class "
110                                + notPublicClassTest + " is not public\n")
111                .failure("test2",
112                        "junit.framework.AssertionFailedError: Class "
113                                + notPublicClassTest + " is not public\n")
114                .forTestClass(NotPublicConstructorTest.class)
115                .failure("test1",
116                        "junit.framework.AssertionFailedError: Class "
117                                + NotPublicConstructorTest.class.getName()
118                                + " has no public constructor TestCase(String name) or TestCase()\n")
119                .failure("test2",
120                        "junit.framework.AssertionFailedError: Class "
121                                + NotPublicConstructorTest.class.getName()
122                                + " has no public constructor TestCase(String name) or TestCase()\n")
123                .forTestClass(TwoConstructorsTest.class)
124                .success("test")
125                .forTestClass(AbstractJUnit4Test.class)
126                .unsupported()
127                .forTestClass(JUnit4Test.class)
128                .success("testBar")
129                .success("testBaz")
130                .forTestClass(MainApp.class)
131                .success("main", "Main\n")
132                .completedNormally();
133    }
134}
135