Search in sources :

Example 61 with AbstractModule

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

the class FactoryProvider2Test method testAssistedJavaxProviderIsDisallowed.

public void testAssistedJavaxProviderIsDisallowed() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                bind(JavaxProviderBasedColoredCarFactory.class).toProvider(FactoryProvider.newFactory(JavaxProviderBasedColoredCarFactory.class, Subaru.class));
            }
        });
        fail();
    } catch (CreationException expected) {
        assertEquals(expected.getMessage(), 4, expected.getErrorMessages().size());
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">] on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [2] with key" + " [com.google.inject.Provider<java.lang.String>] on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createCar()]");
        assertContains(expected.getMessage(), ") A Provider may not be a type in a factory method of an AssistedInject." + "\n  Offending instance is parameter [1] with key" + " [com.google.inject.Provider<" + Color.class.getName() + ">" + " annotated with @com.google.inject.assistedinject.Assisted(value=color)]" + " on method [" + JavaxProviderBasedColoredCarFactory.class.getName() + ".createMustang()]");
        assertContains(expected.getMessage(), ") No implementation for com.google.inject.assistedinject." + "FactoryProvider2Test$JavaxProviderBasedColoredCarFactory was bound.");
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 62 with AbstractModule

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

the class FactoryProvider2Test method testInjectingNullParameter.

public void testInjectingNullParameter() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Subaru.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    Subaru subaru = (Subaru) carFactory.create(null);
    assertNull(subaru.colorProvider.get());
    assertNull(subaru.colorProvider.get());
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 63 with AbstractModule

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

the class FactoryProviderTest method testMethodsDeclaredInObject.

@SuppressWarnings("SelfEquals")
public void testMethodsDeclaredInObject() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    carFactory.equals(carFactory);
    carFactory.hashCode();
    carFactory.toString();
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 64 with AbstractModule

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

the class FactoryProviderTest method testTypeTokenInjection.

public void testTypeTokenInjection() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(new TypeLiteral<Set<String>>() {
            }).toInstance(Collections.singleton("Flux Capacitor"));
            bind(new TypeLiteral<Set<Integer>>() {
            }).toInstance(Collections.singleton(88));
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, DeLorean.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    DeLorean deLorean = (DeLorean) carFactory.create(Color.GRAY);
    assertEquals(Color.GRAY, deLorean.color);
    assertEquals("Flux Capacitor", deLorean.features.iterator().next());
    assertEquals(Integer.valueOf(88), deLorean.featureActivationSpeeds.iterator().next());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 65 with AbstractModule

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

the class FactoryProviderTest method testAssistedFactory.

public void testAssistedFactory() {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            bind(Double.class).toInstance(5.0d);
            bind(ColoredCarFactory.class).toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class));
        }
    });
    ColoredCarFactory carFactory = injector.getInstance(ColoredCarFactory.class);
    Mustang blueMustang = (Mustang) carFactory.create(Color.BLUE);
    assertEquals(Color.BLUE, blueMustang.color);
    assertEquals(5.0d, blueMustang.engineSize, 0.0);
    Mustang redMustang = (Mustang) carFactory.create(Color.RED);
    assertEquals(Color.RED, redMustang.color);
    assertEquals(5.0d, redMustang.engineSize, 0.0);
}
Also used : Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)845 Injector (com.google.inject.Injector)525 Module (com.google.inject.Module)232 CreationException (com.google.inject.CreationException)162 Provider (com.google.inject.Provider)64 Key (com.google.inject.Key)55 ConfigModule (co.cask.cdap.common.guice.ConfigModule)54 AuthorizationEnforcementModule (co.cask.cdap.security.authorization.AuthorizationEnforcementModule)49 DataSetsModules (co.cask.cdap.data.runtime.DataSetsModules)46 PrivateModule (com.google.inject.PrivateModule)46 AuthenticationContextModules (co.cask.cdap.security.auth.context.AuthenticationContextModules)45 AuthorizationTestModule (co.cask.cdap.security.authorization.AuthorizationTestModule)43 TypeLiteral (com.google.inject.TypeLiteral)43 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 BeforeClass (org.junit.BeforeClass)38 Map (java.util.Map)37 CConfiguration (co.cask.cdap.common.conf.CConfiguration)36 UnsupportedUGIProvider (co.cask.cdap.security.impersonation.UnsupportedUGIProvider)36 NonCustomLocationUnitTestModule (co.cask.cdap.common.guice.NonCustomLocationUnitTestModule)35 DefaultOwnerAdmin (co.cask.cdap.security.impersonation.DefaultOwnerAdmin)35