/sources/stressTestScope/src/test/java/com/plugtree/stressTestScope/TestScopeTestCase.java

http://solrmeter.googlecode.com/ · Java · 95 lines · 62 code · 13 blank · 20 comment · 2 complexity · 47db0bfb8c6b05e6a323d4eb05217a9e MD5 · raw file

  1. /**
  2. * Copyright Plugtree LLC
  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. package com.plugtree.stressTestScope;
  17. import junit.framework.TestCase;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.plugtree.stressTestScope.StressTestRegistry;
  21. import com.plugtree.stressTestScope.StressTestScopeModule;
  22. /**
  23. *
  24. * @author tflobbe
  25. *
  26. */
  27. public class TestScopeTestCase extends TestCase {
  28. private Injector injector;
  29. @Override
  30. protected void setUp() throws Exception {
  31. injector = Guice.createInjector(new StressTestScopeModule(), new TestModule());
  32. TestObject.restartCounter();
  33. }
  34. public void testJustOneObject() {
  35. StressTestRegistry.start();
  36. assertEquals(0, TestObject.createdObjects);
  37. injector.getInstance(TestObject.class);
  38. assertEquals(1, TestObject.createdObjects);
  39. injector.getInstance(TestObject.class);
  40. assertEquals(1, TestObject.createdObjects);
  41. for(int i = 0; i < 10 ; i++) {
  42. injector.getInstance(TestObject.class);
  43. assertEquals(1, TestObject.createdObjects);
  44. }
  45. }
  46. public void testManyObject() {
  47. StressTestRegistry.start();
  48. assertEquals(0, TestObject.createdObjects);
  49. injector.getInstance(TestObject.class);
  50. assertEquals(1, TestObject.createdObjects);
  51. StressTestRegistry.restart();
  52. injector.getInstance(TestObject.class);
  53. assertEquals(2, TestObject.createdObjects);
  54. for(int i = 0; i < 10 ; i++) {
  55. StressTestRegistry.restart();
  56. injector.getInstance(TestObject.class);
  57. assertEquals(3 + i, TestObject.createdObjects);
  58. }
  59. }
  60. public void testObjectGraph() {
  61. StressTestRegistry.start();
  62. assertEquals(0, TestObject.createdObjects);
  63. assertEquals(0, TestObject2.createdObjects);
  64. TestObject object1 = injector.getInstance(TestObject.class);
  65. assertEquals(1, TestObject.createdObjects);
  66. assertEquals(0, TestObject2.createdObjects);
  67. TestObject2 object2 = injector.getInstance(TestObject2.class);
  68. assertEquals(1, TestObject.createdObjects);
  69. assertEquals(1, TestObject2.createdObjects);
  70. assertEquals(object1,object2.getTestObject());
  71. StressTestRegistry.restart();
  72. object2 = injector.getInstance(TestObject2.class);
  73. assertEquals(2, TestObject.createdObjects);
  74. assertEquals(2, TestObject2.createdObjects);
  75. object1 = injector.getInstance(TestObject.class);
  76. assertEquals(2, TestObject.createdObjects);
  77. assertEquals(2, TestObject2.createdObjects);
  78. assertEquals(object1,object2.getTestObject());
  79. System.out.println(object1);
  80. System.out.println(object2);
  81. }
  82. }