• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class JUnit4Test {
38         @Test
thisIsATest()39         public void thisIsATest() {
40         }
41     }
42 
43     @RunWith(value = Parameterized.class)
44     public static class JUnit4RunTest {
thisIsMayBeATest()45         public void thisIsMayBeATest() {
46         }
47     }
48 
49     public static class NotATest {
thisIsNotATest()50         public void thisIsNotATest() {
51         }
52     }
53 
54     private TestLoader mLoader;
55 
56     @Before
setUp()57     public void setUp() throws Exception {
58         mLoader = new TestLoader(new PrintStream(new ByteArrayOutputStream()));
59     }
60 
61     @Test
testLoadTests_junit3()62     public void testLoadTests_junit3() {
63         assertLoadTestSuccess(JUnit3Test.class);
64     }
65 
66     @Test
testLoadTests_junit4()67     public void testLoadTests_junit4() {
68         assertLoadTestSuccess(JUnit4Test.class);
69     }
70 
71     @Test
testLoadTests_runWith()72     public void testLoadTests_runWith() {
73         assertLoadTestSuccess(JUnit4RunTest.class);
74     }
75 
76     @Test
testLoadTests_notATest()77     public void testLoadTests_notATest() {
78         Assert.assertNull(mLoader.loadIfTest(NotATest.class.getName()));
79         Assert.assertEquals(0, mLoader.getLoadedClasses().size());
80         Assert.assertEquals(0, mLoader.getLoadFailures().size());
81     }
82 
83     @Test
testLoadTests_notExist()84     public void testLoadTests_notExist() {
85         Assert.assertNull(mLoader.loadIfTest("notexist"));
86         Assert.assertEquals(0, mLoader.getLoadedClasses().size());
87         Assert.assertEquals(1, mLoader.getLoadFailures().size());
88     }
89 
assertLoadTestSuccess(Class<?> clazz)90     private void assertLoadTestSuccess(Class<?> clazz) {
91         Assert.assertNotNull(mLoader.loadIfTest(clazz.getName()));
92         Assert.assertEquals(1, mLoader.getLoadedClasses().size());
93         Assert.assertEquals(0, mLoader.getLoadFailures().size());
94         Assert.assertTrue(mLoader.getLoadedClasses().contains(clazz));
95     }
96 }
97