Home | History | Annotate | Download | only in main
      1 /*
      2  * Copyright (C) 2016 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.main;
     18 
     19 import com.google.common.base.Joiner;
     20 import org.junit.Test;
     21 import org.junit.runner.RunWith;
     22 import org.junit.runners.JUnit4;
     23 import vogar.RunnerType;
     24 import vogar.target.AbstractTestRunnerTest;
     25 import vogar.target.TestRunner;
     26 import vogar.target.TestRunnerProperties;
     27 
     28 /**
     29  * Tests for using TestRunner to run classes that provide a main method.
     30  */
     31 @RunWith(JUnit4.class)
     32 public class TestRunnerMainTest extends AbstractTestRunnerTest {
     33 
     34     @TestRunnerProperties(testClass = Main.class, runnerType = RunnerType.MAIN)
     35     @Test
     36     public void testConstructor_Main_RunnerType_MAIN() throws Exception {
     37         TestRunner runner = testRunnerRule.createTestRunner();
     38         runner.run();
     39 
     40         expectedResults()
     41                 .success("main", "Args: \n")
     42                 .completedNormally();
     43     }
     44 
     45     @TestRunnerProperties(testClass = Main.class)
     46     @Test
     47     public void testRunner_Main_NoArgs() throws Exception {
     48         TestRunner runner = testRunnerRule.createTestRunner();
     49         runner.run();
     50 
     51         expectedResults()
     52                 .success("main", "Args: \n")
     53                 .completedNormally();
     54     }
     55 
     56     @TestRunnerProperties(testClass = Main.class)
     57     @Test
     58     public void testRunner_Main_Args() throws Exception {
     59         TestRunner runner = testRunnerRule.createTestRunner("arg1", "arg2");
     60         runner.run();
     61 
     62         expectedResults()
     63                 .success("main", "Args: arg1, arg2\n")
     64                 .completedNormally();
     65     }
     66 
     67     public static class Main {
     68         public static void main(String[] args) {
     69             System.out.println("Args: " + Joiner.on(", ").join(args));
     70         }
     71     }
     72 
     73     @TestRunnerProperties(testClass = MainLong.class)
     74     @Test
     75     public void testRunner_MainLong_NoTimeout() throws Exception {
     76         TestRunner runner = testRunnerRule.createTestRunner();
     77         runner.run();
     78 
     79         expectedResults()
     80                 .success("main", "Args: \n")
     81                 .completedNormally();
     82     }
     83 
     84     public static class MainLong {
     85         public static void main(String[] args) throws InterruptedException {
     86             Thread.sleep(2000);
     87             System.out.println("Args: " + Joiner.on(", ").join(args));
     88         }
     89     }
     90 }
     91