PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/scanner/reflections/src/test/java/de/devsurf/injection/guice/scanner/reflections/tests/autobind/multiple/MultibindTests.java

https://github.com/manzke/guice-automatic-injection
Java | 209 lines | 158 code | 34 blank | 17 comment | 14 complexity | 965e5149c9b0396d121dad9a70b0aab1 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.reflections.tests.autobind.multiple;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertTrue;
  19. import static org.junit.Assert.fail;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Set;
  23. import javax.inject.Inject;
  24. import org.junit.Test;
  25. import com.google.inject.ConfigurationException;
  26. import com.google.inject.Guice;
  27. import com.google.inject.Injector;
  28. import de.devsurf.injection.guice.annotations.Bind;
  29. import de.devsurf.injection.guice.scanner.PackageFilter;
  30. import de.devsurf.injection.guice.scanner.StartupModule;
  31. import de.devsurf.injection.guice.scanner.reflections.ReflectionsScanner;
  32. public class MultibindTests {
  33. @Test
  34. public void createDynamicModule() {
  35. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  36. PackageFilter.create(MultibindTests.class)));
  37. assertNotNull(injector);
  38. }
  39. @Test
  40. public void testWithWrongPackage1() {
  41. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  42. PackageFilter.create("java.util.concurrent")));
  43. assertNotNull(injector);
  44. try {
  45. FirstContainer container = injector.getInstance(FirstContainer.class);
  46. fail("The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
  47. + (container == null));
  48. } catch (ConfigurationException e) {
  49. // ok
  50. }
  51. }
  52. @Test
  53. public void testWithWrongPackage2() {
  54. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  55. PackageFilter.create("java.util.concurrent")));
  56. assertNotNull(injector);
  57. try {
  58. SecondContainer container = injector.getInstance(SecondContainer.class);
  59. fail("The Scanner scanned the wrong package, so no Implementation should be bound to this Interface. Instance null? "
  60. + (container == null));
  61. } catch (ConfigurationException e) {
  62. // ok
  63. }
  64. }
  65. @Test
  66. public void createFirstContainer() {
  67. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  68. PackageFilter.create(MultibindTests.class)));
  69. assertNotNull(injector);
  70. FirstContainer container = injector.getInstance(FirstContainer.class);
  71. assertNotNull(container);
  72. assertTrue(container.size() == 2);
  73. for (FirstInterface obj : container.get()) {
  74. assertTrue(obj instanceof FirstInterface);
  75. assertTrue(obj instanceof SecondInterface);
  76. assertTrue(obj instanceof FirstImplementation || obj instanceof SecondImplementation);
  77. }
  78. }
  79. @Test
  80. public void createSecondTestInterface() {
  81. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  82. PackageFilter.create(MultibindTests.class)));
  83. assertNotNull(injector);
  84. SecondContainer container = injector.getInstance(SecondContainer.class);
  85. assertNotNull(container);
  86. assertTrue(container.size() == 2);
  87. for (SecondInterface obj : container.get()) {
  88. assertTrue(obj instanceof FirstInterface);
  89. assertTrue(obj instanceof SecondInterface);
  90. assertTrue(obj instanceof FirstImplementation || obj instanceof SecondImplementation);
  91. }
  92. }
  93. @Test
  94. public void createAllInterfaces() {
  95. Injector injector = Guice.createInjector(StartupModule.create(ReflectionsScanner.class,
  96. PackageFilter.create(MultibindTests.class)));
  97. assertNotNull(injector);
  98. FirstContainer firstContainer = injector.getInstance(FirstContainer.class);
  99. assertNotNull(firstContainer);
  100. assertTrue(firstContainer.size() == 2);
  101. for (FirstInterface obj : firstContainer.get()) {
  102. assertTrue(obj instanceof FirstInterface);
  103. assertTrue(obj instanceof SecondInterface);
  104. assertTrue(obj instanceof FirstImplementation || obj instanceof SecondImplementation);
  105. }
  106. SecondContainer secondContainer = injector.getInstance(SecondContainer.class);
  107. assertNotNull(secondContainer);
  108. assertTrue(secondContainer.size() == 2);
  109. for (SecondInterface obj : secondContainer.get()) {
  110. assertTrue(obj instanceof FirstInterface);
  111. assertTrue(obj instanceof SecondInterface);
  112. assertTrue(obj instanceof FirstImplementation || obj instanceof SecondImplementation);
  113. }
  114. }
  115. public static interface FirstInterface {
  116. String sayHello();
  117. }
  118. public static interface SecondInterface {
  119. String fireEvent();
  120. }
  121. public static class FirstContainer {
  122. private List<FirstInterface> implementations;
  123. @Inject
  124. public FirstContainer(Set<FirstInterface> implementations) {
  125. super();
  126. this.implementations = new ArrayList<FirstInterface>(implementations);
  127. }
  128. public int size() {
  129. return implementations.size();
  130. }
  131. public List<FirstInterface> get() {
  132. return implementations;
  133. }
  134. }
  135. public static class SecondContainer {
  136. private List<SecondInterface> implementations;
  137. @Inject
  138. public SecondContainer(Set<SecondInterface> implementations) {
  139. super();
  140. this.implementations = new ArrayList<SecondInterface>(implementations);
  141. }
  142. public int size() {
  143. return implementations.size();
  144. }
  145. public List<SecondInterface> get() {
  146. return implementations;
  147. }
  148. }
  149. @Bind(multiple = true)
  150. public static class FirstImplementation implements FirstInterface, SecondInterface {
  151. public static final String TEST = "test1";
  152. public static final String EVENT = "event1";
  153. @Override
  154. public String sayHello() {
  155. return TEST;
  156. }
  157. @Override
  158. public String fireEvent() {
  159. return EVENT;
  160. }
  161. }
  162. @Bind(multiple = true)
  163. public static class SecondImplementation implements FirstInterface, SecondInterface {
  164. public static final String TEST = "test2";
  165. public static final String EVENT = "event2";
  166. @Override
  167. public String sayHello() {
  168. return TEST;
  169. }
  170. @Override
  171. public String fireEvent() {
  172. return EVENT;
  173. }
  174. }
  175. }