Skip to content

Commit

Permalink
Adding Illegal Binding error
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemures committed May 6, 2016
1 parent 5b48024 commit 58056ad
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
20 changes: 20 additions & 0 deletions toothpick-runtime/src/main/java/toothpick/Configuration.java
@@ -1,5 +1,6 @@
package toothpick;

import java.lang.annotation.Annotation;
import java.util.Stack;
import toothpick.config.Binding;

Expand Down Expand Up @@ -31,7 +32,26 @@ protected Stack<Class> initialValue() {

@Override
void checkIllegalBinding(Binding binding) {
Class<?> clazz;
switch (binding.getMode()) {
case SIMPLE:
clazz = binding.getKey();
break;
case CLASS:
clazz = binding.getImplementationClass();
break;
case PROVIDER_CLASS:
clazz = binding.getProviderClass();
break;
default:
return;
}

for (Annotation annotation : clazz.getAnnotations()) {
if (annotation.annotationType().isAnnotationPresent(javax.inject.Scope.class)) {
throw new IllegalBindingException();
}
}
}

@Override
Expand Down
@@ -0,0 +1,21 @@
package toothpick;

/**
* Thrown when a binding is illegal.
*/
public class IllegalBindingException extends IllegalStateException {
public IllegalBindingException() {
}

public IllegalBindingException(String s) {
super(s);
}

public IllegalBindingException(String message, Throwable cause) {
super(message, cause);
}

public IllegalBindingException(Throwable cause) {
super(cause);
}
}
3 changes: 3 additions & 0 deletions toothpick-runtime/src/main/java/toothpick/ScopeImpl.java
@@ -1,5 +1,6 @@
package toothpick;

import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -175,6 +176,8 @@ private void installModule(Module module) {
if (binding == null) {
throw new IllegalStateException("null binding are not allowed. Should not happen unless getBindingSet is overridden.");
}
Configuration.instance.checkIllegalBinding(binding);

switch (binding.getMode()) {
case SIMPLE:
return createInternalProvider(this, binding.getKey(), false, binding.isScoped());
Expand Down
65 changes: 65 additions & 0 deletions toothpick-runtime/src/test/java/toothpick/scoping/ScopingTest.java
@@ -1,21 +1,26 @@
package toothpick.scoping;

import org.junit.Test;
import toothpick.IllegalBindingException;
import toothpick.Scope;
import toothpick.ScopeImpl;
import toothpick.ToothPick;
import toothpick.ToothPickBaseTest;
import toothpick.config.Module;
import toothpick.data.Bar;
import toothpick.data.BarChild;
import toothpick.data.Foo;
import toothpick.data.FooChildWithoutInjectedFields;
import toothpick.data.FooSingleton;
import toothpick.data.IFooSingleton;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.sameInstance;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static toothpick.Configuration.development;

/*
* Tests scopes related features of toothpick.
Expand Down Expand Up @@ -155,4 +160,64 @@ public void singletonDiscoveredDynamically_shouldGoInRootScope() throws Exceptio
assertThat(instance, sameInstance(instance2));
assertThat(instance, notNullValue());
}

@Test
public void singleton_shouldBeSharedBySubscopes() throws Exception {
//GIVEN
Scope scopeParent = new ScopeImpl("");
Scope scope1 = new ScopeImpl("");
Scope scope2 = new ScopeImpl("");
scopeParent.addChild(scope1);
scopeParent.addChild(scope2);

//WHEN
FooSingleton instance = scope1.getInstance(FooSingleton.class);
FooSingleton instance2 = scope2.getInstance(FooSingleton.class);

//THEN
assertThat(instance, sameInstance(instance2));
}

@Test(expected = IllegalBindingException.class)
public void binding_shouldCrashForScopeAnnotatedClass_whenBindingIsSimple() throws Exception {
//GIVEN
ToothPick.setConfiguration(development());
Scope scopeParent = new ScopeImpl("");
Scope scope1 = new ScopeImpl("");
Scope scope2 = new ScopeImpl("");
scopeParent.addChild(scope1);
scopeParent.addChild(scope2);

//WHEN
scope1.installModules(new Module() {
{
bind(FooSingleton.class);
}
});

//THEN
fail("This test should throw a IllegalBindingException.");
}

@Test(expected = IllegalBindingException.class)
public void binding_shouldCrashForScopeAnnotatedClass_whenBindingToAClass() throws Exception {
//GIVEN
ToothPick.setConfiguration(development());
Scope scopeParent = new ScopeImpl("");
Scope scope1 = new ScopeImpl("");
Scope scope2 = new ScopeImpl("");
scopeParent.addChild(scope1);
scopeParent.addChild(scope2);


//WHEN
scope1.installModules(new Module() {
{
bind(IFooSingleton.class).to(FooSingleton.class);
}
});

//THEN
fail("This test should throw a IllegalBindingException.");
}
}

0 comments on commit 58056ad

Please sign in to comment.