• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.reflect;
18 
19 import com.google.common.testing.EqualsTester;
20 import com.google.common.testing.NullPointerTester;
21 
22 import junit.framework.TestCase;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.lang.reflect.Constructor;
27 
28 /**
29  * Unit tests of {@link Element}.
30  *
31  * @author Ben Yu
32  */
33 public class ElementTest extends TestCase {
34 
testPrivateField()35   public void testPrivateField() throws Exception {
36     Element element = A.field("privateField");
37     assertTrue(element.isPrivate());
38     assertFalse(element.isAbstract());
39     assertFalse(element.isPackagePrivate());
40     assertFalse(element.isProtected());
41     assertFalse(element.isPublic());
42     assertFalse(element.isFinal());
43     assertFalse(element.isStatic());
44     assertTrue(element.isAnnotationPresent(Tested.class));
45   }
46 
testPackagePrivateField()47   public void testPackagePrivateField() throws Exception {
48     Element element = A.field("packagePrivateField");
49     assertFalse(element.isPrivate());
50     assertTrue(element.isPackagePrivate());
51     assertFalse(element.isProtected());
52     assertFalse(element.isPublic());
53     assertFalse(element.isFinal());
54     assertFalse(element.isStatic());
55     assertTrue(element.isAnnotationPresent(Tested.class));
56   }
57 
testProtectedField()58   public void testProtectedField() throws Exception {
59     Element element = A.field("protectedField");
60     assertFalse(element.isPrivate());
61     assertFalse(element.isPackagePrivate());
62     assertTrue(element.isProtected());
63     assertFalse(element.isPublic());
64     assertFalse(element.isFinal());
65     assertFalse(element.isStatic());
66     assertTrue(element.isAnnotationPresent(Tested.class));
67   }
68 
testPublicField()69   public void testPublicField() throws Exception {
70     Element element = A.field("publicField");
71     assertFalse(element.isPrivate());
72     assertFalse(element.isPackagePrivate());
73     assertFalse(element.isProtected());
74     assertTrue(element.isPublic());
75     assertFalse(element.isFinal());
76     assertFalse(element.isStatic());
77     assertTrue(element.isAnnotationPresent(Tested.class));
78   }
79 
testFinalField()80   public void testFinalField() throws Exception {
81     Element element = A.field("finalField");
82     assertTrue(element.isFinal());
83     assertFalse(element.isStatic());
84     assertTrue(element.isAnnotationPresent(Tested.class));
85   }
86 
testStaticField()87   public void testStaticField() throws Exception {
88     Element element = A.field("staticField");
89     assertTrue(element.isStatic());
90     assertTrue(element.isAnnotationPresent(Tested.class));
91   }
92 
testVolatileField()93   public void testVolatileField() throws Exception {
94     Element element = A.field("volatileField");
95     assertTrue(element.isVolatile());
96   }
97 
testTransientField()98   public void testTransientField() throws Exception {
99     Element element = A.field("transientField");
100     assertTrue(element.isTransient());
101   }
102 
testConstructor()103   public void testConstructor() throws Exception {
104     Element element = A.constructor();
105     assertTrue(element.isPublic());
106     assertFalse(element.isPackagePrivate());
107     assertFalse(element.isAbstract());
108     assertFalse(element.isStatic());
109     assertTrue(element.isAnnotationPresent(Tested.class));
110   }
111 
testAbstractMethod()112   public void testAbstractMethod() throws Exception {
113     Element element = A.method("abstractMethod");
114     assertTrue(element.isPackagePrivate());
115     assertTrue(element.isAbstract());
116     assertFalse(element.isFinal());
117     assertTrue(element.isAnnotationPresent(Tested.class));
118   }
119 
testOverridableMethod()120   public void testOverridableMethod() throws Exception {
121     Element element = A.method("overridableMethod");
122     assertTrue(element.isPackagePrivate());
123     assertFalse(element.isAbstract());
124     assertFalse(element.isFinal());
125     assertTrue(element.isAnnotationPresent(Tested.class));
126   }
127 
testPrivateMethod()128   public void testPrivateMethod() throws Exception {
129     Element element = A.method("privateMethod");
130     assertFalse(element.isAbstract());
131     assertTrue(element.isPrivate());
132     assertFalse(element.isPackagePrivate());
133     assertFalse(element.isPublic());
134     assertFalse(element.isProtected());
135     assertTrue(element.isAnnotationPresent(Tested.class));
136   }
137 
testProtectedMethod()138   public void testProtectedMethod() throws Exception {
139     Element element = A.method("protectedMethod");
140     assertFalse(element.isAbstract());
141     assertFalse(element.isPrivate());
142     assertFalse(element.isPackagePrivate());
143     assertFalse(element.isFinal());
144     assertFalse(element.isPublic());
145     assertTrue(element.isProtected());
146     assertTrue(element.isAnnotationPresent(Tested.class));
147   }
148 
testFinalMethod()149   public void testFinalMethod() throws Exception {
150     Element element = A.method("publicFinalMethod");
151     assertFalse(element.isAbstract());
152     assertFalse(element.isPrivate());
153     assertTrue(element.isFinal());
154     assertTrue(element.isPublic());
155     assertTrue(element.isAnnotationPresent(Tested.class));
156   }
157 
testNativeMethod()158   public void testNativeMethod() throws Exception {
159     Element element = A.method("nativeMethod");
160     assertTrue(element.isNative());
161     assertTrue(element.isPackagePrivate());
162   }
163 
testSynchronizedMethod()164   public void testSynchronizedMethod() throws Exception {
165     Element element = A.method("synchronizedMethod");
166     assertTrue(element.isSynchronized());
167   }
168 
testUnannotatedMethod()169   public void testUnannotatedMethod() throws Exception {
170     Element element = A.method("notAnnotatedMethod");
171     assertFalse(element.isAnnotationPresent(Tested.class));
172   }
173 
testEquals()174   public void testEquals() throws Exception {
175     new EqualsTester()
176         .addEqualityGroup(A.field("privateField"), A.field("privateField"))
177         .addEqualityGroup(A.field("publicField"))
178         .addEqualityGroup(A.constructor(), A.constructor())
179         .addEqualityGroup(A.method("privateMethod"), A.method("privateMethod"))
180         .addEqualityGroup(A.method("publicFinalMethod"))
181         .testEquals();
182   }
183 
testNulls()184   public void testNulls() {
185     new NullPointerTester()
186         .testAllPublicStaticMethods(Element.class);
187   }
188 
189   @Retention(RetentionPolicy.RUNTIME)
190   private @interface Tested {}
191 
192   private static abstract class A {
193     @Tested private boolean privateField;
194     @Tested int packagePrivateField;
195     @Tested protected int protectedField;
196     @Tested public String publicField;
197     @Tested private static Iterable<String> staticField;
198     @Tested private final Object finalField;
199     private volatile char volatileField;
200     private transient long transientField;
201 
A(Object finalField)202     @Tested public A(Object finalField) {
203       this.finalField = finalField;
204     }
205 
abstractMethod()206     @Tested abstract void abstractMethod();
207 
overridableMethod()208     @Tested void overridableMethod() {}
209 
protectedMethod()210     @Tested protected void protectedMethod() {}
211 
privateMethod()212     @Tested private void privateMethod() {}
213 
publicFinalMethod()214     @Tested public final void publicFinalMethod() {}
215 
notAnnotatedMethod()216     void notAnnotatedMethod() {}
217 
field(String name)218     static Element field(String name) throws Exception {
219       Element element = new Element(A.class.getDeclaredField(name));
220       assertEquals(name, element.getName());
221       assertEquals(A.class, element.getDeclaringClass());
222       return element;
223     }
224 
constructor()225     static Element constructor() throws Exception {
226       Constructor<?> constructor = A.class.getDeclaredConstructor(Object.class);
227       Element element = new Element(constructor);
228       assertEquals(constructor.getName(), element.getName());
229       assertEquals(A.class, element.getDeclaringClass());
230       return element;
231     }
232 
method(String name, Class<?>... parameterTypes)233     static Element method(String name, Class<?>... parameterTypes) throws Exception {
234       Element element = new Element(A.class.getDeclaredMethod(name, parameterTypes));
235       assertEquals(name, element.getName());
236       assertEquals(A.class, element.getDeclaringClass());
237       return element;
238     }
239 
nativeMethod()240     native void nativeMethod();
241 
synchronizedMethod()242     synchronized void synchronizedMethod() {}
243   }
244 }
245