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

http://github.com/cucumber/cucumber-jvm · Java · 113 lines · 97 code · 16 blank · 0 comment · 0 complexity · 7235c1d0527919fac99a69fe262b8cee MD5 · raw file

  1. package cucumber.runtime.java.guice.impl;
  2. import com.google.inject.Injector;
  3. import cucumber.runtime.Env;
  4. import cucumber.runtime.java.guice.InjectorSource;
  5. import org.junit.Test;
  6. import java.util.Properties;
  7. import static org.hamcrest.CoreMatchers.instanceOf;
  8. import static org.hamcrest.CoreMatchers.is;
  9. import static org.junit.Assert.assertThat;
  10. import static org.junit.Assert.fail;
  11. public class InjectorSourceFactoryTest {
  12. private InjectorSourceFactory createInjectorSourceFactory(Properties properties) {
  13. return new InjectorSourceFactory(new Env(properties));
  14. }
  15. @Test
  16. public void createsDefaultInjectorSourceWhenGuiceModulePropertyIsNotSet() throws Exception {
  17. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(new Properties());
  18. assertThat(injectorSourceFactory.create(), is(instanceOf(InjectorSource.class)));
  19. }
  20. static class CustomInjectorSource implements InjectorSource {
  21. @Override
  22. public Injector getInjector() {
  23. return null;
  24. }
  25. }
  26. @Test
  27. public void instantiatesInjectorSourceByFullyQualifiedName() throws Exception {
  28. Properties properties = new Properties();
  29. properties.setProperty(InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY, CustomInjectorSource.class.getName());
  30. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(properties);
  31. assertThat(injectorSourceFactory.create(), is(instanceOf(CustomInjectorSource.class)));
  32. }
  33. @Test
  34. public void failsToInstantiateNonExistantClass() throws Exception {
  35. Properties properties = new Properties();
  36. properties.setProperty(InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY, "some.bogus.Class");
  37. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(properties);
  38. try {
  39. injectorSourceFactory.create();
  40. fail();
  41. } catch (InjectorSourceInstantiationFailed exception) {
  42. assertThat(exception.getCause(), instanceOf(ClassNotFoundException.class));
  43. }
  44. }
  45. @Test
  46. public void failsToInstantiateClassNotImplementingInjectorSource() throws Exception {
  47. Properties properties = new Properties();
  48. properties.setProperty(InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY, String.class.getName());
  49. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(properties);
  50. try {
  51. injectorSourceFactory.create();
  52. fail();
  53. } catch (InjectorSourceInstantiationFailed exception) {
  54. assertThat(exception.getCause(), instanceOf(ClassCastException.class));
  55. }
  56. }
  57. static class PrivateConstructor implements InjectorSource {
  58. private PrivateConstructor() {
  59. }
  60. @Override
  61. public Injector getInjector() {
  62. return null;
  63. }
  64. }
  65. @Test
  66. public void failsToInstantiateClassWithPrivateConstructor() throws Exception {
  67. Properties properties = new Properties();
  68. properties.setProperty(InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY, PrivateConstructor.class.getName());
  69. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(properties);
  70. try {
  71. injectorSourceFactory.create();
  72. fail();
  73. } catch (InjectorSourceInstantiationFailed exception) {
  74. assertThat(exception.getCause(), instanceOf(IllegalAccessException.class));
  75. }
  76. }
  77. static class NoDefaultConstructor implements InjectorSource {
  78. private NoDefaultConstructor(String someParameter) {
  79. }
  80. @Override
  81. public Injector getInjector() {
  82. return null;
  83. }
  84. }
  85. @Test
  86. public void failsToInstantiateClassWithNoDefaultConstructor() throws Exception {
  87. Properties properties = new Properties();
  88. properties.setProperty(InjectorSourceFactory.GUICE_INJECTOR_SOURCE_KEY, NoDefaultConstructor.class.getName());
  89. InjectorSourceFactory injectorSourceFactory = createInjectorSourceFactory(properties);
  90. try {
  91. injectorSourceFactory.create();
  92. fail();
  93. } catch (InjectorSourceInstantiationFailed exception) {
  94. assertThat(exception.getCause(), instanceOf(InstantiationException.class));
  95. }
  96. }
  97. }