PageRenderTime 666ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/guice/src/test/java/io/cucumber/guice/GuiceFactoryTest.java

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