Home | History | Annotate | Download | only in target
      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 
     17 package vogar.target;
     18 
     19 import java.io.ByteArrayOutputStream;
     20 import java.io.PrintStream;
     21 import java.util.List;
     22 import java.util.concurrent.atomic.AtomicReference;
     23 import junit.framework.TestCase;
     24 import static org.mockito.Mockito.mock;
     25 import static org.mockito.Mockito.times;
     26 import static org.mockito.Mockito.verify;
     27 import vogar.Result;
     28 import vogar.monitor.TargetMonitor;
     29 import vogar.target.junit.JUnitRunner;
     30 import vogar.target.junit.JUnitRunnerFactory;
     31 import vogar.target.junit.VogarTest;
     32 import vogar.target.junit3.FailTest;
     33 import vogar.target.junit3.LongTest;
     34 import vogar.target.junit3.LongTest2;
     35 import vogar.target.junit3.SimpleTest;
     36 import vogar.target.junit3.SimpleTest2;
     37 import vogar.target.junit3.SuiteTest;
     38 
     39 /**
     40  * Test of {@link JUnitRunner}
     41  */
     42 public class JUnitRunnerTest extends TestCase {
     43     private static final String[] EMPTY_ARGS = {};
     44     private TargetMonitor monitor;
     45     private TestEnvironment testEnvironment = new TestEnvironment();
     46     private final AtomicReference<String> skipPastReference = new AtomicReference<>();
     47 
     48     public void setUp() {
     49         monitor = mock(TargetMonitor.class);
     50     }
     51 
     52     public void test_run_for_SimpleTest_should_perform_test() {
     53         Class<?> target = SimpleTest.class;
     54         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
     55         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
     56         runner.run(null);
     57 
     58         verify(monitor).outcomeStarted(JUnitRunner.class,
     59                 target.getName() + "#testSimple");
     60         verify(monitor).outcomeFinished(Result.SUCCESS);
     61     }
     62 
     63     public void test_run_for_SuiteTest_should_perform_tests() {
     64         Class<?> target = SuiteTest.class;
     65         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
     66         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
     67         runner.run(null);
     68 
     69         verify(monitor).outcomeStarted(JUnitRunner.class,
     70                 "vogar.target.junit3.SimpleTest#testSimple");
     71         verify(monitor).outcomeStarted(JUnitRunner.class,
     72                 "vogar.target.junit3.SimpleTest2#testSimple1");
     73         verify(monitor).outcomeStarted(JUnitRunner.class,
     74                 "vogar.target.junit3.SimpleTest2#testSimple2");
     75         verify(monitor).outcomeStarted(JUnitRunner.class,
     76                 "vogar.target.junit3.SimpleTest2#testSimple3");
     77         verify(monitor, times(4)).outcomeFinished(Result.SUCCESS);
     78     }
     79 
     80     public void test_run_for_SimpleTest2_with_ActionName_should_perform_test() {
     81         Class<?> target = SimpleTest2.class;
     82         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
     83         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
     84         runner.run(null);
     85 
     86         verify(monitor).outcomeStarted(JUnitRunner.class,
     87                 target.getName() + "#testSimple1");
     88         verify(monitor).outcomeStarted(JUnitRunner.class,
     89                 target.getName() + "#testSimple2");
     90         verify(monitor).outcomeStarted(JUnitRunner.class,
     91                 target.getName() + "#testSimple3");
     92         verify(monitor, times(3)).outcomeFinished(Result.SUCCESS);
     93     }
     94 
     95     public void test_run_for_SimpleTest2_limiting_to_1method_should_perform_test() {
     96         Class<?> target = SimpleTest2.class;
     97         List<VogarTest> tests =
     98                 JUnitRunnerFactory.createVogarTests(target, null,  new String[] { "testSimple2" });
     99         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    100         runner.run(null);
    101 
    102         verify(monitor).outcomeStarted(JUnitRunner.class,
    103                 target.getName() + "#testSimple2");
    104         verify(monitor).outcomeFinished(Result.SUCCESS);
    105     }
    106 
    107     public void test_run_for_SimpleTest2_limiting_to_2methods_should_perform_test() {
    108         Class<?> target = SimpleTest2.class;
    109         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null,
    110                 new String[] { "testSimple2", "testSimple3" });
    111         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    112         runner.run(null);
    113 
    114         verify(monitor).outcomeStarted(JUnitRunner.class,
    115                 target.getName() + "#testSimple2");
    116         verify(monitor).outcomeStarted(JUnitRunner.class,
    117                 target.getName() + "#testSimple3");
    118         verify(monitor, times(2)).outcomeFinished(Result.SUCCESS);
    119     }
    120 
    121     public void test_limiting_to_1method_and_run_for_SimpleTest2_should_perform_test() {
    122         Class<?> target = SimpleTest2.class;
    123         List<VogarTest> tests =
    124                 JUnitRunnerFactory.createVogarTests(target, "testSimple2", EMPTY_ARGS);
    125         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    126         runner.run(null);
    127 
    128         verify(monitor).outcomeStarted(JUnitRunner.class,
    129                 target.getName() + "#testSimple2");
    130         verify(monitor).outcomeFinished(Result.SUCCESS);
    131     }
    132 
    133     public void test_limiting_to_wrong_1method_and_run_for_SimpleTest2_should_fail_test() {
    134         Class<?> target = SimpleTest2.class;
    135         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    136         System.setOut(new PrintStream(baos));
    137 
    138         List<VogarTest> tests =
    139                 JUnitRunnerFactory.createVogarTests(target, "testSimple5", EMPTY_ARGS);
    140         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    141         runner.run(null);
    142 
    143         verify(monitor).outcomeStarted(JUnitRunner.class,
    144                 target.getName() + "#testSimple5");
    145         verify(monitor).outcomeFinished(Result.EXEC_FAILED);
    146 
    147         String outStr = baos.toString();
    148         assertTrue(outStr
    149                 .contains("junit.framework.AssertionFailedError: Method " + '"'
    150                         + "testSimple5" + '"' + " not found"));
    151     }
    152 
    153     public void test_run_for_SimpleTest2_limiting_to_1method_with_both_run_should_perform_test() {
    154         Class<?> target = SimpleTest2.class;
    155         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, "testSimple3",
    156                 new String[]{"testSimple2"});
    157         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    158         runner.run(null);
    159 
    160         verify(monitor).outcomeStarted(JUnitRunner.class,
    161                 target.getName() + "#testSimple2");
    162         verify(monitor).outcomeStarted(JUnitRunner.class,
    163                 target.getName() + "#testSimple3");
    164         verify(monitor, times(2)).outcomeFinished(Result.SUCCESS);
    165     }
    166 
    167     public void test_run_for_FailTest_should_perform_test() {
    168         Class<?> target = FailTest.class;
    169 
    170         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    171         System.setOut(new PrintStream(baos));
    172 
    173         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
    174         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    175         runner.run(null);
    176 
    177         verify(monitor).outcomeStarted(JUnitRunner.class,
    178                 target.getName() + "#testSuccess");
    179         verify(monitor).outcomeStarted(JUnitRunner.class, target.getName() + "#testFail");
    180         verify(monitor).outcomeStarted(JUnitRunner.class,
    181                 target.getName() + "#testThrowException");
    182         verify(monitor).outcomeFinished(Result.SUCCESS);
    183         verify(monitor, times(2)).outcomeFinished(Result.EXEC_FAILED);
    184 
    185         String outStr = baos.toString();
    186         assertTrue(outStr
    187                 .contains("junit.framework.AssertionFailedError: failed."));
    188         assertTrue(outStr.contains("java.lang.RuntimeException: exception"));
    189     }
    190 
    191     public void test_run_for_LongTest_with_time_limit_should_report_time_out() {
    192         Class<?> target = LongTest.class;
    193 
    194         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    195         System.setOut(new PrintStream(baos));
    196 
    197         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
    198         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 1, tests);
    199         runner.run(null);
    200 
    201         verify(monitor).outcomeStarted(JUnitRunner.class, target.getName() + "#test");
    202         verify(monitor).outcomeFinished(Result.EXEC_FAILED);
    203 
    204         String outStr = baos.toString();
    205         assertTrue(outStr.contains("java.util.concurrent.TimeoutException"));
    206     }
    207 
    208     public void test_run_for_LongTest2_without_time_limit_should_not_report_time_out() {
    209         Class<?> target = LongTest2.class;
    210 
    211         List<VogarTest> tests = JUnitRunnerFactory.createVogarTests(target, null, EMPTY_ARGS);
    212         Runner runner = new JUnitRunner(monitor, skipPastReference, testEnvironment, 0, tests);
    213         runner.run(null);
    214 
    215         verify(monitor, times(8)).outcomeFinished(Result.SUCCESS);
    216     }
    217 }
    218