Java
133 строки · 5.3 Кб
1/*
2* Copyright (C) 2012 The Guava Authors
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
17package com.google.common.testing;18
19import static com.google.common.truth.Truth.assertThat;20
21import com.google.common.base.Predicates;22import com.google.common.collect.ImmutableList;23import java.util.Arrays;24import java.util.List;25import junit.framework.TestCase;26
27/**
28* Unit tests for {@link AbstractPackageSanityTests}.
29*
30* @author Ben Yu
31*/
32public class AbstractPackageSanityTestsTest extends TestCase {33/*34* This is a public type so that the Android test runner can create an instance directly as it
35* insists upon doing. It then runs the test, which behaves exactly like this package's existing
36* PackageSanityTests. (The test would run on the JVM, too, if not for the suppression below, and
37* that would be a problem because it violates small-test rules. Note that we strip the
38* suppression externally, but it's OK because we don't enforce test-size rules there.)
39*
40* We'd just use PackageSanityTests directly, saving us from needing this separate type, but we're
41* currently skipping MediumTests on Android, and we skip them by not making them present at
42* runtime at all. I could just make _this_ test a MediumTest, but then it wouldn't run on
43* Android.... The right long-term fix is probably to get MediumTests running under Android by
44* default and then suppress them strategically as needed.
45*/
46public static final class ConcretePackageSanityTests extends AbstractPackageSanityTests {}47
48private final AbstractPackageSanityTests sanityTests = new ConcretePackageSanityTests();49
50public void testFindClassesToTest_testClass() {51assertThat(findClassesToTest(ImmutableList.of(EmptyTest.class))).isEmpty();52assertThat(findClassesToTest(ImmutableList.of(EmptyTests.class))).isEmpty();53assertThat(findClassesToTest(ImmutableList.of(EmptyTestCase.class))).isEmpty();54assertThat(findClassesToTest(ImmutableList.of(EmptyTestSuite.class))).isEmpty();55}56
57public void testFindClassesToTest_noCorrespondingTestClass() {58assertThat(findClassesToTest(ImmutableList.of(Foo.class))).containsExactly(Foo.class);59assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo2Test.class)))60.containsExactly(Foo.class);61}62
63public void testFindClassesToTest_publicApiOnly() {64sanityTests.publicApiOnly();65assertThat(findClassesToTest(ImmutableList.of(Foo.class))).isEmpty();66assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class))).contains(PublicFoo.class);67}68
69public void testFindClassesToTest_ignoreClasses() {70sanityTests.ignoreClasses(Predicates.<Object>equalTo(PublicFoo.class));71assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class))).isEmpty();72assertThat(findClassesToTest(ImmutableList.of(Foo.class))).contains(Foo.class);73}74
75public void testFindClassesToTest_ignoreUnderscores() {76assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo_Bar.class)))77.containsExactly(Foo.class, Foo_Bar.class);78sanityTests.ignoreClasses(AbstractPackageSanityTests.UNDERSCORE_IN_NAME);79assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo_Bar.class)))80.containsExactly(Foo.class);81}82
83public void testFindClassesToTest_withCorrespondingTestClassButNotExplicitlyTested() {84assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotThere"))85.containsExactly(Foo.class);86assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotPublic"))87.containsExactly(Foo.class);88}89
90public void testFindClassesToTest_withCorrespondingTestClassAndExplicitlyTested() {91ImmutableList<Class<?>> classes = ImmutableList.of(Foo.class, FooTest.class);92assertThat(findClassesToTest(classes, "testPublic")).isEmpty();93assertThat(findClassesToTest(classes, "testNotThere", "testPublic")).isEmpty();94}95
96public void testFindClassesToTest_withCorrespondingTestClass_noTestName() {97assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class)))98.containsExactly(Foo.class);99}100
101static class EmptyTestCase {}102
103static class EmptyTest {}104
105static class EmptyTests {}106
107static class EmptyTestSuite {}108
109static class Foo {}110
111static class Foo_Bar {}112
113public static class PublicFoo {}114
115static class FooTest {116@SuppressWarnings("unused") // accessed reflectively117public void testPublic() {}118
119@SuppressWarnings("unused") // accessed reflectively120void testNotPublic() {}121}122
123// Shouldn't be mistaken as Foo's test124static class Foo2Test {125@SuppressWarnings("unused") // accessed reflectively126public void testPublic() {}127}128
129private List<Class<?>> findClassesToTest(130Iterable<? extends Class<?>> classes, String... explicitTestNames) {131return sanityTests.findClassesToTest(classes, Arrays.asList(explicitTestNames));132}133}
134