| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.inject.testing.fieldbinder; |
| 18 |
|
| 19 |
import static com.google.inject.Asserts.assertContains; |
| 20 |
import static java.lang.annotation.ElementType.TYPE_USE; |
| 21 |
import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 22 |
|
| 23 |
import com.google.common.collect.Iterables; |
| 24 |
import com.google.inject.BindingAnnotation; |
| 25 |
import com.google.inject.ConfigurationException; |
| 26 |
import com.google.inject.CreationException; |
| 27 |
import com.google.inject.Guice; |
| 28 |
import com.google.inject.Inject; |
| 29 |
import com.google.inject.Injector; |
| 30 |
import com.google.inject.Key; |
| 31 |
import com.google.inject.Provider; |
| 32 |
import com.google.inject.ProvisionException; |
| 33 |
import com.google.inject.RestrictedBindingSource; |
| 34 |
import com.google.inject.name.Named; |
| 35 |
import com.google.inject.name.Names; |
| 36 |
import com.google.inject.spi.ElementSource; |
| 37 |
import com.google.inject.testing.fieldbinder.BoundFieldModule.BoundFieldInfo; |
| 38 |
import com.google.inject.util.Providers; |
| 39 |
import java.lang.annotation.Retention; |
| 40 |
import java.lang.annotation.RetentionPolicy; |
| 41 |
import java.lang.annotation.Target; |
| 42 |
import java.util.Arrays; |
| 43 |
import java.util.List; |
| 44 |
import javax.inject.Qualifier; |
| 45 |
import junit.framework.TestCase; |
| 46 |
|
| 47 |
|
| 48 |
public class BoundFieldModuleTest extends TestCase { |
| 49 |
public void testBindingNothing() { |
| 50 |
Object instance = new Object() {}; |
| 51 |
|
| 52 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 53 |
Guice.createInjector(module); |
| 54 |
|
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
public void testBindingOnePrivate() { |
| 59 |
final Integer testValue = 1024; |
| 60 |
Object instance = |
| 61 |
new Object() { |
| 62 |
@Bind private Integer anInt = testValue; |
| 63 |
}; |
| 64 |
|
| 65 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 66 |
Injector injector = Guice.createInjector(module); |
| 67 |
|
| 68 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 69 |
} |
| 70 |
|
| 71 |
public void testBindingOnePublic() { |
| 72 |
final Integer testValue = 1024; |
| 73 |
Object instance = |
| 74 |
new Object() { |
| 75 |
@Bind public Integer anInt = testValue; |
| 76 |
}; |
| 77 |
|
| 78 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 79 |
Injector injector = Guice.createInjector(module); |
| 80 |
|
| 81 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 82 |
} |
| 83 |
|
| 84 |
private static class FieldBindableClass { |
| 85 |
@Bind Integer anInt; |
| 86 |
|
| 87 |
FieldBindableClass(Integer anInt) { |
| 88 |
this.anInt = anInt; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private static class FieldBindableSubclass extends FieldBindableClass { |
| 93 |
FieldBindableSubclass(Integer anInt) { |
| 94 |
super(anInt); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
public void testSuperTypeBinding() { |
| 99 |
FieldBindableSubclass instance = new FieldBindableSubclass(1024); |
| 100 |
|
| 101 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 102 |
Injector injector = Guice.createInjector(module); |
| 103 |
|
| 104 |
assertEquals(instance.anInt, injector.getInstance(Integer.class)); |
| 105 |
} |
| 106 |
|
| 107 |
public void testBindingTwo() { |
| 108 |
final Integer testValue = 1024; |
| 109 |
final String testString = "Hello World!"; |
| 110 |
Object instance = |
| 111 |
new Object() { |
| 112 |
@Bind private Integer anInt = testValue; |
| 113 |
@Bind private String aString = testString; |
| 114 |
}; |
| 115 |
|
| 116 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 117 |
Injector injector = Guice.createInjector(module); |
| 118 |
|
| 119 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 120 |
assertEquals(testString, injector.getInstance(String.class)); |
| 121 |
} |
| 122 |
|
| 123 |
public void testBindingSuperType() { |
| 124 |
final Integer testValue = 1024; |
| 125 |
Object instance = |
| 126 |
new Object() { |
| 127 |
@Bind(to = Number.class) |
| 128 |
private Integer anInt = testValue; |
| 129 |
}; |
| 130 |
|
| 131 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 132 |
Injector injector = Guice.createInjector(module); |
| 133 |
|
| 134 |
assertEquals(testValue, injector.getInstance(Number.class)); |
| 135 |
} |
| 136 |
|
| 137 |
public void testBindingSuperTypeAccessSubType() { |
| 138 |
final Integer testValue = 1024; |
| 139 |
Object instance = |
| 140 |
new Object() { |
| 141 |
@Bind(to = Number.class) |
| 142 |
private Integer anInt = testValue; |
| 143 |
}; |
| 144 |
|
| 145 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 146 |
Injector injector = Guice.createInjector(module); |
| 147 |
|
| 148 |
try { |
| 149 |
injector.getInstance(Integer.class); |
| 150 |
fail(); |
| 151 |
} catch (ConfigurationException e) { |
| 152 |
assertContains(e.getMessage(), "No injectable constructor for type Integer."); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
public void testBindingIncorrectTypeProviderFails() { |
| 157 |
final Integer testValue = 1024; |
| 158 |
Object instance = |
| 159 |
new Object() { |
| 160 |
@Bind(to = String.class) |
| 161 |
private Provider<Integer> anIntProvider = |
| 162 |
new Provider<Integer>() { |
| 163 |
@Override |
| 164 |
public Integer get() { |
| 165 |
return testValue; |
| 166 |
} |
| 167 |
}; |
| 168 |
}; |
| 169 |
|
| 170 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 171 |
|
| 172 |
try { |
| 173 |
Guice.createInjector(module); |
| 174 |
fail(); |
| 175 |
} catch (CreationException e) { |
| 176 |
assertContains( |
| 177 |
e.getMessage(), |
| 178 |
"Requested binding type \"java.lang.String\" is not " |
| 179 |
+ "assignable from field binding type \"java.lang.Integer\""); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
public void testBindingPrimitive() { |
| 184 |
Object instance = |
| 185 |
new Object() { |
| 186 |
@Bind boolean boolValue = true; |
| 187 |
@Bind char charValue = 'b'; |
| 188 |
@Bind byte byteValue = 3; |
| 189 |
@Bind short shortValue = 4; |
| 190 |
@Bind int intValue = 5; |
| 191 |
@Bind long longValue = 6; |
| 192 |
@Bind float floatValue = 7; |
| 193 |
@Bind double doubleValue = 8; |
| 194 |
}; |
| 195 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 196 |
Injector injector = Guice.createInjector(module); |
| 197 |
|
| 198 |
assertTrue(injector.getInstance(Boolean.class)); |
| 199 |
assertEquals('b', (char) injector.getInstance(Character.class)); |
| 200 |
assertEquals(3, (byte) injector.getInstance(Byte.class)); |
| 201 |
assertEquals(4, (short) injector.getInstance(Short.class)); |
| 202 |
assertEquals(5, (int) injector.getInstance(Integer.class)); |
| 203 |
assertEquals(6, (long) injector.getInstance(Long.class)); |
| 204 |
assertEquals(7, injector.getInstance(Float.class), 0); |
| 205 |
assertEquals(8, injector.getInstance(Double.class), 0); |
| 206 |
} |
| 207 |
|
| 208 |
public void testBindingPrimitiveToBoxed() { |
| 209 |
Object instance = |
| 210 |
new Object() { |
| 211 |
@Bind(to = Integer.class) |
| 212 |
int value = 1; |
| 213 |
}; |
| 214 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 215 |
Injector injector = Guice.createInjector(module); |
| 216 |
|
| 217 |
assertEquals(1, (int) injector.getInstance(Integer.class)); |
| 218 |
} |
| 219 |
|
| 220 |
@BindingAnnotation |
| 221 |
@Retention(RUNTIME) |
| 222 |
private static @interface SomeBindingAnnotation {} |
| 223 |
|
| 224 |
public void testBindingWithBindingAnnotation() { |
| 225 |
final Integer testValue1 = 1024, testValue2 = 2048; |
| 226 |
Object instance = |
| 227 |
new Object() { |
| 228 |
@Bind private Integer anInt = testValue1; |
| 229 |
|
| 230 |
@Bind @SomeBindingAnnotation private Integer anotherInt = testValue2; |
| 231 |
}; |
| 232 |
|
| 233 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 234 |
Injector injector = Guice.createInjector(module); |
| 235 |
|
| 236 |
assertEquals(testValue1, injector.getInstance(Integer.class)); |
| 237 |
assertEquals( |
| 238 |
testValue2, injector.getInstance(Key.get(Integer.class, SomeBindingAnnotation.class))); |
| 239 |
} |
| 240 |
|
| 241 |
@Qualifier |
| 242 |
@Retention(RUNTIME) |
| 243 |
private static @interface SomeQualifier {} |
| 244 |
|
| 245 |
public void testBindingWithQualifier() { |
| 246 |
final Integer testValue1 = 1024, testValue2 = 2048; |
| 247 |
Object instance = |
| 248 |
new Object() { |
| 249 |
@Bind private Integer anInt = testValue1; |
| 250 |
|
| 251 |
@Bind @SomeQualifier private Integer anotherInt = testValue2; |
| 252 |
}; |
| 253 |
|
| 254 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 255 |
Injector injector = Guice.createInjector(module); |
| 256 |
|
| 257 |
assertEquals(testValue1, injector.getInstance(Integer.class)); |
| 258 |
assertEquals(testValue2, injector.getInstance(Key.get(Integer.class, SomeQualifier.class))); |
| 259 |
} |
| 260 |
|
| 261 |
public void testCanReuseBindingAnnotationsWithDifferentValues() { |
| 262 |
final Integer testValue1 = 1024, testValue2 = 2048; |
| 263 |
final String name1 = "foo", name2 = "bar"; |
| 264 |
Object instance = |
| 265 |
new Object() { |
| 266 |
@Bind |
| 267 |
@Named(name1) |
| 268 |
private Integer anInt = testValue1; |
| 269 |
|
| 270 |
@Bind |
| 271 |
@Named(name2) |
| 272 |
private Integer anotherInt = testValue2; |
| 273 |
}; |
| 274 |
|
| 275 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 276 |
Injector injector = Guice.createInjector(module); |
| 277 |
|
| 278 |
assertEquals(testValue1, injector.getInstance(Key.get(Integer.class, Names.named(name1)))); |
| 279 |
assertEquals(testValue2, injector.getInstance(Key.get(Integer.class, Names.named(name2)))); |
| 280 |
} |
| 281 |
|
| 282 |
public void testBindingWithValuedBindingAnnotation() { |
| 283 |
final Integer testValue1 = 1024, testValue2 = 2048; |
| 284 |
final String name = "foo"; |
| 285 |
Object instance = |
| 286 |
new Object() { |
| 287 |
@Bind private Integer anInt = testValue1; |
| 288 |
|
| 289 |
@Bind |
| 290 |
@Named(name) |
| 291 |
private Integer anotherInt = testValue2; |
| 292 |
}; |
| 293 |
|
| 294 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 295 |
Injector injector = Guice.createInjector(module); |
| 296 |
|
| 297 |
assertEquals(testValue1, injector.getInstance(Integer.class)); |
| 298 |
assertEquals(testValue2, injector.getInstance(Key.get(Integer.class, Names.named(name)))); |
| 299 |
} |
| 300 |
|
| 301 |
public void testBindingWithGenerics() { |
| 302 |
final List<Integer> testIntList = Arrays.asList(new Integer[] {1, 2, 3}); |
| 303 |
final List<Boolean> testBoolList = Arrays.asList(new Boolean[] {true, true, false}); |
| 304 |
Object instance = |
| 305 |
new Object() { |
| 306 |
@Bind private List<Integer> anIntList = testIntList; |
| 307 |
@Bind private List<Boolean> aBoolList = testBoolList; |
| 308 |
}; |
| 309 |
|
| 310 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 311 |
Injector injector = Guice.createInjector(module); |
| 312 |
|
| 313 |
assertEquals(testIntList, injector.getInstance(new Key<List<Integer>>() {})); |
| 314 |
assertEquals(testBoolList, injector.getInstance(new Key<List<Boolean>>() {})); |
| 315 |
} |
| 316 |
|
| 317 |
public void testBoundValueDoesntChange() { |
| 318 |
Integer testValue = 1024; |
| 319 |
FieldBindableClass instance = new FieldBindableClass(testValue); |
| 320 |
|
| 321 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 322 |
Injector injector = Guice.createInjector(module); |
| 323 |
|
| 324 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 325 |
instance.anInt++; |
| 326 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 327 |
} |
| 328 |
|
| 329 |
public void testIncompatibleBindingType() { |
| 330 |
final Integer testInt = 1024; |
| 331 |
Object instance = |
| 332 |
new Object() { |
| 333 |
@Bind(to = String.class) |
| 334 |
private Integer anInt = testInt; |
| 335 |
}; |
| 336 |
|
| 337 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 338 |
|
| 339 |
try { |
| 340 |
Guice.createInjector(module); |
| 341 |
fail(); |
| 342 |
} catch (CreationException e) { |
| 343 |
assertContains( |
| 344 |
e.getMessage(), |
| 345 |
"Requested binding type \"java.lang.String\" is not assignable from field binding type " |
| 346 |
+ "\"java.lang.Integer\""); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
public void testIncompatiblePrimitiveBindingType() { |
| 351 |
Object instance = |
| 352 |
new Object() { |
| 353 |
@Bind(to = Long.class) |
| 354 |
int value = 1; |
| 355 |
}; |
| 356 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 357 |
|
| 358 |
try { |
| 359 |
Guice.createInjector(module); |
| 360 |
fail(); |
| 361 |
} catch (CreationException e) { |
| 362 |
assertContains( |
| 363 |
e.getMessage(), |
| 364 |
"Requested binding type \"java.lang.Long\" is not assignable from field binding type " |
| 365 |
+ "\"java.lang.Integer\""); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
public void testFailureOnMultipleBindingAnnotations() { |
| 370 |
final Integer testInt = 1024; |
| 371 |
Object instance = |
| 372 |
new Object() { |
| 373 |
@Bind |
| 374 |
@Named("a") |
| 375 |
@SomeBindingAnnotation |
| 376 |
private Integer anInt = testInt; |
| 377 |
}; |
| 378 |
|
| 379 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 380 |
|
| 381 |
try { |
| 382 |
Guice.createInjector(module); |
| 383 |
fail(); |
| 384 |
} catch (CreationException e) { |
| 385 |
assertContains(e.getMessage(), "More than one annotation is specified for this binding."); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
public void testBindingSuperTypeAndBindingAnnotation() { |
| 390 |
final Integer testValue = 1024; |
| 391 |
Object instance = |
| 392 |
new Object() { |
| 393 |
@Bind(to = Number.class) |
| 394 |
@Named("foo") |
| 395 |
private Integer anInt = testValue; |
| 396 |
}; |
| 397 |
|
| 398 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 399 |
Injector injector = Guice.createInjector(module); |
| 400 |
|
| 401 |
assertEquals(testValue, injector.getInstance(Key.get(Number.class, Names.named("foo")))); |
| 402 |
} |
| 403 |
|
| 404 |
public void testBindingProvider() { |
| 405 |
final Integer testValue = 1024; |
| 406 |
Object instance = |
| 407 |
new Object() { |
| 408 |
@Bind |
| 409 |
private Provider<Integer> anInt = |
| 410 |
new Provider<Integer>() { |
| 411 |
@Override |
| 412 |
public Integer get() { |
| 413 |
return testValue; |
| 414 |
} |
| 415 |
}; |
| 416 |
}; |
| 417 |
|
| 418 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 419 |
Injector injector = Guice.createInjector(module); |
| 420 |
|
| 421 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 422 |
} |
| 423 |
|
| 424 |
public void testBindingJavaxProvider() { |
| 425 |
final Integer testValue = 1024; |
| 426 |
Object instance = |
| 427 |
new Object() { |
| 428 |
@Bind |
| 429 |
private javax.inject.Provider<Integer> anInt = |
| 430 |
new javax.inject.Provider<Integer>() { |
| 431 |
@Override |
| 432 |
public Integer get() { |
| 433 |
return testValue; |
| 434 |
} |
| 435 |
}; |
| 436 |
}; |
| 437 |
|
| 438 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 439 |
Injector injector = Guice.createInjector(module); |
| 440 |
|
| 441 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 442 |
} |
| 443 |
|
| 444 |
public void testBindingNonNullableNullField() { |
| 445 |
Object instance = |
| 446 |
new Object() { |
| 447 |
@Bind private Integer anInt = null; |
| 448 |
}; |
| 449 |
|
| 450 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 451 |
|
| 452 |
try { |
| 453 |
Guice.createInjector(module); |
| 454 |
fail(); |
| 455 |
} catch (CreationException e) { |
| 456 |
assertContains( |
| 457 |
e.getMessage(), |
| 458 |
"Binding to null values is only allowed for fields that are annotated @Nullable."); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
@Retention(RUNTIME) |
| 463 |
private @interface Nullable {} |
| 464 |
|
| 465 |
private static class TypeUse { |
| 466 |
@Retention(RUNTIME) |
| 467 |
@Target(TYPE_USE) |
| 468 |
private @interface Nullable {} |
| 469 |
} |
| 470 |
|
| 471 |
public void testBindingNullableNullField() { |
| 472 |
Object instance = |
| 473 |
new Object() { |
| 474 |
@Bind @Nullable private Integer anInt = null; |
| 475 |
@Bind @TypeUse.Nullable private Long aLong = null; |
| 476 |
}; |
| 477 |
|
| 478 |
Injector injector = Guice.createInjector(BoundFieldModule.of(instance)); |
| 479 |
assertNull(injector.getInstance(Integer.class)); |
| 480 |
assertNull(injector.getInstance(Long.class)); |
| 481 |
} |
| 482 |
|
| 483 |
public void testBindingNullProvider() { |
| 484 |
Object instance = |
| 485 |
new Object() { |
| 486 |
@Bind private Provider<Integer> anIntProvider = null; |
| 487 |
}; |
| 488 |
|
| 489 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 490 |
|
| 491 |
try { |
| 492 |
Guice.createInjector(module); |
| 493 |
fail(); |
| 494 |
} catch (CreationException e) { |
| 495 |
assertContains( |
| 496 |
e.getMessage(), |
| 497 |
"Binding to null is not allowed. Use Providers.of(null) if this is your intended " |
| 498 |
+ "behavior."); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
public void testBindingNullableNullProvider() { |
| 503 |
Object instance = |
| 504 |
new Object() { |
| 505 |
@Bind @Nullable private Provider<Integer> anIntProvider = null; |
| 506 |
}; |
| 507 |
|
| 508 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 509 |
|
| 510 |
try { |
| 511 |
Guice.createInjector(module); |
| 512 |
fail(); |
| 513 |
} catch (CreationException e) { |
| 514 |
assertContains( |
| 515 |
e.getMessage(), |
| 516 |
"Binding to null is not allowed. Use Providers.of(null) if this is your intended " |
| 517 |
+ "behavior."); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
private static class IntegerProvider implements Provider<Integer> { |
| 522 |
private final Integer value; |
| 523 |
|
| 524 |
IntegerProvider(Integer value) { |
| 525 |
this.value = value; |
| 526 |
} |
| 527 |
|
| 528 |
@Override |
| 529 |
public Integer get() { |
| 530 |
return value; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
public void testProviderSubclassesBindToTheProviderItself() { |
| 535 |
final IntegerProvider integerProvider = new IntegerProvider(1024); |
| 536 |
Object instance = |
| 537 |
new Object() { |
| 538 |
@Bind private IntegerProvider anIntProvider = integerProvider; |
| 539 |
}; |
| 540 |
|
| 541 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 542 |
Injector injector = Guice.createInjector(module); |
| 543 |
|
| 544 |
assertEquals(integerProvider, injector.getInstance(IntegerProvider.class)); |
| 545 |
} |
| 546 |
|
| 547 |
public void testProviderSubclassesDoNotBindParameterizedType() { |
| 548 |
final Integer testValue = 1024; |
| 549 |
Object instance = |
| 550 |
new Object() { |
| 551 |
@Bind private IntegerProvider anIntProvider = new IntegerProvider(testValue); |
| 552 |
}; |
| 553 |
|
| 554 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 555 |
Injector injector = Guice.createInjector(module); |
| 556 |
|
| 557 |
try { |
| 558 |
injector.getInstance(Integer.class); |
| 559 |
fail(); |
| 560 |
} catch (ConfigurationException e) { |
| 561 |
assertContains(e.getMessage(), "No injectable constructor for type Integer."); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
public void testNullableProviderSubclassesAllowNull() { |
| 566 |
Object instance = |
| 567 |
new Object() { |
| 568 |
@Bind @Nullable private IntegerProvider anIntProvider = null; |
| 569 |
}; |
| 570 |
|
| 571 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 572 |
Injector injector = Guice.createInjector(module); |
| 573 |
|
| 574 |
assertNull(injector.getInstance(IntegerProvider.class)); |
| 575 |
} |
| 576 |
|
| 577 |
private static class ParameterizedObject<T> { |
| 578 |
ParameterizedObject(T instance) { |
| 579 |
this.instance = instance; |
| 580 |
} |
| 581 |
|
| 582 |
@Bind private T instance; |
| 583 |
} |
| 584 |
|
| 585 |
public void testBindParameterizedTypeFails() { |
| 586 |
ParameterizedObject<Integer> instance = new ParameterizedObject<>(0); |
| 587 |
|
| 588 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 589 |
|
| 590 |
try { |
| 591 |
Guice.createInjector(module); |
| 592 |
fail(); |
| 593 |
} catch (CreationException e) { |
| 594 |
assertContains(e.getMessage(), "T cannot be used as a key; It is not fully specified."); |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
public void testBindSubclassOfParameterizedTypeSucceeds() { |
| 599 |
final Integer testValue = 1024; |
| 600 |
ParameterizedObject<Integer> instance = new ParameterizedObject<Integer>(testValue) {}; |
| 601 |
|
| 602 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 603 |
Injector injector = Guice.createInjector(module); |
| 604 |
|
| 605 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 606 |
} |
| 607 |
|
| 608 |
public void testBindArray() { |
| 609 |
final Integer[] testArray = new Integer[] {1024, 2048}; |
| 610 |
Object instance = |
| 611 |
new Object() { |
| 612 |
@Bind private Integer[] anIntArray = testArray; |
| 613 |
}; |
| 614 |
|
| 615 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 616 |
Injector injector = Guice.createInjector(module); |
| 617 |
|
| 618 |
assertEquals(testArray, injector.getInstance(Integer[].class)); |
| 619 |
} |
| 620 |
|
| 621 |
@SuppressWarnings("rawtypes") |
| 622 |
public void testRawProviderCannotBeBound() { |
| 623 |
final Integer testValue = 1024; |
| 624 |
Object instance = |
| 625 |
new Object() { |
| 626 |
@Bind |
| 627 |
private Provider anIntProvider = |
| 628 |
new Provider() { |
| 629 |
@Override |
| 630 |
public Object get() { |
| 631 |
return testValue; |
| 632 |
} |
| 633 |
}; |
| 634 |
}; |
| 635 |
|
| 636 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 637 |
|
| 638 |
try { |
| 639 |
Guice.createInjector(module); |
| 640 |
fail(); |
| 641 |
} catch (CreationException e) { |
| 642 |
assertContains( |
| 643 |
e.getMessage(), |
| 644 |
"Non parameterized Provider fields must have an " |
| 645 |
+ "explicit binding class via @Bind(to = Foo.class)"); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
@SuppressWarnings("rawtypes") |
| 650 |
public void testExplicitlyBoundRawProviderCanBeBound() { |
| 651 |
final Integer testValue = 1024; |
| 652 |
Object instance = |
| 653 |
new Object() { |
| 654 |
@Bind(to = Integer.class) |
| 655 |
private Provider anIntProvider = |
| 656 |
new Provider() { |
| 657 |
@Override |
| 658 |
public Object get() { |
| 659 |
return testValue; |
| 660 |
} |
| 661 |
}; |
| 662 |
}; |
| 663 |
|
| 664 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 665 |
Injector injector = Guice.createInjector(module); |
| 666 |
|
| 667 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 668 |
} |
| 669 |
|
| 670 |
@SuppressWarnings("rawtypes") |
| 671 |
public void testRawProviderCanBindToIncorrectType() { |
| 672 |
final Integer testValue = 1024; |
| 673 |
Object instance = |
| 674 |
new Object() { |
| 675 |
@Bind(to = String.class) |
| 676 |
private Provider anIntProvider = |
| 677 |
new Provider() { |
| 678 |
@Override |
| 679 |
public Object get() { |
| 680 |
return testValue; |
| 681 |
} |
| 682 |
}; |
| 683 |
}; |
| 684 |
|
| 685 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 686 |
Injector injector = Guice.createInjector(module); |
| 687 |
|
| 688 |
assertEquals(testValue, injector.getInstance(String.class)); |
| 689 |
} |
| 690 |
|
| 691 |
public void testMultipleBindErrorsAreAggregated() { |
| 692 |
Object instance = |
| 693 |
new Object() { |
| 694 |
@Bind private Provider<Object> aProvider; |
| 695 |
|
| 696 |
@Bind(to = String.class) |
| 697 |
private Integer anInt; |
| 698 |
}; |
| 699 |
|
| 700 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 701 |
try { |
| 702 |
Guice.createInjector(module); |
| 703 |
fail(); |
| 704 |
} catch (CreationException e) { |
| 705 |
assertEquals(2, e.getErrorMessages().size()); |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
public void testMultipleNullValueErrorsAreAggregated() { |
| 710 |
Object instance = |
| 711 |
new Object() { |
| 712 |
@Bind private String first; |
| 713 |
@Bind private String second; |
| 714 |
}; |
| 715 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 716 |
try { |
| 717 |
Guice.createInjector(module); |
| 718 |
fail(); |
| 719 |
} catch (CreationException e) { |
| 720 |
assertEquals(2, e.getErrorMessages().size()); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
public void testBindingProviderWithProviderSubclassValue() { |
| 725 |
final Integer testValue = 1024; |
| 726 |
Object instance = |
| 727 |
new Object() { |
| 728 |
@Bind private Provider<Integer> anIntProvider = new IntegerProvider(testValue); |
| 729 |
}; |
| 730 |
|
| 731 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 732 |
Injector injector = Guice.createInjector(module); |
| 733 |
|
| 734 |
assertEquals(testValue, injector.getInstance(Integer.class)); |
| 735 |
} |
| 736 |
|
| 737 |
public void testBoundFieldsCannotBeInjected() { |
| 738 |
Object instance = |
| 739 |
new Object() { |
| 740 |
@Bind @Inject Integer anInt = 0; |
| 741 |
}; |
| 742 |
|
| 743 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 744 |
|
| 745 |
try { |
| 746 |
Guice.createInjector(module); |
| 747 |
fail(); |
| 748 |
} catch (CreationException e) { |
| 749 |
assertContains(e.getMessage(), "Fields annotated with both @Bind and @Inject are illegal."); |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
public void testIncrementingProvider() { |
| 754 |
final Integer testBaseValue = 1024; |
| 755 |
Object instance = |
| 756 |
new Object() { |
| 757 |
@Bind |
| 758 |
private Provider<Integer> anIntProvider = |
| 759 |
new Provider<Integer>() { |
| 760 |
private int value = testBaseValue; |
| 761 |
|
| 762 |
@Override |
| 763 |
public Integer get() { |
| 764 |
return value++; |
| 765 |
} |
| 766 |
}; |
| 767 |
}; |
| 768 |
|
| 769 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 770 |
Injector injector = Guice.createInjector(module); |
| 771 |
|
| 772 |
assertEquals(testBaseValue, injector.getInstance(Integer.class)); |
| 773 |
assertEquals((Integer) (testBaseValue + 1), injector.getInstance(Integer.class)); |
| 774 |
assertEquals((Integer) (testBaseValue + 2), injector.getInstance(Integer.class)); |
| 775 |
} |
| 776 |
|
| 777 |
public void testProviderDoesNotProvideDuringInjectorConstruction() { |
| 778 |
Object instance = |
| 779 |
new Object() { |
| 780 |
@Bind |
| 781 |
private Provider<Integer> myIntProvider = |
| 782 |
new Provider<Integer>() { |
| 783 |
@Override |
| 784 |
public Integer get() { |
| 785 |
throw new UnsupportedOperationException(); |
| 786 |
} |
| 787 |
}; |
| 788 |
}; |
| 789 |
|
| 790 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 791 |
Guice.createInjector(module); |
| 792 |
|
| 793 |
|
| 794 |
} |
| 795 |
|
| 796 |
private static class InvalidBindableClass { |
| 797 |
@Bind(to = String.class) |
| 798 |
Integer anInt; |
| 799 |
} |
| 800 |
|
| 801 |
public void testIncompatibleBindingTypeStackTraceHasUserFrame() { |
| 802 |
Object instance = new InvalidBindableClass(); |
| 803 |
|
| 804 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 805 |
|
| 806 |
try { |
| 807 |
Guice.createInjector(module); |
| 808 |
fail(); |
| 809 |
} catch (CreationException e) { |
| 810 |
assertContains(e.getMessage(), "at BoundFieldModuleTest$InvalidBindableClass.anInt"); |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
private static class InjectedNumberProvider implements Provider<Number> { |
| 815 |
@Inject Integer anInt; |
| 816 |
|
| 817 |
@Override |
| 818 |
public Number get() { |
| 819 |
return anInt; |
| 820 |
} |
| 821 |
} |
| 822 |
|
| 823 |
public void testBoundProvidersAreInjected() { |
| 824 |
final Integer testValue = 1024; |
| 825 |
Object instance = |
| 826 |
new Object() { |
| 827 |
@Bind private Integer anInt = testValue; |
| 828 |
@Bind private Provider<Number> aNumberProvider = new InjectedNumberProvider(); |
| 829 |
}; |
| 830 |
|
| 831 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 832 |
Injector injector = Guice.createInjector(module); |
| 833 |
|
| 834 |
assertEquals(testValue, injector.getInstance(Number.class)); |
| 835 |
} |
| 836 |
|
| 837 |
public void testBoundInstancesAreInjected() { |
| 838 |
final Integer testValue = 1024; |
| 839 |
final InjectedNumberProvider testNumberProvider = new InjectedNumberProvider(); |
| 840 |
Object instance = |
| 841 |
new Object() { |
| 842 |
@Bind private Integer anInt = testValue; |
| 843 |
@Bind private InjectedNumberProvider aNumberProvider = testNumberProvider; |
| 844 |
}; |
| 845 |
|
| 846 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 847 |
Guice.createInjector(module); |
| 848 |
|
| 849 |
assertEquals(testValue, testNumberProvider.anInt); |
| 850 |
} |
| 851 |
|
| 852 |
private static class InvalidBindableSubclass extends InvalidBindableClass {} |
| 853 |
|
| 854 |
public void testClassIsPrintedInErrorsWhenCauseIsSuperclass() { |
| 855 |
Object instance = new InvalidBindableSubclass(); |
| 856 |
|
| 857 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 858 |
|
| 859 |
try { |
| 860 |
Guice.createInjector(module); |
| 861 |
fail(); |
| 862 |
} catch (CreationException e) { |
| 863 |
assertContains( |
| 864 |
e.getMessage(), |
| 865 |
"Requested binding type \"java.lang.String\" is not assignable from field binding type " |
| 866 |
+ "\"java.lang.Integer\""); |
| 867 |
} |
| 868 |
} |
| 869 |
|
| 870 |
private static class FieldBindableSubclass2 extends FieldBindableClass { |
| 871 |
@Bind Number aNumber; |
| 872 |
|
| 873 |
FieldBindableSubclass2(Integer anInt, Number aNumber) { |
| 874 |
super(anInt); |
| 875 |
this.aNumber = aNumber; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
public void testFieldsAreBoundFromFullClassHierarchy() { |
| 880 |
final Integer testValue1 = 1024, testValue2 = 2048; |
| 881 |
FieldBindableSubclass2 instance = new FieldBindableSubclass2(testValue1, testValue2); |
| 882 |
|
| 883 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 884 |
Injector injector = Guice.createInjector(module); |
| 885 |
|
| 886 |
assertEquals(testValue1, injector.getInstance(Integer.class)); |
| 887 |
assertEquals(testValue2, injector.getInstance(Number.class)); |
| 888 |
} |
| 889 |
|
| 890 |
static final class LazyClass { |
| 891 |
@Bind(lazy = true) |
| 892 |
Integer foo = 1; |
| 893 |
} |
| 894 |
|
| 895 |
public void testFieldBound_lazy() { |
| 896 |
LazyClass asProvider = new LazyClass(); |
| 897 |
Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider)); |
| 898 |
assertEquals(1, injector.getInstance(Integer.class).intValue()); |
| 899 |
asProvider.foo++; |
| 900 |
assertEquals(2, injector.getInstance(Integer.class).intValue()); |
| 901 |
} |
| 902 |
|
| 903 |
public void testNonNullableFieldBound_lazy_rejectNull() { |
| 904 |
LazyClass asProvider = new LazyClass(); |
| 905 |
Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider)); |
| 906 |
assertEquals(1, injector.getInstance(Integer.class).intValue()); |
| 907 |
asProvider.foo = null; |
| 908 |
try { |
| 909 |
injector.getInstance(Integer.class); |
| 910 |
fail(); |
| 911 |
} catch (ProvisionException e) { |
| 912 |
assertContains( |
| 913 |
e.getMessage(), |
| 914 |
"Binding to null values is only allowed for fields that are annotated @Nullable."); |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
static final class LazyClassNullable { |
| 919 |
@Bind(lazy = true) |
| 920 |
@Nullable |
| 921 |
Integer foo = 1; |
| 922 |
} |
| 923 |
|
| 924 |
public void testNullableFieldBound_lazy_allowNull() { |
| 925 |
LazyClassNullable asProvider = new LazyClassNullable(); |
| 926 |
Injector injector = Guice.createInjector(BoundFieldModule.of(asProvider)); |
| 927 |
assertEquals(1, injector.getInstance(Integer.class).intValue()); |
| 928 |
asProvider.foo = null; |
| 929 |
assertNull(injector.getInstance(Integer.class)); |
| 930 |
} |
| 931 |
|
| 932 |
static final class LazyProviderClass { |
| 933 |
@Bind(lazy = true) |
| 934 |
Provider<Integer> foo = Providers.of(null); |
| 935 |
} |
| 936 |
|
| 937 |
public void testFieldBoundAsProvider_lazy() { |
| 938 |
LazyProviderClass asProvider = new LazyProviderClass(); |
| 939 |
Provider<Integer> provider = |
| 940 |
Guice.createInjector(BoundFieldModule.of(asProvider)).getProvider(Integer.class); |
| 941 |
assertNull(provider.get()); |
| 942 |
asProvider.foo = Providers.of(1); |
| 943 |
assertEquals(1, provider.get().intValue()); |
| 944 |
asProvider.foo = |
| 945 |
new Provider<Integer>() { |
| 946 |
@Override |
| 947 |
public Integer get() { |
| 948 |
throw new RuntimeException("boom"); |
| 949 |
} |
| 950 |
}; |
| 951 |
try { |
| 952 |
provider.get(); |
| 953 |
fail(); |
| 954 |
} catch (ProvisionException e) { |
| 955 |
assertContains(e.getMessage(), "boom"); |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
private static final class LazyNonTransparentProvider { |
| 960 |
@Bind(lazy = true) |
| 961 |
@Nullable |
| 962 |
private IntegerProvider anIntProvider = null; |
| 963 |
} |
| 964 |
|
| 965 |
public void testFieldBoundAsNonTransparentProvider_lazy() { |
| 966 |
LazyNonTransparentProvider instance = new LazyNonTransparentProvider(); |
| 967 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 968 |
Injector injector = Guice.createInjector(module); |
| 969 |
|
| 970 |
assertNull(injector.getInstance(IntegerProvider.class)); |
| 971 |
instance.anIntProvider = new IntegerProvider(3); |
| 972 |
assertEquals(3, injector.getInstance(IntegerProvider.class).get().intValue()); |
| 973 |
try { |
| 974 |
injector.getInstance(Integer.class); |
| 975 |
fail(); |
| 976 |
} catch (ConfigurationException expected) { |
| 977 |
|
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
public void testGetBoundFields_getValue() { |
| 982 |
Object instance = |
| 983 |
new Object() { |
| 984 |
@Bind Integer value = 1; |
| 985 |
}; |
| 986 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 987 |
Guice.createInjector(module); |
| 988 |
|
| 989 |
BoundFieldInfo info = Iterables.getOnlyElement(module.getBoundFields()); |
| 990 |
assertEquals(1, info.getValue()); |
| 991 |
} |
| 992 |
|
| 993 |
public void testGetBoundFields_getField() throws Exception { |
| 994 |
Object instance = |
| 995 |
new Object() { |
| 996 |
@Bind(lazy = true) |
| 997 |
String value = "default"; |
| 998 |
}; |
| 999 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 1000 |
Injector injector = Guice.createInjector(module); |
| 1001 |
|
| 1002 |
BoundFieldInfo info = Iterables.getOnlyElement(module.getBoundFields()); |
| 1003 |
String value = "value"; |
| 1004 |
info.getField().set(instance, value); |
| 1005 |
|
| 1006 |
assertEquals(value, injector.getInstance(info.getBoundKey())); |
| 1007 |
} |
| 1008 |
|
| 1009 |
public void testGetBoundFields_getKey() throws Exception { |
| 1010 |
Object instance = |
| 1011 |
new Object() { |
| 1012 |
@Bind @SomeQualifier String value = "default"; |
| 1013 |
}; |
| 1014 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 1015 |
Guice.createInjector(module); |
| 1016 |
BoundFieldInfo info = Iterables.getOnlyElement(module.getBoundFields()); |
| 1017 |
|
| 1018 |
assertEquals(Key.get(String.class, SomeQualifier.class), info.getBoundKey()); |
| 1019 |
} |
| 1020 |
|
| 1021 |
public void testGetBoundFields_getBindAnnotation() throws Exception { |
| 1022 |
Object instance = |
| 1023 |
new Object() { |
| 1024 |
@Bind(lazy = true) |
| 1025 |
@SomeQualifier |
| 1026 |
String value; |
| 1027 |
}; |
| 1028 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 1029 |
Guice.createInjector(module); |
| 1030 |
BoundFieldInfo info = Iterables.getOnlyElement(module.getBoundFields()); |
| 1031 |
|
| 1032 |
assertTrue(info.getBindAnnotation().lazy()); |
| 1033 |
} |
| 1034 |
|
| 1035 |
@Qualifier |
| 1036 |
@RestrictedBindingSource( |
| 1037 |
explanation = "", |
| 1038 |
permits = {TestPermit.class}) |
| 1039 |
@Retention(RetentionPolicy.RUNTIME) |
| 1040 |
@interface Foo {} |
| 1041 |
|
| 1042 |
public void testBoundFieldModuleWithPermits() { |
| 1043 |
class Bindings { |
| 1044 |
@Bind @Foo int foo = 17; |
| 1045 |
} |
| 1046 |
Bindings bindings = new Bindings(); |
| 1047 |
|
| 1048 |
Injector injector = |
| 1049 |
Guice.createInjector(new BoundFieldModule.@TestPermit WithPermits(bindings) {}); |
| 1050 |
|
| 1051 |
assertEquals((Integer) bindings.foo, injector.getInstance(Key.get(Integer.class, Foo.class))); |
| 1052 |
} |
| 1053 |
|
| 1054 |
public void testSourceSetOnBinding() throws Exception { |
| 1055 |
Object instance = |
| 1056 |
new Object() { |
| 1057 |
@Bind Integer value = 1; |
| 1058 |
}; |
| 1059 |
BoundFieldModule module = BoundFieldModule.of(instance); |
| 1060 |
Injector injector = Guice.createInjector(module); |
| 1061 |
assertEquals( |
| 1062 |
instance.getClass().getDeclaredField("value"), |
| 1063 |
((ElementSource) injector.getBinding(Integer.class).getSource()).getDeclaringSource()); |
| 1064 |
} |
| 1065 |
} |