@Test public void testEqualsAndHashCode_shouldReturnFalse_whenNodeHasDifferentName() { //GIVEN Scope scope = new ScopeImpl("foo"); Scope scope2 = new ScopeImpl("bar"); //WHEN boolean equals = scope.equals(scope2); int hashScope = scope.hashCode(); int hashScope2 = scope2.hashCode(); //THEN assertThat(equals, is(false)); assertThat(hashScope, not(is(hashScope2))); }
@Test public void installOverrideModules_shouldNotInstallOverrideBindings_whenCalledWithoutTestModules() { //GIVEN ScopeImpl scope = new ScopeImpl(""); scope.installTestModules(); scope.installModules(new ProdModule()); //WHEN Foo instance = scope.getInstance(Foo.class); //THEN assertThat(instance, notNullValue()); }
@Test(expected = RuntimeException.class) public void testLookup_shouldFail_whenNotFindingABindingForANamedProvider() { //GIVEN //WHEN new ScopeImpl("").lookupProvider(Foo.class, "Bar"); //THEN fail("Should throw an exception"); }
@Override public <T> T getInstance(Class<T> clazz, String name) { crashIfClosed(); ConfigurationHolder.configuration.checkCyclesStart(clazz, name); T t; try { t = lookupProvider(clazz, name).get(this); } finally { ConfigurationHolder.configuration.checkCyclesEnd(clazz, name); } return t; }
@Test(expected = IllegalStateException.class) public void getInstance_shouldFail_whenScopeIsClosed() { //GIVEN ScopeImpl scope = new ScopeImpl(""); //WHEN scope.close(); scope.getInstance(Foo.class); //THEN }
@Test(expected = IllegalStateException.class) public void getProvider_shouldFail_whenScopeIsClosed() { //GIVEN ScopeImpl scope = new ScopeImpl(""); //WHEN scope.close(); scope.getProvider(Foo.class); //THEN }
throw new IllegalArgumentException("TP can't get an instance of a null class."); InternalProviderImpl<? extends T> scopedProvider = getBoundProvider(clazz, bindingName); if (scopedProvider != null) { return scopedProvider; Scope parentScope = iterator.next(); ScopeImpl parentScopeImpl = (ScopeImpl) parentScope; InternalProviderImpl<? extends T> parentScopedProvider = parentScopeImpl.getBoundProvider(clazz, bindingName); if (parentScopedProvider != null) { return parentScopedProvider; + "in scope %s and its parents %s", clazz.getName(), bindingName, getName(), getParentScopesNames())); InternalProviderImpl unScopedProviderInPool = getUnBoundProvider(clazz, null); if (unScopedProviderInPool != null) { return unScopedProviderInPool; return targetScopeImpl.installScopedProvider(clazz, null, newProvider, false); } else { return installUnBoundProvider(clazz, null, newProvider);
private void installModule(boolean isTestModule, Module module) { for (Binding binding : module.getBindingSet()) { if (binding == null) { throw new IllegalStateException("A module can't have a null binding : " + module); } Class clazz = binding.getKey(); String bindingName = binding.getName(); try { if (isTestModule || getBoundProvider(clazz, bindingName) == null) { InternalProviderImpl provider = toProvider(binding); if (binding.isCreatingInstancesInScope()) { installScopedProvider(clazz, bindingName, (ScopedProviderImpl) provider, isTestModule); } else { installBoundProvider(clazz, bindingName, provider, isTestModule); } } } catch (Exception e) { throw new IllegalBindingException(format("Binding %s couldn't be installed", bindingName), e); } } }
@Test public void testToString() { //GIVEN ScopeImpl scope = new ScopeImpl("root"); scope.installModules(new TestModule1()); ScopeImpl childScope = new ScopeImpl("child"); scope.addChild(childScope); //WHEN childScope.getInstance(Bar.class); String dump = scope.toString(); //THEN Pattern expected = Pattern.compile("root:\\d+.*" + "Providers: \\[toothpick.Scope,toothpick.data.Foo\\].*" + "\\\\---child:\\d+.*" + "Providers:.*\\[toothpick.Scope\\].*" + "Unbound providers: \\[toothpick.data.Bar\\].*", Pattern.DOTALL); assertThat(dump, MatchesPattern.matchesPattern(expected)); }
@Test(expected = NoFactoryFoundException.class) public void reset_shouldResetBoundProviders_andFlagTheTestModuleToFalse() throws Exception { //GIVEN ScopeImpl scope = new ScopeImpl("root"); scope.installTestModules(new Module() { { bind(IFoo.class).to(Foo.class); } }); //WHEN scope.reset(); //THEN scope.installTestModules(); // Should not crash scope.getInstance(IFoo.class); // Should crash as we don't have the binding for IFoo anymore }
@Test public void reset_shouldRebindScope() throws Exception { //GIVEN ScopeImpl scope = new ScopeImpl("root"); //WHEN scope.reset(); //THEN assertThat(scope.getInstance(Scope.class), notNullValue()); }
@Test(expected = IllegalStateException.class) public void installModules_shouldThrowAnException_whenModuleHasANullBinding() { //GIVEN ScopeImpl scope = new ScopeImpl(""); //WHEN scope.installModules(new NullBindingModule()); //THEN fail("Should throw an exception."); }
@Test(expected = IllegalStateException.class) public void testToBinding_shouldFail_whenAddingANullBinding() { //GIVEN //WHEN new ScopeImpl("").toProvider(null); //THEN }
@Override public <T> Provider<T> getProvider(Class<T> clazz) { InternalProviderImpl<? extends T> provider = lookupProvider(clazz, null); return new ThreadSafeProviderImpl<>(this, provider, false); }
@Override public void installModules(Module... modules) { installModules(false, modules); }
/** * Obtains the provider of the class {@code clazz} and name {@code bindingName}, if any. The returned provider * will belong to the pool of unbound providers. It can be {@code null} if there is no such provider. * * @param clazz the class for which to obtain the unbound provider. * @param bindingName the name, possibly {@code null}, for which to obtain the unbound provider. * @param <T> the type of {@code clazz}. * @return the unbound provider for class {@code clazz} and {@code bindingName}. Returns {@code null} is there * is no such unbound provider. */ private <T> InternalProviderImpl<? extends T> getUnBoundProvider(Class<T> clazz, String bindingName) { return getInternalProvider(clazz, bindingName, false); }
@Test(expected = IllegalStateException.class) public void installTestModules_shoudFailToInstallTestsBindingsAgain_whenCalledTwice() { //GIVEN Foo testFoo = new Foo(); Foo testFoo2 = new Foo(); ScopeImpl scope = new ScopeImpl(""); scope.installTestModules(new TestModule(testFoo)); //WHEN scope.installTestModules(new TestModule(testFoo2)); //THEN fail("Should throw an exception"); }
return createInternalProvider(this, binding.getKey(), false, false); case CLASS: return createInternalProvider(this, binding.getImplementationClass(), false, return createInternalProvider(this, binding.getProviderClass(), true,
@Override public <T> Lazy<T> getLazy(Class<T> clazz) { return getLazy(clazz, null); }