1 /*
2  * Copyright (C) 2008 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  */
16 
17 package android.test.suitebuilder;
18 
19 import com.android.internal.util.Predicate;
20 import junit.framework.TestCase;
21 
22 import java.lang.annotation.ElementType;
23 import java.lang.annotation.Retention;
24 import java.lang.annotation.RetentionPolicy;
25 import java.lang.annotation.Target;
26 import java.lang.reflect.Method;
27 
28 public class TestPredicatesTest extends TestCase {
29 
testThatMethodWithAnnotationIsReportedAsBeingAnnotated()30     public void testThatMethodWithAnnotationIsReportedAsBeingAnnotated() throws Exception {
31         assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithAnnotation"));
32         assertTrue(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithAnnotation"));
33     }
34 
testThatMethodWithOutAnnotationIsNotReportedAsBeingAnnotated()35     public void testThatMethodWithOutAnnotationIsNotReportedAsBeingAnnotated() throws Exception {
36         assertFalse(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithoutAnnotation"));
37     }
38 
testThatClassAnnotatioCausesAllMethodsToBeReportedAsBeingAnnotated()39     public void testThatClassAnnotatioCausesAllMethodsToBeReportedAsBeingAnnotated()
40             throws Exception {
41         assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithoutAnnotation"));
42     }
43 
hasExampleAnnotation(Class<? extends TestCase> aClass, String methodName)44     private boolean hasExampleAnnotation(Class<? extends TestCase> aClass, String methodName)
45             throws NoSuchMethodException {
46         Method method = aClass.getMethod(methodName);
47         TestMethod testMethod = new TestMethod(method, aClass);
48         return TestPredicates.hasAnnotation(Example.class).apply(testMethod);
49     }
50 
51     @Retention(RetentionPolicy.RUNTIME)
52     @Target({ElementType.TYPE, ElementType.METHOD})
53     public @interface Example {
54     }
55 
56     @Example
57     static class ClassWithAnnotation extends TestCase {
58 
59         @Example
testWithAnnotation()60         public void testWithAnnotation() {
61         }
62 
testWithoutAnnotation()63         public void testWithoutAnnotation() {
64         }
65     }
66 
67     static class ClassWithoutAnnotation extends TestCase {
68 
69         @Example
testWithAnnotation()70         public void testWithAnnotation() {
71         }
72 
testWithoutAnnotation()73         public void testWithoutAnnotation() {
74         }
75     }
76 
77     private static final Predicate<Object> TRUE = new Predicate<Object>() {
78         public boolean apply(Object o) {
79             return true;
80         }
81     };
82 
83     private static final Predicate<Object> FALSE = new Predicate<Object>() {
84         public boolean apply(Object o) {
85             return false;
86         }
87     };
88 
testNotPredicate()89     public void testNotPredicate() throws Exception {
90         assertTrue(TestPredicates.not(FALSE).apply(null));
91         assertFalse(TestPredicates.not(TRUE).apply(null));
92     }
93 }
94