Search in sources :

Example 71 with Injector

use of com.google.inject.Injector in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testWithSource.

public void testWithSource() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            binder().withSource("source").install(new AbstractModule() {

                @Override
                protected void configure() {
                }

                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    };
    Injector injector = Guice.createInjector(module, NamedMunger.module());
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Named(com.google.inject.name.Named) Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 72 with Injector

use of com.google.inject.Injector in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModule_skipSourcesForPrivateModule.

public void testPrivateModule_skipSourcesForPrivateModule() {
    Injector injector = Guice.createInjector(NamedMunger.module(), new AbstractModule() {

        @Override
        protected void configure() {
            binder().skipSources(getClass()).install(new PrivateModule() {

                @Override
                protected void configure() {
                }

                @Exposed
                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule)

Example 73 with Injector

use of com.google.inject.Injector in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testScanning.

public void testScanning() throws Exception {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
        }

        @TestProvides
        @Named("foo")
        String foo() {
            return "foo";
        }

        @TestProvides
        @Named("foo2")
        String foo2() {
            return "foo2";
        }
    };
    Injector injector = Guice.createInjector(module, NamedMunger.module());
    // assert no bindings named "foo" or "foo2" exist -- they were munged.
    assertMungedBinding(injector, String.class, "foo", "foo");
    assertMungedBinding(injector, String.class, "foo2", "foo2");
    Binding<String> fooBinding = injector.getBinding(Key.get(String.class, named("foo-munged")));
    Binding<String> foo2Binding = injector.getBinding(Key.get(String.class, named("foo2-munged")));
    // Validate the provider has a sane toString
    assertEquals(methodName(TestProvides.class, "foo", module), fooBinding.getProvider().toString());
    assertEquals(methodName(TestProvides.class, "foo2", module), foo2Binding.getProvider().toString());
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 74 with Injector

use of com.google.inject.Injector in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModule_skipSourcesWithinPrivateModule.

public void testPrivateModule_skipSourcesWithinPrivateModule() {
    Injector injector = Guice.createInjector(NamedMunger.module(), new PrivateModule() {

        @Override
        protected void configure() {
            binder().skipSources(getClass()).install(new AbstractModule() {

                @Override
                protected void configure() {
                }

                @Exposed
                @TestProvides
                @Named("foo")
                String foo() {
                    return "foo";
                }
            });
        }
    });
    assertMungedBinding(injector, String.class, "foo", "foo");
}
Also used : Injector(com.google.inject.Injector) PrivateModule(com.google.inject.PrivateModule) AbstractModule(com.google.inject.AbstractModule)

Example 75 with Injector

use of com.google.inject.Injector in project guice by google.

the class ModuleAnnotatedMethodScannerTest method testPrivateModuleInheritScanner_usingPrivateModule.

public void testPrivateModuleInheritScanner_usingPrivateModule() {
    Injector injector = Guice.createInjector(NamedMunger.module(), new PrivateModule() {

        @Override
        protected void configure() {
        }

        @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)

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