Moved tests over from testsuite to beans

This commit is contained in:
Arjen Poutsma
2008-10-29 17:32:01 +00:00
parent 6b80dbb4a9
commit bd1e259b4a
23 changed files with 41 additions and 1 deletions

View File

@@ -0,0 +1,28 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
/**
* Used in the tests for the FieldRetrievingFactoryBean class
* (c.f. FieldRetrievingFactoryBeanTests)
*
* @author Rick Evans
*/
class PackageLevelVisibleBean {
public static final String CONSTANT = "Wuby";
}

View File

@@ -0,0 +1,171 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import junit.framework.TestCase;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.beans.propertyeditors.CustomDateEditor;
/**
* @author Juergen Hoeller
* @since 31.07.2004
*/
public class CustomEditorConfigurerTests extends TestCase {
public void testCustomEditorConfigurerWithRequiredTypeAsClassName() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
editors.put(Date.class.getName(), new CustomDateEditor(df, true));
cec.setCustomEditors(editors);
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("date", "2.12.1975");
bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class, pvs));
pvs = new MutablePropertyValues();
pvs.addPropertyValue("someMap[myKey]", new TypedStringValue("2.12.1975", Date.class));
bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb1 = (TestBean) bf.getBean("tb1");
assertEquals(df.parse("2.12.1975"), tb1.getDate());
TestBean tb2 = (TestBean) bf.getBean("tb2");
assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey"));
}
public void testCustomEditorConfigurerWithRequiredTypeAsClass() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
editors.put(Date.class, new CustomDateEditor(df, true));
cec.setCustomEditors(editors);
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("date", "2.12.1975");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb = (TestBean) bf.getBean("tb");
assertEquals(df.parse("2.12.1975"), tb.getDate());
}
public void testCustomEditorConfigurerWithEditorAsClassName() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
editors.put(Date.class, MyDateEditor.class.getName());
cec.setCustomEditors(editors);
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("date", "2.12.1975");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb = (TestBean) bf.getBean("tb");
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
assertEquals(df.parse("2.12.1975"), tb.getDate());
}
public void testCustomEditorConfigurerWithEditorAsClass() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
editors.put(Date.class, MyDateEditor.class);
cec.setCustomEditors(editors);
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("date", "2.12.1975");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb = (TestBean) bf.getBean("tb");
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
assertEquals(df.parse("2.12.1975"), tb.getDate());
}
public void testCustomEditorConfigurerWithRequiredTypeArray() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
editors.put("java.lang.String[]", new PropertyEditorSupport() {
public void setAsText(String text) {
setValue(new String[] {"test"});
}
});
cec.setCustomEditors(editors);
cec.postProcessBeanFactory(bf);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue("stringArray", "xxx");
bf.registerBeanDefinition("tb", new RootBeanDefinition(TestBean.class, pvs));
TestBean tb = (TestBean) bf.getBean("tb");
assertTrue(tb.getStringArray() != null && tb.getStringArray().length == 1);
assertEquals("test", tb.getStringArray()[0]);
}
public void testCustomEditorConfigurerWithUnresolvableEditor() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
editors.put(Date.class, "MyNonExistingEditor");
editors.put("MyNonExistingType", "MyNonExistingEditor");
cec.setCustomEditors(editors);
try {
cec.postProcessBeanFactory(bf);
fail("Should have thrown FatalBeanException");
}
catch (FatalBeanException ex) {
assertTrue(ex.getCause() instanceof ClassNotFoundException);
}
}
public void testCustomEditorConfigurerWithIgnoredUnresolvableEditor() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer();
Map editors = new HashMap();
editors.put(Date.class, "MyNonExistingEditor");
editors.put("MyNonExistingType", "MyNonExistingEditor");
cec.setCustomEditors(editors);
cec.setIgnoreUnresolvableEditors(true);
cec.postProcessBeanFactory(bf);
}
public static class MyDateEditor extends CustomDateEditor {
public MyDateEditor() {
super(DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN), true);
}
}
}

View File

