[webbeans-commits] Webbeans SVN: r3174 - tck/trunk/impl/src/main/java/org/jboss/jsr299/tck.

webbeans-commits at lists.jboss.org webbeans-commits at lists.jboss.org
Thu Jul 23 17:30:55 EDT 2009


Author: dan.j.allen
Date: 2009-07-23 17:30:55 -0400 (Thu, 23 Jul 2009)
New Revision: 3174

Modified:
   tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/AbstractJSR299Test.java
Log:
add convenience method to retrieve a unique bean

Modified: tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/AbstractJSR299Test.java
===================================================================
--- tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/AbstractJSR299Test.java	2009-07-23 21:15:23 UTC (rev 3173)
+++ tck/trunk/impl/src/main/java/org/jboss/jsr299/tck/AbstractJSR299Test.java	2009-07-23 21:30:55 UTC (rev 3174)
@@ -6,13 +6,16 @@
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Set;
 
 import javax.enterprise.context.spi.Context;
+import javax.enterprise.inject.AmbiguousResolutionException;
 import javax.enterprise.inject.TypeLiteral;
+import javax.enterprise.inject.UnsatisfiedResolutionException;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.BeanManager;
 
@@ -125,6 +128,12 @@
       return annotationTypeList.size() == 0;
    }
 
+   public <T> Bean<T> getUniqueBean(Class<T> type, Annotation... bindings)
+   {
+      Set<Bean<T>> beans = getBeans(type, bindings);
+      return resolveUniqueBean(type, beans);
+   }
+   
    public <T> Set<Bean<T>> getBeans(Class<T> type, Annotation... bindings)
    {
       return (Set) getCurrentManager().getBeans(type, bindings);
@@ -150,6 +159,19 @@
       return OldSPIBridge.getInstanceByName(getCurrentManager(), name);
    }
 
+   private <T> Bean<T> resolveUniqueBean(Type type, Set<Bean<T>> beans)
+   {
+      if (beans.size() == 0)
+      {
+         throw new UnsatisfiedResolutionException("Unable to resolve any beans of " + type);
+      }
+      else if (beans.size() > 1)
+      {
+         throw new AmbiguousResolutionException("More than one bean available (" + beans.size() + ")");
+      }
+      return beans.iterator().next();
+   }
+   
    @Override
    protected DeploymentException handleDeploymentFailure(DeploymentException deploymentException)
    {




More information about the weld-commits mailing list