Home | History | Annotate | Download | only in runner
      1 /*
      2  * Copyright (C) 2012 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 package com.android.test.runner;
     17 
     18 import junit.framework.Assert;
     19 import junit.framework.TestCase;
     20 
     21 import org.junit.Before;
     22 import org.junit.Test;
     23 import org.junit.runner.RunWith;
     24 import org.junit.runners.Parameterized;
     25 
     26 import java.io.ByteArrayOutputStream;
     27 import java.io.PrintStream;
     28 
     29 /**
     30  * Unit tests for {@link TestLoader}.
     31  */
     32 public class TestLoaderTest {
     33 
     34     public static class JUnit3Test extends TestCase {
     35     }
     36 
     37     public static abstract class AbstractTest extends TestCase {
     38     }
     39 
     40     public static class JUnit4Test {
     41         @Test
     42         public void thisIsATest() {
     43         }
     44     }
     45 
     46     @RunWith(value = Parameterized.class)
     47     public static class JUnit4RunTest {
     48         public void thisIsMayBeATest() {
     49         }
     50     }
     51 
     52     public static class NotATest {
     53         public void thisIsNotATest() {
     54         }
     55     }
     56 
     57     private TestLoader mLoader;
     58 
     59     @Before
     60     public void setUp() throws Exception {
     61         mLoader = new TestLoader(new PrintStream(new ByteArrayOutputStream()));
     62     }
     63 
     64     @Test
     65     public void testLoadTests_junit3() {
     66         assertLoadTestSuccess(JUnit3Test.class);
     67     }
     68 
     69     @Test
     70     public void testLoadTests_junit4() {
     71         assertLoadTestSuccess(JUnit4Test.class);
     72     }
     73 
     74     @Test
     75     public void testLoadTests_runWith() {
     76         assertLoadTestSuccess(JUnit4RunTest.class);
     77     }
     78 
     79     @Test
     80     public void testLoadTests_notATest() {
     81         Assert.assertNull(mLoader.loadIfTest(NotATest.class.getName()));
     82         Assert.assertEquals(0, mLoader.getLoadedClasses().size());
     83         Assert.assertEquals(0, mLoader.getLoadFailures().size());
     84     }
     85 
     86     @Test
     87     public void testLoadTests_notExist() {
     88         Assert.assertNull(mLoader.loadIfTest("notexist"));
     89         Assert.assertEquals(0, mLoader.getLoadedClasses().size());
     90         Assert.assertEquals(1, mLoader.getLoadFailures().size());
     91     }
     92 
     93     private void assertLoadTestSuccess(Class<?> clazz) {
     94         Assert.assertNotNull(mLoader.loadIfTest(clazz.getName()));
     95         Assert.assertEquals(1, mLoader.getLoadedClasses().size());
     96         Assert.assertEquals(0, mLoader.getLoadFailures().size());
     97         Assert.assertTrue(mLoader.getLoadedClasses().contains(clazz));
     98     }
     99 
    100     @Test
    101     public void testLoadTests_abstract() {
    102         Assert.assertNull(mLoader.loadIfTest(AbstractTest.class.getName()));
    103         Assert.assertEquals(0, mLoader.getLoadedClasses().size());
    104         Assert.assertEquals(0, mLoader.getLoadFailures().size());
    105     }
    106 }
    107