PageRenderTime 73ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/peterlynch/spice
Java | 388 lines | 330 code | 39 blank | 19 comment | 3 complexity | 46867a90f643437237815e072f0902f8 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.spi;
  17. import com.google.inject.AbstractModule;
  18. import static com.google.inject.Asserts.assertContains;
  19. import com.google.inject.Binding;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Inject;
  22. import com.google.inject.Injector;
  23. import com.google.inject.Key;
  24. import com.google.inject.Module;
  25. import com.google.inject.Provider;
  26. import com.google.inject.Scope;
  27. import com.google.inject.Scopes;
  28. import com.google.inject.Singleton;
  29. import com.google.inject.Stage;
  30. import com.google.inject.internal.util.ImmutableSet;
  31. import com.google.inject.internal.util.Lists;
  32. import com.google.inject.name.Names;
  33. import java.lang.reflect.Constructor;
  34. import java.util.Collections;
  35. import java.util.Comparator;
  36. import java.util.Iterator;
  37. import java.util.List;
  38. import java.util.logging.Logger;
  39. import junit.framework.TestCase;
  40. /**
  41. * @author jessewilson@google.com (Jesse Wilson)
  42. */
  43. public class SpiBindingsTest extends TestCase {
  44. public void testBindConstant() {
  45. checkInjector(
  46. new AbstractModule() {
  47. protected void configure() {
  48. bindConstant().annotatedWith(Names.named("one")).to(1);
  49. }
  50. },
  51. new FailingElementVisitor() {
  52. @Override public <T> Void visit(Binding<T> binding) {
  53. assertTrue(binding instanceof InstanceBinding);
  54. assertEquals(Key.get(Integer.class, Names.named("one")), binding.getKey());
  55. return null;
  56. }
  57. }
  58. );
  59. }
  60. public void testToInstanceBinding() {
  61. checkInjector(
  62. new AbstractModule() {
  63. protected void configure() {
  64. bind(String.class).toInstance("A");
  65. }
  66. },
  67. new FailingElementVisitor() {
  68. @Override public <T> Void visit(Binding<T> binding) {
  69. assertTrue(binding instanceof InstanceBinding);
  70. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  71. assertEquals(Key.get(String.class), binding.getKey());
  72. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  73. @Override public Void visit(InstanceBinding<? extends T> binding) {
  74. assertEquals("A", binding.getInstance());
  75. return null;
  76. }
  77. });
  78. binding.acceptScopingVisitor(new FailingBindingScopingVisitor() {
  79. public Void visitEagerSingleton() {
  80. return null;
  81. }
  82. });
  83. return null;
  84. }
  85. }
  86. );
  87. }
  88. public void testToProviderBinding() {
  89. final Provider<String> stringProvider = new StringProvider();
  90. checkInjector(
  91. new AbstractModule() {
  92. protected void configure() {
  93. bind(String.class).toProvider(stringProvider);
  94. }
  95. },
  96. new FailingElementVisitor() {
  97. @Override public <T> Void visit(Binding<T> binding) {
  98. assertTrue(binding instanceof ProviderInstanceBinding);
  99. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  100. assertEquals(Key.get(String.class), binding.getKey());
  101. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  102. @Override public Void visit(
  103. ProviderInstanceBinding<? extends T> binding) {
  104. assertSame(stringProvider, binding.getProviderInstance());
  105. return null;
  106. }
  107. });
  108. return null;
  109. }
  110. }
  111. );
  112. }
  113. public void testToProviderKeyBinding() {
  114. checkInjector(
  115. new AbstractModule() {
  116. protected void configure() {
  117. bind(String.class).toProvider(StringProvider.class);
  118. }
  119. },
  120. new FailingElementVisitor() {
  121. @Override public <T> Void visit(Binding<T> binding) {
  122. assertTrue(binding instanceof ProviderKeyBinding);
  123. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  124. assertEquals(Key.get(String.class), binding.getKey());
  125. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  126. @Override public Void visit(ProviderKeyBinding<? extends T> binding) {
  127. assertEquals(Key.get(StringProvider.class), binding.getProviderKey());
  128. return null;
  129. }
  130. });
  131. return null;
  132. }
  133. }
  134. );
  135. }
  136. public void testToKeyBinding() {
  137. final Key<String> aKey = Key.get(String.class, Names.named("a"));
  138. final Key<String> bKey = Key.get(String.class, Names.named("b"));
  139. checkInjector(
  140. new AbstractModule() {
  141. protected void configure() {
  142. bind(aKey).to(bKey);
  143. bind(bKey).toInstance("B");
  144. }
  145. },
  146. new FailingElementVisitor() {
  147. @Override public <T> Void visit(Binding<T> binding) {
  148. assertTrue(binding instanceof LinkedKeyBinding);
  149. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  150. assertEquals(aKey, binding.getKey());
  151. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  152. @Override public Void visit(LinkedKeyBinding<? extends T> binding) {
  153. assertEquals(bKey, binding.getLinkedKey());
  154. return null;
  155. }
  156. });
  157. return null;
  158. }
  159. },
  160. new FailingElementVisitor() {
  161. @Override public <T> Void visit(Binding<T> binding) {
  162. assertEquals(bKey, binding.getKey());
  163. return null;
  164. }
  165. }
  166. );
  167. }
  168. public void testToConstructorBinding() {
  169. checkInjector(
  170. new AbstractModule() {
  171. protected void configure() {
  172. bind(D.class);
  173. }
  174. },
  175. new FailingElementVisitor() {
  176. @Override public <T> Void visit(Binding<T> binding) {
  177. assertTrue(binding instanceof ConstructorBinding);
  178. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  179. assertEquals(Key.get(D.class), binding.getKey());
  180. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  181. @Override public Void visit(ConstructorBinding<? extends T> binding) {
  182. Constructor<?> expected = D.class.getDeclaredConstructors()[0];
  183. assertEquals(expected, binding.getConstructor().getMember());
  184. assertEquals(ImmutableSet.<InjectionPoint>of(), binding.getInjectableMembers());
  185. return null;
  186. }
  187. });
  188. return null;
  189. }
  190. }
  191. );
  192. }
  193. public void testConstantBinding() {
  194. checkInjector(
  195. new AbstractModule() {
  196. protected void configure() {
  197. bindConstant().annotatedWith(Names.named("one")).to(1);
  198. }
  199. },
  200. new FailingElementVisitor() {
  201. @Override public <T> Void visit(Binding<T> binding) {
  202. assertTrue(binding instanceof InstanceBinding);
  203. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  204. assertEquals(Key.get(Integer.class, Names.named("one")), binding.getKey());
  205. binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
  206. @Override public Void visit(InstanceBinding<? extends T> binding) {
  207. assertEquals(1, binding.getInstance());
  208. return null;
  209. }
  210. });
  211. return null;
  212. }
  213. }
  214. );
  215. }
  216. public void testConvertedConstantBinding() {
  217. Injector injector = Guice.createInjector(new AbstractModule() {
  218. protected void configure() {
  219. bindConstant().annotatedWith(Names.named("one")).to("1");
  220. }
  221. });
  222. Binding<Integer> binding = injector.getBinding(Key.get(Integer.class, Names.named("one")));
  223. assertEquals(Key.get(Integer.class, Names.named("one")), binding.getKey());
  224. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  225. assertTrue(binding instanceof ConvertedConstantBinding);
  226. binding.acceptTargetVisitor(new FailingTargetVisitor<Integer>() {
  227. @Override public Void visit(
  228. ConvertedConstantBinding<? extends Integer> binding) {
  229. assertEquals((Integer) 1, binding.getValue());
  230. assertEquals(Key.get(String.class, Names.named("one")), binding.getSourceKey());
  231. return null;
  232. }
  233. });
  234. }
  235. public void testProviderBinding() {
  236. Injector injector = Guice.createInjector(new AbstractModule() {
  237. protected void configure() {
  238. bind(String.class).toInstance("A");
  239. }
  240. });
  241. Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {};
  242. Binding<Provider<String>> binding = injector.getBinding(providerOfStringKey);
  243. assertEquals(providerOfStringKey, binding.getKey());
  244. assertContains(binding.getSource().toString(), "SpiBindingsTest.java");
  245. assertTrue(binding instanceof ProviderBinding);
  246. binding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {
  247. @Override public Void visit(
  248. ProviderBinding<? extends Provider<String>> binding) {
  249. assertEquals(Key.get(String.class), binding.getProvidedKey());
  250. return null;
  251. }
  252. });
  253. }
  254. public void testScopes() {
  255. checkInjector(
  256. new AbstractModule() {
  257. protected void configure() {
  258. bind(String.class).annotatedWith(Names.named("a"))
  259. .toProvider(StringProvider.class).in(Singleton.class);
  260. bind(String.class).annotatedWith(Names.named("b"))
  261. .toProvider(StringProvider.class).in(Scopes.SINGLETON);
  262. bind(String.class).annotatedWith(Names.named("c"))
  263. .toProvider(StringProvider.class).asEagerSingleton();
  264. bind(String.class).annotatedWith(Names.named("d"))
  265. .toProvider(StringProvider.class);
  266. }
  267. },
  268. new FailingElementVisitor() {
  269. @Override public <T> Void visit(Binding<T> command) {
  270. assertEquals(Key.get(String.class, Names.named("a")), command.getKey());
  271. command.acceptScopingVisitor(new FailingBindingScopingVisitor() {
  272. @Override public Void visitScope(Scope scope) {
  273. // even though we bound with an annotation, the injector always uses instances
  274. assertSame(Scopes.SINGLETON, scope);
  275. return null;
  276. }
  277. });
  278. return null;
  279. }
  280. },
  281. new FailingElementVisitor() {
  282. @Override public <T> Void visit(Binding<T> command) {
  283. assertEquals(Key.get(String.class, Names.named("b")), command.getKey());
  284. command.acceptScopingVisitor(new FailingBindingScopingVisitor() {
  285. @Override public Void visitScope(Scope scope) {
  286. assertSame(Scopes.SINGLETON, scope);
  287. return null;
  288. }
  289. });
  290. return null;
  291. }
  292. },
  293. new FailingElementVisitor() {
  294. @Override public <T> Void visit(Binding<T> command) {
  295. assertEquals(Key.get(String.class, Names.named("c")), command.getKey());
  296. command.acceptScopingVisitor(new FailingBindingScopingVisitor() {
  297. @Override public Void visitEagerSingleton() {
  298. return null;
  299. }
  300. });
  301. return null;
  302. }
  303. },
  304. new FailingElementVisitor() {
  305. @Override public <T> Void visit(Binding<T> command) {
  306. assertEquals(Key.get(String.class, Names.named("d")), command.getKey());
  307. command.acceptScopingVisitor(new FailingBindingScopingVisitor() {
  308. @Override public Void visitNoScoping() {
  309. return null;
  310. }
  311. });
  312. return null;
  313. }
  314. }
  315. );
  316. }
  317. public void checkInjector(Module module, ElementVisitor<?>... visitors) {
  318. Injector injector = Guice.createInjector(module);
  319. List<Binding<?>> bindings = Lists.newArrayList(injector.getBindings().values());
  320. for (Iterator<Binding<?>> i = bindings.iterator(); i.hasNext(); ) {
  321. if (BUILT_IN_BINDINGS.contains(i.next().getKey())) {
  322. i.remove();
  323. }
  324. }
  325. Collections.sort(bindings, orderByKey);
  326. assertEquals(bindings.size(), visitors.length);
  327. for (int i = 0; i < visitors.length; i++) {
  328. ElementVisitor<?> visitor = visitors[i];
  329. Binding<?> binding = bindings.get(i);
  330. binding.acceptVisitor(visitor);
  331. }
  332. }
  333. private final ImmutableSet<Key<?>> BUILT_IN_BINDINGS = ImmutableSet.of(
  334. Key.get(Injector.class), Key.get(Stage.class), Key.get(Logger.class));
  335. private final Comparator<Binding<?>> orderByKey = new Comparator<Binding<?>>() {
  336. public int compare(Binding<?> a, Binding<?> b) {
  337. return a.getKey().toString().compareTo(b.getKey().toString());
  338. }
  339. };
  340. private static class StringProvider implements Provider<String> {
  341. public String get() {
  342. return "A";
  343. }
  344. }
  345. private static class C { }
  346. private static class D extends C {
  347. @Inject public D(Injector unused) { }
  348. }
  349. }