CacheProvider implements Provider<Cache> { @Override public Cache get() { return this.supplier.get(); } @Inject  CacheProvider(Injector injector, NinjaProperties ninjaProperties); @Override Cache get();  }
@Test public void defaultImplementation() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty(NinjaConstant.CACHE_IMPLEMENTATION, null); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); CacheProvider cacheProvider = injector.getInstance(CacheProvider.class); assertThat(cacheProvider.get(), instanceOf(CacheEhCacheImpl.class)); }
@Test public void configuredImplementation() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty(NinjaConstant.CACHE_IMPLEMENTATION, CacheMemcachedImpl.class.getName()); ninjaProperties.setProperty(NinjaConstant.MEMCACHED_HOST, "127.0.0.1:1234"); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); Provider<Cache> cacheProvider = injector.getProvider(Cache.class); assertThat(cacheProvider.get(), instanceOf(CacheMemcachedImpl.class)); }
@Test public void verifySingletonProviderAndInstance() { NinjaPropertiesImpl ninjaProperties = new NinjaPropertiesImpl(NinjaMode.test); ninjaProperties.setProperty(NinjaConstant.CACHE_IMPLEMENTATION, CacheMockImpl.class.getCanonicalName()); Injector injector = Guice.createInjector(new BaseAndClassicModules(ninjaProperties)); CacheProvider cacheProvider = injector.getInstance(CacheProvider.class); assertThat(cacheProvider, sameInstance(injector.getInstance(CacheProvider.class))); assertThat(cacheProvider, sameInstance(injector.getInstance(CacheProvider.class))); Cache cache = cacheProvider.get(); assertThat(cache, sameInstance(cacheProvider.get())); assertThat(cache, sameInstance(injector.getInstance(Cache.class))); }