You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/09/07 21:42:59 UTC
svn commit: r1520793 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/beanutils/
test/java/org/apache/commons/configuration/beanutils/
Author: oheger
Date: Sat Sep 7 19:42:58 2013
New Revision: 1520793
URL: http://svn.apache.org/r1520793
Log:
Made BeanHelper a bean class.
Instances can now be created and configured in isolation.
BeanHelper provides a straight-forward implementation of BeanCreationContext
for interacting with the used BeanFactory.
Note that this commit breaks the build because there are still a bunch of
references to BeanHelper's static methods.
Modified:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java?rev=1520793&r1=1520792&r2=1520793&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java (original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/beanutils/BeanHelper.java Sat Sep 7 19:42:58 2013
@@ -40,7 +40,7 @@ import org.apache.commons.lang3.ClassUti
* files.
* </p>
* <p>
- * This class provides static utility methods related to bean creation
+ * This class provides utility methods related to bean creation
* operations. These methods simplify such operations because a client need not
* deal with all involved interfaces. Usually, if a bean declaration has already
* been obtained, a single method call is necessary to create a new bean
@@ -54,17 +54,23 @@ import org.apache.commons.lang3.ClassUti
* specified in the bean declaration. Then this factory will be used to create
* the bean.
* </p>
+ * <p>
+ * In order to create beans using {@code BeanHelper}, create and instance of
+ * this class and initialize it accordingly - a default {@link BeanFactory}
+ * can be passed to the constructor, and additional bean factories can be
+ * registered (see above). Then this instance can be used to create beans from
+ * {@link BeanDeclaration} objects. {@code BeanHelper} is thread-safe. So an
+ * instance can be passed around in an application and shared between multiple
+ * components.
+ * </p>
*
* @since 1.3
- * @author <a
- * href="http://commons.apache.org/configuration/team-list.html">Commons
- * Configuration team</a>
* @version $Id$
*/
public final class BeanHelper
{
/** Stores a map with the registered bean factories. */
- private static final Map<String, BeanFactory> BEAN_FACTORIES = Collections
+ private final Map<String, BeanFactory> BEAN_FACTORIES = Collections
.synchronizedMap(new HashMap<String, BeanFactory>());
/** A format string for generating error messages for constructor matching. */
@@ -72,16 +78,31 @@ public final class BeanHelper
"%s! Bean class = %s, constructor arguments = %s";
/**
- * Stores the default bean factory, which will be used if no other factory
- * is provided.
+ * Stores the default bean factory, which is used if no other factory
+ * is provided in a bean declaration.
+ */
+ private final BeanFactory defaultBeanFactory;
+
+ /**
+ * Creates a new instance of {@code BeanHelper} with the default instance of
+ * {@link DefaultBeanFactory} as default {@link BeanFactory}.
*/
- private static BeanFactory defaultBeanFactory = DefaultBeanFactory.INSTANCE;
+ public BeanHelper()
+ {
+ this(null);
+ }
/**
- * Private constructor, so no instances can be created.
+ * Creates a new instance of {@code BeanHelper} and sets the specified
+ * default {@code BeanFactory}.
+ *
+ * @param defFactory the default {@code BeanFactory} (can be <b>null</b>,
+ * then a default instance is used)
*/
- private BeanHelper()
+ public BeanHelper(BeanFactory defFactory)
{
+ defaultBeanFactory =
+ (defFactory != null) ? defFactory : DefaultBeanFactory.INSTANCE;
}
/**
@@ -93,7 +114,7 @@ public final class BeanHelper
* @param name the name of the factory
* @param factory the factory to be registered
*/
- public static void registerBeanFactory(String name, BeanFactory factory)
+ public void registerBeanFactory(String name, BeanFactory factory)
{
if (name == null)
{
@@ -116,7 +137,7 @@ public final class BeanHelper
* @return the factory that was registered under this name; <b>null</b> if
* there was no such factory
*/
- public static BeanFactory deregisterBeanFactory(String name)
+ public BeanFactory deregisterBeanFactory(String name)
{
return BEAN_FACTORIES.remove(name);
}
@@ -126,7 +147,7 @@ public final class BeanHelper
*
* @return a set with the names of the registered bean factories
*/
- public static Set<String> registeredFactoryNames()
+ public Set<String> registeredFactoryNames()
{
return BEAN_FACTORIES.keySet();
}
@@ -136,29 +157,12 @@ public final class BeanHelper
*
* @return the default bean factory
*/
- public static BeanFactory getDefaultBeanFactory()
+ public BeanFactory getDefaultBeanFactory()
{
return defaultBeanFactory;
}
/**
- * Sets the default bean factory. This factory will be used for all create
- * operations, for which no special factory is provided in the bean
- * declaration.
- *
- * @param factory the default bean factory (must not be <b>null</b>)
- */
- public static void setDefaultBeanFactory(BeanFactory factory)
- {
- if (factory == null)
- {
- throw new IllegalArgumentException(
- "Default bean factory must not be null!");
- }
- defaultBeanFactory = factory;
- }
-
- /**
* Initializes the passed in bean. This method will obtain all the bean's
* properties that are defined in the passed in bean declaration. These
* properties will be set on the bean. If necessary, further beans will be
@@ -168,7 +172,7 @@ public final class BeanHelper
* @param data the bean declaration
* @throws ConfigurationRuntimeException if a property cannot be set
*/
- public static void initBean(Object bean, BeanDeclaration data)
+ public void initBean(Object bean, BeanDeclaration data)
throws ConfigurationRuntimeException
{
initBeanProperties(bean, data);
@@ -381,7 +385,7 @@ public final class BeanHelper
* @return the new bean
* @throws ConfigurationRuntimeException if an error occurs
*/
- public static Object createBean(BeanDeclaration data, Class<?> defaultClass,
+ public Object createBean(BeanDeclaration data, Class<?> defaultClass,
Object param) throws ConfigurationRuntimeException
{
if (data == null)
@@ -413,7 +417,7 @@ public final class BeanHelper
* @return the new bean
* @throws ConfigurationRuntimeException if an error occurs
*/
- public static Object createBean(BeanDeclaration data, Class<?> defaultClass)
+ public Object createBean(BeanDeclaration data, Class<?> defaultClass)
throws ConfigurationRuntimeException
{
return createBean(data, defaultClass, null);
@@ -427,7 +431,7 @@ public final class BeanHelper
* @return the new bean
* @throws ConfigurationRuntimeException if an error occurs
*/
- public static Object createBean(BeanDeclaration data)
+ public Object createBean(BeanDeclaration data)
throws ConfigurationRuntimeException
{
return createBean(data, null);
@@ -526,7 +530,7 @@ public final class BeanHelper
* @return the bean factory to use
* @throws ConfigurationRuntimeException if the factory cannot be determined
*/
- private static BeanFactory fetchBeanFactory(BeanDeclaration data)
+ private BeanFactory fetchBeanFactory(BeanDeclaration data)
throws ConfigurationRuntimeException
{
String factoryName = data.getBeanFactoryName();
@@ -563,38 +567,12 @@ public final class BeanHelper
* @throws ConfigurationRuntimeException if the bean class cannot be
* determined
*/
- private static BeanCreationContext createBeanCreationContext(
+ private BeanCreationContext createBeanCreationContext(
final BeanDeclaration data, Class<?> defaultClass,
final Object param, BeanFactory factory)
{
final Class<?> beanClass = fetchBeanClass(data, defaultClass, factory);
- return new BeanCreationContext()
- {
- public void initBean(Object bean, BeanDeclaration data)
- {
- BeanHelper.initBean(bean, data);
- }
-
- public Object getParameter()
- {
- return param;
- }
-
- public BeanDeclaration getBeanDeclaration()
- {
- return data;
- }
-
- public Class<?> getBeanClass()
- {
- return beanClass;
- }
-
- public Object createBean(BeanDeclaration data)
- {
- return BeanHelper.createBean(data);
- }
- };
+ return new BeanCreationContextImpl(this, beanClass, data, param);
}
/**
@@ -712,4 +690,61 @@ public final class BeanHelper
return new ConfigurationRuntimeException(String.format(FMT_CTOR_ERROR,
msg, beanClass.getName(), getConstructorArgs(data).toString()));
}
+
+ /**
+ * An implementation of the {@code BeanCreationContext} interface used by
+ * {@code BeanHelper} to communicate with a {@code BeanFactory}. This class
+ * contains all information required for the creation of a bean. The methods
+ * for creating and initializing bean instances are implemented by calling
+ * back to the provided {@code BeanHelper} instance (which is the instance
+ * that created this object).
+ */
+ private static class BeanCreationContextImpl implements BeanCreationContext
+ {
+ /** The association BeanHelper instance. */
+ private final BeanHelper beanHelper;
+
+ /** The class of the bean to be created. */
+ private final Class<?> beanClass;
+
+ /** The underlying bean declaration. */
+ private final BeanDeclaration data;
+
+ /** The parameter for the bean factory. */
+ private final Object param;
+
+ private BeanCreationContextImpl(BeanHelper helper, Class<?> beanClass,
+ BeanDeclaration data, Object param)
+ {
+ beanHelper = helper;
+ this.beanClass = beanClass;
+ this.param = param;
+ this.data = data;
+ }
+
+ public void initBean(Object bean, BeanDeclaration data)
+ {
+ beanHelper.initBean(bean, data);
+ }
+
+ public Object getParameter()
+ {
+ return param;
+ }
+
+ public BeanDeclaration getBeanDeclaration()
+ {
+ return data;
+ }
+
+ public Class<?> getBeanClass()
+ {
+ return beanClass;
+ }
+
+ public Object createBean(BeanDeclaration data)
+ {
+ return beanHelper.createBean(data);
+ }
+ }
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java?rev=1520793&r1=1520792&r2=1520793&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestBeanHelper.java Sat Sep 7 19:42:58 2013
@@ -31,7 +31,7 @@ import java.util.List;
import java.util.Map;
import org.apache.commons.configuration.ConfigurationRuntimeException;
-import org.junit.After;
+import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
@@ -55,38 +55,48 @@ public class TestBeanHelper
/** Constant for the name of the test bean factory. */
private static final String TEST_FACTORY = "testFactory";
- /**
- * Stores the default bean factory. Because this is a static field in
- * BeanHelper it is temporarily stored and reset after the tests.
- */
- private BeanFactory tempDefaultBeanFactory;
+ /** The test bean helper instance. */
+ private BeanHelper helper;
@Before
public void setUp() throws Exception
{
- tempDefaultBeanFactory = BeanHelper.getDefaultBeanFactory();
- deregisterFactories();
+ helper = new BeanHelper(new TestBeanFactory());
}
- @After
- public void tearDown() throws Exception
+ /**
+ * Tests whether the correct default bean factory is set.
+ */
+ @Test
+ public void testDefaultBeanFactory()
{
- deregisterFactories();
+ helper = new BeanHelper();
+ assertSame("Wrong default bean factory", DefaultBeanFactory.INSTANCE,
+ helper.getDefaultBeanFactory());
+ }
- // Reset old default bean factory
- BeanHelper.setDefaultBeanFactory(tempDefaultBeanFactory);
+ /**
+ * Tests whether a specific default bean factory can be set when
+ * constructing an instance.
+ */
+ @Test
+ public void testInitWithBeanFactory()
+ {
+ BeanFactory factory = EasyMock.createMock(BeanFactory.class);
+ EasyMock.replay(factory);
+ helper = new BeanHelper(factory);
+ assertSame("Wrong default bean factory", factory,
+ helper.getDefaultBeanFactory());
}
/**
- * Removes all bean factories that might have been registered during a test.
+ * Tests that a newly created instance does not have any bean factories
+ * registered.
*/
- private void deregisterFactories()
+ @Test
+ public void testRegisteredFactoriesEmptyForNewInstance()
{
- for (String name : BeanHelper.registeredFactoryNames())
- {
- BeanHelper.deregisterBeanFactory(name);
- }
- assertTrue("Remaining registered bean factories", BeanHelper
+ assertTrue("List of registered factories is not empty", helper
.registeredFactoryNames().isEmpty());
}
@@ -96,12 +106,10 @@ public class TestBeanHelper
@Test
public void testRegisterBeanFactory()
{
- assertTrue("List of registered factories is not empty", BeanHelper
- .registeredFactoryNames().isEmpty());
- BeanHelper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
- assertEquals("Wrong number of registered factories", 1, BeanHelper
+ helper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
+ assertEquals("Wrong number of registered factories", 1, helper
.registeredFactoryNames().size());
- assertTrue("Test factory is not contained", BeanHelper
+ assertTrue("Test factory is not contained", helper
.registeredFactoryNames().contains(TEST_FACTORY));
}
@@ -111,7 +119,7 @@ public class TestBeanHelper
@Test(expected = IllegalArgumentException.class)
public void testRegisterBeanFactoryNull()
{
- BeanHelper.registerBeanFactory(TEST_FACTORY, null);
+ helper.registerBeanFactory(TEST_FACTORY, null);
}
/**
@@ -121,7 +129,7 @@ public class TestBeanHelper
@Test(expected = IllegalArgumentException.class)
public void testRegisterBeanFactoryNullName()
{
- BeanHelper.registerBeanFactory(null, new TestBeanFactory());
+ helper.registerBeanFactory(null, new TestBeanFactory());
}
/**
@@ -130,36 +138,31 @@ public class TestBeanHelper
@Test
public void testDeregisterBeanFactory()
{
- assertNull("deregistering non existing factory", BeanHelper
- .deregisterBeanFactory(TEST_FACTORY));
- assertNull("deregistering null factory", BeanHelper
- .deregisterBeanFactory(null));
BeanFactory factory = new TestBeanFactory();
- BeanHelper.registerBeanFactory(TEST_FACTORY, factory);
- assertSame("Could not deregister factory", factory, BeanHelper
+ helper.registerBeanFactory(TEST_FACTORY, factory);
+ assertSame("Could not deregister factory", factory, helper
.deregisterBeanFactory(TEST_FACTORY));
- assertTrue("List of factories is not empty", BeanHelper
+ assertTrue("List of factories is not empty", helper
.registeredFactoryNames().isEmpty());
}
/**
- * Tests whether the default bean factory is correctly initialized.
+ * Tests deregisterBeanFactory() for a non-existing factory name.
*/
@Test
- public void testGetDefaultBeanFactory()
+ public void testDeregisterBeanFactoryNonExisting()
{
- assertSame("Incorrect default bean factory",
- DefaultBeanFactory.INSTANCE, tempDefaultBeanFactory);
+ assertNull("deregistering non existing factory",
+ helper.deregisterBeanFactory(TEST_FACTORY));
}
/**
- * Tests setting the default bean factory to null. This should caus an
- * exception.
+ * Tests deregisterBeanFactory() for a null factory name.
*/
- @Test(expected = IllegalArgumentException.class)
- public void testSetDefaultBeanFactoryNull()
- {
- BeanHelper.setDefaultBeanFactory(null);
+ @Test
+ public void testDeregisterBeanFactoryNull() {
+ assertNull("deregistering null factory",
+ helper.deregisterBeanFactory(null));
}
/**
@@ -168,10 +171,9 @@ public class TestBeanHelper
@Test
public void testInitBean()
{
- BeanHelper.setDefaultBeanFactory(new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
BeanCreationTestBean bean = new BeanCreationTestBean();
- BeanHelper.initBean(bean, data);
+ helper.initBean(bean, data);
checkBean(bean);
}
@@ -184,7 +186,7 @@ public class TestBeanHelper
{
BeanDeclarationTestImpl data = new BeanDeclarationTestImpl();
BeanCreationTestBean bean = new BeanCreationTestBean();
- BeanHelper.initBean(bean, data);
+ helper.initBean(bean, data);
assertNull("Wrong string property", bean.getStringValue());
assertEquals("Wrong int property", 0, bean.getIntValue());
assertNull("Buddy was set", bean.getBuddy());
@@ -199,7 +201,7 @@ public class TestBeanHelper
{
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.getBeanProperties().put("nonExistingProperty", Boolean.TRUE);
- BeanHelper.initBean(new BeanCreationTestBean(), data);
+ helper.initBean(new BeanCreationTestBean(), data);
}
/**
@@ -210,12 +212,13 @@ public class TestBeanHelper
public void testCreateBean()
{
TestBeanFactory factory = new TestBeanFactory();
- BeanHelper.registerBeanFactory(TEST_FACTORY, factory);
+ helper.registerBeanFactory(TEST_FACTORY, factory);
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName(BeanCreationTestBean.class.getName());
- checkBean((BeanCreationTestBean) BeanHelper.createBean(data, null));
+ checkBean((BeanCreationTestBean) helper.createBean(data, null));
assertNull("A parameter was passed", factory.parameter);
+ assertEquals("Factory not called", 1, factory.getCreateBeanCount());
}
/**
@@ -225,12 +228,13 @@ public class TestBeanHelper
public void testCreateBeanWithListChildBean()
{
TestBeanFactory factory = new TestBeanFactory();
- BeanHelper.registerBeanFactory(TEST_FACTORY, factory);
+ helper.registerBeanFactory(TEST_FACTORY, factory);
BeanDeclarationTestImpl data = setUpBeanDeclarationWithListChild();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName(BeanCreationTestBeanWithListChild.class.getName());
- checkBean((BeanCreationTestBeanWithListChild) BeanHelper.createBean(data, null));
+ checkBean((BeanCreationTestBeanWithListChild) helper.createBean(data, null));
assertNull("A parameter was passed", factory.parameter);
+ assertEquals("Factory not called", 1, factory.getCreateBeanCount());
}
/**
@@ -240,7 +244,7 @@ public class TestBeanHelper
@Test(expected = IllegalArgumentException.class)
public void testCreateBeanWithNullDeclaration()
{
- BeanHelper.createBean(null);
+ helper.createBean(null);
}
/**
@@ -250,10 +254,10 @@ public class TestBeanHelper
@Test
public void testCreateBeanWithDefaultClass()
{
- BeanHelper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
+ helper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
- checkBean((BeanCreationTestBean) BeanHelper.createBean(data, BeanCreationTestBean.class));
+ checkBean((BeanCreationTestBean) helper.createBean(data, BeanCreationTestBean.class));
}
/**
@@ -265,23 +269,24 @@ public class TestBeanHelper
{
TestBeanFactory factory = new TestBeanFactory();
factory.supportsDefaultClass = true;
- BeanHelper.registerBeanFactory(TEST_FACTORY, factory);
+ helper.registerBeanFactory(TEST_FACTORY, factory);
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
- checkBean((BeanCreationTestBean) BeanHelper.createBean(data, null));
+ checkBean((BeanCreationTestBean) helper.createBean(data, null));
+ assertEquals("Factory not called", 1, factory.getCreateBeanCount());
}
/**
- * Tries to create a bean when no class is provided. This should cause an
+ * Tries to create a bean if no class is provided. This should cause an
* exception.
*/
@Test(expected = ConfigurationRuntimeException.class)
public void testCreateBeanWithNoClass()
{
- BeanHelper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
+ helper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
- BeanHelper.createBean(data, null);
+ helper.createBean(data, null);
}
/**
@@ -291,11 +296,11 @@ public class TestBeanHelper
@Test(expected = ConfigurationRuntimeException.class)
public void testCreateBeanWithInvalidClass()
{
- BeanHelper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
+ helper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName("non.existing.ClassName");
- BeanHelper.createBean(data, null);
+ helper.createBean(data, null);
}
/**
@@ -304,10 +309,11 @@ public class TestBeanHelper
@Test
public void testCreateBeanWithDefaultFactory()
{
- BeanHelper.setDefaultBeanFactory(new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanClassName(BeanCreationTestBean.class.getName());
- checkBean((BeanCreationTestBean) BeanHelper.createBean(data, null));
+ checkBean((BeanCreationTestBean) helper.createBean(data, null));
+ TestBeanFactory factory = (TestBeanFactory) helper.getDefaultBeanFactory();
+ assertTrue("Factory not called", factory.getCreateBeanCount() > 0);
}
/**
@@ -319,7 +325,7 @@ public class TestBeanHelper
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName(BeanCreationTestBean.class.getName());
- BeanHelper.createBean(data, null);
+ helper.createBean(data, null);
}
/**
@@ -328,11 +334,11 @@ public class TestBeanHelper
@Test(expected = ConfigurationRuntimeException.class)
public void testCreateBeanWithException()
{
- BeanHelper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
+ helper.registerBeanFactory(TEST_FACTORY, new TestBeanFactory());
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName(getClass().getName());
- BeanHelper.createBean(data, null);
+ helper.createBean(data, null);
}
/**
@@ -343,11 +349,11 @@ public class TestBeanHelper
{
Object param = new Integer(42);
TestBeanFactory factory = new TestBeanFactory();
- BeanHelper.registerBeanFactory(TEST_FACTORY, factory);
+ helper.registerBeanFactory(TEST_FACTORY, factory);
BeanDeclarationTestImpl data = setUpBeanDeclaration();
data.setBeanFactoryName(TEST_FACTORY);
data.setBeanClassName(BeanCreationTestBean.class.getName());
- checkBean((BeanCreationTestBean) BeanHelper.createBean(data, null, param));
+ checkBean((BeanCreationTestBean) helper.createBean(data, null, param));
assertSame("Wrong parameter", param, factory.parameter);
}
@@ -461,10 +467,6 @@ public class TestBeanHelper
properties2.put("intValue", new Integer(100));
buddyData.setBeanProperties(properties2);
buddyData.setBeanClassName(BeanCreationTestBean.class.getName());
- if (BeanHelper.getDefaultBeanFactory() == null)
- {
- buddyData.setBeanFactoryName(TEST_FACTORY);
- }
Map<String, Object> nested = new HashMap<String, Object>();
nested.put("buddy", buddyData);
@@ -511,10 +513,6 @@ public class TestBeanHelper
properties2.put("intValue", new Integer(100));
childBean.setBeanProperties(properties2);
childBean.setBeanClassName(BeanCreationTestBean.class.getName());
- if (BeanHelper.getDefaultBeanFactory() == null)
- {
- childBean.setBeanFactoryName(TEST_FACTORY);
- }
return childBean;
}
@@ -560,25 +558,31 @@ public class TestBeanHelper
* implementation is really simple: If the BeanCreationTestBean class is provided, a new
* instance will be created. Otherwise an exception is thrown.
*/
- static class TestBeanFactory implements BeanFactory
+ private class TestBeanFactory implements BeanFactory
{
Object parameter;
boolean supportsDefaultClass;
+ /** A counter for the created instances. */
+ private int createBeanCount;
+
public Object createBean(BeanCreationContext bcc) throws Exception
{
+ createBeanCount++;
parameter = bcc.getParameter();
if (BeanCreationTestBean.class.equals(bcc.getBeanClass()))
{
BeanCreationTestBean bean = new BeanCreationTestBean();
- BeanHelper.initBean(bean, bcc.getBeanDeclaration());
+ helper.initBean(bean, bcc.getBeanDeclaration());
return bean;
}
- else if (BeanCreationTestBeanWithListChild.class.equals(bcc.getBeanClass()))
+ else if (BeanCreationTestBeanWithListChild.class.equals(bcc
+ .getBeanClass()))
{
- BeanCreationTestBeanWithListChild bean = new BeanCreationTestBeanWithListChild();
- BeanHelper.initBean(bean, bcc.getBeanDeclaration());
+ BeanCreationTestBeanWithListChild bean =
+ new BeanCreationTestBeanWithListChild();
+ helper.initBean(bean, bcc.getBeanDeclaration());
return bean;
}
else
@@ -596,5 +600,15 @@ public class TestBeanHelper
{
return supportsDefaultClass ? BeanCreationTestBean.class : null;
}
+
+ /**
+ * Returns the number of beans created via this factory.
+ *
+ * @return the number of created beans
+ */
+ public int getCreateBeanCount()
+ {
+ return createBeanCount;
+ }
}
}
Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java?rev=1520793&r1=1520792&r2=1520793&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java (original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/beanutils/TestDefaultBeanFactory.java Sat Sep 7 19:42:58 2013
@@ -67,9 +67,11 @@ public class TestDefaultBeanFactory
{
return new BeanCreationContext()
{
+ private final BeanHelper beanHelper = new BeanHelper();
+
public void initBean(Object bean, BeanDeclaration data)
{
- BeanHelper.initBean(bean, data);
+ beanHelper.initBean(bean, data);
}
public Object getParameter()
@@ -89,7 +91,7 @@ public class TestDefaultBeanFactory
public Object createBean(BeanDeclaration data)
{
- return BeanHelper.createBean(data);
+ return beanHelper.createBean(data);
}
};
}