View Javadoc

1   package com.atlassian.util.profiling.object;
2   
3   import com.atlassian.util.profiling.UtilTimerStack;
4   import junit.framework.TestCase;
5   
6   import java.lang.reflect.Proxy;
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  /**
11   * Tests for ObjectProfiler.
12   */
13  public class ObjectProfilerTest extends TestCase
14  {
15      private boolean wasActive;
16  
17      public void testProfileMethodThatReturnsInterface() throws Exception
18      {
19          ServiceImpl svcImpl = new ServiceImpl();
20          Service svc = (Service) ObjectProfiler.getProfiledObject(Service.class, svcImpl);
21  
22          B b1 = (B) svcImpl.foo(new BImpl()); // works with Impl
23          B b2 = (B) svc.foo(new BImpl());     // proxy shouldn't break the above behaviour
24          assertTrue(Proxy.isProxyClass(b2.getClass()));
25      }
26  
27      public void testClassThatImplementsAnInterfaceDoublyShouldBeProxiable() throws Exception
28      {
29          Service svc = (Service) ObjectProfiler.getProfiledObject(Service.class, new ServiceImpl());
30  
31          Map map = svc.getMap(); // JRA-23098: shouldn't throw IllegalArgumentException
32          assertTrue(Proxy.isProxyClass(map.getClass()));
33      }
34  
35      protected void setUp() throws Exception
36      {
37          super.setUp();
38          wasActive = UtilTimerStack.isActive();
39          UtilTimerStack.setActive(true);
40      }
41  
42      protected void tearDown() throws Exception
43      {
44          UtilTimerStack.setActive(wasActive);
45          super.tearDown();
46      }
47  
48      interface A
49      {
50      }
51  
52      interface B extends A
53      {
54      }
55  
56      static class AImpl implements A
57      {
58      }
59  
60      static class BImpl implements B
61      {
62      }
63  
64      interface Service
65      {
66          // returns A
67          A foo(A a);
68  
69          Map getMap();
70      }
71  
72      static class ServiceImpl implements Service
73      {
74          public A foo(A a)
75          {
76              return a;
77          }
78  
79          public Map getMap()
80          {
81              return new M();
82          }
83      }
84  
85      static class M extends HashMap implements Map
86      {
87          // M doubly-implements java.util.Map
88      }
89  }