@@ -0,0 +1,123 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.sql.Connection;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
/**
* @author Juergen Hoeller
* @since 31.07.2004
*/
public class FieldRetrievingFactoryBeanTests extends TestCase {
public void testStaticField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE");
fr.afterPropertiesSet();
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
}
public void testStaticFieldWithWhitespace() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE ");
fr.afterPropertiesSet();
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
}
public void testStaticFieldViaClassAndFieldName() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetClass(Connection.class);
fr.setTargetField("TRANSACTION_SERIALIZABLE");
fr.afterPropertiesSet();
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
}
public void testNonStaticField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
PublicFieldHolder target = new PublicFieldHolder();
fr.setTargetObject(target);
fr.setTargetField("publicField");
fr.afterPropertiesSet();
assertEquals(target.publicField, fr.getObject());
}
public void testNothingButBeanName() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE");
fr.afterPropertiesSet();
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
}
public void testJustTargetField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetField("TRANSACTION_SERIALIZABLE");
try {
fr.afterPropertiesSet();
}
catch (IllegalArgumentException expected) {
}
}
public void testJustTargetClass() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetClass(Connection.class);
try {
fr.afterPropertiesSet();
}
catch (IllegalArgumentException expected) {
}
}
public void testJustTargetObject() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetObject(new PublicFieldHolder());
try {
fr.afterPropertiesSet();
}
catch (IllegalArgumentException expected) {
}
}
public void testWithConstantOnClassWithPackageLevelVisibility() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setBeanName("org.springframework.beans.factory.PackageLevelVisibleBean.CONSTANT");
fr.afterPropertiesSet();
assertEquals("Wuby", fr.getObject());
}
public void testBeanNameSyntaxWithBeanFactory() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("fieldRetrieving.xml", getClass()));
TestBean testBean = (TestBean) bf.getBean("testBean");
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]);
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]);
}
private static class PublicFieldHolder {
public String publicField = "test";
}
}

View File

@@ -0,0 +1,127 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.util.Date;
import junit.framework.TestCase;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.easymock.MockControl;
/**
* Unit tests for the ObjectFactoryCreatingFactoryBean class.
*
* @author Colin Sampaleanu
* @author Rick Evans
* @since 2004-05-11
*/
public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase {
private BeanFactory beanFactory;
protected void setUp() throws Exception {
this.beanFactory = new XmlBeanFactory(new ClassPathResource(
"ObjectFactoryCreatingFactoryBeanTests.xml", getClass()));
}
public void testBasicOperation() throws BeansException {
TestBean testBean = (TestBean) beanFactory.getBean("testBean");
ObjectFactory objectFactory = testBean.getObjectFactory();
Date date1 = (Date) objectFactory.getObject();
Date date2 = (Date) objectFactory.getObject();
assertTrue(date1 != date2);
}
public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exception {
final String targetBeanName = "singleton";
final String expectedSingleton = "Alicia Keys";
MockControl mock = MockControl.createControl(BeanFactory.class);
BeanFactory beanFactory = (BeanFactory) mock.getMock();
beanFactory.getBean(targetBeanName);
mock.setReturnValue(expectedSingleton);
mock.replay();
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
factory.setTargetBeanName(targetBeanName);
factory.setBeanFactory(beanFactory);
factory.afterPropertiesSet();
ObjectFactory objectFactory = (ObjectFactory) factory.getObject();
Object actualSingleton = objectFactory.getObject();
assertSame(expectedSingleton, actualSingleton);
mock.verify();
}
public void testWhenTargetBeanNameIsNull() throws Exception {
try {
new ObjectFactoryCreatingFactoryBean().afterPropertiesSet();
fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property not set.");
}
catch (IllegalArgumentException expected) {}
}
public void testWhenTargetBeanNameIsEmptyString() throws Exception {
try {
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
factory.setTargetBeanName("");
factory.afterPropertiesSet();
fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property set to (invalid) empty string.");
}
catch (IllegalArgumentException expected) {}
}
public void testWhenTargetBeanNameIsWhitespacedString() throws Exception {
try {
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
factory.setTargetBeanName(" \t");
factory.afterPropertiesSet();
fail("Must have thrown an IllegalArgumentException; 'targetBeanName' property set to (invalid) only-whitespace string.");
}
catch (IllegalArgumentException expected) {}
}
public void testEnsureOFBFBReportsThatItActuallyCreatesObjectFactoryInstances() throws Exception {
assertEquals("Must be reporting that it creates ObjectFactory instances (as per class contract).",
ObjectFactory.class, new ObjectFactoryCreatingFactoryBean().getObjectType());
}
public static class TestBean {
public ObjectFactory objectFactory;
public ObjectFactory getObjectFactory() {
return objectFactory;
}
public void setObjectFactory(ObjectFactory objectFactory) {
this.objectFactory = objectFactory;
}
}
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.core.JdkVersion;
import org.springframework.core.io.ClassPathResource;
/**
* @author Juergen Hoeller
* @since 01.11.2003
*/
public class PropertiesFactoryBeanTests extends TestCase {
public void testWithPropertiesFile() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
}
public void testWithPropertiesXmlFile() throws Exception {
// ignore for JDK < 1.5
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
return;
}
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test-properties.xml"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
}
public void testWithLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPropertiesFileAndLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties props1 = new Properties();
props1.setProperty("key2", "value2");
props1.setProperty("tb.array[0].age", "0");
Properties props2 = new Properties();
props2.setProperty("spring", "framework");
props2.setProperty("Don", "Mattingly");
Properties props3 = new Properties();
props3.setProperty("spider", "man");
props3.setProperty("bat", "man");
pfb.setPropertiesArray(new Properties[] {props1, props2, props3});
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
assertEquals("framework", props.getProperty("spring"));
assertEquals("Mattingly", props.getProperty("Don"));
assertEquals("man", props.getProperty("spider"));
assertEquals("man", props.getProperty("bat"));
}
public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.setLocalOverride(true);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("0", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPrototype() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setSingleton(false);
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
Properties newProps = (Properties) pfb.getObject();
assertTrue(props != newProps);
assertEquals("99", newProps.getProperty("tb.array[0].age"));
assertEquals("value2", newProps.getProperty("key2"));
}
}

