TestLoaderTest.java revision bb23e68d4dc19a37df318b8d169e3dfd0dd1c20e
1bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot/*
2bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * Copyright (C) 2012 The Android Open Source Project
3bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot *
4bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * Licensed under the Apache License, Version 2.0 (the "License");
5bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * you may not use this file except in compliance with the License.
6bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * You may obtain a copy of the License at
7bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot *
8bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot *      http://www.apache.org/licenses/LICENSE-2.0
9bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot *
10bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * Unless required by applicable law or agreed to in writing, software
11bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * distributed under the License is distributed on an "AS IS" BASIS,
12bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * See the License for the specific language governing permissions and
14bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * limitations under the License.
15bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot */
16bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotpackage com.android.test.runner;
17bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
18bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport com.android.test.runner.TestLoader.LoadResults;
19bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
20bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport junit.framework.Assert;
21bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport junit.framework.TestCase;
22bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
23bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport org.junit.Before;
24bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport org.junit.Test;
25bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport org.junit.runner.RunWith;
26bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport org.junit.runners.Parameterized;
27bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
28bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport java.io.ByteArrayOutputStream;
29bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport java.io.PrintStream;
30bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotimport java.util.Arrays;
31bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
32bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot/**
33bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot * Unit tests for {@link TestLoader}.
34bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot */
35bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabotpublic class TestLoaderTest {
36bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
37bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Before
38bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void setUp() throws Exception {
39bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
40bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
41bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public static class JUnit3Test extends TestCase {
42bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
43bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
44bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public static class JUnit4Test {
45bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        @Test
46bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        public void thisIsATest() {
47bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        }
48bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
49bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
50bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @RunWith(value = Parameterized.class)
51bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public static class JUnit4RunTest {
52bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        public void thisIsMayBeATest() {
53bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        }
54bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
55bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
56bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public static class NotATest {
57bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        public void thisIsNotATest() {
58bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        }
59bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
60bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
61bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Test
62bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void testLoadTests_junit3() {
63bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        assertLoadTestSuccess(JUnit3Test.class);
64bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
65bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
66bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Test
67bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void testLoadTests_junit4() {
68bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        assertLoadTestSuccess(JUnit4Test.class);
69bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
70bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
71bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Test
72bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void testLoadTests_runWith() {
73bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        assertLoadTestSuccess(JUnit4RunTest.class);
74bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
75bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
76bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Test
77bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void testLoadTests_notATest() {
78bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        TestLoader loader = new TestLoader();
79bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        LoadResults r = loader.loadTests(Arrays.asList(NotATest.class.getName()), new PrintStream(
80bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot                new ByteArrayOutputStream()));
81bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(0, r.getLoadedClasses().size());
82bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(0, r.getLoadFailures().size());
83bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
84bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
85bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    @Test
86bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    public void testLoadTests_notExist() {
87bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        TestLoader loader = new TestLoader();
88bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        LoadResults r = loader.loadTests(Arrays.asList("notexist"), new PrintStream(
89bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot                new ByteArrayOutputStream()));
90bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(0, r.getLoadedClasses().size());
91bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(1, r.getLoadFailures().size());
92bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
93bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot
94bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    private void assertLoadTestSuccess(Class<?> clazz) {
95bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        TestLoader loader = new TestLoader();
96bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        LoadResults r = loader.loadTests(Arrays.asList(clazz.getName()), new PrintStream(
97bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot                new ByteArrayOutputStream()));
98bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(1, r.getLoadedClasses().size());
99bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertEquals(0, r.getLoadFailures().size());
100bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot        Assert.assertTrue(r.getLoadedClasses().contains(clazz));
101bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot    }
102bb23e68d4dc19a37df318b8d169e3dfd0dd1c20eBrett Chabot}
103