PageRenderTime 40ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/test/java/com/atlassian/util/profiling/object/ObjectProfilerTest.java

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