View File

@@ -0,0 +1,78 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import junit.framework.TestCase;
import org.springframework.beans.ITestBean;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author Juergen Hoeller
* @since 04.10.2004
*/
public class PropertyPathFactoryBeanTests extends TestCase {
public void testPropertyPathFactoryBeanWithSingletonResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass()));
assertEquals(new Integer(12), xbf.getBean("propertyPath1"));
assertEquals(new Integer(11), xbf.getBean("propertyPath2"));
assertEquals(new Integer(10), xbf.getBean("tb.age"));
assertEquals(ITestBean.class, xbf.getType("otb.spouse"));
Object result1 = xbf.getBean("otb.spouse");
Object result2 = xbf.getBean("otb.spouse");
assertTrue(result1 instanceof TestBean);
assertTrue(result1 == result2);
assertEquals(99, ((TestBean) result1).getAge());
}
public void testPropertyPathFactoryBeanWithPrototypeResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass()));
assertNull(xbf.getType("tb.spouse"));
assertEquals(TestBean.class, xbf.getType("propertyPath3"));
Object result1 = xbf.getBean("tb.spouse");
Object result2 = xbf.getBean("propertyPath3");
Object result3 = xbf.getBean("propertyPath3");
assertTrue(result1 instanceof TestBean);
assertTrue(result2 instanceof TestBean);
assertTrue(result3 instanceof TestBean);
assertEquals(11, ((TestBean) result1).getAge());
assertEquals(11, ((TestBean) result2).getAge());
assertEquals(11, ((TestBean) result3).getAge());
assertTrue(result1 != result2);
assertTrue(result1 != result3);
assertTrue(result2 != result3);
}
public void testPropertyPathFactoryBeanWithNullResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass()));
assertNull(xbf.getType("tb.spouse.spouse"));
assertNull(xbf.getBean("tb.spouse.spouse"));
}
public void testPropertyPathFactoryBeanAsInnerBean() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass()));
TestBean spouse = (TestBean) xbf.getBean("otb.spouse");
TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner");
assertSame(spouse, tbWithInner.getSpouse());
assertTrue(!tbWithInner.getFriends().isEmpty());
assertSame(spouse, tbWithInner.getFriends().iterator().next());
}
}

