use of com.google.inject.Injector in project guice by google.
the class ModuleAnnotatedMethodScannerTest method testChildInjectorInheritsScanner.
public void testChildInjectorInheritsScanner() {
Injector parent = Guice.createInjector(NamedMunger.module());
Injector child = parent.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
}
@TestProvides
@Named("foo")
String foo() {
return "foo";
}
});
assertMungedBinding(child, String.class, "foo", "foo");
}
use of com.google.inject.Injector in project guice by google.
the class ModuleAnnotatedMethodScannerTest method testPrivateModuleScannersDontImpactSiblings_usingPrivateModule.
public void testPrivateModuleScannersDontImpactSiblings_usingPrivateModule() {
Injector injector = Guice.createInjector(new PrivateModule() {
@Override
protected void configure() {
install(NamedMunger.module());
}
@Exposed
@TestProvides
@Named("foo")
String foo() {
return "foo";
}
}, new PrivateModule() {
@Override
protected void configure() {
}
// ignored! (because the scanner doesn't run over this module)
@Exposed
@TestProvides
@Named("foo")
String foo() {
return "foo";
}
});
assertMungedBinding(injector, String.class, "foo", "foo");
}
use of com.google.inject.Injector in project guice by google.
the class BytecodeGenTest method testFastClassUsesBridgeClassloader.
// This tests for a situation where a osgi bundle contains a version of guice. When guice
// generates a fast class it will use a bridge classloader
public void testFastClassUsesBridgeClassloader() throws Throwable {
Injector injector = Guice.createInjector();
// These classes are all in the same classloader as guice itself, so other than the private one
// they can all be fast class invoked
injector.getInstance(PublicInject.class).assertIsFastClassInvoked();
injector.getInstance(ProtectedInject.class).assertIsFastClassInvoked();
injector.getInstance(PackagePrivateInject.class).assertIsFastClassInvoked();
injector.getInstance(PrivateInject.class).assertIsReflectionInvoked();
// This classloader will load the types in an loader with a different version of guice/cglib
// this prevents the use of fastclass for all but the public types (where the bridge
// classloader can be used).
MultipleVersionsOfGuiceClassLoader fakeLoader = new MultipleVersionsOfGuiceClassLoader();
injector.getInstance(fakeLoader.loadLogCreatorType(PublicInject.class)).assertIsFastClassInvoked();
injector.getInstance(fakeLoader.loadLogCreatorType(ProtectedInject.class)).assertIsReflectionInvoked();
injector.getInstance(fakeLoader.loadLogCreatorType(PackagePrivateInject.class)).assertIsReflectionInvoked();
injector.getInstance(fakeLoader.loadLogCreatorType(PrivateInject.class)).assertIsReflectionInvoked();
}
use of com.google.inject.Injector in project guice by google.
the class DefaultMethodInterceptionTest method testInterceptedDefaultMethod.
public void testInterceptedDefaultMethod() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(InterceptMe.class), interceptor);
bind(Foo.class).to(NonOverridingFoo.class);
}
});
Foo foo = injector.getInstance(Foo.class);
assertEquals("Foo", foo.defaultMethod());
assertEquals(1, callCount.get());
assertEquals(1, interceptedCallCount.get());
}
use of com.google.inject.Injector in project guice by google.
the class DefaultMethodInterceptionTest method testInterceptedDefaultMethod_whenParentClassDefinesInterceptedMethod.
public void testInterceptedDefaultMethod_whenParentClassDefinesInterceptedMethod() {
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.annotatedWith(InterceptMe.class), interceptor);
bind(Foo.class).to(InheritingFoo2.class);
}
});
// the concrete implementation that wins is not annotated
Foo foo = injector.getInstance(Foo.class);
assertEquals("BaseClass2", foo.defaultMethod());
assertEquals(1, callCount.get());
assertEquals(1, interceptedCallCount.get());
}
Aggregations