PageRenderTime 118ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/spice-inject/guice-patches/vanilla.test/com/google/inject/OptionalBindingTest.java

https://github.com/peterlynch/spice
Java | 293 lines | 225 code | 41 blank | 27 comment | 0 complexity | c5f819a6212fb0c617ff1469c741389f MD5 | raw file
  1. /**
  2. * Copyright (C) 2008 Google Inc.
  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 com.google.inject;
  17. import static com.google.inject.Asserts.assertContains;
  18. import com.google.inject.name.Named;
  19. import com.google.inject.name.Names;
  20. import junit.framework.TestCase;
  21. /**
  22. * This test verifies the ways things are injected (ie. getInstance(),
  23. * injectMembers(), bind to instance, and bind to provider instance) for all
  24. * states of optional bindings (fields, methods, multiple-argument methods,
  25. * provider fields, provider methods, constructors).
  26. *
  27. * @author jessewilson@google.com (Jesse Wilson)
  28. */
  29. public class OptionalBindingTest extends TestCase {
  30. private static final A injectA = new A() {};
  31. private static final B injectB = new B() {};
  32. private static final C injectC = new C() {};
  33. private static final D injectD = new D() {};
  34. private static final E injectE = new E() {};
  35. private static final F injectF = new F() {};
  36. private static final G injectG = new G() {};
  37. private Module everythingModule = new AbstractModule() {
  38. protected void configure() {
  39. bind(A.class).toInstance(injectA);
  40. bind(B.class).toInstance(injectB);
  41. bind(C.class).toInstance(injectC);
  42. bind(D.class).toInstance(injectD);
  43. bind(E.class).annotatedWith(Names.named("e")).toInstance(injectE);
  44. bind(F.class).toInstance(injectF);
  45. bind(G.class).toInstance(injectG);
  46. }
  47. };
  48. private Module partialModule = new AbstractModule() {
  49. protected void configure() {
  50. bind(C.class).toInstance(new C() {});
  51. }
  52. };
  53. private Module toInstanceModule = new AbstractModule() {
  54. protected void configure() {
  55. bind(HasOptionalInjections.class)
  56. .toInstance(new HasOptionalInjections());
  57. }
  58. };
  59. private Module toProviderInstanceModule = new AbstractModule() {
  60. protected void configure() {
  61. bind(HasOptionalInjections.class)
  62. .toProvider(new HasOptionalInjectionsProvider());
  63. }
  64. };
  65. private Module toProviderModule = new AbstractModule() {
  66. protected void configure() {
  67. bind(HasOptionalInjections.class)
  68. .toProvider(HasOptionalInjectionsProvider.class);
  69. }
  70. };
  71. public void testEverythingInjectorGetInstance() {
  72. Guice.createInjector(everythingModule)
  73. .getInstance(HasOptionalInjections.class)
  74. .assertEverythingInjected();
  75. }
  76. public void testPartialInjectorGetInstance() {
  77. Guice.createInjector(partialModule)
  78. .getInstance(HasOptionalInjections.class)
  79. .assertNothingInjected();
  80. }
  81. public void testNothingInjectorGetInstance() {
  82. Guice.createInjector()
  83. .getInstance(HasOptionalInjections.class)
  84. .assertNothingInjected();
  85. }
  86. public void testEverythingInjectorInjectMembers() {
  87. HasOptionalInjections instance = new HasOptionalInjections();
  88. Guice.createInjector(everythingModule).injectMembers(instance);
  89. instance.assertEverythingInjected();
  90. }
  91. public void testPartialInjectorInjectMembers() {
  92. HasOptionalInjections instance = new HasOptionalInjections();
  93. Guice.createInjector(partialModule).injectMembers(instance);
  94. instance.assertNothingInjected();
  95. }
  96. public void testNothingInjectorInjectMembers() {
  97. HasOptionalInjections instance = new HasOptionalInjections();
  98. Guice.createInjector().injectMembers(instance);
  99. instance.assertNothingInjected();
  100. }
  101. public void testEverythingInjectorToInstance() {
  102. Guice.createInjector(everythingModule, toInstanceModule)
  103. .getInstance(HasOptionalInjections.class)
  104. .assertEverythingInjected();
  105. }
  106. public void testPartialInjectorToInstance() {
  107. Guice.createInjector(partialModule, toInstanceModule)
  108. .getInstance(HasOptionalInjections.class)
  109. .assertNothingInjected();
  110. }
  111. public void testNothingInjectorToInstance() {
  112. Guice.createInjector(toInstanceModule)
  113. .getInstance(HasOptionalInjections.class)
  114. .assertNothingInjected();
  115. }
  116. public void testEverythingInjectorToProviderInstance() {
  117. Guice.createInjector(everythingModule, toProviderInstanceModule)
  118. .getInstance(HasOptionalInjections.class)
  119. .assertEverythingInjected();
  120. }
  121. public void testPartialInjectorToProviderInstance() {
  122. Guice.createInjector(partialModule, toProviderInstanceModule)
  123. .getInstance(HasOptionalInjections.class)
  124. .assertNothingInjected();
  125. }
  126. public void testNothingInjectorToProviderInstance() {
  127. Guice.createInjector(toProviderInstanceModule)
  128. .getInstance(HasOptionalInjections.class)
  129. .assertNothingInjected();
  130. }
  131. public void testEverythingInjectorToProvider() {
  132. Guice.createInjector(everythingModule, toProviderModule)
  133. .getInstance(HasOptionalInjections.class)
  134. .assertEverythingInjected();
  135. }
  136. public void testPartialInjectorToProvider() {
  137. Guice.createInjector(partialModule, toProviderModule)
  138. .getInstance(HasOptionalInjections.class)
  139. .assertNothingInjected();
  140. }
  141. public void testNothingInjectorToProvider() {
  142. Guice.createInjector(toProviderModule)
  143. .getInstance(HasOptionalInjections.class)
  144. .assertNothingInjected();
  145. }
  146. static class HasOptionalInjections {
  147. A originalA = new A() {};
  148. @Inject(optional=true) A a = originalA; // field injection
  149. B b; // method injection with one argument
  150. C c; // method injection with two arguments
  151. D d; // method injection with two arguments
  152. E e; // annotated injection
  153. @Inject(optional=true) Provider<F> fProvider; // provider
  154. Provider<G> gProvider; // method injection of provider
  155. boolean invoked0, invoked1, invoked2, invokedAnnotated, invokeProvider;
  156. @Inject(optional=true) void methodInjectZeroArguments() {
  157. invoked0 = true;
  158. }
  159. @Inject(optional=true) void methodInjectOneArgument(B b) {
  160. this.b = b;
  161. invoked1 = true;
  162. }
  163. @Inject(optional=true) void methodInjectTwoArguments(C c, D d) {
  164. this.c = c;
  165. this.d = d;
  166. invoked2 = true;
  167. }
  168. @Inject(optional=true) void methodInjectAnnotated(@Named("e") E e) {
  169. this.e = e;
  170. invokedAnnotated = true;
  171. }
  172. @Inject(optional=true) void methodInjectProvider(Provider<G> gProvider) {
  173. this.gProvider = gProvider;
  174. invokeProvider = true;
  175. }
  176. void assertNothingInjected() {
  177. assertSame(originalA, a);
  178. assertNull(b);
  179. assertNull(c);
  180. assertNull(d);
  181. assertNull(e);
  182. assertNull(fProvider);
  183. assertNull(gProvider);
  184. assertTrue(invoked0);
  185. assertFalse(invoked1);
  186. assertFalse(invoked2);
  187. assertFalse(invokedAnnotated);
  188. }
  189. public void assertEverythingInjected() {
  190. assertNotSame(injectA, originalA);
  191. assertSame(injectA, a);
  192. assertSame(injectB, b);
  193. assertSame(injectC, c);
  194. assertSame(injectD, d);
  195. assertSame(injectE, e);
  196. assertSame(injectF, fProvider.get());
  197. assertSame(injectG, gProvider.get());
  198. assertTrue(invoked0);
  199. assertTrue(invoked1);
  200. assertTrue(invoked2);
  201. assertTrue(invokedAnnotated);
  202. }
  203. }
  204. static class HasOptionalInjectionsProvider
  205. extends HasOptionalInjections implements Provider<HasOptionalInjections> {
  206. public HasOptionalInjections get() {
  207. return this;
  208. }
  209. }
  210. public void testOptionalConstructorBlowsUp() {
  211. try {
  212. Guice.createInjector().getInstance(HasOptionalConstructor.class);
  213. fail();
  214. } catch (ConfigurationException expected) {
  215. assertContains(expected.getMessage(), "OptionalBindingTest$HasOptionalConstructor.<init>() "
  216. + "is annotated @Inject(optional=true), but constructors cannot be optional.");
  217. }
  218. }
  219. static class HasOptionalConstructor {
  220. @Inject(optional=true)
  221. HasOptionalConstructor() {}
  222. }
  223. @Inject(optional=true) static A staticInjectA;
  224. public void testStaticInjection() {
  225. staticInjectA = injectA;
  226. Guice.createInjector(new AbstractModule() {
  227. protected void configure() {
  228. requestStaticInjection(OptionalBindingTest.class);
  229. }
  230. });
  231. assertSame(staticInjectA, injectA);
  232. }
  233. /**
  234. * Test for bug 107, where we weren't doing optional injection properly for
  235. * indirect injections.
  236. */
  237. public void testIndirectOptionalInjection() {
  238. Indirect indirect = Guice.createInjector().getInstance(Indirect.class);
  239. assertNotNull(indirect.hasOptionalInjections);
  240. indirect.hasOptionalInjections.assertNothingInjected();
  241. }
  242. static class Indirect {
  243. @Inject HasOptionalInjections hasOptionalInjections;
  244. }
  245. interface A {}
  246. interface B {}
  247. interface C {}
  248. interface D {}
  249. interface E {}
  250. interface F {}
  251. interface G {}
  252. }