View File

@@ -0,0 +1,69 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.util.Arrays;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
/**
* @author Rod Johnson
* @author Juergen Hoeller
*/
public class BeanDefinitionBuilderTests extends TestCase {
public void testBeanClassWithSimpleProperty() {
String[] dependsOn = new String[] { "A", "B", "C" };
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
bdb.setSingleton(false).addPropertyReference("age", "15");
for (int i = 0; i < dependsOn.length; i++) {
bdb.addDependsOn(dependsOn[i]);
}
RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
assertFalse(rbd.isSingleton());
assertEquals(TestBean.class, rbd.getBeanClass());
assertTrue("Depends on was added", Arrays.equals(dependsOn, rbd.getDependsOn()));
assertTrue(rbd.getPropertyValues().contains("age"));
}
public void testBeanClassWithFactoryMethod() {
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class, "create");
RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
assertTrue(rbd.hasBeanClass());
assertEquals(TestBean.class, rbd.getBeanClass());
assertEquals("create", rbd.getFactoryMethodName());
}
public void testBeanClassName() {
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class.getName());
RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
assertFalse(rbd.hasBeanClass());
assertEquals(TestBean.class.getName(), rbd.getBeanClassName());
}
public void testBeanClassNameWithFactoryMethod() {
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class.getName(), "create");
RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
assertFalse(rbd.hasBeanClass());
assertEquals(TestBean.class.getName(), rbd.getBeanClassName());
assertEquals("create", rbd.getFactoryMethodName());
}
}

View File

