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
17package android.test.suitebuilder.annotation;
18
19import android.test.suitebuilder.TestMethod;
20import junit.framework.TestCase;
21
22import java.lang.annotation.ElementType;
23import java.lang.annotation.Retention;
24import java.lang.annotation.RetentionPolicy;
25import java.lang.annotation.Target;
26import java.lang.reflect.Method;
27
28public class HasAnnotationTest extends TestCase {
29
30    public void testThatMethodWithAnnotationIsReportedAsBeingAnnotated() throws Exception {
31        assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithAnnotation"));
32        assertTrue(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithAnnotation"));
33    }
34
35    public void testThatMethodWithOutAnnotationIsNotReportedAsBeingAnnotated() throws Exception {
36        assertFalse(hasExampleAnnotation(ClassWithoutAnnotation.class, "testWithoutAnnotation"));
37    }
38
39    public void testThatClassAnnotatioCausesAllMethodsToBeReportedAsBeingAnnotated()
40            throws Exception {
41        assertTrue(hasExampleAnnotation(ClassWithAnnotation.class, "testWithoutAnnotation"));
42    }
43
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 new 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
60        public void testWithAnnotation() {
61        }
62
63        public void testWithoutAnnotation() {
64        }
65    }
66
67    static class ClassWithoutAnnotation extends TestCase {
68
69        @Example
70        public void testWithAnnotation() {
71        }
72
73        public void testWithoutAnnotation() {
74        }
75    }
76}
77