PageRenderTime 60ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/core/test/com/google/inject/ParentInjectorTest.java

https://gitlab.com/metamorphiccode/guice
Java | 331 lines | 259 code | 52 blank | 20 comment | 0 complexity | 4f376cf8a502976ce13145dcb0ec7f6f MD5 | raw file
  1. /*
  2. Copyright (C) 2007 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package com.google.inject;
  14. import static com.google.inject.Asserts.assertContains;
  15. import static com.google.inject.Asserts.getDeclaringSourcePart;
  16. import static java.lang.annotation.ElementType.TYPE;
  17. import static java.lang.annotation.RetentionPolicy.RUNTIME;
  18. import com.google.common.collect.ImmutableList;
  19. import com.google.common.collect.Iterables;
  20. import com.google.inject.matcher.Matchers;
  21. import com.google.inject.name.Names;
  22. import com.google.inject.spi.TypeConverter;
  23. import junit.framework.TestCase;
  24. import java.lang.annotation.Retention;
  25. import java.lang.annotation.Target;
  26. import java.util.List;
  27. /**
  28. * @author jessewilson@google.com (Jesse Wilson)
  29. */
  30. public class ParentInjectorTest extends TestCase {
  31. public void testParentAndChildCannotShareExplicitBindings() {
  32. Injector parent = Guice.createInjector(bindsA);
  33. try {
  34. parent.createChildInjector(bindsA);
  35. fail("Created the same explicit binding on both parent and child");
  36. } catch (CreationException e) {
  37. assertContains(e.getMessage(), "A binding to ", A.class.getName(), " was already configured",
  38. " at ", getClass().getName(), getDeclaringSourcePart(getClass()),
  39. " at ", getClass().getName(), getDeclaringSourcePart(getClass()));
  40. }
  41. }
  42. public void testParentJitBindingWontClobberChildBinding() {
  43. Injector parent = Guice.createInjector();
  44. parent.createChildInjector(bindsA);
  45. try {
  46. parent.getInstance(A.class);
  47. fail("Created a just-in-time binding on the parent that's the same as a child's binding");
  48. } catch (ConfigurationException e) {
  49. assertContains(e.getMessage(),
  50. "Unable to create binding for " + A.class.getName(),
  51. "It was already configured on one or more child injectors or private modules",
  52. "bound at " + bindsA.getClass().getName() + ".configure(",
  53. "If it was in a PrivateModule, did you forget to expose the binding?",
  54. "while locating " + A.class.getName());
  55. }
  56. }
  57. public void testChildCannotBindToAParentJitBinding() {
  58. Injector parent = Guice.createInjector();
  59. parent.getInstance(A.class);
  60. try {
  61. parent.createChildInjector(bindsA);
  62. fail();
  63. } catch(CreationException ce) {
  64. assertContains(Iterables.getOnlyElement(ce.getErrorMessages()).getMessage(),
  65. "A just-in-time binding to " + A.class.getName() + " was already configured on a parent injector.");
  66. }
  67. }
  68. public void testJustInTimeBindingsAreSharedWithParentIfPossible() {
  69. Injector parent = Guice.createInjector();
  70. Injector child = parent.createChildInjector();
  71. assertSame(child.getInstance(A.class), parent.getInstance(A.class));
  72. Injector anotherChild = parent.createChildInjector();
  73. assertSame(anotherChild.getInstance(A.class), parent.getInstance(A.class));
  74. Injector grandchild = child.createChildInjector();
  75. assertSame(grandchild.getInstance(A.class), parent.getInstance(A.class));
  76. }
  77. public void testBindingsInherited() {
  78. Injector parent = Guice.createInjector(bindsB);
  79. Injector child = parent.createChildInjector();
  80. assertSame(RealB.class, child.getInstance(B.class).getClass());
  81. }
  82. public void testGetParent() {
  83. Injector top = Guice.createInjector(bindsA);
  84. Injector middle = top.createChildInjector(bindsB);
  85. Injector bottom = middle.createChildInjector();
  86. assertSame(middle, bottom.getParent());
  87. assertSame(top, middle.getParent());
  88. assertNull(top.getParent());
  89. }
  90. public void testChildBindingsNotVisibleToParent() {
  91. Injector parent = Guice.createInjector();
  92. parent.createChildInjector(bindsB);
  93. try {
  94. parent.getBinding(B.class);
  95. fail();
  96. } catch (ConfigurationException expected) {
  97. }
  98. }
  99. public void testScopesInherited() {
  100. Injector parent = Guice.createInjector(new AbstractModule() {
  101. @Override protected void configure() {
  102. bindScope(MyScope.class, Scopes.SINGLETON);
  103. }
  104. });
  105. Injector child = parent.createChildInjector(new AbstractModule() {
  106. @Override protected void configure() {
  107. bind(A.class).in(MyScope.class);
  108. }
  109. });
  110. assertSame(child.getInstance(A.class), child.getInstance(A.class));
  111. }
  112. /*if[AOP]*/
  113. private final org.aopalliance.intercept.MethodInterceptor returnNullInterceptor
  114. = new org.aopalliance.intercept.MethodInterceptor() {
  115. public Object invoke(org.aopalliance.intercept.MethodInvocation methodInvocation) {
  116. return null;
  117. }
  118. };
  119. public void testInterceptorsInherited() {
  120. Injector parent = Guice.createInjector(new AbstractModule() {
  121. @Override protected void configure() {
  122. super.bindInterceptor(Matchers.any(), Matchers.returns(Matchers.identicalTo(A.class)),
  123. returnNullInterceptor);
  124. }
  125. });
  126. Injector child = parent.createChildInjector(new AbstractModule() {
  127. @Override protected void configure() {
  128. bind(C.class);
  129. }
  130. });
  131. assertNull(child.getInstance(C.class).interceptedMethod());
  132. }
  133. /*end[AOP]*/
  134. public void testTypeConvertersInherited() {
  135. Injector parent = Guice.createInjector(bindListConverterModule);
  136. Injector child = parent.createChildInjector(bindStringNamedB);
  137. assertEquals(ImmutableList.of(), child.getInstance(Key.get(List.class, Names.named("B"))));
  138. }
  139. public void testTypeConvertersConflicting() {
  140. Injector parent = Guice.createInjector(bindListConverterModule);
  141. Injector child = parent.createChildInjector(bindListConverterModule, bindStringNamedB);
  142. try {
  143. child.getInstance(Key.get(List.class, Names.named("B")));
  144. fail();
  145. } catch (ConfigurationException expected) {
  146. Asserts.assertContains(expected.getMessage(), "Multiple converters can convert");
  147. }
  148. }
  149. public void testInjectorInjectionSpanningInjectors() {
  150. Injector parent = Guice.createInjector();
  151. Injector child = parent.createChildInjector(new AbstractModule() {
  152. @Override protected void configure() {
  153. bind(D.class);
  154. }
  155. });
  156. D d = child.getInstance(D.class);
  157. assertSame(d.injector, child);
  158. E e = child.getInstance(E.class);
  159. assertSame(e.injector, parent);
  160. }
  161. public void testSeveralLayersOfHierarchy() {
  162. Injector top = Guice.createInjector(bindsA);
  163. Injector left = top.createChildInjector();
  164. Injector leftLeft = left.createChildInjector(bindsD);
  165. Injector right = top.createChildInjector(bindsD);
  166. assertSame(leftLeft, leftLeft.getInstance(D.class).injector);
  167. assertSame(right, right.getInstance(D.class).injector);
  168. assertSame(top, leftLeft.getInstance(E.class).injector);
  169. assertSame(top.getInstance(A.class), leftLeft.getInstance(A.class));
  170. Injector leftRight = left.createChildInjector(bindsD);
  171. assertSame(leftRight, leftRight.getInstance(D.class).injector);
  172. try {
  173. top.getInstance(D.class);
  174. fail();
  175. } catch (ConfigurationException expected) {
  176. }
  177. try {
  178. left.getInstance(D.class);
  179. fail();
  180. } catch (ConfigurationException expected) {
  181. }
  182. }
  183. public void testScopeBoundInChildInjectorOnly() {
  184. Injector parent = Guice.createInjector();
  185. Injector child = parent.createChildInjector(new AbstractModule() {
  186. @Override protected void configure() {
  187. bindScope(MyScope.class, Scopes.SINGLETON);
  188. }
  189. });
  190. try {
  191. parent.getProvider(F.class);
  192. fail();
  193. } catch (ConfigurationException expected) {
  194. assertContains(expected.getMessage(),
  195. "No scope is bound to com.google.inject.ParentInjectorTest$MyScope.",
  196. "at " + F.class.getName() + ".class(ParentInjectorTest.java",
  197. " while locating " + F.class.getName());
  198. }
  199. assertNotNull(child.getProvider(F.class).get());
  200. }
  201. public void testErrorInParentButOkayInChild() {
  202. Injector parent = Guice.createInjector();
  203. Injector childInjector = parent.createChildInjector(new AbstractModule() {
  204. @Override protected void configure() {
  205. bindScope(MyScope.class, Scopes.SINGLETON);
  206. bind(Object.class).to(F.class);
  207. }
  208. });
  209. Object one = childInjector.getInstance(Object.class);
  210. Object two = childInjector.getInstance(Object.class);
  211. assertSame(one, two);
  212. }
  213. public void testErrorInParentAndChild() {
  214. Injector parent = Guice.createInjector();
  215. Injector childInjector = parent.createChildInjector();
  216. try {
  217. childInjector.getInstance(G.class);
  218. fail();
  219. } catch(ConfigurationException expected) {
  220. assertContains(expected.getMessage(), "No scope is bound to " + MyScope.class.getName(),
  221. "at " + F.class.getName() + ".class(ParentInjectorTest.java:",
  222. " while locating " + G.class.getName());
  223. }
  224. }
  225. @Singleton
  226. static class A {}
  227. private final Module bindsA = new AbstractModule() {
  228. @Override protected void configure() {
  229. bind(A.class).toInstance(new A());
  230. }
  231. };
  232. interface B {}
  233. static class RealB implements B {}
  234. private final Module bindsB = new AbstractModule() {
  235. @Override protected void configure() {
  236. bind(B.class).to(RealB.class);
  237. }
  238. };
  239. @Target(TYPE) @Retention(RUNTIME) @ScopeAnnotation
  240. public @interface MyScope {}
  241. private final TypeConverter listConverter = new TypeConverter() {
  242. public Object convert(String value, TypeLiteral<?> toType) {
  243. return ImmutableList.of();
  244. }
  245. };
  246. private final Module bindListConverterModule = new AbstractModule() {
  247. @Override protected void configure() {
  248. convertToTypes(Matchers.any(), listConverter);
  249. }
  250. };
  251. private final Module bindStringNamedB = new AbstractModule() {
  252. @Override protected void configure() {
  253. bind(String.class).annotatedWith(Names.named("B")).toInstance("buzz");
  254. }
  255. };
  256. public static class C {
  257. public A interceptedMethod() {
  258. return new A();
  259. }
  260. }
  261. static class D {
  262. @Inject Injector injector;
  263. }
  264. static class E {
  265. @Inject Injector injector;
  266. }
  267. private final Module bindsD = new AbstractModule() {
  268. @Override protected void configure() {
  269. bind(D.class);
  270. }
  271. };
  272. @MyScope
  273. static class F implements G {}
  274. @ImplementedBy(F.class)
  275. interface G {}
  276. }