You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2012/03/19 09:23:05 UTC
git commit: DELTASPIKE-120 add BeanProvider#getContextualReference
methods wo 'optional'
Updated Branches:
refs/heads/master bf266f55a -> 56af95587
DELTASPIKE-120 add BeanProvider#getContextualReference methods wo 'optional'
Since the optional is set to false most of the times, we introduced
a few convenient functions for the default case.
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/56af9558
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/56af9558
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/56af9558
Branch: refs/heads/master
Commit: 56af95587cb0506fd9cefded4a90b888cee69c2a
Parents: bf266f5
Author: Mark Struberg <st...@apache.org>
Authored: Mon Mar 19 09:20:32 2012 +0100
Committer: Mark Struberg <st...@apache.org>
Committed: Mon Mar 19 09:22:14 2012 +0100
----------------------------------------------------------------------
.../deltaspike/core/api/provider/BeanProvider.java | 37 +++++++++++-
.../test/core/api/provider/BeanProviderTest.java | 48 ++++++++++++++-
2 files changed, 81 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/56af9558/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
index 12553f7..6af4afa 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/api/provider/BeanProvider.java
@@ -67,11 +67,28 @@ public final class BeanProvider
* used to create the contextual instance which this method will not provide.</p>
*
* @param type the type of the bean in question
+ * @param qualifiers additional qualifiers which further distinct the resolved bean
+ * @param <T> target type
+ * @return the resolved Contextual Reference
+ * @throws {@code IllegalStateException} if the bean could not be found.
+ * @see #getContextualReference(Class, boolean, Annotation...)
+ */
+ public static <T> T getContextualReference(Class<T> type, Annotation... qualifiers)
+ {
+ return getContextualReference(type, false, qualifiers);
+ }
+
+ /**
+ * {@link #getContextualReference(Class, Annotation...)} which returns <code>null</code> if the
+ * 'optional' parameter is set to <code>true</code>.
+ *
+ * @param type the type of the bean in question
* @param optional if <code>true</code> it will return <code>null</code> if no bean could be found or created.
* Otherwise it will throw an {@code IllegalStateException}
* @param qualifiers additional qualifiers which further distinct the resolved bean
* @param <T> target type
* @return the resolved Contextual Reference
+ * @see #getContextualReference(Class, Annotation...)
*/
public static <T> T getContextualReference(Class<T> type, boolean optional, Annotation... qualifiers)
{
@@ -100,6 +117,23 @@ public final class BeanProvider
* in {@link #getContextualReference(Class, boolean, java.lang.annotation.Annotation...)}!</p>
*
* @param name the EL name of the bean
+ * @return the resolved Contextual Reference
+ * @throws {@code IllegalStateException} if the bean could not be found.
+ * @see #getContextualReference(String, boolean)
+ */
+ public static Object getContextualReference(String name)
+ {
+ return getContextualReference(name, false);
+ }
+
+ /**
+ * <p>Get a Contextual Reference by it's EL Name.
+ * This only works for beans with the @Named annotation.</p>
+ *
+ * <p><b>Attention:</b> please see the notes on manually resolving @Dependent bean
+ * in {@link #getContextualReference(Class, boolean, java.lang.annotation.Annotation...)}!</p>
+ *
+ * @param name the EL name of the bean
* @param optional if <code>true</code> it will return <code>null</code> if no bean could be found or created.
* Otherwise it will throw an {@code IllegalStateException}
* @return the resolved Contextual Reference
@@ -161,8 +195,7 @@ public final class BeanProvider
* @param <T> target type
* @return the resolved list of Contextual Reference or an empty-list if optional is true
*/
- public static <T> List<T> getContextualReferences(Class<T> type,
- boolean optional)
+ public static <T> List<T> getContextualReferences(Class<T> type, boolean optional)
{
return getContextualReferences(type, optional, true);
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/56af9558/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
index 2fbbecc..33506c8 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/api/provider/BeanProviderTest.java
@@ -32,6 +32,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
@RunWith(Arquillian.class)
public class BeanProviderTest
@@ -88,9 +89,52 @@ public class BeanProviderTest
@Test
public void simpleBeanLookupByNameWithoutType()
{
- Object testBean = BeanProvider.getContextualReference("extraNameBean", false);
+ {
+ // test with the convenient operator (optional=false implied)
+ Object testBean = BeanProvider.getContextualReference("extraNameBean");
+ Assert.assertNotNull(testBean);
+ Assert.assertTrue(testBean instanceof TestBean);
+ TestBean tb = (TestBean) testBean;
+ Assert.assertEquals(4711, tb.getI());
+ }
+
+ {
+ // test with the 'optional' flag set to false
+ Object testBean = BeanProvider.getContextualReference("extraNameBean", false);
+ Assert.assertNotNull(testBean);
+ Assert.assertTrue(testBean instanceof TestBean);
+ TestBean tb = (TestBean) testBean;
+ Assert.assertEquals(4711, tb.getI());
+ }
+
+ {
+ // test by name lookup with the 'optional' flag set to false
+ try
+ {
+ Object testBean = BeanProvider.getContextualReference("thisBeanDoesNotExist");
+ Assert.fail("BeanProvider#getContextualReference should have blown up with a non-existing bean!");
+ }
+ catch(IllegalStateException ise)
+ {
+ // all is well, this is exactly what should happen!
+ }
+ }
+
+ {
+ // test by type lookup with the 'optional' flag set to false
+ try
+ {
+ // guess we can safely assume that there is no producer for a ConcurrentHashMap in the system...
+ ConcurrentHashMap chm = BeanProvider.getContextualReference(ConcurrentHashMap.class);
+ Assert.fail("BeanProvider#getContextualReference should have blown up with a non-existing bean!");
+ }
+ catch(IllegalStateException ise)
+ {
+ // all is well, this is exactly what should happen!
+ }
+ }
+
- Assert.assertNotNull(testBean);
}
/*