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

/guice/src/test/java/cucumber/runtime/java/guice/impl/GuiceFactoryTest.java

http://github.com/cucumber/cucumber-jvm
Java | 199 lines | 156 code | 38 blank | 5 comment | 0 complexity | 39bd1ef3df51c6bd5f07520e03857c25 MD5 | raw file
  1. package cucumber.runtime.java.guice.impl;
  2. import com.google.inject.*;
  3. import cucumber.api.guice.CucumberModules;
  4. import cucumber.api.guice.CucumberScopes;
  5. import cucumber.api.java.ObjectFactory;
  6. import cucumber.runtime.java.guice.ScenarioScoped;
  7. import org.junit.After;
  8. import org.junit.Test;
  9. import javax.inject.Singleton;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import static cucumber.runtime.java.guice.matcher.ElementsAreAllEqualMatcher.elementsAreAllEqual;
  13. import static cucumber.runtime.java.guice.matcher.ElementsAreAllUniqueMatcher.elementsAreAllUnique;
  14. import static org.hamcrest.CoreMatchers.containsString;
  15. import static org.hamcrest.CoreMatchers.notNullValue;
  16. import static org.junit.Assert.assertThat;
  17. import static org.junit.Assert.fail;
  18. public class GuiceFactoryTest {
  19. private ObjectFactory factory;
  20. private List<?> instancesFromSameScenario;
  21. private List<?> instancesFromDifferentScenarios;
  22. @After
  23. public void tearDown() {
  24. // If factory is left in start state it can cause cascading failures due to scope being left open
  25. try { factory.stop(); } catch (Exception e) {}
  26. }
  27. @Test
  28. public void factoryCanBeIntantiatedWithDefaultConstructor() throws Exception {
  29. factory = new GuiceFactory();
  30. assertThat(factory, notNullValue());
  31. }
  32. @Test
  33. public void factoryCanBeIntantiatedWithArgConstructor() {
  34. factory = new GuiceFactory(Guice.createInjector());
  35. assertThat(factory, notNullValue());
  36. }
  37. @Test
  38. public void factoryStartFailsIfScenarioScopeIsNotBound() {
  39. factory = new GuiceFactory(Guice.createInjector());
  40. try {
  41. factory.start();
  42. fail();
  43. } catch (ConfigurationException e) {
  44. assertThat(e.getMessage(),
  45. containsString("No implementation for cucumber.runtime.java.guice.ScenarioScope was bound"));
  46. }
  47. }
  48. static class UnscopedClass {}
  49. @Test
  50. public void shouldGiveNewInstancesOfUnscopedClassWithinAScenario() {
  51. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  52. instancesFromSameScenario = getInstancesFromSameScenario(factory, UnscopedClass.class);
  53. assertThat(instancesFromSameScenario, elementsAreAllUnique());
  54. }
  55. @Test
  56. public void shouldGiveNewInstanceOfUnscopedClassForEachScenario() {
  57. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  58. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, UnscopedClass.class);
  59. assertThat(instancesFromDifferentScenarios, elementsAreAllUnique());
  60. }
  61. @ScenarioScoped static class AnnotatedScenarioScopedClass {}
  62. @Test
  63. public void shouldGiveTheSameInstanceOfAnnotatedScenarioScopedClassWithinAScenario() {
  64. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  65. instancesFromSameScenario = getInstancesFromSameScenario(factory, AnnotatedScenarioScopedClass.class);
  66. assertThat(instancesFromSameScenario, elementsAreAllEqual());
  67. }
  68. @Test
  69. public void shouldGiveNewInstanceOfAnnotatedScenarioScopedClassForEachScenario() {
  70. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  71. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedScenarioScopedClass.class);
  72. assertThat(instancesFromDifferentScenarios, elementsAreAllUnique());
  73. }
  74. @Singleton static class AnnotatedSingletonClass {}
  75. @Test
  76. public void shouldGiveTheSameInstanceOfAnnotatedSingletonClassWithinAScenario() {
  77. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  78. instancesFromSameScenario = getInstancesFromSameScenario(factory, AnnotatedSingletonClass.class);
  79. assertThat(instancesFromSameScenario, elementsAreAllEqual());
  80. }
  81. @Test
  82. public void shouldGiveTheSameInstanceOfAnnotatedSingletonClassForEachScenario() {
  83. factory = new GuiceFactory(injector(CucumberModules.SCENARIO));
  84. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedSingletonClass.class);
  85. assertThat(instancesFromDifferentScenarios, elementsAreAllEqual());
  86. }
  87. static class BoundScenarioScopedClass {}
  88. final AbstractModule boundScenarioScopedClassModule = new AbstractModule() {
  89. @Override protected void configure() {
  90. bind(BoundScenarioScopedClass.class).in(CucumberScopes.SCENARIO);
  91. }
  92. };
  93. @Test
  94. public void shouldGiveTheSameInstanceOfBoundScenarioScopedClassWithinAScenario() {
  95. factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundScenarioScopedClassModule));
  96. instancesFromSameScenario = getInstancesFromSameScenario(factory, BoundScenarioScopedClass.class);
  97. assertThat(instancesFromSameScenario, elementsAreAllEqual());
  98. }
  99. @Test
  100. public void shouldGiveNewInstanceOfBoundScenarioScopedClassForEachScenario() {
  101. factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundScenarioScopedClassModule));
  102. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, BoundScenarioScopedClass.class);
  103. assertThat(instancesFromDifferentScenarios, elementsAreAllUnique());
  104. }
  105. static class BoundSingletonClass {}
  106. final AbstractModule boundSingletonClassModule = new AbstractModule() {
  107. @Override protected void configure() {
  108. bind(BoundSingletonClass.class).in(Scopes.SINGLETON);
  109. }
  110. };
  111. @Test
  112. public void shouldGiveTheSameInstanceOfBoundSingletonClassWithinAScenario() {
  113. factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundSingletonClassModule));
  114. instancesFromSameScenario = getInstancesFromSameScenario(factory, BoundSingletonClass.class);
  115. assertThat(instancesFromSameScenario, elementsAreAllEqual());
  116. }
  117. @Test
  118. public void shouldGiveTheSameInstanceOfBoundSingletonClassForEachScenario() {
  119. factory = new GuiceFactory(injector(CucumberModules.SCENARIO, boundSingletonClassModule));
  120. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, BoundSingletonClass.class);
  121. assertThat(instancesFromDifferentScenarios, elementsAreAllEqual());
  122. }
  123. @Test
  124. public void shouldGiveNewInstanceOfAnnotatedSingletonClassForEachScenarioWhenOverridingModuleBindingIsScenarioScope() {
  125. factory = new GuiceFactory(injector(CucumberModules.SCENARIO, new AbstractModule() {
  126. @Override
  127. protected void configure() {
  128. bind(AnnotatedSingletonClass.class).in(CucumberScopes.SCENARIO);
  129. }
  130. }));
  131. instancesFromDifferentScenarios = getInstancesFromDifferentScenarios(factory, AnnotatedSingletonClass.class);
  132. assertThat(instancesFromDifferentScenarios, elementsAreAllUnique());
  133. }
  134. private Injector injector(Module... module) {
  135. return Guice.createInjector(Stage.PRODUCTION, module);
  136. }
  137. private <E> List<E> getInstancesFromSameScenario(ObjectFactory factory, Class<E> aClass) {
  138. // Scenario
  139. factory.start();
  140. E o1 = factory.getInstance(aClass);
  141. E o2 = factory.getInstance(aClass);
  142. E o3 = factory.getInstance(aClass);
  143. factory.stop();
  144. return Arrays.asList(o1, o2, o3);
  145. }
  146. private <E> List<E> getInstancesFromDifferentScenarios(ObjectFactory factory, Class<E> aClass) {
  147. // Scenario 1
  148. factory.start();
  149. E o1 = factory.getInstance(aClass);
  150. factory.stop();
  151. // Scenario 2
  152. factory.start();
  153. E o2 = factory.getInstance(aClass);
  154. factory.stop();
  155. // Scenario 3
  156. factory.start();
  157. E o3 = factory.getInstance(aClass);
  158. factory.stop();
  159. return Arrays.asList(o1, o2, o3);
  160. }
  161. }