[webbeans-commits] Webbeans SVN: r305 - in ri/trunk/webbeans-ri/src: test/java/org/jboss/webbeans/test and 1 other directories.
webbeans-commits at lists.jboss.org
webbeans-commits at lists.jboss.org
Thu Nov 13 07:59:29 EST 2008
Author: pete.muir at jboss.org
Date: 2008-11-13 07:59:29 -0500 (Thu, 13 Nov 2008)
New Revision: 305
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/DeploymentProperties.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockWebBeanDiscovery.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/BoostrapTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockBootstrap.java
Log:
Basic tests for discovery and load discovery class from properties file or system property
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java 2008-11-13 12:28:05 UTC (rev 304)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/Bootstrap.java 2008-11-13 12:59:29 UTC (rev 305)
@@ -4,7 +4,9 @@
import static org.jboss.webbeans.util.BeanFactory.createProducerMethodBean;
import static org.jboss.webbeans.util.BeanFactory.createSimpleBean;
+import java.util.Arrays;
import java.util.HashSet;
+import java.util.List;
import java.util.Set;
import javax.webbeans.manager.Bean;
@@ -12,12 +14,20 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bean.AbstractBean;
import org.jboss.webbeans.bean.AbstractClassBean;
+import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
import org.jboss.webbeans.introspector.AnnotatedMethod;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
public class Bootstrap
{
+ private LogProvider log = Logging.getLogProvider(Bootstrap.class);
+
+ private static String WEB_BEAN_DISCOVERY_PROPERTY_NAME = "org.jboss.webbeans.bootstrap.webBeanDiscovery";
+
private ManagerImpl manager;
+ private DeploymentProperties deploymentProperties;
public Bootstrap()
{
@@ -27,14 +37,64 @@
protected Bootstrap(ManagerImpl manager)
{
this.manager = manager;
+ this.deploymentProperties = new DeploymentProperties(Thread.currentThread().getContextClassLoader());
}
+ protected List<String> getWebBeanDiscoveryClassNames()
+ {
+ return deploymentProperties.getPropertyValues(WEB_BEAN_DISCOVERY_PROPERTY_NAME);
+ }
+
+ public void discoverBeans()
+ {
+ WebBeanDiscovery webBeanDiscovery = null;
+ for (String className : getWebBeanDiscoveryClassNames())
+ {
+ Class<WebBeanDiscovery> webBeanDiscoveryClass = null;
+ try
+ {
+ webBeanDiscoveryClass = (Class<WebBeanDiscovery>) Class.forName(className);
+ }
+ catch (ClassNotFoundException e)
+ {
+ log.debug("Unable to load WebBeanDiscovery provider " + className, e);
+ }
+ catch (NoClassDefFoundError e) {
+ log.warn("Unable to load WebBeanDiscovery provider " + className + " due classDependencyProblem", e);
+ }
+
+ try
+ {
+ webBeanDiscovery = webBeanDiscoveryClass.newInstance();
+ break;
+ }
+ catch (InstantiationException e)
+ {
+ log.warn("Error creating WebBeanDiscovery provider" + className, e);
+ }
+ catch (IllegalAccessException e)
+ {
+ log.warn("Error creating WebBeanDiscovery provider" + className, e);
+ }
+ }
+ if (webBeanDiscovery == null)
+ {
+ throw new IllegalStateException("No WebBeanDiscovery provider found, you need to implement the org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery interface, and tell the RI to use it by specifying -D" + WEB_BEAN_DISCOVERY_PROPERTY_NAME + "=<classname>");
+ }
+ registerBeans(webBeanDiscovery.discoverWebBeanClasses());
+ }
+
/**
* Register any beans defined by the provided classes with the manager
*/
public void registerBeans(Class<?>...classes)
{
- Set<AbstractBean<?, ?>> beans = discoverBeans(classes);
+ registerBeans(new HashSet<Class<?>>(Arrays.asList(classes)));
+ }
+
+ public void registerBeans(Set<Class<?>> classes)
+ {
+ Set<AbstractBean<?, ?>> beans = createBeans(classes);
for (Bean<?> bean : beans)
{
manager.addBean(bean);
@@ -46,8 +106,13 @@
*
* Beans discovered are not registered with the manager
*/
- public Set<AbstractBean<?, ?>> discoverBeans(Class<?>... classes)
+ public Set<AbstractBean<?, ?>> createBeans(Class<?>... classes)
{
+ return createBeans(new HashSet<Class<?>>(Arrays.asList(classes)));
+ }
+
+ public Set<AbstractBean<?, ?>> createBeans(Set<Class<?>> classes)
+ {
Set<AbstractBean<?, ?>> beans = new HashSet<AbstractBean<?, ?>>();
for (Class<?> clazz : classes)
{
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/DeploymentProperties.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/DeploymentProperties.java (rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/DeploymentProperties.java 2008-11-13 12:59:29 UTC (rev 305)
@@ -0,0 +1,109 @@
+package org.jboss.webbeans.bootstrap;
+
+import static org.jboss.webbeans.util.Strings.split;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Properties;
+
+/*
+ * Utility class to load deployment properties
+ */
+public class DeploymentProperties
+{
+
+ private ClassLoader classLoader;
+ private Enumeration<URL> urlEnum;
+
+ public DeploymentProperties(ClassLoader classLoader)
+ {
+ this.classLoader = classLoader;
+ }
+
+ /**
+ * The resource bundle used to control Web Beans RI deployment
+ */
+ public static final String RESOURCE_BUNDLE = "META-INF/web-beans-ri.properties";
+
+ /**
+ * Get a list of possible values for a given key.
+ *
+ * First, System properties are tried, followed by the specified resource
+ * bundle (first in classpath only).
+ *
+ * Colon (:) deliminated lists are split out.
+ *
+ */
+ public List<String> getPropertyValues(String key)
+ {
+ List<String>values = new ArrayList<String>();
+ addPropertiesFromSystem(key, values);
+ addPropertiesFromResourceBundle(key, values);
+ return values;
+ }
+
+ private void addPropertiesFromSystem(String key, List<String> values)
+ {
+ addProperty(key, System.getProperty(key), values);
+ }
+
+ private void addPropertiesFromResourceBundle(String key, List<String> values)
+ {
+ try
+ {
+ while ( getResources().hasMoreElements() )
+ {
+ URL url = getResources().nextElement();
+ Properties properties = new Properties();
+ InputStream propertyStream = url.openStream();
+ try
+ {
+ properties.load(propertyStream);
+ addProperty(key, properties.getProperty(key), values);
+ }
+ finally
+ {
+ if (propertyStream != null)
+ {
+ propertyStream.close();
+ }
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ // No - op, file is optional
+ }
+ }
+
+ /*
+ * Add the property to the set of properties only if it hasn't already been added
+ */
+ private void addProperty(String key, String value, List<String> values)
+ {
+ if (value != null)
+ {
+ String[] properties = split(value, ":");
+ for (String property : properties)
+ {
+ values.add(property);
+ }
+
+ }
+ }
+
+ private Enumeration<URL> getResources() throws IOException
+ {
+
+ if (urlEnum == null)
+ {
+ urlEnum = classLoader.getResources(RESOURCE_BUNDLE);
+ }
+ return urlEnum;
+ }
+
+}
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/bootstrap/DeploymentProperties.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/BoostrapTest.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/BoostrapTest.java 2008-11-13 12:28:05 UTC (rev 304)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/BoostrapTest.java 2008-11-13 12:59:29 UTC (rev 305)
@@ -1,6 +1,10 @@
package org.jboss.webbeans.test;
+import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -21,6 +25,8 @@
import org.jboss.webbeans.test.beans.Tiger;
import org.jboss.webbeans.test.beans.Tuna;
import org.jboss.webbeans.test.ejb.model.valid.Hound;
+import org.jboss.webbeans.test.mock.MockBootstrap;
+import org.jboss.webbeans.test.mock.MockWebBeanDiscovery;
import org.testng.annotations.Test;
public class BoostrapTest extends AbstractTest
@@ -28,7 +34,7 @@
@Test(groups="bootstrap")
public void testSingleSimpleBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(Tuna.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Tuna.class);
assert beans.size() == 1;
assert beans.iterator().next().getType().equals(Tuna.class);
}
@@ -36,7 +42,7 @@
@Test(groups="bootstrap")
public void testSingleEnterpriseBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(Hound.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Hound.class);
assert beans.size() == 1;
assert beans.iterator().next().getType().equals(Hound.class);
}
@@ -44,7 +50,7 @@
@Test(groups="bootstrap")
public void testMultipleSimpleBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(Tuna.class, Salmon.class, SeaBass.class, Sole.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Tuna.class, Salmon.class, SeaBass.class, Sole.class);
assert beans.size() == 4;
Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
for (AbstractBean<?, ?> bean : beans)
@@ -65,7 +71,7 @@
@Test(groups="bootstrap")
public void testProducerMethodBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(TarantulaProducer.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(TarantulaProducer.class);
assert beans.size() == 2;
Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
for (AbstractBean<?, ?> bean : beans)
@@ -82,7 +88,7 @@
@Test(groups="bootstrap")
public void testMultipleEnterpriseBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(Hound.class, Elephant.class, Panther.class, Tiger.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Hound.class, Elephant.class, Panther.class, Tiger.class);
assert beans.size() == 4;
Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
for (AbstractBean<?, ?> bean : beans)
@@ -103,7 +109,7 @@
@Test(groups="bootstrap")
public void testMultipleEnterpriseAndSimpleBean()
{
- Set<AbstractBean<?, ?>> beans = bootstrap.discoverBeans(Hound.class, Elephant.class, Panther.class, Tiger.class, Tuna.class, Salmon.class, SeaBass.class, Sole.class);
+ Set<AbstractBean<?, ?>> beans = bootstrap.createBeans(Hound.class, Elephant.class, Panther.class, Tiger.class, Tuna.class, Salmon.class, SeaBass.class, Sole.class);
assert beans.size() == 8;
Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
for (AbstractBean<?, ?> bean : beans)
@@ -177,4 +183,53 @@
assert classes.get(SeaBass.class) instanceof SimpleBean;
assert classes.get(Sole.class) instanceof SimpleBean;
}
+
+ @Test(groups="bootstrap", expectedExceptions=IllegalStateException.class)
+ public void testDiscoverFails()
+ {
+ bootstrap = new MockBootstrap(manager)
+ {
+
+ @Override
+ protected List<String> getWebBeanDiscoveryClassNames()
+ {
+ return new ArrayList<String>();
+ }
+
+ };
+ bootstrap.discoverBeans();
+ }
+
+ @Test(groups="bootstrap")
+ public void testDiscover()
+ {
+ MockWebBeanDiscovery.webBeanClasses = new HashSet<Class<?>>(Arrays.asList(Hound.class, Elephant.class, Panther.class, Tiger.class, Tuna.class, Salmon.class, SeaBass.class, Sole.class));
+ bootstrap.discoverBeans();
+
+ assert manager.getBeans().size() == 9;
+ Map<Class<?>, Bean<?>> classes = new HashMap<Class<?>, Bean<?>>();
+ for (Bean<?> bean : manager.getBeans())
+ {
+
+ classes.put(((AbstractBean<?, ?>) bean).getType(), bean);
+ }
+ assert classes.containsKey(DefaultEnterpriseBeanLookup.class);
+ assert classes.containsKey(Hound.class);
+ assert classes.containsKey(Elephant.class);
+ assert classes.containsKey(Panther.class);
+ assert classes.containsKey(Tiger.class);
+ assert classes.containsKey(Tuna.class);
+ assert classes.containsKey(Salmon.class);
+ assert classes.containsKey(SeaBass.class);
+ assert classes.containsKey(Sole.class);
+
+ assert classes.get(Hound.class) instanceof EnterpriseBean;
+ assert classes.get(Elephant.class) instanceof EnterpriseBean;
+ assert classes.get(Panther.class) instanceof EnterpriseBean;
+ assert classes.get(Tiger.class) instanceof EnterpriseBean;
+ assert classes.get(Tuna.class) instanceof SimpleBean;
+ assert classes.get(Salmon.class) instanceof SimpleBean;
+ assert classes.get(SeaBass.class) instanceof SimpleBean;
+ assert classes.get(Sole.class) instanceof SimpleBean;
+ }
}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockBootstrap.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockBootstrap.java 2008-11-13 12:28:05 UTC (rev 304)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockBootstrap.java 2008-11-13 12:59:29 UTC (rev 305)
@@ -1,11 +1,22 @@
package org.jboss.webbeans.test.mock;
+import java.util.Arrays;
+import java.util.List;
+
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.bootstrap.Bootstrap;
public class MockBootstrap extends Bootstrap
{
+
+ private static final List<String> MOCK_WEB_BEAN_DISCOVERY_CLASS_NAMES = Arrays.asList(MockWebBeanDiscovery.class.getName());
+ @Override
+ protected List<String> getWebBeanDiscoveryClassNames()
+ {
+ return MOCK_WEB_BEAN_DISCOVERY_CLASS_NAMES;
+ }
+
public MockBootstrap(ManagerImpl manager)
{
super(manager);
Added: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockWebBeanDiscovery.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockWebBeanDiscovery.java (rev 0)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockWebBeanDiscovery.java 2008-11-13 12:59:29 UTC (rev 305)
@@ -0,0 +1,18 @@
+package org.jboss.webbeans.test.mock;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.jboss.webbeans.bootstrap.spi.WebBeanDiscovery;
+
+public class MockWebBeanDiscovery implements WebBeanDiscovery
+{
+
+ public static Set<Class<?>> webBeanClasses = new HashSet<Class<?>>();
+
+ public Set<Class<?>> discoverWebBeanClasses()
+ {
+ return webBeanClasses;
+ }
+
+}
Property changes on: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/mock/MockWebBeanDiscovery.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
More information about the weld-commits
mailing list