1/*
2 * Copyright (C) 2016 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 libcore.java.lang.reflect.annotations;
18
19import junit.framework.TestCase;
20
21import java.lang.annotation.Annotation;
22import java.lang.reflect.Constructor;
23import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationA;
24import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.AnnotationC;
25import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Container;
26import libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.Repeated;
27
28import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.EXPECT_EMPTY;
29import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.checkAnnotatedElementPresentMethods;
30import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertGetDeclaredAnnotation;
31import static libcore.java.lang.reflect.annotations.AnnotatedElementTestSupport.assertIsAnnotationPresent;
32
33public class ConstructorTest extends TestCase {
34
35    private static class Type {
36        @AnnotationA
37        @AnnotationC
38        public Type() {}
39    }
40
41    public void testConstructorAnnotations() throws Exception {
42        Constructor<Type> constructor = Type.class.getConstructor();
43        checkAnnotatedElementPresentMethods(constructor, AnnotationA.class, AnnotationC.class);
44    }
45
46    // A class with multiple constructors that differ by their argument count.
47    private static class AnnotatedClass {
48        @Repeated(1)
49        public AnnotatedClass() {}
50
51        @Repeated(1)
52        @Repeated(2)
53        public AnnotatedClass(int a) {}
54
55        @Container({@Repeated(1)})
56        public AnnotatedClass(int a, int b) {}
57
58        @Repeated(1)
59        @Container({@Repeated(2), @Repeated(3)})
60        public AnnotatedClass(int a, int b, int c) {}
61
62        public AnnotatedClass(int a, int b, int c, int d) {}
63    }
64
65    // Tests for isAnnotationPresent and getDeclaredAnnotation.
66    public void testDeclaredAnnotation() throws Exception {
67        Class<?> c = AnnotatedClass.class;
68
69        Class<? extends Annotation> repeated = Repeated.class;
70        checkDeclaredAnnotation(c, 4, repeated, null);
71        checkDeclaredAnnotation(c, 3, repeated, "@Repeated(1)");
72        checkDeclaredAnnotation(c, 2, repeated, null);
73        checkDeclaredAnnotation(c, 1, repeated, null);
74        checkDeclaredAnnotation(c, 0, repeated, "@Repeated(1)");
75
76        Class<? extends Annotation> container = Container.class;
77        checkDeclaredAnnotation(c, 4, container, null);
78        checkDeclaredAnnotation(c, 3, container, "@Container({@Repeated(2), @Repeated(3)})");
79        checkDeclaredAnnotation(c, 2, container, "@Container({@Repeated(1)})");
80        checkDeclaredAnnotation(c, 1, container, "@Container({@Repeated(1), @Repeated(2)})");
81        checkDeclaredAnnotation(c, 0, container, null);
82    }
83
84    private static void checkDeclaredAnnotation(Class<?> c, int constructorArgCount,
85            Class<? extends Annotation> annotationType,
86            String expectedAnnotationString) throws Exception {
87        Constructor constructor = getConstructor(c, constructorArgCount);
88
89        // isAnnotationPresent
90        assertIsAnnotationPresent(constructor, annotationType,
91                expectedAnnotationString != null);
92
93        // getDeclaredAnnotation
94        assertGetDeclaredAnnotation(constructor, annotationType, expectedAnnotationString);
95    }
96
97    public void testGetDeclaredAnnotationsByType() throws Exception {
98        Class<?> c = AnnotatedClass.class;
99
100        Class<? extends Annotation> repeated = Repeated.class;
101        assertGetDeclaredAnnotationsByType(c, 4, repeated, EXPECT_EMPTY);
102        assertGetDeclaredAnnotationsByType(c, 3, repeated,
103                "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
104        assertGetDeclaredAnnotationsByType(c, 2, repeated, "@Repeated(1)");
105        assertGetDeclaredAnnotationsByType(c, 1, repeated, "@Repeated(1)", "@Repeated(2)");
106        assertGetDeclaredAnnotationsByType(c, 0, repeated, "@Repeated(1)");
107
108        Class<? extends Annotation> container = Container.class;
109        assertGetDeclaredAnnotationsByType(c, 4, container, EXPECT_EMPTY);
110        assertGetDeclaredAnnotationsByType(c, 3, container,
111                "@Container({@Repeated(2), @Repeated(3)})");
112        assertGetDeclaredAnnotationsByType(c, 2, container, "@Container({@Repeated(1)})");
113        assertGetDeclaredAnnotationsByType(c, 1, container,
114                "@Container({@Repeated(1), @Repeated(2)})");
115        assertGetDeclaredAnnotationsByType(c, 0, container, EXPECT_EMPTY);
116    }
117
118    private static void assertGetDeclaredAnnotationsByType(Class<?> c, int constructorArgCount,
119            Class<? extends Annotation> annotationType,
120            String... expectedAnnotationStrings) throws Exception {
121        Constructor<?> constructor = getConstructor(c, constructorArgCount);
122        AnnotatedElementTestSupport.assertGetDeclaredAnnotationsByType(
123                constructor, annotationType, expectedAnnotationStrings);
124    }
125
126    public void testGetAnnotationsByType() throws Exception {
127        Class<?> c = AnnotatedClass.class;
128
129        Class<? extends Annotation> repeated = Repeated.class;
130        assertGetAnnotationsByType(c, 4, repeated, EXPECT_EMPTY);
131        assertGetAnnotationsByType(c, 3, repeated, "@Repeated(1)", "@Repeated(2)", "@Repeated(3)");
132        assertGetAnnotationsByType(c, 2, repeated, "@Repeated(1)");
133        assertGetAnnotationsByType(c, 1, repeated, "@Repeated(1)", "@Repeated(2)");
134        assertGetAnnotationsByType(c, 0, repeated, "@Repeated(1)");
135
136        Class<? extends Annotation> container = Container.class;
137        assertGetAnnotationsByType(c, 4, container, EXPECT_EMPTY);
138        assertGetAnnotationsByType(c, 3, container, "@Container({@Repeated(2), @Repeated(3)})");
139        assertGetAnnotationsByType(c, 2, container, "@Container({@Repeated(1)})");
140        assertGetAnnotationsByType(c, 1, container, "@Container({@Repeated(1), @Repeated(2)})");
141        assertGetAnnotationsByType(c, 0, container, EXPECT_EMPTY);
142    }
143
144    private static void assertGetAnnotationsByType(Class<?> c, int constructorArgCount,
145            Class<? extends Annotation> annotationType,
146            String... expectedAnnotationStrings) throws Exception {
147        Constructor<?> constructor = getConstructor(c, constructorArgCount);
148        AnnotatedElementTestSupport.assertGetAnnotationsByType(
149                constructor, annotationType, expectedAnnotationStrings);
150    }
151
152    private static Constructor<?> getConstructor(Class<?> c, int constructorArgCount)
153            throws NoSuchMethodException {
154
155        Class<?>[] args = new Class[constructorArgCount];
156        for (int i = 0; i < constructorArgCount; i++) {
157            args[i] = Integer.TYPE;
158        }
159        return c.getDeclaredConstructor(args);
160    }
161}
162