Skip to content

Commit

Permalink
Updates to instance resolution
Browse files Browse the repository at this point in the history
git-svn-id: http://anonsvn.jboss.org/repos/weld/ri/trunk@447 1c488680-804c-0410-94cd-c6b725194a0e
  • Loading branch information
pmuir committed Dec 7, 2008
1 parent afbc010 commit 8ec537d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
14 changes: 14 additions & 0 deletions webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
Expand Up @@ -19,6 +19,9 @@

import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -234,6 +237,17 @@ public <T> Set<Bean<T>> resolveByType(AnnotatedItem<T, ?> element, Annotation...
throw new IllegalArgumentException("Not a binding type " + annotation);
}
}
for (Type type : element.getActualTypeArguments())
{
if (type instanceof WildcardType)
{
throw new IllegalArgumentException("Cannot resolve a type parameterized with a wildcard " + element);
}
if (type instanceof TypeVariable)
{
throw new IllegalArgumentException("Cannot resolve a type parameterized with a type parameter " + element);
}
}
if (bindings.length > element.getMetaAnnotations(BindingType.class).size())
{
throw new DuplicateBindingTypeException(element.toString());
Expand Down
Expand Up @@ -5,6 +5,7 @@
import javax.webbeans.AmbiguousDependencyException;
import javax.webbeans.AnnotationLiteral;
import javax.webbeans.DuplicateBindingTypeException;
import javax.webbeans.TypeLiteral;
import javax.webbeans.UnproxyableDependencyException;
import javax.webbeans.UnsatisfiedDependencyException;
import javax.webbeans.manager.Bean;
Expand All @@ -22,37 +23,51 @@
import org.jboss.webbeans.test.beans.ScottishFish;
import org.jboss.webbeans.test.beans.Sole;
import org.jboss.webbeans.test.beans.Tuna;
import org.jboss.webbeans.test.beans.broken.ParameterizedBean;
import org.jboss.webbeans.test.beans.broken.PlaiceFarm;
import org.jboss.webbeans.test.bindings.AnotherDeploymentTypeAnnotationLiteral;
import org.testng.annotations.Test;

@SpecVersion("PDR")
@SpecVersion("20081206")
public class InstantiationByTypeTest extends AbstractTest
{

private AnnotatedClass<FishFarm> fishFarmClass = new AnnotatedClassImpl<FishFarm>(FishFarm.class);

@Test(groups={"resolution", "beanLifecycle"}) @SpecAssertion(section="4.9")
@Test(groups={"resolution", "beanLifecycle"}) @SpecAssertion(section="5.9")
public void testCurrentBindingTypeAssumed()
{
Bean<Tuna> tunaBean = createSimpleBean(Tuna.class);
manager.addBean(tunaBean);
assert manager.getInstanceByType(Tuna.class) != null;
}

@Test(groups="resolution", expectedExceptions=DuplicateBindingTypeException.class) @SpecAssertion(section="4.9")
@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="5.9")
public void testParameterizedTypeWithWildcardParameter()
{
manager.resolveByType(new TypeLiteral<ParameterizedBean<?>>(){});
}

@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="5.9")
public <T> void testParameterizedTypeWithTypeParameter()
{
manager.resolveByType(new TypeLiteral<ParameterizedBean<T>>(){});
}


@Test(groups="resolution", expectedExceptions=DuplicateBindingTypeException.class) @SpecAssertion(section="5.9")
public void testDuplicateBindingTypesUsed()
{
manager.getInstanceByType(Tuna.class, new CurrentAnnotationLiteral(), new CurrentAnnotationLiteral());
}

@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="4.9")
@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="5.9")
public void testNonBindingTypeUsed()
{
manager.getInstanceByType(Tuna.class, new AnotherDeploymentTypeAnnotationLiteral());
}

@Test(expectedExceptions=AmbiguousDependencyException.class) @SpecAssertion(section="4.9")
@Test(expectedExceptions=AmbiguousDependencyException.class) @SpecAssertion(section="5.9")
public void testAmbiguousDependencies() throws Exception
{
AnnotatedField<ScottishFish> whiteScottishFishField = new AnnotatedFieldImpl<ScottishFish>(FishFarm.class.getDeclaredField("whiteScottishFish"), fishFarmClass);
Expand All @@ -66,7 +81,7 @@ public void testAmbiguousDependencies() throws Exception
manager.getInstanceByType(ScottishFish.class, new AnnotationLiteral<Whitefish>(){});
}

