Search in sources :

Example 16 with AbstractModule

use of com.google.inject.AbstractModule in project scdl by passy.

the class AboutActivityTest method setup.

@Before
public void setup() {
    final PackageInfo fakeInfo = new PackageInfo();
    fakeInfo.versionName = "1.0.1";
    TestHelper.overridenInjector(this, new AbstractModule() {

        @Override
        protected void configure() {
            bind(PackageInfo.class).toInstance(fakeInfo);
        }
    });
}
Also used : PackageInfo(android.content.pm.PackageInfo) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 17 with AbstractModule

use of com.google.inject.AbstractModule in project scdl by passy.

the class TestHelper method overridenInjector.

public static void overridenInjector(Object instance, AbstractModule module) {
    final Application app = Robolectric.application;
    // Allow overriding of integrated classes like Activity
    Module moduleOverride = Modules.override(RoboGuice.newDefaultRoboModule(app)).with(module);
    // Also allow overriding custom bindings like URLWrapper
    moduleOverride = Modules.override(new SCDLModule()).with(moduleOverride);
    RoboGuice.setBaseApplicationInjector(app, RoboGuice.DEFAULT_STAGE, moduleOverride);
    final Injector injector = TestHelper.getInjector();
    injector.injectMembers(instance);
}
Also used : SCDLModule(net.rdrei.android.scdl2.guice.SCDLModule) Injector(com.google.inject.Injector) Module(com.google.inject.Module) SCDLModule(net.rdrei.android.scdl2.guice.SCDLModule) AbstractModule(com.google.inject.AbstractModule) Application(android.app.Application)

Example 18 with AbstractModule

use of com.google.inject.AbstractModule in project scdl by passy.

the class ResolveServiceTest method setUp.

@Before
public void setUp() {
    final AbstractModule module = new AbstractModule() {

        @Override
        protected void configure() {
            mUrlConnectionFactory = new FakeURLConnectionFactoryImpl("/fixtures/resolve.json");
            mUrlConnectionFactory.setResponseCode(302);
            bind(URLConnectionFactory.class).toInstance(mUrlConnectionFactory);
            bind(PinningTrustManager.class).toInstance(new TrustManagerStub());
        }
    };
    TestHelper.overridenInjector(this, module);
}
Also used : URLConnectionFactory(net.rdrei.android.scdl2.api.URLConnectionFactory) PinningTrustManager(org.thoughtcrime.ssl.pinning.PinningTrustManager) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 19 with AbstractModule

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

the class MapBinderTest method testMapBinderMultimapWithAnotation.

public void testMapBinderMultimapWithAnotation() {
    AbstractModule ab1 = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("a").toInstance("A");
            multibinder.addBinding("b").toInstance("B1");
        }
    };
    AbstractModule b2c = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder<String, String> multibinder = MapBinder.newMapBinder(binder(), String.class, String.class, Abc.class);
            multibinder.addBinding("b").toInstance("B2");
            multibinder.addBinding("c").toInstance("C");
            multibinder.permitDuplicates();
        }
    };
    Injector injector = Guice.createInjector(ab1, b2c);
    assertEquals(mapOf("a", setOf("A"), "b", setOf("B1", "B2"), "c", setOf("C")), injector.getInstance(Key.get(mapOfSetOfString, Abc.class)));
    try {
        injector.getInstance(Key.get(mapOfSetOfString));
        fail();
    } catch (ConfigurationException expected) {
    }
    assertMapVisitor(Key.get(mapOfString, Abc.class), stringType, stringType, setOf(ab1, b2c), BOTH, true, 0, instance("a", "A"), instance("b", "B1"), instance("b", "B2"), instance("c", "C"));
}
Also used : ConfigurationException(com.google.inject.ConfigurationException) Injector(com.google.inject.Injector) AbstractModule(com.google.inject.AbstractModule)

Example 20 with AbstractModule

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

the class MapBinderTest method testMapBinderMapIsLazy.

public void testMapBinderMapIsLazy() {
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            MapBinder.newMapBinder(binder(), String.class, Integer.class).addBinding("num").toProvider(new Provider<Integer>() {

                int nextValue = 1;

                @Override
                public Integer get() {
                    return nextValue++;
                }
            });
        }
    };
    Injector injector = Guice.createInjector(module);
    assertEquals(mapOf("num", 1), injector.getInstance(Key.get(mapOfInteger)));
    assertEquals(mapOf("num", 2), injector.getInstance(Key.get(mapOfInteger)));
    assertEquals(mapOf("num", 3), injector.getInstance(Key.get(mapOfInteger)));
    assertMapVisitor(Key.get(mapOfInteger), stringType, intType, setOf(module), BOTH, false, 0, providerInstance("num", 1));
}
Also used : Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Aggregations

AbstractModule (com.google.inject.AbstractModule)840 Injector (com.google.inject.Injector)522 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 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)42 TypeLiteral (com.google.inject.TypeLiteral)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