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