Search in sources :

Example 6 with ServiceContext

use of keywhiz.test.ServiceContext in project keywhiz by square.

the class ServiceDataSourceModuleTest method dataSourcesAreSingletons.

@Test
public void dataSourcesAreSingletons() {
    class Holder {

        @Inject
        ManagedDataSource readWrite;

        @Inject
        @Readonly
        ManagedDataSource readonly;
    }
    Holder holder1 = new Holder();
    Holder holder2 = new Holder();
    ServiceContext context = ServiceContext.create();
    Injector injector = Guice.createInjector(new ContextModule(context.getConfig(), context.getEnvironment()), new ServiceDataSourceModule());
    injector.injectMembers(holder1);
    injector.injectMembers(holder2);
    assertSame(holder1.readWrite, holder2.readWrite);
    assertSame(holder1.readonly, holder2.readonly);
}
Also used : ServiceContext(keywhiz.test.ServiceContext) Injector(com.google.inject.Injector) ContextModule(keywhiz.inject.ContextModule) ServiceDataSourceModule(keywhiz.ServiceDataSourceModule) ManagedDataSource(io.dropwizard.db.ManagedDataSource) Test(org.junit.Test)

Example 7 with ServiceContext

use of keywhiz.test.ServiceContext in project keywhiz by square.

the class KeywhizServiceTest method doesNotRecreateInjector.

@Test
public void doesNotRecreateInjector() {
    ServiceContext context = ServiceContext.create();
    KeywhizService service = context.getService();
    service.ensureInjectorCreated(context.getConfig(), context.getEnvironment());
    Injector first = service.getInjector();
    service.ensureInjectorCreated(context.getConfig(), context.getEnvironment());
    Injector second = service.getInjector();
    assertSame(first, second);
}
Also used : ServiceContext(keywhiz.test.ServiceContext) Injector(com.google.inject.Injector) Test(org.junit.Test)

Example 8 with ServiceContext

use of keywhiz.test.ServiceContext in project keywhiz by square.

the class ContextModuleTest method injectsContext.

@Test
public void injectsContext() {
    class Holder {

        @Inject
        KeywhizConfig keywhizConfig;

        @Inject
        Configuration config;

        @Inject
        Environment environment;
    }
    Holder holder = new Holder();
    ServiceContext context = ServiceContext.create();
    Guice.createInjector(new ContextModule(context.getConfig(), context.getEnvironment())).injectMembers(holder);
    assertSame(context.getConfig(), holder.keywhizConfig);
    assertSame(context.getConfig(), holder.config);
    assertSame(context.getEnvironment(), holder.environment);
}
Also used : Configuration(io.dropwizard.Configuration) ServiceContext(keywhiz.test.ServiceContext) KeywhizConfig(keywhiz.KeywhizConfig) Environment(io.dropwizard.setup.Environment) Test(org.junit.Test)

Aggregations

ServiceContext (keywhiz.test.ServiceContext)8 Test (org.junit.Test)7 Injector (com.google.inject.Injector)2 ManagedDataSource (io.dropwizard.db.ManagedDataSource)2 ServiceDataSourceModule (keywhiz.ServiceDataSourceModule)2 ContextModule (keywhiz.inject.ContextModule)2 Configuration (io.dropwizard.Configuration)1 Environment (io.dropwizard.setup.Environment)1 KeywhizConfig (keywhiz.KeywhizConfig)1 Namespace (net.sourceforge.argparse4j.inf.Namespace)1