use of com.google.inject.AbstractModule in project guice by google.
the class OverrideModuleTest method testOverrideOfOverride.
public void testOverrideOfOverride() {
Module original = new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("A1");
bind(key2).toInstance("A2");
bind(key3).toInstance("A3");
}
};
Module replacements1 = new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("B1");
bind(key2).toInstance("B2");
}
};
Module overrides = Modules.override(original).with(replacements1);
Module replacements2 = new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("C1");
bind(key3).toInstance("C3");
}
};
Injector injector = createInjector(Modules.override(overrides).with(replacements2));
assertEquals("C1", injector.getInstance(String.class));
assertEquals("B2", injector.getInstance(key2));
assertEquals("C3", injector.getInstance(key3));
}
use of com.google.inject.AbstractModule in project guice by google.
the class SpiBindingsTest method testToConstructorBinding.
public void testToConstructorBinding() {
checkInjector(new AbstractModule() {
@Override
protected void configure() {
bind(D.class);
}
}, new FailingElementVisitor() {
@Override
public <T> Void visit(Binding<T> binding) {
assertTrue(binding instanceof ConstructorBinding);
checkBindingSource(binding);
assertEquals(Key.get(D.class), binding.getKey());
binding.acceptTargetVisitor(new FailingTargetVisitor<T>() {
@Override
public Void visit(ConstructorBinding<? extends T> binding) {
Constructor<?> expected = D.class.getDeclaredConstructors()[0];
assertEquals(expected, binding.getConstructor().getMember());
assertEquals(ImmutableSet.<InjectionPoint>of(), binding.getInjectableMembers());
return null;
}
});
return null;
}
});
}
use of com.google.inject.AbstractModule in project guice by google.
the class SpiBindingsTest method testExtensionSpi.
public void testExtensionSpi() {
final AtomicBoolean visiting = new AtomicBoolean(false);
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toProvider(new ProviderWithExtensionVisitor<String>() {
@Override
public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) {
assertSame(this, binding.getUserSuppliedProvider());
// to process the binding as normal
if (visiting.get()) {
assertTrue("visitor: " + visitor, visitor instanceof FailingSpiTargetVisitor);
return (V) "visited";
} else {
return visitor.visit(binding);
}
}
@Override
public String get() {
return "FooBar";
}
});
}
});
visiting.set(true);
// Check for Provider<String> binding -- that is still a ProviderBinding.
Key<Provider<String>> providerOfStringKey = new Key<Provider<String>>() {
};
Binding<Provider<String>> providerBinding = injector.getBinding(providerOfStringKey);
assertEquals(providerOfStringKey, providerBinding.getKey());
checkBindingSource(providerBinding);
assertTrue("binding: " + providerBinding, providerBinding instanceof ProviderBinding);
providerBinding.acceptTargetVisitor(new FailingTargetVisitor<Provider<String>>() {
@Override
public Void visit(ProviderBinding<? extends Provider<String>> binding) {
assertEquals(Key.get(String.class), binding.getProvidedKey());
return null;
}
});
// Check for String binding -- that one is ProviderInstanceBinding, and gets hooked
Binding<String> binding = injector.getBinding(String.class);
assertEquals(Key.get(String.class), binding.getKey());
checkBindingSource(binding);
assertTrue(binding instanceof ProviderInstanceBinding);
assertEquals("visited", binding.acceptTargetVisitor(new FailingSpiTargetVisitor<String>()));
}
use of com.google.inject.AbstractModule in project guice by google.
the class ToolStageInjectorTest method testToolStageDoesntInjectProviders.
public void testToolStageDoesntInjectProviders() {
final Foo foo = new Foo();
Guice.createInjector(Stage.TOOL, new AbstractModule() {
@Override
protected void configure() {
requestStaticInjection(Foo.class);
bind(Object.class).toProvider(foo);
}
});
assertNull(Foo.s);
assertNull(Foo.sm);
assertNull(foo.f);
assertNull(foo.m);
}
use of com.google.inject.AbstractModule in project guice by google.
the class ToolStageInjectorTest method testToolStageWarnsOfMissingObjectGraph.
public void testToolStageWarnsOfMissingObjectGraph() {
final Bar bar = new Bar();
try {
Guice.createInjector(Stage.TOOL, new AbstractModule() {
@Override
protected void configure() {
requestStaticInjection(Bar.class);
requestInjection(bar);
}
});
fail("expected exception");
} catch (CreationException expected) {
Asserts.assertContains(expected.toString(), "No implementation for java.util.Collection was bound.", "No implementation for java.util.Map was bound.", "No implementation for java.util.List was bound.", "No implementation for java.util.Set was bound.");
}
}
Aggregations