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 */
16package com.android.test.runner;
17
18import junit.framework.Assert;
19import junit.framework.TestCase;
20
21import org.junit.Before;
22import org.junit.Test;
23import org.junit.runner.RunWith;
24import org.junit.runners.Parameterized;
25
26import java.io.ByteArrayOutputStream;
27import java.io.PrintStream;
28
29/**
30 * Unit tests for {@link TestLoader}.
31 */
32public class TestLoaderTest {
33
34    public static class JUnit3Test extends TestCase {
35    }
36
37    public static class JUnit4Test {
38        @Test
39        public void thisIsATest() {
40        }
41    }
42
43    @RunWith(value = Parameterized.class)
44    public static class JUnit4RunTest {
45        public void thisIsMayBeATest() {
46        }
47    }
48
49    public static class NotATest {
50        public void thisIsNotATest() {
51        }
52    }
53
54    private TestLoader mLoader;
55
56    @Before
57    public void setUp() throws Exception {
58        mLoader = new TestLoader(new PrintStream(new ByteArrayOutputStream()));
59    }
60
61    @Test
62    public void testLoadTests_junit3() {
63        assertLoadTestSuccess(JUnit3Test.class);
64    }
65
66    @Test
67    public void testLoadTests_junit4() {
68        assertLoadTestSuccess(JUnit4Test.class);
69    }
70
71    @Test
72    public void testLoadTests_runWith() {
73        assertLoadTestSuccess(JUnit4RunTest.class);
74    }
75
76    @Test
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
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
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