• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2006 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 static org.junit.Assert.assertThrows;
20 
21 import com.google.common.testing.NullPointerTester;
22 import java.lang.reflect.InvocationHandler;
23 import java.lang.reflect.Method;
24 import java.util.Map;
25 import junit.framework.TestCase;
26 
27 /** Tests for {@link Reflection} */
28 public class ReflectionTest extends TestCase {
29 
testGetPackageName()30   public void testGetPackageName() throws Exception {
31     assertEquals("java.lang", Reflection.getPackageName(Iterable.class));
32     assertEquals("java", Reflection.getPackageName("java.MyType"));
33     assertEquals("java.lang", Reflection.getPackageName(Iterable.class.getName()));
34     assertEquals("", Reflection.getPackageName("NoPackage"));
35     assertEquals("java.util", Reflection.getPackageName(Map.Entry.class));
36   }
37 
testNewProxy()38   public void testNewProxy() throws Exception {
39     Runnable runnable = Reflection.newProxy(Runnable.class, X_RETURNER);
40     assertEquals("x", runnable.toString());
41   }
42 
testNewProxyCantWorkOnAClass()43   public void testNewProxyCantWorkOnAClass() throws Exception {
44     assertThrows(
45         IllegalArgumentException.class, () -> Reflection.newProxy(Object.class, X_RETURNER));
46   }
47 
48   private static final InvocationHandler X_RETURNER =
49       new InvocationHandler() {
50         @Override
51         public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
52           return "x";
53         }
54       };
55 
56   private static int classesInitialized = 0;
57 
58   private static class A {
59     static {
60       ++classesInitialized;
61     }
62   }
63 
64   private static class B {
65     static {
66       ++classesInitialized;
67     }
68   }
69 
70   private static class C {
71     static {
72       ++classesInitialized;
73     }
74   }
75 
testInitialize()76   public void testInitialize() {
77     assertEquals("This test can't be included twice in the same suite.", 0, classesInitialized);
78 
79     Reflection.initialize(A.class);
80     assertEquals(1, classesInitialized);
81 
82     Reflection.initialize(
83         A.class, // Already initialized (above)
84         B.class, C.class);
85     assertEquals(3, classesInitialized);
86   }
87 
testNullPointers()88   public void testNullPointers() {
89     new NullPointerTester().testAllPublicStaticMethods(Reflection.class);
90   }
91 }
92