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 import java.util.Arrays; 24 import java.util.List; 25 import junit.framework.TestCase; 26 27 /** 28 * Unit tests for {@link AbstractPackageSanityTests}. 29 * 30 * @author Ben Yu 31 */ 32 public 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 */ 46 public static final class ConcretePackageSanityTests extends AbstractPackageSanityTests {} 47 48 private final AbstractPackageSanityTests sanityTests = new ConcretePackageSanityTests(); 49 testFindClassesToTest_testClass()50 public void testFindClassesToTest_testClass() { 51 assertThat(findClassesToTest(ImmutableList.of(EmptyTest.class))).isEmpty(); 52 assertThat(findClassesToTest(ImmutableList.of(EmptyTests.class))).isEmpty(); 53 assertThat(findClassesToTest(ImmutableList.of(EmptyTestCase.class))).isEmpty(); 54 assertThat(findClassesToTest(ImmutableList.of(EmptyTestSuite.class))).isEmpty(); 55 } 56 testFindClassesToTest_noCorrespondingTestClass()57 public void testFindClassesToTest_noCorrespondingTestClass() { 58 assertThat(findClassesToTest(ImmutableList.of(Foo.class))).containsExactly(Foo.class); 59 assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo2Test.class))) 60 .containsExactly(Foo.class); 61 } 62 testFindClassesToTest_publicApiOnly()63 public void testFindClassesToTest_publicApiOnly() { 64 sanityTests.publicApiOnly(); 65 assertThat(findClassesToTest(ImmutableList.of(Foo.class))).isEmpty(); 66 assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class))).contains(PublicFoo.class); 67 } 68 testFindClassesToTest_ignoreClasses()69 public void testFindClassesToTest_ignoreClasses() { 70 sanityTests.ignoreClasses(Predicates.<Object>equalTo(PublicFoo.class)); 71 assertThat(findClassesToTest(ImmutableList.of(PublicFoo.class))).isEmpty(); 72 assertThat(findClassesToTest(ImmutableList.of(Foo.class))).contains(Foo.class); 73 } 74 testFindClassesToTest_ignoreUnderscores()75 public void testFindClassesToTest_ignoreUnderscores() { 76 assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo_Bar.class))) 77 .containsExactly(Foo.class, Foo_Bar.class); 78 sanityTests.ignoreClasses(AbstractPackageSanityTests.UNDERSCORE_IN_NAME); 79 assertThat(findClassesToTest(ImmutableList.of(Foo.class, Foo_Bar.class))) 80 .containsExactly(Foo.class); 81 } 82 testFindClassesToTest_withCorrespondingTestClassButNotExplicitlyTested()83 public void testFindClassesToTest_withCorrespondingTestClassButNotExplicitlyTested() { 84 assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotThere")) 85 .containsExactly(Foo.class); 86 assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class), "testNotPublic")) 87 .containsExactly(Foo.class); 88 } 89 testFindClassesToTest_withCorrespondingTestClassAndExplicitlyTested()90 public void testFindClassesToTest_withCorrespondingTestClassAndExplicitlyTested() { 91 ImmutableList<Class<?>> classes = ImmutableList.of(Foo.class, FooTest.class); 92 assertThat(findClassesToTest(classes, "testPublic")).isEmpty(); 93 assertThat(findClassesToTest(classes, "testNotThere", "testPublic")).isEmpty(); 94 } 95 testFindClassesToTest_withCorrespondingTestClass_noTestName()96 public void testFindClassesToTest_withCorrespondingTestClass_noTestName() { 97 assertThat(findClassesToTest(ImmutableList.of(Foo.class, FooTest.class))) 98 .containsExactly(Foo.class); 99 } 100 101 static class EmptyTestCase {} 102 103 static class EmptyTest {} 104 105 static class EmptyTests {} 106 107 static class EmptyTestSuite {} 108 109 static class Foo {} 110 111 static class Foo_Bar {} 112 113 public static class PublicFoo {} 114 115 static class FooTest { 116 @SuppressWarnings("unused") // accessed reflectively testPublic()117 public void testPublic() {} 118 119 @SuppressWarnings("unused") // accessed reflectively testNotPublic()120 void testNotPublic() {} 121 } 122 123 // Shouldn't be mistaken as Foo's test 124 static class Foo2Test { 125 @SuppressWarnings("unused") // accessed reflectively testPublic()126 public void testPublic() {} 127 } 128 findClassesToTest( Iterable<? extends Class<?>> classes, String... explicitTestNames)129 private List<Class<?>> findClassesToTest( 130 Iterable<? extends Class<?>> classes, String... explicitTestNames) { 131 return sanityTests.findClassesToTest(classes, Arrays.asList(explicitTestNames)); 132 } 133 } 134