• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.google.common.testing;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.base.Predicates;
22 import com.google.common.collect.ImmutableList;
23 
24 import junit.framework.TestCase;
25 
26 import java.util.Arrays;
27 import java.util.List;
28 
29 /**
30  * Unit tests for {@link AbstractPackageSanityTests}.
31  *
32  * @author Ben Yu
33  */
34 public class AbstractPackageSanityTestsTest extends TestCase {
35 
36   private final AbstractPackageSanityTests sanityTests = new AbstractPackageSanityTests() {};
37 
testFindClassesToTest_testClass()38   public void testFindClassesToTest_testClass() {
39     assertThat(findClassesToTest(ImmutableList.of(EmptyTest.class)))
40         .isEmpty();
41     assertThat(findClassesToTest(ImmutableList.of(EmptyTests.class)))
42         .isEmpty();
43     assertThat(findClassesToTest(ImmutableList.of(EmptyTestCase.class)))
44         .isEmpty();
45     assertThat(findClassesToTest(ImmutableList.of(EmptyTestSuite.class)))
46         .isEmpty();
47   }
48 
testFindClassesToTest_noCorrespondingTestClass()49   public void testFindClassesToTest_noCorrespondingTestClass() {
50     assertThat(findClassesToTest(ImmutableList.of(Foo.class)))
51         .has().exactly(Foo.class).inOrder();
52     assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo2Test.class)))
53         .has().exactly(Foo.class).inOrder();
54   }
55 
testFindClassesToTest_publicApiOnly()56   public void testFindClassesToTest_publicApiOnly() {
57     sanityTests.publicApiOnly();
58     assertThat(findClassesToTest(ImmutableList.of(Foo.class)))
59         .isEmpty();
60     assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class))).has().item(PublicFoo.class);
61   }
62 
testFindClassesToTest_ignoreClasses()63   public void testFindClassesToTest_ignoreClasses() {
64     sanityTests.ignoreClasses(Predicates.<Object>equalTo(PublicFoo.class));
65     assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class)))
66         .isEmpty();
67     assertThat(findClassesToTest(ImmutableList.of(Foo.class))).has().item(Foo.class);
68   }
69 
testFindClassesToTest_withCorrespondingTestClassButNotExplicitlyTested()70   public void testFindClassesToTest_withCorrespondingTestClassButNotExplicitlyTested() {
71     assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotThere"))
72         .has().exactly(Foo.class).inOrder();
73     assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotPublic"))
74         .has().exactly(Foo.class).inOrder();
75   }
76 
testFindClassesToTest_withCorrespondingTestClassAndExplicitlyTested()77   public void testFindClassesToTest_withCorrespondingTestClassAndExplicitlyTested() {
78     ImmutableList<Class<? extends Object>> classes = ImmutableList.of(Foo.class, FooTest.class);
79     assertThat(findClassesToTest(classes, "testPublic"))
80         .isEmpty();
81     assertThat(findClassesToTest(classes, "testNotThere", "testPublic"))
82         .isEmpty();
83   }
84 
testFindClassesToTest_withCorrespondingTestClass_noTestName()85   public void testFindClassesToTest_withCorrespondingTestClass_noTestName() {
86     assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class)))
87         .has().exactly(Foo.class).inOrder();
88   }
89 
90   static class EmptyTestCase {}
91 
92   static class EmptyTest {}
93 
94   static class EmptyTests {}
95 
96   static class EmptyTestSuite {}
97 
98   static class Foo {}
99 
100   public static class PublicFoo {}
101 
102   static class FooTest {
103     @SuppressWarnings("unused") // accessed reflectively
testPublic()104     public void testPublic() {}
105     @SuppressWarnings("unused") // accessed reflectively
testNotPublic()106     void testNotPublic() {}
107   }
108 
109   // Shouldn't be mistaken as Foo's test
110   static class Foo2Test {
111     @SuppressWarnings("unused") // accessed reflectively
testPublic()112     public void testPublic() {}
113   }
114 
findClassesToTest( Iterable<? extends Class<?>> classes, String... explicitTestNames)115   private List<Class<?>> findClassesToTest(
116       Iterable<? extends Class<?>> classes, String... explicitTestNames) {
117     return sanityTests.findClassesToTest(classes, Arrays.asList(explicitTestNames));
118   }
119 }
120