@Test public void testMultiConditionalBind_cat() { props.setProperty("animal.type", "cat"); Injector injector = Guice.createInjector(new Module() { @Override public void configure(Binder binder) { ConditionalMultibind.create(props, binder, Animal.class) .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("cat"), Cat.class) .addConditionBinding(ANIMAL_TYPE, Predicates.equalTo("dog"), Dog.class); } }); Set<Animal> animalSet = injector.getInstance(Key.get(new TypeLiteral<Set<Animal>>() { })); Assert.assertEquals(1, animalSet.size()); Assert.assertEquals(animalSet, ImmutableSet.<Animal>of(new Cat())); }
@Test public void testWithoutConfigAllIdsMatch() { Set<String> regionIds = ImmutableSet.of("us-east-1", "eu-west-1"); RegionIdFilter filter = Guice.createInjector(new AbstractModule(){ @Override protected void configure() { bindConstant().annotatedWith(Provider.class).to("aws-ec2"); } }).getInstance(AnyOrConfiguredRegionId.class); assertEquals(Sets.filter(regionIds, filter), ImmutableSet.of("us-east-1", "eu-west-1")); }
public void testEmptyWhenNotStringsBound() { FilterStringsBoundToInjectorByName fn = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bindConstant().annotatedWith(Names.named("foo")).to(1l); } }).getInstance(FilterStringsBoundToInjectorByName.class); assertEquals(fn.apply(Predicates.<String> alwaysTrue()), ImmutableMap.<String, String> of()); }
@Test public void testNamedWorksGlobalDisabled() { final Config config = Config.getFixedConfig(ImmutableMap.of("ness.jms.enabled", "false")); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ConfigModule(config), new JmsModule(config, "test")); final JmsConfig jmsConfig = injector.getInstance(Key.get(JmsConfig.class, Names.named("test"))); Assert.assertNotNull(jmsConfig); Assert.assertFalse(jmsConfig.isEnabled()); Assert.assertNull(injector.getExistingBinding(Key.get(ConnectionFactory.class, Names.named("test")))); }
@BeforeClass public static void setup() throws InterruptedException, IOException { new MockNodeProbe(); injector = Guice.createInjector(new BRTestModule()); filesystem = (FakeBackupFileSystem) injector.getInstance( Key.get(IBackupFileSystem.class, Names.named("backup"))); }
@Test public void testProviderOverridesApiMetadataProperty() { ProviderMetadata md = AnonymousProviderMetadata.forClientMappedToAsyncClientOnEndpoint( IntegrationTestClient.class, IntegrationTestAsyncClient.class, "http://localhost"); Properties defaultProps = md.getDefaultProperties(); defaultProps.setProperty(Constants.PROPERTY_SESSION_INTERVAL, Integer.MAX_VALUE + ""); md = md.toBuilder().defaultProperties(defaultProps).build(); Supplier<Credentials> creds = Suppliers.<Credentials> ofInstance(LoginCredentials.builder().user("user").build()); int session = Guice.createInjector(new BindProviderMetadataContextAndCredentials(md, creds)).getInstance( Key.get(int.class, Names.named(Constants.PROPERTY_SESSION_INTERVAL))); assertEquals(session, Integer.MAX_VALUE); } }
public final void testWrongValueClasses() throws Exception { Injector injector = Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN); bind(SomeObject.class).in(RequestScoped.class); } }); OffRequestCallable offRequestCallable = injector.getInstance(OffRequestCallable.class); try { ServletScopes.scopeRequest( offRequestCallable, ImmutableMap.<Key<?>, Object>of(Key.get(SomeObject.class), "Boo!")); fail(); } catch (IllegalArgumentException iae) { assertEquals( "Value[Boo!] of type[java.lang.String] is not compatible with key[" + Key.get(SomeObject.class) + "]", iae.getMessage()); } }
public static void main(String... args) { final long start = System.currentTimeMillis(); try { final Injector injector = Guice.createInjector(Stage.PRODUCTION, new SingularityRunnerBaseModule(SingularityExecutorCleanupConfiguration.class, ImmutableSet.of(SingularityS3Configuration.class, SingularityExecutorConfiguration.class)), new SingularityExecutorModule(), new SingularityExecutorCleanupModule(), new SingularityClientModule(), new SingularityMesosClientModule()); final SingularityExecutorCleanupRunner runner = injector.getInstance(SingularityExecutorCleanupRunner.class); LOG.info("Starting cleanup"); final SingularityExecutorCleanupStatistics statistics = runner.cleanup(); LOG.info("Finished with {} after {}", statistics, JavaUtils.duration(start)); System.exit(0); } catch (Throwable t) { LOG.error("Finished after {} with error", JavaUtils.duration(start), t); System.exit(1); } }
@BeforeMethod public void startUp() { Injector injector = Guice.createInjector(new JsonModule(), new HandleJsonModule()); objectMapper = injector.getInstance(ObjectMapper.class); }
@Test public void testLinkedContextBindsContextSupplier() { Injector injector = Guice.createInjector(linkContext(contextFor(IntegrationTestClient.class))); assertNotNull(injector.getExistingBinding(Key.get(CONTEXT_SUPPLIER, Names.named("IntegrationTestClient")))); }
@Test(groups = "fast") public void testAOPForClass() throws Exception { // Make sure it works as expected without any AOP magic final IAopTester simpleTester = new AopTester(); try { simpleTester.createRefund(); } catch (Exception e) { Assert.fail(e.getLocalizedMessage()); } // Now, verify the interception works configureShiro(); final Injector injector = Guice.createInjector(Stage.PRODUCTION, new ShiroModuleNoDB(configSource), new KillBillShiroAopModule(), new TestSecurityModuleNoDB(configSource), new CacheModule(configSource), new AbstractModule() { @Override protected void configure() { bind(IDBI.class).toInstance(Mockito.mock(IDBI.class)); bind(IDBI.class).annotatedWith(Names.named(MAIN_RO_DATA_SOURCE_ID)).toInstance(Mockito.mock(IDBI.class)); bind(TenantInternalApi.class).toInstance(Mockito.mock(TenantInternalApi.class)); bind(NonEntityDao.class).toInstance(Mockito.mock(NonEntityDao.class)); } }); final AopTester aopedTester = injector.getInstance(AopTester.class); verifyAopedTester(aopedTester); }
public void testTransferNonHttpRequest_closeable() throws Exception { final Injector injector = Guice.createInjector( new AbstractModule() { @Override ImmutableMap<Key<?>, Object> seedMap = ImmutableMap.of(); Data data = ServletScopes.scopeRequest(callable, seedMap).call(); RequestScoper.CloseableScope scope = data.scoper.open(); try { assertSame(data.object, injector.getInstance(Object.class)); } finally { scope.close();
public void testIsRequestScopedPositive() { final Key<String> a = Key.get(String.class, named("A")); final Key<String> b = Key.get(String.class, named("B")); final Key<String> c = Key.get(String.class, named("C")); final Key<String> d = Key.get(String.class, named("D")); final Key<Object> e = Key.get(Object.class, named("E")); ImmutableMap<Key<?>, Binding<?>> map = indexBindings(moduleBindings); assertFalse(ServletScopes.isRequestScoped(map.get(a))); assertFalse(ServletScopes.isRequestScoped(map.get(b))); assertTrue(ServletScopes.isRequestScoped(map.get(c))); assertTrue(ServletScopes.isRequestScoped(map.get(d))); assertTrue(ServletScopes.isRequestScoped(map.get(g))); Injector injector = Guice.createInjector(requestScopedBindings, new ServletModule()); assertTrue(ServletScopes.isRequestScoped(injector.getBinding(a))); assertTrue(ServletScopes.isRequestScoped(injector.getBinding(b))); assertTrue(ServletScopes.isRequestScoped(injector.getBinding(c))); assertTrue(ServletScopes.isRequestScoped(injector.getBinding(d))); assertTrue(ServletScopes.isRequestScoped(injector.getBinding(e)));
Injector injector = Guice.createInjector( new Module() injector.injectMembers(shop); Assert.assertEquals(shop.animals, ImmutableSet.of(new Bird(), new Cat(), new Dog(), new Fish()));
Injector injector = Guice.createInjector(Stage.PRODUCTION, allModules); Object obj = injector.getInstance(key); if (Closeable.class.isAssignableFrom(obj.getClass())) { LOG.info("Managing lifecycle of {}", key.getTypeLiteral()); closer.register(Closeable.class.cast(obj));
@BeforeClass(groups = "fast") protected void beforeClass() throws Exception { if (hasFailed()) { return; } final Injector injector = Guice.createInjector(new TestTenantModuleNoDB(configSource, clock)); injector.injectMembers(this); } }
public final void testNullReplacement() throws Exception { Injector injector = Guice.createInjector( new ServletModule() { @Override protected void configureServlets() { bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN); bind(SomeObject.class).in(RequestScoped.class); } }); Callable<SomeObject> callable = injector.getInstance(Caller.class); try { assertNotNull(callable.call()); fail(); } catch (ProvisionException pe) { assertTrue(pe.getCause() instanceof OutOfScopeException); } // Validate that an actual null entry in the map results in a null injected object. Map<Key<?>, Object> map = Maps.newHashMap(); map.put(Key.get(SomeObject.class), null); callable = ServletScopes.scopeRequest(injector.getInstance(Caller.class), map); assertNull(callable.call()); }
public void testNoMatchingConstrucotsBecauseTooLittleParams() { try { Guice.createInjector( new AbstractModule() { @Override protected void configure() { install(new FactoryModuleBuilder().build(NullFactory.class)); } }); fail("should have failed"); } catch (CreationException expected) { Asserts.assertContains( expected.getMessage(), "1) " + Foo.class.getName() + " has @AssistedInject constructors, but none of them match the parameters in method " + NullFactory.class.getName()); } }
public static Injector createInjector(final List<Module> bindingsModules) { if (injector == null) { injector = Guice.createInjector(Stage.PRODUCTION, bindingsModules); } return injector; }
@BeforeClass(groups = "slow") public void beforeClass() throws Exception { if (hasFailed()) { return; } final Injector g = Guice.createInjector(Stage.PRODUCTION, new TestUtilModuleWithEmbeddedDB(configSource, clock)); g.injectMembers(this); Assert.assertTrue(locker.isFree("a", "b")); }