PageRenderTime 834ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/scanner/sonatype/src/test/java/de/devsurf/injection/guice/scanner/sonatype/tests/autobind/filter/PackageFilterTests.java

https://github.com/simonetripodi/guice-automatic-injection
Java | 145 lines | 107 code | 22 blank | 16 comment | 1 complexity | e55b18ea96df7b1b34fae065d2fbd624 MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 Daniel Manzke <daniel.manzke@googlemail.com>
  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 de.devsurf.injection.guice.scanner.sonatype.tests.autobind.filter;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import org.junit.Test;
  21. import com.google.inject.Binder;
  22. import com.google.inject.ConfigurationException;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Injector;
  25. import com.google.inject.multibindings.Multibinder;
  26. import de.devsurf.injection.guice.annotations.Bind;
  27. import de.devsurf.injection.guice.annotations.features.AutoBindingFeature;
  28. import de.devsurf.injection.guice.scanner.ClasspathScanner;
  29. import de.devsurf.injection.guice.scanner.PackageFilter;
  30. import de.devsurf.injection.guice.scanner.StartupModule;
  31. import de.devsurf.injection.guice.scanner.features.ScannerFeature;
  32. import de.devsurf.injection.guice.scanner.sonatype.SonatypeScanner;
  33. public class PackageFilterTests {
  34. @Test
  35. public void createDynamicModule() {
  36. Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
  37. PackageFilter.create(PackageFilterTests.class)));
  38. assertNotNull(injector);
  39. }
  40. @Test
  41. public void testWithWrongPackage() {
  42. Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
  43. PackageFilter.create("java")));
  44. assertNotNull(injector);
  45. TestInterface testInstance;
  46. try {
  47. testInstance = injector.getInstance(TestInterface.class);
  48. fail("The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
  49. + (testInstance == null));
  50. } catch (ConfigurationException e) {
  51. // ok
  52. }
  53. }
  54. @Test
  55. public void createTestInterface() {
  56. Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
  57. PackageFilter.create(PackageFilterTests.class)));
  58. assertNotNull(injector);
  59. TestInterface testInstance = injector.getInstance(TestInterface.class);
  60. assertNotNull(testInstance);
  61. assertTrue(testInstance.sayHello().equals(TestInterfaceImplementation.TEST));
  62. assertTrue(testInstance instanceof TestInterfaceImplementation);
  63. assertTrue(testInstance instanceof SecondTestInterface);
  64. }
  65. @Test
  66. public void createSecondTestInterface() {
  67. Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
  68. PackageFilter.create(PackageFilterTests.class)));
  69. assertNotNull(injector);
  70. SecondTestInterface sameInstance = injector.getInstance(SecondTestInterface.class);
  71. assertNotNull(sameInstance);
  72. assertTrue(sameInstance.fireEvent().equals(TestInterfaceImplementation.EVENT));
  73. assertTrue(sameInstance instanceof TestInterfaceImplementation);
  74. assertTrue(sameInstance instanceof TestInterface);
  75. }
  76. @Test
  77. public void createAllInterfaces() {
  78. Injector injector = Guice.createInjector(new TestStartupModule(SonatypeScanner.class,
  79. PackageFilter.create(PackageFilterTests.class)));
  80. assertNotNull(injector);
  81. TestInterface testInstance = injector.getInstance(TestInterface.class);
  82. assertNotNull(testInstance);
  83. assertTrue(testInstance.sayHello().equals(TestInterfaceImplementation.TEST));
  84. assertTrue(testInstance instanceof TestInterfaceImplementation);
  85. assertTrue(testInstance instanceof SecondTestInterface);
  86. SecondTestInterface sameInstance = injector.getInstance(SecondTestInterface.class);
  87. assertNotNull(sameInstance);
  88. assertTrue(sameInstance.fireEvent().equals(TestInterfaceImplementation.EVENT));
  89. assertTrue(sameInstance instanceof TestInterfaceImplementation);
  90. assertTrue(testInstance instanceof TestInterface);
  91. }
  92. public static interface TestInterface {
  93. String sayHello();
  94. }
  95. public static interface SecondTestInterface {
  96. String fireEvent();
  97. }
  98. @Bind
  99. public static class TestInterfaceImplementation implements TestInterface, SecondTestInterface {
  100. public static final String TEST = "test";
  101. public static final String EVENT = "event";
  102. @Override
  103. public String sayHello() {
  104. return TEST;
  105. }
  106. @Override
  107. public String fireEvent() {
  108. return EVENT;
  109. }
  110. }
  111. public static class TestStartupModule extends StartupModule {
  112. public TestStartupModule(Class<? extends ClasspathScanner> scanner, PackageFilter... packages) {
  113. super(scanner, packages);
  114. }
  115. @Override
  116. protected Multibinder<ScannerFeature> bindFeatures(Binder binder) {
  117. Multibinder<ScannerFeature> listeners = Multibinder.newSetBinder(binder,
  118. ScannerFeature.class);
  119. listeners.addBinding().to(AutoBindingFeature.class);
  120. return listeners;
  121. }
  122. }
  123. }