PageRenderTime 4276ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/javamelody-core/src/test/java/net/bull/javamelody/TestMonitoringGuiceInterceptor.java

https://gitlab.com/tuandung.bui/javamelody
Java | 117 lines | 73 code | 16 blank | 28 comment | 0 complexity | ceb919af64c3d6ebcdf93025b0223cd7 MD5 | raw file
  1. /*
  2. * Copyright 2008-2016 by Emeric Vernat
  3. *
  4. * This file is part of Java Melody.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package net.bull.javamelody;
  19. import static org.junit.Assert.assertNotNull;
  20. import static org.junit.Assert.assertSame;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import com.google.inject.AbstractModule;
  24. import com.google.inject.Guice;
  25. import com.google.inject.Injector;
  26. import com.google.inject.Key;
  27. import com.google.inject.Module;
  28. import com.google.inject.name.Names;
  29. import net.bull.javamelody.TestMonitoringSpringInterceptor.AnnotatedTest;
  30. import net.bull.javamelody.TestMonitoringSpringInterceptor.AnnotatedTestClass;
  31. import net.bull.javamelody.TestMonitoringSpringInterceptor.AnnotatedTestMethod;
  32. import net.bull.javamelody.TestMonitoringSpringInterceptor.AnnotatedTestOtherClass;
  33. /**
  34. * Test unitaire de la classe MonitoringGuiceInterceptor.
  35. * @author Emeric Vernat
  36. */
  37. public class TestMonitoringGuiceInterceptor {
  38. private static final String REQUESTS_COUNT = "requestsCount";
  39. /** Check. */
  40. @Before
  41. public void setUp() {
  42. Utils.initialize();
  43. }
  44. /** Test. */
  45. @Test
  46. public void testNewInstance() {
  47. assertNotNull("new MonitoringGuiceInterceptor", new MonitoringGuiceInterceptor());
  48. }
  49. /** Test. */
  50. @Test
  51. public void testGetGuiceCounter() {
  52. assertNotNull("getGuiceCounter", MonitoringProxy.getGuiceCounter());
  53. }
  54. /** Test. */
  55. @Test
  56. public void testGuiceAOP() {
  57. final Counter guiceCounter = MonitoringProxy.getGuiceCounter();
  58. guiceCounter.clear();
  59. final Key<AnnotatedTest> annotatedTestMethodKey = Key.get(AnnotatedTest.class,
  60. Names.named("annotatedTestMethod"));
  61. final Key<AnnotatedTest> annotatedTestOtherClassKey = Key.get(AnnotatedTest.class,
  62. Names.named("annotatedTestOtherClass"));
  63. final Module testModule = new AbstractModule() {
  64. /** {@inheritDoc} */
  65. @Override
  66. protected void configure() {
  67. // configuration du monitoring Guice
  68. install(new MonitoringGuiceModule());
  69. // implémentation de test
  70. bind(SpringTestFacade.class).to(SpringTestFacadeImpl.class);
  71. bind(AnnotatedTest.class).to(AnnotatedTestClass.class);
  72. bind(annotatedTestOtherClassKey).to(AnnotatedTestOtherClass.class);
  73. bind(annotatedTestMethodKey).to(AnnotatedTestMethod.class);
  74. }
  75. };
  76. final Injector injector = Guice.createInjector(testModule);
  77. final SpringTestFacade springTestFacade = injector.getInstance(SpringTestFacade.class);
  78. guiceCounter.setDisplayed(false);
  79. assertNotNull("now()", springTestFacade.now());
  80. assertSame(REQUESTS_COUNT, 0, guiceCounter.getRequestsCount());
  81. guiceCounter.setDisplayed(true);
  82. assertNotNull("now()", springTestFacade.now());
  83. assertSame(REQUESTS_COUNT, 1, guiceCounter.getRequestsCount());
  84. try {
  85. springTestFacade.throwError();
  86. } catch (final Error e) {
  87. assertSame(REQUESTS_COUNT, 2, guiceCounter.getRequestsCount());
  88. }
  89. final AnnotatedTest annotatedTestClass = injector.getInstance(AnnotatedTestClass.class);
  90. assertNotNull("annotatedTestClass", annotatedTestClass.myMethod());
  91. assertSame(REQUESTS_COUNT, 3, guiceCounter.getRequestsCount());
  92. final AnnotatedTest annotatedTestOtherClass = injector
  93. .getInstance(annotatedTestOtherClassKey);
  94. assertNotNull("annotatedTestOtherClass", annotatedTestOtherClass.myMethod());
  95. assertSame(REQUESTS_COUNT, 4, guiceCounter.getRequestsCount());
  96. final AnnotatedTest annotatedTestMethod = injector.getInstance(annotatedTestMethodKey);
  97. assertNotNull("annotatedTestMethod", annotatedTestMethod.myMethod());
  98. assertNotNull("annotatedTestMethod", annotatedTestMethod.myOtherMethod());
  99. assertSame(REQUESTS_COUNT, 6, guiceCounter.getRequestsCount());
  100. }
  101. }