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

/extensions/assistedinject/test/com/google/inject/assistedinject/ManyConstructorsTest.java

https://gitlab.com/metamorphiccode/guice
Java | 277 lines | 219 code | 40 blank | 18 comment | 0 complexity | eeaf87a50aad26c1ab6f3a758cabf08c MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 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.assistedinject;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Asserts;
  19. import com.google.inject.CreationException;
  20. import com.google.inject.Guice;
  21. import com.google.inject.Injector;
  22. import junit.framework.TestCase;
  23. /**
  24. * @author sameb@google.com (Sam Berlin)
  25. */
  26. public class ManyConstructorsTest extends TestCase {
  27. public void testTwoConstructors() {
  28. Injector injector = Guice.createInjector(new AbstractModule() {
  29. @Override
  30. protected void configure() {
  31. install(new FactoryModuleBuilder().build(Factory.class));
  32. }
  33. });
  34. Factory factory = injector.getInstance(Factory.class);
  35. Foo noIndex = factory.create("no index");
  36. assertEquals("no index", noIndex.name);
  37. assertNull(noIndex.index);
  38. Foo index = factory.create("index", 1);
  39. assertEquals("index", index.name);
  40. assertEquals(1, index.index.intValue());
  41. }
  42. public void testDifferentOrderParameters() {
  43. Injector injector = Guice.createInjector(new AbstractModule() {
  44. @Override
  45. protected void configure() {
  46. install(new FactoryModuleBuilder().build(OtherFactory.class));
  47. }
  48. });
  49. OtherFactory factory = injector.getInstance(OtherFactory.class);
  50. Foo noIndex = factory.create("no index");
  51. assertEquals("no index", noIndex.name);
  52. assertNull(noIndex.index);
  53. Foo index = factory.create(1, "index");
  54. assertEquals("index", index.name);
  55. assertEquals(1, index.index.intValue());
  56. Foo index2 = factory.create("index", 2);
  57. assertEquals("index", index2.name);
  58. assertEquals(2, index2.index.intValue());
  59. }
  60. public void testInterfaceToImpl() {
  61. Injector injector = Guice.createInjector(new AbstractModule() {
  62. @Override
  63. protected void configure() {
  64. install(new FactoryModuleBuilder()
  65. .implement(Bar.class, Foo.class)
  66. .build(BarFactory.class));
  67. }
  68. });
  69. BarFactory factory = injector.getInstance(BarFactory.class);
  70. Bar noIndex = factory.create("no index");
  71. assertEquals("no index", noIndex.getName());
  72. assertNull(noIndex.getIndex());
  73. Bar index = factory.create("index", 1);
  74. assertEquals("index", index.getName());
  75. assertEquals(1, index.getIndex().intValue());
  76. }
  77. public void testUsingOneConstructor() {
  78. Injector injector = Guice.createInjector(new AbstractModule() {
  79. @Override
  80. protected void configure() {
  81. install(new FactoryModuleBuilder().build(SimpleFactory.class));
  82. }
  83. });
  84. SimpleFactory factory = injector.getInstance(SimpleFactory.class);
  85. Foo noIndex = factory.create("no index");
  86. assertEquals("no index", noIndex.name);
  87. assertNull(noIndex.index);
  88. injector = Guice.createInjector(new AbstractModule() {
  89. @Override
  90. protected void configure() {
  91. install(new FactoryModuleBuilder().build(SimpleFactory2.class));
  92. }
  93. });
  94. SimpleFactory2 factory2 = injector.getInstance(SimpleFactory2.class);
  95. Foo index = factory2.create("index", 1);
  96. assertEquals("index", index.name);
  97. assertEquals(1, index.index.intValue());
  98. }
  99. public void testTooManyMatchingConstructors() {
  100. try {
  101. Guice.createInjector(new AbstractModule() {
  102. @Override
  103. protected void configure() {
  104. install(new FactoryModuleBuilder()
  105. .implement(Foo.class, TooManyMatches.class)
  106. .build(SimpleFactory2.class));
  107. }
  108. });
  109. fail("should have failed");
  110. } catch (CreationException expected) {
  111. Asserts.assertContains(expected.getMessage(), "1) " + TooManyMatches.class.getName()
  112. + " has more than one constructor annotated with @AssistedInject that "
  113. + "matches the parameters in method " + SimpleFactory2.class.getName());
  114. }
  115. }
  116. public void testNoMatchingConstructorsBecauseTooManyParams() {
  117. try {
  118. Guice.createInjector(new AbstractModule() {
  119. @Override
  120. protected void configure() {
  121. install(new FactoryModuleBuilder().build(ComplexFactory.class));
  122. }
  123. });
  124. fail("should have failed");
  125. } catch (CreationException expected) {
  126. Asserts.assertContains(expected.getMessage(), "1) " + Foo.class.getName()
  127. + " has @AssistedInject constructors, but none of them match the parameters in method "
  128. + ComplexFactory.class.getName());
  129. }
  130. }
  131. public void testNoMatchingConstrucotsBecauseTooLittleParams() {
  132. try {
  133. Guice.createInjector(new AbstractModule() {
  134. @Override
  135. protected void configure() {
  136. install(new FactoryModuleBuilder().build(NullFactory.class));
  137. }
  138. });
  139. fail("should have failed");
  140. } catch (CreationException expected) {
  141. Asserts.assertContains(expected.getMessage(), "1) " + Foo.class.getName()
  142. + " has @AssistedInject constructors, but none of them match the parameters in method "
  143. + NullFactory.class.getName());
  144. }
  145. }
  146. public static interface ComplexFactory {
  147. Foo create(String name, int idx, float weight);
  148. }
  149. public static interface NullFactory {
  150. Foo create();
  151. }
  152. public static interface OtherFactory {
  153. Foo create(String name, int idx);
  154. Foo create(int idx, String name);
  155. Foo create(String name);
  156. }
  157. public static interface Factory {
  158. Foo create(String name);
  159. Foo create(String name, int idx);
  160. }
  161. public static interface BarFactory {
  162. Bar create(String name);
  163. Bar create(String name, int idx);
  164. }
  165. public static interface SimpleFactory {
  166. Foo create(String name);
  167. }
  168. public static interface SimpleFactory2 {
  169. Foo create(String name, int idx);
  170. }
  171. public static class TooManyMatches extends Foo {
  172. @AssistedInject TooManyMatches(@Assisted String name, @Assisted int index) {
  173. }
  174. @AssistedInject TooManyMatches(@Assisted int index, @Assisted String name) {
  175. }
  176. }
  177. public static class Foo implements Bar {
  178. private String name;
  179. private Integer index;
  180. Foo() {}
  181. @AssistedInject Foo(@Assisted String name) {
  182. this.name = name;
  183. this.index = null;
  184. }
  185. @AssistedInject Foo(@Assisted String name, @Assisted int index) {
  186. this.name = name;
  187. this.index = index;
  188. }
  189. Foo(String a, String b, String c) {
  190. }
  191. public String getName() { return name; }
  192. public Integer getIndex() { return index; }
  193. }
  194. public static interface Bar {
  195. String getName();
  196. Integer getIndex();
  197. }
  198. public void testDependenciesAndOtherAnnotations() {
  199. Injector injector = Guice.createInjector(new AbstractModule() {
  200. @Override
  201. protected void configure() {
  202. install(new FactoryModuleBuilder().build(FamilyFarmFactory.class));
  203. }
  204. });
  205. FamilyFarmFactory factory = injector.getInstance(FamilyFarmFactory.class);
  206. Farm pops = factory.popsFarm("Pop");
  207. assertEquals("Pop", pops.pop);
  208. assertEquals(null, pops.mom);
  209. Farm moms = factory.momsFarm("Mom");
  210. assertEquals(null, moms.pop);
  211. assertEquals("Mom", moms.mom);
  212. Farm momAndPop = factory.momAndPopsFarm("Mom", "Pop");
  213. assertEquals("Pop", momAndPop.pop);
  214. assertEquals("Mom", momAndPop.mom);
  215. }
  216. public static interface FamilyFarmFactory {
  217. Farm popsFarm(String pop);
  218. Farm momsFarm(@Assisted("mom") String mom);
  219. Farm momAndPopsFarm(@Assisted("mom") String mom, @Assisted("pop") String pop);
  220. }
  221. public static class Farm {
  222. String pop;
  223. String mom;
  224. @AssistedInject Farm(@Assisted String pop, Dog dog) {
  225. this.pop = pop;
  226. }
  227. @AssistedInject Farm(@Assisted("mom") String mom, @Assisted("pop") String pop, Cow cow, Dog dog) {
  228. this.pop = pop;
  229. this.mom = mom;
  230. }
  231. @AssistedInject Farm(@Assisted("mom") String mom, Cow cow) {
  232. this.mom = mom;
  233. }
  234. }
  235. public static class Cow {}
  236. public static class Dog {}
  237. }