View Javadoc
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 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  /** Tests for {@link Reflection} */
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,  // Already initialized (above)
84          B.class,
85          C.class);
86      assertEquals(3, classesInitialized);
87    }
88  
89    public void testNullPointers() {
90      new NullPointerTester().testAllPublicStaticMethods(Reflection.class);
91    }
92  }