@@ -0,0 +1,134 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
/**
* @author Juergen Hoeller
*/
public class BeanDefinitionTests extends TestCase {
public void testBeanDefinitionEquality() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAbstract(true);
bd.setLazyInit(true);
bd.setScope("request");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
otherBd.setScope("request");
assertTrue(bd.equals(otherBd));
assertTrue(otherBd.equals(bd));
assertTrue(bd.hashCode() == otherBd.hashCode());
}
public void testBeanDefinitionEqualityWithPropertyValues() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.getPropertyValues().addPropertyValue("name", "myName");
bd.getPropertyValues().addPropertyValue("age", "99");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getPropertyValues().addPropertyValue("name", "myName");
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getPropertyValues().addPropertyValue("age", "11");
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getPropertyValues().addPropertyValue("age", "99");
assertTrue(bd.equals(otherBd));
assertTrue(otherBd.equals(bd));
assertTrue(bd.hashCode() == otherBd.hashCode());
}
public void testBeanDefinitionEqualityWithConstructorArguments() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.getConstructorArgumentValues().addGenericArgumentValue("test");
bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5));
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getConstructorArgumentValues().addGenericArgumentValue("test");
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(9));
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5));
assertTrue(bd.equals(otherBd));
assertTrue(otherBd.equals(bd));
assertTrue(bd.hashCode() == otherBd.hashCode());
}
public void testBeanDefinitionEqualityWithTypedConstructorArguments() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.getConstructorArgumentValues().addGenericArgumentValue("test", "int");
bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "long");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
otherBd.getConstructorArgumentValues().addGenericArgumentValue("test", "int");
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5));
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "int");
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5), "long");
assertTrue(bd.equals(otherBd));
assertTrue(otherBd.equals(bd));
assertTrue(bd.hashCode() == otherBd.hashCode());
}
public void testBeanDefinitionHolderEquality() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setAbstract(true);
bd.setLazyInit(true);
bd.setScope("request");
BeanDefinitionHolder holder = new BeanDefinitionHolder(bd, "bd");
RootBeanDefinition otherBd = new RootBeanDefinition(TestBean.class);
assertTrue(!bd.equals(otherBd));
assertTrue(!otherBd.equals(bd));
otherBd.setAbstract(true);
otherBd.setLazyInit(true);
otherBd.setScope("request");
BeanDefinitionHolder otherHolder = new BeanDefinitionHolder(bd, "bd");
assertTrue(holder.equals(otherHolder));
assertTrue(otherHolder.equals(holder));
assertTrue(holder.hashCode() == otherHolder.hashCode());
}
public void testBeanDefinitionMerging() {
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.getConstructorArgumentValues().addGenericArgumentValue("test");
bd.getConstructorArgumentValues().addIndexedArgumentValue(1, new Integer(5));
bd.getPropertyValues().addPropertyValue("name", "myName");
bd.getPropertyValues().addPropertyValue("age", "99");
ChildBeanDefinition childBd = new ChildBeanDefinition("bd");
RootBeanDefinition mergedBd = new RootBeanDefinition(bd);
mergedBd.overrideFrom(childBd);
assertEquals(2, mergedBd.getConstructorArgumentValues().getArgumentCount());
assertEquals(2, mergedBd.getPropertyValues().size());
assertEquals(bd, mergedBd);
mergedBd.getConstructorArgumentValues().getArgumentValue(1, null).setValue(new Integer(9));
assertEquals(new Integer(5), bd.getConstructorArgumentValues().getArgumentValue(1, null).getValue());
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.RuntimeBeanReference;
/**
* @author Rob Harrop
*/
public class DefinitionMetadataEqualsHashCodeTests extends TestCase {
public void testRootBeanDefinitionEqualsAndHashCode() throws Exception {
RootBeanDefinition master = new RootBeanDefinition(TestBean.class);
RootBeanDefinition equal = new RootBeanDefinition(TestBean.class);
RootBeanDefinition notEqual = new RootBeanDefinition(String.class);
RootBeanDefinition subclass = new RootBeanDefinition(TestBean.class) {};
setBaseProperties(master);
setBaseProperties(equal);
setBaseProperties(notEqual);
setBaseProperties(subclass);
assertEqualsContract(master, equal, notEqual, subclass);
assertEquals("Hash code for equal instances should match", master.hashCode(), equal.hashCode());
}
public void testChildBeanDefinitionEqualsAndHashCode() throws Exception {
ChildBeanDefinition master = new ChildBeanDefinition("foo");
ChildBeanDefinition equal = new ChildBeanDefinition("foo");
ChildBeanDefinition notEqual = new ChildBeanDefinition("bar");
ChildBeanDefinition subclass = new ChildBeanDefinition("foo"){};
setBaseProperties(master);
setBaseProperties(equal);
setBaseProperties(notEqual);
setBaseProperties(subclass);
assertEqualsContract(master, equal, notEqual, subclass);
assertEquals("Hash code for equal instances should match", master.hashCode(), equal.hashCode());
}
public void testRuntimeBeanReference() throws Exception {
RuntimeBeanReference master = new RuntimeBeanReference("name");
RuntimeBeanReference equal = new RuntimeBeanReference("name");
RuntimeBeanReference notEqual = new RuntimeBeanReference("someOtherName");
RuntimeBeanReference subclass = new RuntimeBeanReference("name"){};
assertEqualsContract(master, equal, notEqual, subclass);
}
private void setBaseProperties(AbstractBeanDefinition definition) {
definition.setAbstract(true);
definition.setAttribute("foo", "bar");
definition.setAutowireCandidate(false);
definition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE);
//definition.getConstructorArgumentValues().addGenericArgumentValue("foo");
definition.setDependencyCheck(AbstractBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
definition.setDependsOn(new String[]{"foo", "bar"});
definition.setDestroyMethodName("destroy");
definition.setEnforceDestroyMethod(false);
definition.setEnforceInitMethod(true);
definition.setFactoryBeanName("factoryBean");
definition.setFactoryMethodName("factoryMethod");
definition.setInitMethodName("init");
definition.setLazyInit(true);
definition.getMethodOverrides().addOverride(new LookupOverride("foo", "bar"));
definition.getMethodOverrides().addOverride(new ReplaceOverride("foo", "bar"));
definition.getPropertyValues().addPropertyValue("foo", "bar");
definition.setResourceDescription("desc");
definition.setRole(BeanDefinition.ROLE_APPLICATION);
definition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
definition.setSource("foo");
}
private void assertEqualsContract(Object master, Object equal, Object notEqual, Object subclass) {
assertEquals("Should be equal", master, equal);
assertFalse("Should not be equal", master.equals(notEqual));
assertEquals("Subclass should be equal", master, subclass);
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.util.List;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class ManagedListTests extends TestCase {
public void testMergeSunnyDay() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("three");
child.setMergeEnabled(true);
List mergedList = (List) child.merge(parent);
assertEquals("merge() obviously did not work.", 3, mergedList.size());
}
public void testMergeWithNullParent() {
ManagedList child = new ManagedList();
child.add("one");
child.setMergeEnabled(true);
assertSame(child, child.merge(null));
}
public void testMergeNotAllowedWhenMergeNotEnabled() {
ManagedList child = new ManagedList();
try {
child.merge(null);
fail("Must have failed by this point (cannot merge() when the mergeEnabled property is false.");
}
catch (IllegalStateException expected) {
}
}
public void testMergeWithNonCompatibleParentType() {
ManagedList child = new ManagedList();
child.add("one");
child.setMergeEnabled(true);
try {
child.merge("hello");
fail("Must have failed by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testMergeEmptyChild() {
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.setMergeEnabled(true);
List mergedList = (List) child.merge(parent);
assertEquals("merge() obviously did not work.", 2, mergedList.size());
}
public void testMergeChildValuesOverrideTheParents() {
// doesn't make a whole lotta sense in the context of a list...
ManagedList parent = new ManagedList();
parent.add("one");
parent.add("two");
ManagedList child = new ManagedList();
child.add("one");
child.setMergeEnabled(true);
List mergedList = (List) child.merge(parent);
assertEquals("merge() obviously did not work.", 3, mergedList.size());
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class ManagedMapTests extends TestCase {
public void testMergeSunnyDay() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("three", "three");
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertEquals("merge() obviously did not work.", 3, mergedMap.size());
}
public void testMergeWithNullParent() {
ManagedMap child = new ManagedMap();
child.setMergeEnabled(true);
assertSame(child, child.merge(null));
}
public void testMergeWithNonCompatibleParentType() {
ManagedMap map = new ManagedMap();
map.setMergeEnabled(true);
try {
map.merge("hello");
fail("Must have failed by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testMergeNotAllowedWhenMergeNotEnabled() {
ManagedMap map = new ManagedMap();
try {
map.merge(null);
fail("Must have failed by this point (cannot merge() when the mergeEnabled property is false.");
}
catch (IllegalStateException expected) {
}
}
public void testMergeEmptyChild() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertEquals("merge() obviously did not work.", 2, mergedMap.size());
}
public void testMergeChildValuesOverrideTheParents() {
ManagedMap parent = new ManagedMap();
parent.put("one", "one");
parent.put("two", "two");
ManagedMap child = new ManagedMap();
child.put("one", "fork");
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
// child value for 'one' must override parent value...
assertEquals("merge() obviously did not work.", 2, mergedMap.size());
assertEquals("Parent value not being overridden during merge().", "fork", mergedMap.get("one"));
}
}

View File

@@ -0,0 +1,90 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.util.Map;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class ManagedPropertiesTests extends TestCase {
public void testMergeSunnyDay() {
ManagedProperties parent = new ManagedProperties();
parent.setProperty("one", "one");
parent.setProperty("two", "two");
ManagedProperties child = new ManagedProperties();
child.setProperty("three", "three");
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertEquals("merge() obviously did not work.", 3, mergedMap.size());
}
public void testMergeWithNullParent() {
ManagedProperties child = new ManagedProperties();
child.setMergeEnabled(true);
assertSame(child, child.merge(null));
}
public void testMergeWithNonCompatibleParentType() {
ManagedProperties map = new ManagedProperties();
map.setMergeEnabled(true);
try {
map.merge("hello");
fail("Must have failed by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testMergeNotAllowedWhenMergeNotEnabled() {
ManagedProperties map = new ManagedProperties();
try {
map.merge(null);
fail("Must have failed by this point (cannot merge() when the mergeEnabled property is false.");
}
catch (IllegalStateException expected) {
}
}
public void testMergeEmptyChild() {
ManagedProperties parent = new ManagedProperties();
parent.setProperty("one", "one");
parent.setProperty("two", "two");
ManagedProperties child = new ManagedProperties();
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
assertEquals("merge() obviously did not work.", 2, mergedMap.size());
}
public void testMergeChildValuesOverrideTheParents() {
ManagedProperties parent = new ManagedProperties();
parent.setProperty("one", "one");
parent.setProperty("two", "two");
ManagedProperties child = new ManagedProperties();
child.setProperty("one", "fork");
child.setMergeEnabled(true);
Map mergedMap = (Map) child.merge(parent);
// child value for 'one' must override parent value...
assertEquals("merge() obviously did not work.", 2, mergedMap.size());
assertEquals("Parent value not being overridden during merge().", "fork", mergedMap.get("one"));
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.util.Set;
import junit.framework.TestCase;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class ManagedSetTests extends TestCase {
public void testMergeSunnyDay() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
child.add("three");
child.setMergeEnabled(true);
Set mergedSet = (Set) child.merge(parent);
assertEquals("merge() obviously did not work.", 3, mergedSet.size());
}
public void testMergeWithNullParent() {
ManagedSet child = new ManagedSet();
child.add("one");
child.setMergeEnabled(true);
assertSame(child, child.merge(null));
}
public void testMergeNotAllowedWhenMergeNotEnabled() {
ManagedSet child = new ManagedSet();
try {
child.merge(null);
fail("Must have failed by this point (cannot merge() when the mergeEnabled property is false.");
}
catch (IllegalStateException expected) {
}
}
public void testMergeWithNonCompatibleParentType() {
ManagedSet child = new ManagedSet();
child.add("one");
child.setMergeEnabled(true);
try {
child.merge("hello");
fail("Must have failed by this point.");
}
catch (IllegalArgumentException expected) {
}
}
public void testMergeEmptyChild() {
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
child.setMergeEnabled(true);
Set mergedSet = (Set) child.merge(parent);
assertEquals("merge() obviously did not work.", 2, mergedSet.size());
}
public void testMergeChildValuesOverrideTheParents() {
// asserts that the set contract is not violated during a merge() operation...
ManagedSet parent = new ManagedSet();
parent.add("one");
parent.add("two");
ManagedSet child = new ManagedSet();
child.add("one");
child.setMergeEnabled(true);
Set mergedSet = (Set) child.merge(parent);
assertEquals("merge() obviously did not work.", 2, mergedSet.size());
}
}

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import junit.framework.TestCase;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.TestBean;
/**
* @author Rob Harrop
*/
public class PropertiesBeanDefinitionReaderTests extends TestCase {
private DefaultListableBeanFactory beanFactory;
private PropertiesBeanDefinitionReader reader;
protected void setUp() throws Exception {
this.beanFactory = new DefaultListableBeanFactory();
this.reader = new PropertiesBeanDefinitionReader(beanFactory);
}
public void testWithSimpleConstructorArg() {
this.reader.loadBeanDefinitions(new ClassPathResource("simpleConstructorArg.properties", getClass()));
TestBean bean = (TestBean)this.beanFactory.getBean("testBean");
assertEquals("Rob Harrop", bean.getName());
}
public void testWithConstructorArgRef() throws Exception {
this.reader.loadBeanDefinitions(new ClassPathResource("refConstructorArg.properties", getClass()));
TestBean rob = (TestBean)this.beanFactory.getBean("rob");
TestBean sally = (TestBean)this.beanFactory.getBean("sally");
assertEquals(sally, rob.getSpouse());
}
public void testWithMultipleConstructorsArgs() throws Exception {
this.reader.loadBeanDefinitions(new ClassPathResource("multiConstructorArgs.properties", getClass()));
TestBean bean = (TestBean)this.beanFactory.getBean("testBean");
assertEquals("Rob Harrop", bean.getName());
assertEquals(23, bean.getAge());
}
}