1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.reflect;
18
19 import com.google.common.testing.NullPointerTester;
20
21 import junit.framework.TestCase;
22
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.Method;
25 import java.util.Map;
26
27
28 public class ReflectionTest extends TestCase {
29
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
38 public void testNewProxy() throws Exception {
39 Runnable runnable = Reflection.newProxy(Runnable.class, X_RETURNER);
40 assertEquals("x", runnable.toString());
41 }
42
43 public void testNewProxyCantWorkOnAClass() throws Exception {
44 try {
45 Reflection.newProxy(Object.class, X_RETURNER);
46 fail();
47 } catch (IllegalArgumentException expected) {
48 }
49 }
50
51 private static final InvocationHandler X_RETURNER = new InvocationHandler() {
52 @Override
53 public Object invoke(Object proxy, Method method, Object[] args)
54 throws Throwable {
55 return "x";
56 }
57 };
58
59 private static int classesInitialized = 0;
60 private static class A {
61 static {
62 ++classesInitialized;
63 }
64 }
65 private static class B {
66 static {
67 ++classesInitialized;
68 }
69 }
70 private static class C {
71 static {
72 ++classesInitialized;
73 }
74 }
75
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,
84 B.class,
85 C.class);
86 assertEquals(3, classesInitialized);
87 }
88
89 public void testNullPointers() {
90 new NullPointerTester().testAllPublicStaticMethods(Reflection.class);
91 }
92 }