@Test(expectedExceptions=UnsatisfiedDependencyException.class) @SpecAssertion(section="4.9")
@Test(expectedExceptions=UnsatisfiedDependencyException.class) @SpecAssertion(section="5.9")
public void testUnsatisfiedDependencies() throws Exception
{
AnnotatedField<ScottishFish> whiteScottishFishField = new AnnotatedFieldImpl<ScottishFish>(FishFarm.class.getDeclaredField("whiteScottishFish"), fishFarmClass);
Expand All @@ -80,7 +95,7 @@ public void testUnsatisfiedDependencies() throws Exception
manager.getInstanceByType(Tuna.class, new CurrentAnnotationLiteral());
}

@Test(expectedExceptions=UnproxyableDependencyException.class) @SpecAssertion(section="4.9")
@Test(expectedExceptions=UnproxyableDependencyException.class) @SpecAssertion(section="5.9")
public void testUnproxyableDependencies() throws Exception
{
AnnotatedField<Plaice> plaiceField = new AnnotatedFieldImpl<Plaice>(PlaiceFarm.class.getDeclaredField("plaice"), fishFarmClass);
Expand All @@ -92,7 +107,7 @@ public void testUnproxyableDependencies() throws Exception

/*
@Test(groups="resolution") @SpecAssertion(section="4.9")
@Test(groups="resolution") @SpecAssertion(section="5.9")
public void test
{
assert false;
Expand Down
Expand Up @@ -44,7 +44,7 @@
import org.jboss.webbeans.test.bindings.ExpensiveAnnotationLiteral;
import org.testng.annotations.Test;

@SpecVersion("PDR")
@SpecVersion("20081206")
public class ResolutionByTypeTest extends AbstractTest
{

Expand All @@ -60,7 +60,7 @@ public void testAnnotatedField() throws Exception
assert tuna.getType().isAssignableFrom(Tuna.class);
}

@Test(groups="resolution") @SpecAssertion(section="4.9.2")
@Test(groups="resolution") @SpecAssertion(section="5.9.2")
public void testSingleApiTypeWithCurrent() throws Exception
{
AnnotatedField<Tuna> tunaField = new AnnotatedFieldImpl<Tuna>(FishFarm.class.getDeclaredField("tuna"), fishFarmClass);
Expand All @@ -71,13 +71,13 @@ public void testSingleApiTypeWithCurrent() throws Exception
assert possibleTargets.contains(tunaBean);
}

@Test(groups="resolution", expectedExceptions=DuplicateBindingTypeException.class) @SpecAssertion(section="4.9.2")
@Test(groups="resolution", expectedExceptions=DuplicateBindingTypeException.class) @SpecAssertion(section="5.9.2")
public void testDuplicateBindingTypesUsed()
{
manager.resolveByType(Tuna.class, new CurrentAnnotationLiteral(), new CurrentAnnotationLiteral());
}

@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="4.9.2")
@Test(groups="resolution", expectedExceptions=IllegalArgumentException.class) @SpecAssertion(section="5.9.2")
public void testNonBindingTypeUsed()
{
manager.resolveByType(Tuna.class, new AnotherDeploymentTypeAnnotationLiteral());
Expand Down Expand Up @@ -130,7 +130,7 @@ public void testMultipleApiTypeWithCurrent() throws Exception
assert possibleTargets.contains(haddockBean);
}

@Test(groups="resolution") @SpecAssertion(section={"4.9.2", "4.9.4"})
@Test(groups="resolution") @SpecAssertion(section={"5.9.2", "5.9.4"})
public void testResolveByType() throws Exception
{
Bean<Tuna> tunaBean = createSimpleBean(Tuna.class);
Expand Down Expand Up @@ -188,7 +188,7 @@ public boolean realChunky()
assert manager.resolveByType(ScottishFish.class, new AnnotationLiteral<Whitefish>() {}).contains(soleBean);
}

@Test(groups="resolution") @SpecAssertion(section="4.9.2")
@Test(groups="resolution") @SpecAssertion(section="5.9.2")
public void testResolveByTypeWithTypeParameter() throws Exception
{
AnnotatedField<Farmer<ScottishFish>> scottishFishFarmerField = new AnnotatedFieldImpl<Farmer<ScottishFish>>(FishFarm.class.getDeclaredField("scottishFishFarmer"), fishFarmClass);
Expand All @@ -203,7 +203,7 @@ public void testResolveByTypeWithTypeParameter() throws Exception
assert manager.resolveByType(new TypeLiteral<Farmer<ScottishFish>>(){}).contains(scottishFishFarmerBean);
}

@Test(groups={"resolution", "producerMethod"}) @SpecAssertion(section="4.9.2")
@Test(groups={"resolution", "producerMethod"}) @SpecAssertion(section="5.9.2")
public void testResolveByTypeWithArray() throws Exception
{
SimpleBean<SpiderProducer> spiderProducerBean = createSimpleBean(SpiderProducer.class);
Expand All @@ -218,7 +218,7 @@ public void testResolveByTypeWithArray() throws Exception
assert manager.resolveByType(Spider[].class).size() == 1;
}

@Test @SpecAssertion(section="4.9.2")
@Test @SpecAssertion(section="5.9.2")
public void testOnlyHighestEnabledPreecedenceWebBeansResolved() throws Exception
{
AnnotatedField<Animal> whiteFishField = new AnnotatedFieldImpl<Animal>(FishFarm.class.getDeclaredField("whiteFish"), fishFarmClass);
Expand All @@ -236,7 +236,7 @@ public void testOnlyHighestEnabledPreecedenceWebBeansResolved() throws Exception

}

@Test(groups="resolution") @SpecAssertion(section="4.9.2")
@Test(groups="resolution") @SpecAssertion(section="5.9.2")
public void testResolveByTypeWithNonBindingMembers() throws Exception
{
AnnotatedField<Animal> veryExpensiveWhitefishField = new AnnotatedFieldImpl<Animal>(FishFarm.class.getDeclaredField("veryExpensiveWhitefish"), fishFarmClass);
Expand Down Expand Up @@ -266,7 +266,7 @@ public boolean veryExpensive()
assert beans.contains(roundWhiteFishBean);
}

@Test(groups="resolution") @SpecAssertion(section="4.9.2")
@Test(groups="resolution") @SpecAssertion(section="5.9.2")
public void testNoWebBeansFound() throws Exception
{
AnnotatedField<ScottishFish> whiteScottishFishField = new AnnotatedFieldImpl<ScottishFish>(FishFarm.class.getDeclaredField("whiteScottishFish"), fishFarmClass);
Expand All @@ -280,7 +280,7 @@ public void testNoWebBeansFound() throws Exception
assert manager.resolveByType(Tuna.class, new CurrentAnnotationLiteral()).size() == 0;
}

@Test(groups="resolution") @SpecAssertion(section={"4.9.2", "2.2"})
@Test(groups="resolution") @SpecAssertion(section={"5.9.2", "2.2"})
public void testResolveObject() throws Exception
{
Bean<Salmon> salmonBean = createSimpleBean(Salmon.class);
Expand All @@ -297,7 +297,7 @@ public void testResolveObject() throws Exception

}

@Test(groups="resolution", expectedExceptions=DefinitionException.class) @SpecAssertion(section="4.9.2.1")
@Test(groups="resolution", expectedExceptions=DefinitionException.class) @SpecAssertion(section="5.9.2.1")
public void testArrayValuedAnnotationMemberWithoutNonBinding()
{
manager.resolveByType(Animal.class, new BindingTypeWithBindingArrayTypeMemberAnnotationLiteral() {
Expand All @@ -310,7 +310,7 @@ public boolean[] bool()
});
}

@Test(groups="resolution", expectedExceptions=DefinitionException.class) @SpecAssertion(section="4.9.2.1")
@Test(groups="resolution", expectedExceptions=DefinitionException.class) @SpecAssertion(section="5.9.2.1")
public void testAnnotationValuedAnnotationMemberWithoutNonBinding()
{
manager.resolveByType(Animal.class, new BindingTypeWithBindingAnnotationMemberAnnotationLiteral()
Expand Down

0 comments on commit 8ec537d

Please sign in to comment.