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