Search in sources :

Example 76 with Injector

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");
}
Also used : Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 77 with Injector

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");
}
Also used : Exposed(com.google.inject.Exposed) Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule)

Example 78 with Injector

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();
}
Also used : Injector(com.google.inject.Injector)

Example 79 with Injector

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());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 80 with Injector

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());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Aggregations

Injector (com.google.inject.Injector)2159 AbstractModule (com.google.inject.AbstractModule)623 Test (org.junit.Test)513 Module (com.google.inject.Module)364 Test (org.testng.annotations.Test)131 Before (org.junit.Before)116 Binder (com.google.inject.Binder)114 Properties (java.util.Properties)110 Key (com.google.inject.Key)84 Map (java.util.Map)78 HttpServletRequest (javax.servlet.http.HttpServletRequest)78 Provider (com.google.inject.Provider)74 IOException (java.io.IOException)71 TypeLiteral (com.google.inject.TypeLiteral)70 Set (java.util.Set)64 BeforeClass (org.junit.BeforeClass)61 File (java.io.File)60 CConfiguration (co.cask.cdap.common.conf.CConfiguration)55 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)55 PrivateModule (com.google.inject.PrivateModule)55