@Override protected void configure() { LinkedBindingBuilder<Iterable<InetSocketAddress>> clusterBinder = bind(ServiceDiscoveryBindings.ZOO_KEEPER_CLUSTER_KEY); if (zooKeeperConfig.isInProcess()) { requireBinding(ShutdownRegistry.class); File tempDir = Files.createTempDir(); bind(ZooKeeperTestServer.class).toInstance(new ZooKeeperTestServer(tempDir, tempDir)); SchedulerServicesModule.addAppStartupServiceBinding(binder()).to(TestServerService.class); clusterBinder.toProvider(LocalZooKeeperClusterProvider.class); } else { clusterBinder.toInstance(zooKeeperConfig.getServers()); } install(discoveryModule()); }
@Override protected void configure() { install(new ServletModule() { @Override protected void configureServlets() { serve("/private/healthcheck/live").with(Key.get(AbstractDaemonCheckReportServlet.class, Live.class)); serve("/private/healthcheck").with(Key.get(AbstractDaemonCheckReportServlet.class, Background.class)); } }); bind(RuistDependencyManager.class).annotatedWith(Live.class).to(RuistDependencyManager.class).asEagerSingleton(); bind(RuistDependencyManager.class).annotatedWith(Background.class).to(RuistDependencyManager.class).asEagerSingleton(); final LinkedBindingBuilder<Dependency> builder = Multibinder.newSetBinder(binder(), Dependency.class).addBinding(); for (final Class<? extends Dependency> dep : deps) { builder.to(dep); } }
@SuppressWarnings({"rawtypes", "unchecked" }) private Block fileeventoptions(final Object handler) { return unit -> { Binder binder = unit.get(Binder.class); FileEventOptions options; if (handler instanceof FileEventHandler) { options = unit.constructor(FileEventOptions.class) .args(Path.class, FileEventHandler.class) .build(unit.get(Path.class), handler); } else { options = unit.constructor(FileEventOptions.class) .args(Path.class, Class.class) .build(unit.get(Path.class), handler); } unit.registerMock(FileEventOptions.class, options); LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(options); Multibinder mbinder = unit.mock(Multibinder.class); unit.mockStatic(Multibinder.class); expect(Multibinder.newSetBinder(binder, FileEventOptions.class)).andReturn(mbinder); expect(mbinder.addBinding()).andReturn(lbb); }; }
@SuppressWarnings("unchecked") @Test public void withWPPInstance() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, WebPageProvider.class) .expect(confWithSiteMapUrl) .expect(unit -> { LinkedBindingBuilder<WebPageProvider> lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(unit.get(WebPageProvider.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(WebPageProvider.class, Names.named("/sitemap.xml")))) .andReturn(lbb); }) .expect(handler) .expect(route) .run(unit -> { new Sitemap() .with(unit.get(WebPageProvider.class)) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void withWPPType() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(confWithSiteMapUrl) .expect(unit -> { LinkedBindingBuilder<WebPageProvider> lbb = unit.mock(LinkedBindingBuilder.class); expect(lbb.to(WebPageProvider.class)).andReturn(null); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(WebPageProvider.class, Names.named("/sitemap.xml")))) .andReturn(lbb); }) .expect(handler) .expect(route) .run(unit -> { new Sitemap() .with(WebPageProvider.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings({"unchecked", "rawtypes"}) private Block embeddedAccess(final String dbkey) { return unit -> { EmbeddedDBAccess db = unit.constructor(EmbeddedDBAccess.class) .build(); unit.registerMock(IDBAccess.class, db); db.initialize(unit.get(Properties.class)); LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(db); lbb.toInstance(db); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(IDBAccess.class))).andReturn(lbb); expect(binder.bind(Key.get(db.getClass()))).andReturn(lbb); ServiceKey keys = unit.get(ServiceKey.class); keys.generate(eq(IDBAccess.class), eq(dbkey), unit.capture(Consumer.class)); keys.generate(eq(db.getClass()), eq(dbkey), unit.capture(Consumer.class)); }; }
@SuppressWarnings("unchecked") private Block dbFactory(final Path dbdir, final String dbkey) { return unit -> { GraphDatabaseService dbservice = unit.registerMock(GraphDatabaseService.class); // unit.mockStatic(RuntimeRegistry.class); // expect(RuntimeRegistry.getStartedRuntime(dbservice)).andReturn(null); LinkedBindingBuilder<GraphDatabaseService> lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(dbservice); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(GraphDatabaseService.class))).andReturn(lbb); ServiceKey keys = unit.get(ServiceKey.class); keys.generate(eq(GraphDatabaseService.class), eq(dbkey), unit.capture(Consumer.class)); GraphDatabaseBuilder dbbuilder = unit.registerMock(GraphDatabaseBuilder.class); expect(dbbuilder.newGraphDatabase()).andReturn(dbservice); GraphDatabaseFactory factory = unit.constructor(GraphDatabaseFactory.class) .build(); expect(factory.setUserLogProvider(isA(Slf4jLogProvider.class))).andReturn(factory); expect(factory.newEmbeddedDatabaseBuilder(dbdir.toFile())).andReturn(dbbuilder); }; }
@SuppressWarnings({"unchecked" }) @Test public void defaultUse() throws Exception { Map<JobDetail, Trigger> jobs = Collections.emptyMap(); new MockUnit(Env.class, Config.class, Binder.class, LinkedBindingBuilder.class) .expect(unit -> { unit.mockStatic(JobExpander.class); expect(JobExpander.jobs(unit.get(Config.class), Arrays.asList(Job.class))) .andReturn(jobs); }) .expect(scheduler) .expect(jobUnit) .expect(unit -> { LinkedBindingBuilder<Map<JobDetail, Trigger>> namedJobsBB = unit .get(LinkedBindingBuilder.class); namedJobsBB.toInstance(jobs); }) .expect(onManaged) .run(unit -> { new Quartz(Job.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings({"unchecked" }) @Test public void withJob() throws Exception { Map<JobDetail, Trigger> jobs = Collections.emptyMap(); new MockUnit(Env.class, Config.class, Binder.class, LinkedBindingBuilder.class) .expect(unit -> { unit.mockStatic(JobExpander.class); expect(JobExpander.jobs(unit.get(Config.class), Arrays.asList(Job.class))) .andReturn(jobs); }) .expect(scheduler) .expect(jobUnit) .expect(unit -> { LinkedBindingBuilder<Map<JobDetail, Trigger>> namedJobsBB = unit .get(LinkedBindingBuilder.class); namedJobsBB.toInstance(jobs); }) .expect(onManaged) .run(unit -> { new Quartz() .with(Job.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void bindEntityStore() throws Exception { Key k = Key.get(Object.class); new MockUnit(Env.class, Config.class, Binder.class, EntityModel.class) .expect(newDataSource(true)) .expect(keys) .expect(zeroModels) .expect(store(EntityStore.class, "DEFAULT")) .expect(onStart) .expect(unit -> { LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(isA(EntityStore.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(k)).andReturn(lbb); }) .run(unit -> { new Requery(unit.get(EntityModel.class)) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }, unit -> { unit.captured(Consumer.class).iterator().next().accept(k); }); }
@SuppressWarnings({"rawtypes", "unchecked" }) @Test public void newInstance() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(unit -> { RockerRenderer renderer = unit.constructor(RockerRenderer.class) .build("", ".rocker.html"); Binder binder = unit.get(Binder.class); unit.mockStatic(Multibinder.class); Multibinder mb = unit.mock(Multibinder.class); LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(renderer); expect(mb.addBinding()).andReturn(lbb); expect(Multibinder.newSetBinder(binder, Renderer.class)).andReturn(mb); }) .run(unit -> { new Rockerby().configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
/** * Bind the given key to an {@link ElementParser<T>}, which will be applied to * the root {@link Element} of the {@link Document} at parse-time, and the * result bound in {@link MapScoped}. */ default <T> LinkedBindingBuilder<ElementParser<T>> bindRootElementParser(Key<T> key) { install(new ProvisionAtParseTime<>(key)); bind(key).toProvider(new ResolvableType<RootElementParsingProvider<T>>(){} .with(new TypeArgument<T>(key.getTypeLiteral()){})) .in(MapScoped.class); return bindElementParser(key); }
private static void setupBullshit() { Injector injector = Guice.createInjector(new Module() { @SuppressWarnings({"unchecked", "nls"}) @Override public void configure(Binder arg0) { arg0.bind(PluginService.class).toInstance(new FakePluginService()); ParameterizedType type = Types.newParameterizedType(PluginTracker.class, SectionsConverter.class); TrackerProvider<SectionsConverter> trackerProvider = new TrackerProvider<SectionsConverter>( "", "", "") { @Override public PluginTracker<SectionsConverter> get() { return new FakePluginTracker(); } }; TypeLiteral<PluginTracker<SectionsConverter>> typeLiteral = (TypeLiteral<PluginTracker<SectionsConverter>>) TypeLiteral .get(type); LinkedBindingBuilder<PluginTracker<SectionsConverter>> bindingBuilder = arg0.bind(typeLiteral); bindingBuilder.toProvider(trackerProvider).in(Scopes.SINGLETON); } }); injector.getInstance(Conversion.class); }
protected <T> TrackerProvider<T> bindTracker(java.lang.reflect.Type typeParam, Annotation annotatedWith, String extensionPoint, String beanParameter) { ParameterizedType type = Types.newParameterizedType(PluginTracker.class, typeParam); TrackerProvider<T> trackerProvider = new TrackerProvider<T>(getPluginId(), extensionPoint, beanParameter); @SuppressWarnings("unchecked") TypeLiteral<PluginTracker<T>> typeLiteral = (TypeLiteral<PluginTracker<T>>) TypeLiteral.get(type); LinkedBindingBuilder<PluginTracker<T>> bindingBuilder = bind(typeLiteral); if( annotatedWith != null ) { bindingBuilder = ((AnnotatedBindingBuilder<PluginTracker<T>>) bindingBuilder).annotatedWith(annotatedWith); } bindingBuilder.toProvider(trackerProvider).in(Scopes.SINGLETON); return trackerProvider; }
/** * Binds individual flattened key-value properties in the configuration yml * file. So one can directly inject something like this: * * @Named("Hibernate.hibernate.jdbcDriver") String jdbcDriver OR * @Named("Dashboard.service.port") int port */ @SuppressWarnings({"rawtypes", "unchecked"}) private void bindConfigProperties() { bind(YamlConfiguration.class).toInstance(yamlConfiguration); Iterator<String> propertyKeys = yamlConfiguration.getKeys(); while (propertyKeys.hasNext()) { String propertyKey = propertyKeys.next(); Object propertyValue = yamlConfiguration.getProperty(propertyKey); LinkedBindingBuilder annotatedWith = bind(propertyValue.getClass()).annotatedWith(Names.named(propertyKey)); annotatedWith.toInstance(propertyValue); } }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testPropertiesModule() { // Create Mock Binder. final Binder binder = Mockito.mock(Binder.class); // Bind mock methods. Only the ones that are called in the process are required. Mockito.when(binder.skipSources(Names.class)).thenReturn(binder); Mockito.when(binder.bind(Mockito.any(Key.class))).thenReturn(Mockito.mock(LinkedBindingBuilder.class)); final Module module = new GuiceConfigurationModule("classpath:" + PROPERTY_FILE); Assert.assertNotNull(module); module.configure(binder); Mockito.verify(binder, Mockito.times(0)).addError(Mockito.any(Throwable.class)); // This will cause a NamingException that will be caught and added to the binder. final Module badModule = new GuiceConfigurationModule("jndi:" + PROPERTY_FILE); Assert.assertNotNull(badModule); badModule.configure(binder); Mockito.verify(binder, Mockito.times(1)).addError(Mockito.any(Throwable.class)); // Difficult to test IOException flow in GuiceUtils.bindProperties. }
@Override public LinkedBindingBuilder<T> annotatedWith(Class<? extends Annotation> annotationType) { PotentialAnnotation newAnnotation = potentialAnnotation.annotatedWith(annotationType); Key<T> key = newAnnotation.getKey(type); lazyBinding.annotatedWith(annotationType) .toProvider(new LazyProvider<>(binder.getProvider(key), key)); return new LazyBindingBuilder<>(binder, type, null, newAnnotation); }
@Override public LinkedBindingBuilder<T> annotatedWith(Annotation annotation) { PotentialAnnotation newAnnotation = potentialAnnotation.annotatedWith(annotation); Key<T> key = newAnnotation.getKey(type); lazyBinding.annotatedWith(annotation) .toProvider(new LazyProvider<>(binder.getProvider(key), key)); return new LazyBindingBuilder<>(binder, type, null, newAnnotation); }
private LinkedBindingBuilder<?> verifyBindingAnnotations( Field field, AnnotatedBindingBuilder<?> annotatedBinder) { LinkedBindingBuilder<?> binderRet = annotatedBinder; for (Annotation annotation : field.getAnnotations()) { Class<? extends Annotation> annotationType = annotation.annotationType(); if (Annotations.isBindingAnnotation(annotationType)) { // not returning here ensures that annotatedWith will be called multiple times if this field // has multiple BindingAnnotations, relying on the binder to throw an error in this case. binderRet = annotatedBinder.annotatedWith(annotation); } } return binderRet; }
/** * Adds a new binding to a multibindings collection, returning its {@link BindingBuilder}. * * @param binder the current Guice binder * @param type whether the collection is a set or a map * @param elementType the type of element stored in the collection * @param setName the string used internally to identify the collection */ static <T> BindingBuilder<T> addBinding( Binder binder, Element.Type type, TypeLiteral<T> elementType, String setName) { RealElement annotation = new RealElement(setName, type, null, null); LinkedBindingBuilder<T> delegate = binder .skipSources(RealElement.class) .bind(Key.get(elementType, annotation)); return new BindingBuilder<T>(annotation, delegate); }
@Override public LinkedBindingBuilder<T> setDefault() { checkConfiguration(!isInitialized(), "already initialized"); addDirectTypeBinding(binder); RealElement.BindingBuilder<T> valueBinding = RealElement.addBinding(binder, Element.Type.OPTIONALBINDER, typeKey.getTypeLiteral(), RealElement.nameOf(typeKey)); Key<T> valueKey = Key.get(typeKey.getTypeLiteral(), valueBinding.getAnnotation()); mapBinder.addBinding(Source.DEFAULT).toProvider( new ValueProvider<T>(valueKey, binder.getProvider(valueKey))); return valueBinding; }
@Override public LinkedBindingBuilder<T> setBinding() { checkConfiguration(!isInitialized(), "already initialized"); addDirectTypeBinding(binder); RealElement.BindingBuilder<T> valueBinding = RealElement.addBinding(binder, Element.Type.OPTIONALBINDER, typeKey.getTypeLiteral(), RealElement.nameOf(typeKey)); Key<T> valueKey = Key.get(typeKey.getTypeLiteral(), valueBinding.getAnnotation()); mapBinder.addBinding(Source.ACTUAL).toProvider( new ValueProvider<T>(valueKey, binder.getProvider(valueKey))); return valueBinding; }
/** * This creates two bindings. One for the {@code Map.Entry<K, Provider<V>>} * and another for {@code V}. */ @Override public LinkedBindingBuilder<V> addBinding(K key) { checkNotNull(key, "key"); checkConfiguration(!isInitialized(), "MapBinder was already initialized"); RealElement.BindingBuilder<V> valueBinding = RealElement.addMapBinding( binder, key, keyType, valueType, entrySetBinder.getSetName()); Key<V> valueKey = Key.get(valueType, valueBinding.getAnnotation()); entrySetBinder.addBinding().toProvider(new ProviderMapEntry<K, V>( key, binder.getProvider(valueKey), valueKey)); return valueBinding; }
@SuppressWarnings({"unchecked", "rawtypes"}) private Block sqlqueryfactory(final String name) { return unit -> { LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(unit.get(SQLQueryFactory.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(key(SQLQueryFactory.class, name))).andReturn(lbb); }; }
@SuppressWarnings("unchecked") private Block bindconf(final String name) { return unit -> { LinkedBindingBuilder<Configuration> lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(unit.get(Configuration.class)); Binder binder = unit.get(Binder.class); expect(binder.bind(key(Configuration.class, name))).andReturn(lbb); }; }
@SuppressWarnings({"unchecked", "rawtypes"}) private <T extends SQLTemplates> Block sqlTemplate(final Class<T> template, final String name) { return unit -> { LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(isA(template)); Binder binder = unit.get(Binder.class); expect(binder.bind(key(SQLTemplates.class, name))).andReturn(lbb); }; }
@SuppressWarnings({"unchecked", "rawtypes"}) private Block bind(final Key key) { return unit -> { Binder binder = unit.get(Binder.class); LinkedBindingBuilder bb = unit.mock(LinkedBindingBuilder.class); bb.toInstance(unit.get(key.getTypeLiteral().getRawType())); expect(binder.bind(key)).andReturn(bb); }; }
@SuppressWarnings({"rawtypes", "unchecked"}) private Block bind(final String name, final Class type) { return unit -> { LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(unit.get(type)); Binder binder = unit.get(Binder.class); Key key = name == null ? Key.get(type) : Key.get(type, Names.named(name)); expect(binder.bind(key)).andReturn(lbb); }; }
@SuppressWarnings({"rawtypes", "unchecked"}) private Block bind(final String name, final Class type, final Class<? extends Provider> provider) { return unit -> { LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); expect(lbb.toProvider(unit.get(provider))).andReturn(lbb); Binder binder = unit.get(Binder.class); Key key = name == null ? Key.get(type) : Key.get(type, Names.named(name)); expect(binder.bind(key)).andReturn(lbb); }; }
@SuppressWarnings({"unchecked", "rawtypes"}) private Block bolt(final String uri, final String dbkey, final String user, final String pass) { return unit -> { AuthToken token = unit.mock(AuthToken.class); unit.mockStatic(AuthTokens.class); expect(AuthTokens.basic(user, pass)).andReturn(token); Binder binder = unit.get(Binder.class); Session session = unit.registerMock(Session.class); LinkedBindingBuilder<Session> lbbs = unit.mock(LinkedBindingBuilder.class); lbbs.toInstance(session); expect(binder.bind(Key.get(Session.class))).andReturn(lbbs); Driver driver = unit.registerMock(Driver.class); expect(driver.session()).andReturn(session); LinkedBindingBuilder lbb = unit.mock(LinkedBindingBuilder.class); lbb.toInstance(driver); expect(binder.bind(Key.get(Driver.class))).andReturn(lbb); unit.mockStatic(GraphDatabase.class); expect(GraphDatabase.driver(uri, token)).andReturn(driver); ServiceKey keys = unit.get(ServiceKey.class); keys.generate(eq(Driver.class), eq(dbkey), unit.capture(Consumer.class)); keys.generate(eq(Session.class), eq(dbkey), unit.capture(Consumer.class)); }; }
@SuppressWarnings("unchecked") @Test public void defaults() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, ActorSystem.class) .expect(unit -> { unit.mockStatic(ActorSystem.class); expect(ActorSystem.create("default", unit.get(Config.class))) .andReturn(unit.get(ActorSystem.class)); }) .expect(unit -> { Env env = unit.get(Env.class); expect(env.serviceKey()).andReturn(new Env.ServiceKey()); ActorSystem sys = unit.get(ActorSystem.class); LinkedBindingBuilder<ActorSystem> lbbSys = unit.mock(LinkedBindingBuilder.class); lbbSys.toInstance(sys); lbbSys.toInstance(sys); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(ActorSystem.class, Names.named("default")))).andReturn(lbbSys); expect(binder.bind(Key.get(ActorSystem.class))).andReturn(lbbSys); }) .run(unit -> { new Akka().configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void metricInstance() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, Metric.class) .expect(newMetricRegistry) .expect(newHealthCheckRegistry) .expect(mapBinderStatic) .expect(mapbinder(Metric.class, (unit, binder) -> { })) .expect(mapbinder(Metric.class, (unit, binder) -> { LinkedBindingBuilder<Metric> mLBB = unit.mock(LinkedBindingBuilder.class); mLBB.toInstance(unit.get(Metric.class)); expect(binder.addBinding("m")).andReturn(mLBB); })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { })) .expect(multibinderStatic) .expect(routes) .expect(setbinder(Reporter.class, (unit, binder) -> { })) .expect(bindMetricRegistry) .expect(bindMetricRegistryInitializer) .expect(bindHealthCheckRegistry) .expect(bindHealthCheckRegistryInitializer) .expect(onStop) .run(unit -> { new Metrics() .metric("m", unit.get(Metric.class)) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void metricRef() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(newMetricRegistry) .expect(newHealthCheckRegistry) .expect(mapBinderStatic) .expect(mapbinder(Metric.class, (unit, binder) -> { })) .expect(mapbinder(Metric.class, (unit, binder) -> { LinkedBindingBuilder<Metric> mLBB = unit.mock(LinkedBindingBuilder.class); expect(mLBB.to(Meter.class)).andReturn(null); expect(binder.addBinding("m")).andReturn(mLBB); })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { })) .expect(multibinderStatic) .expect(routes) .expect(setbinder(Reporter.class, (unit, binder) -> { })) .expect(bindMetricRegistry) .expect(bindMetricRegistryInitializer) .expect(bindHealthCheckRegistry) .expect(bindHealthCheckRegistryInitializer) .expect(onStop) .run(unit -> { new Metrics() .metric("m", Meter.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void healthCheck() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, HealthCheck.class) .expect(newMetricRegistry) .expect(newHealthCheckRegistry) .expect(mapBinderStatic) .expect(mapbinder(Metric.class, (unit, binder) -> { })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { LinkedBindingBuilder<HealthCheck> mLBB = unit.mock(LinkedBindingBuilder.class); mLBB.toInstance(unit.get(HealthCheck.class)); expect(binder.addBinding("h")).andReturn(mLBB); })) .expect(multibinderStatic) .expect(routes) .expect(setbinder(Reporter.class, (unit, binder) -> { })) .expect(bindMetricRegistry) .expect(bindMetricRegistryInitializer) .expect(bindHealthCheckRegistry) .expect(bindHealthCheckRegistryInitializer) .expect(onStop) .run(unit -> { new Metrics() .healthCheck("h", unit.get(HealthCheck.class)) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void healthRef() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(newMetricRegistry) .expect(newHealthCheckRegistry) .expect(mapBinderStatic) .expect(mapbinder(Metric.class, (unit, binder) -> { })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { LinkedBindingBuilder<HealthCheck> mLBB = unit.mock(LinkedBindingBuilder.class); expect(mLBB.to(HealthCheck.class)).andReturn(null); expect(binder.addBinding("h")).andReturn(mLBB); })) .expect(multibinderStatic) .expect(routes) .expect(setbinder(Reporter.class, (unit, binder) -> { })) .expect(bindMetricRegistry) .expect(bindMetricRegistryInitializer) .expect(bindHealthCheckRegistry) .expect(bindHealthCheckRegistryInitializer) .expect(onStop) .run(unit -> { new Metrics() .healthCheck("h", HealthCheck.class) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") @Test public void reporter() throws Exception { new MockUnit(Env.class, Config.class, Binder.class) .expect(newMetricRegistry) .expect(newHealthCheckRegistry) .expect(mapBinderStatic) .expect(mapbinder(Metric.class, (unit, binder) -> { })) .expect(mapbinder(HealthCheck.class, (unit, binder) -> { })) .expect(multibinderStatic) .expect(routes) .expect(setbinder(Reporter.class, (unit, binder) -> { LinkedBindingBuilder<Reporter> rLBB = unit.mock(LinkedBindingBuilder.class); rLBB.toInstance(isA(ConsoleReporter.class)); expect(binder.addBinding()).andReturn(rLBB); })) .expect(bindMetricRegistry) .expect(bindMetricRegistryInitializer) .expect(bindHealthCheckRegistry) .expect(bindHealthCheckRegistryInitializer) .expect(onStop) .run(unit -> { new Metrics() .reporter(r -> { return ConsoleReporter.forRegistry(r).build(); }) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings("unchecked") private Block banner() { return unit -> { LinkedBindingBuilder<String> lbb = unit.mock(LinkedBindingBuilder.class); expect(lbb.toProvider(isA(Provider.class))).andReturn(lbb); Binder binder = unit.get(Binder.class); expect(binder.bind(Key.get(String.class, Names.named("application.banner")))).andReturn(lbb); }; }
@SuppressWarnings({"unchecked" }) @Test public void withJobAndTrigger() throws Exception { Map<JobDetail, Trigger> jobs = new HashMap<JobDetail, Trigger>(); new MockUnit(Env.class, Config.class, Binder.class, LinkedBindingBuilder.class, JobDetail.class, Trigger.class) .expect(unit -> { jobs.put(unit.get(JobDetail.class), unit.get(Trigger.class)); unit.mockStatic(JobExpander.class); expect(JobExpander.jobs(unit.get(Config.class), Arrays.asList())) .andReturn(jobs); }) .expect(scheduler) .expect(jobUnit) .expect(unit -> { LinkedBindingBuilder<Map<JobDetail, Trigger>> namedJobsBB = unit .get(LinkedBindingBuilder.class); namedJobsBB.toInstance(jobs); }) .expect(onManaged) .run(unit -> { new Quartz() .with(unit.get(JobDetail.class), unit.get(Trigger.class)) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }); }
@SuppressWarnings({"unchecked" }) @Test public void withJobAndTriggerBuilder() throws Exception { new MockUnit(Env.class, Config.class, Binder.class, LinkedBindingBuilder.class, JobDetail.class, Trigger.class) .expect(unit -> { unit.mockStatic(JobExpander.class); expect(JobExpander.jobs(unit.get(Config.class), Arrays.asList())) .andReturn(Collections.emptyMap()); }) .expect(scheduler) .expect(jobUnit) .expect(unit -> { LinkedBindingBuilder<Map<JobDetail, Trigger>> namedJobsBB = unit .get(LinkedBindingBuilder.class); namedJobsBB.toInstance(unit.capture(Map.class)); }) .expect(onManaged) .run(unit -> { new Quartz() .with(Job.class, trigger -> { trigger.withSchedule( SimpleScheduleBuilder.simpleSchedule() .withIntervalInMinutes(5) ); }) .configure(unit.get(Env.class), unit.get(Config.class), unit.get(Binder.class)); }, unit -> { assertEquals(1, unit.captured(Map.class).iterator().next().size()); }); }