polishing .beans tests

This commit is contained in:
Chris Beams 2008-12-24 21:52:19 +00:00
parent a0bf1b4ade
commit 62db6af879
29 changed files with 216 additions and 114 deletions

View File

@ -17,6 +17,7 @@
package org.springframework.beans.factory; package org.springframework.beans.factory;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
@ -35,7 +36,7 @@ import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource;
/** /**
* @author Guillaume Poirier * @author Guillaume Poirier
@ -46,17 +47,15 @@ import org.springframework.core.io.ClassPathResource;
public final class ConcurrentBeanFactoryTests { public final class ConcurrentBeanFactoryTests {
private static final Log logger = LogFactory.getLog(ConcurrentBeanFactoryTests.class); private static final Log logger = LogFactory.getLog(ConcurrentBeanFactoryTests.class);
private static final Resource CONTEXT = qualifiedResource(ConcurrentBeanFactoryTests.class, "context.xml");
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd"); private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
private static final Date DATE_1, DATE_2;
private static final Date date1;
private static final Date date2;
static { static {
try { try {
date1 = df.parse("2004/08/08"); DATE_1 = DATE_FORMAT.parse("2004/08/08");
date2 = df.parse("2000/02/02"); DATE_2 = DATE_FORMAT.parse("2000/02/02");
} }
catch (ParseException e) { catch (ParseException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
@ -71,10 +70,10 @@ public final class ConcurrentBeanFactoryTests {
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("concurrent.xml", getClass())); XmlBeanFactory factory = new XmlBeanFactory(CONTEXT);
factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() { factory.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
public void registerCustomEditors(PropertyEditorRegistry registry) { public void registerCustomEditors(PropertyEditorRegistry registry) {
registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) df.clone(), false)); registry.registerCustomEditor(Date.class, new CustomDateEditor((DateFormat) DATE_FORMAT.clone(), false));
} }
}); });
this.factory = factory; this.factory = factory;
@ -119,8 +118,8 @@ public final class ConcurrentBeanFactoryTests {
ConcurrentBean b1 = (ConcurrentBean) factory.getBean("bean1"); ConcurrentBean b1 = (ConcurrentBean) factory.getBean("bean1");
ConcurrentBean b2 = (ConcurrentBean) factory.getBean("bean2"); ConcurrentBean b2 = (ConcurrentBean) factory.getBean("bean2");
assertEquals(b1.getDate(), date1); assertEquals(b1.getDate(), DATE_1);
assertEquals(b2.getDate(), date2); assertEquals(b2.getDate(), DATE_2);
} }

View File

@ -25,6 +25,8 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
/** /**
* Unit tests for {@link SingletonBeanFactoryLocator}.
*
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Chris Beams * @author Chris Beams
*/ */

View File

@ -40,12 +40,14 @@ import test.beans.NestedTestBean;
import test.beans.TestBean; import test.beans.TestBean;
/** /**
* Unit tests for {@link AutowiredAnnotationBeanPostProcessor}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Mark Fisher * @author Mark Fisher
* @author Sam Brannen * @author Sam Brannen
* @author Chris Beams * @author Chris Beams
*/ */
public class AutowiredAnnotationBeanPostProcessorTests { public final class AutowiredAnnotationBeanPostProcessorTests {
@Test @Test
public void testIncompleteBeanDefinition() { public void testIncompleteBeanDefinition() {

View File

@ -17,6 +17,7 @@
package org.springframework.beans.factory.annotation; package org.springframework.beans.factory.annotation;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.BeanDefinitionHolder;
@ -25,23 +26,24 @@ import org.springframework.beans.factory.support.AutowireCandidateResolver;
import org.springframework.beans.factory.support.BeanDefinitionReader; import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
/** /**
* Unit tests for {@link CustomAutowireConfigurer}.
*
* @author Mark Fisher * @author Mark Fisher
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class CustomAutowireConfigurerTests { public final class CustomAutowireConfigurerTests {
private static final String CONFIG_LOCATION =
"classpath:org/springframework/beans/factory/annotation/customAutowireConfigurer.xml";
private static final Resource CONTEXT = qualifiedResource(CustomAutowireConfigurerTests.class, "context.xml");
@Test @Test
public void testCustomResolver() { public void testCustomResolver() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf); BeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
reader.loadBeanDefinitions(CONFIG_LOCATION); reader.loadBeanDefinitions(CONTEXT);
CustomAutowireConfigurer cac = new CustomAutowireConfigurer(); CustomAutowireConfigurer cac = new CustomAutowireConfigurer();
CustomResolver customResolver = new CustomResolver(); CustomResolver customResolver = new CustomResolver();
bf.setAutowireCandidateResolver(customResolver); bf.setAutowireCandidateResolver(customResolver);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,7 +22,7 @@ import org.apache.commons.logging.Log;
import org.junit.Test; import org.junit.Test;
/** /**
* Unit tests for the {@link CommonsLogFactoryBean} class. * Unit tests for {@link CommonsLogFactoryBean}.
* *
* @author Rick Evans * @author Rick Evans
* @author Chris Beams * @author Chris Beams

View File

@ -16,6 +16,8 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import java.beans.PropertyEditorSupport; import java.beans.PropertyEditorSupport;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
@ -24,8 +26,7 @@ import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.FatalBeanException; import org.springframework.beans.FatalBeanException;
import org.springframework.beans.MutablePropertyValues; import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistrar;
@ -37,11 +38,15 @@ import org.springframework.beans.propertyeditors.CustomDateEditor;
import test.beans.TestBean; import test.beans.TestBean;
/** /**
* Unit tests for {@link CustomEditorConfigurer}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 31.07.2004 * @since 31.07.2004
*/ */
public class CustomEditorConfigurerTests extends TestCase { public final class CustomEditorConfigurerTests {
@Test
public void testCustomEditorConfigurerWithPropertyEditorRegistrar() throws ParseException { public void testCustomEditorConfigurerWithPropertyEditorRegistrar() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer(); CustomEditorConfigurer cec = new CustomEditorConfigurer();
@ -67,6 +72,7 @@ public class CustomEditorConfigurerTests extends TestCase {
assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey")); assertEquals(df.parse("2.12.1975"), tb2.getSomeMap().get("myKey"));
} }
@Test
public void testCustomEditorConfigurerWithEditorClassName() throws ParseException { public void testCustomEditorConfigurerWithEditorClassName() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer(); CustomEditorConfigurer cec = new CustomEditorConfigurer();
@ -84,6 +90,7 @@ public class CustomEditorConfigurerTests extends TestCase {
assertEquals(df.parse("2.12.1975"), tb.getDate()); assertEquals(df.parse("2.12.1975"), tb.getDate());
} }
@Test
public void testCustomEditorConfigurerWithRequiredTypeArray() throws ParseException { public void testCustomEditorConfigurerWithRequiredTypeArray() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer(); CustomEditorConfigurer cec = new CustomEditorConfigurer();
@ -101,6 +108,7 @@ public class CustomEditorConfigurerTests extends TestCase {
assertEquals("test", tb.getStringArray()[0]); assertEquals("test", tb.getStringArray()[0]);
} }
@Test
public void testCustomEditorConfigurerWithUnresolvableEditor() throws ParseException { public void testCustomEditorConfigurerWithUnresolvableEditor() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer(); CustomEditorConfigurer cec = new CustomEditorConfigurer();
@ -117,6 +125,7 @@ public class CustomEditorConfigurerTests extends TestCase {
} }
} }
@Test
public void testCustomEditorConfigurerWithIgnoredUnresolvableEditor() throws ParseException { public void testCustomEditorConfigurerWithIgnoredUnresolvableEditor() throws ParseException {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
CustomEditorConfigurer cec = new CustomEditorConfigurer(); CustomEditorConfigurer cec = new CustomEditorConfigurer();

View File

@ -15,6 +15,7 @@
*/ */
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.easymock.EasyMock.*; import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@ -26,11 +27,13 @@ import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
/** /**
* Unit tests for {@link CustomScopeConfigurer}.
*
* @author Rick Evans * @author Rick Evans
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class CustomScopeConfigurerTests { public final class CustomScopeConfigurerTests {
private static final String FOO_SCOPE = "fooScope"; private static final String FOO_SCOPE = "fooScope";
private ConfigurableListableBeanFactory factory; private ConfigurableListableBeanFactory factory;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,22 +16,31 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.assertEquals;
import static test.util.TestResourceUtils.qualifiedResource;
import java.sql.Connection; import java.sql.Connection;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource;
import test.beans.TestBean; import test.beans.TestBean;
/** /**
* Unit tests for {@link FieldRetrievingFactoryBean}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 31.07.2004 * @since 31.07.2004
*/ */
public class FieldRetrievingFactoryBeanTests extends TestCase { public final class FieldRetrievingFactoryBeanTests {
private static final Resource CONTEXT =
qualifiedResource(FieldRetrievingFactoryBeanTests.class, "context.xml");
@Test
public void testStaticField() throws Exception { public void testStaticField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE"); fr.setStaticField("java.sql.Connection.TRANSACTION_SERIALIZABLE");
@ -39,6 +48,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
} }
@Test
public void testStaticFieldWithWhitespace() throws Exception { public void testStaticFieldWithWhitespace() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE "); fr.setStaticField(" java.sql.Connection.TRANSACTION_SERIALIZABLE ");
@ -46,6 +56,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
} }
@Test
public void testStaticFieldViaClassAndFieldName() throws Exception { public void testStaticFieldViaClassAndFieldName() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetClass(Connection.class); fr.setTargetClass(Connection.class);
@ -54,6 +65,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
} }
@Test
public void testNonStaticField() throws Exception { public void testNonStaticField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
PublicFieldHolder target = new PublicFieldHolder(); PublicFieldHolder target = new PublicFieldHolder();
@ -63,6 +75,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals(target.publicField, fr.getObject()); assertEquals(target.publicField, fr.getObject());
} }
@Test
public void testNothingButBeanName() throws Exception { public void testNothingButBeanName() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE"); fr.setBeanName("java.sql.Connection.TRANSACTION_SERIALIZABLE");
@ -70,6 +83,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject()); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), fr.getObject());
} }
@Test
public void testJustTargetField() throws Exception { public void testJustTargetField() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetField("TRANSACTION_SERIALIZABLE"); fr.setTargetField("TRANSACTION_SERIALIZABLE");
@ -80,6 +94,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testJustTargetClass() throws Exception { public void testJustTargetClass() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetClass(Connection.class); fr.setTargetClass(Connection.class);
@ -90,6 +105,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testJustTargetObject() throws Exception { public void testJustTargetObject() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setTargetObject(new PublicFieldHolder()); fr.setTargetObject(new PublicFieldHolder());
@ -100,6 +116,7 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testWithConstantOnClassWithPackageLevelVisibility() throws Exception { public void testWithConstantOnClassWithPackageLevelVisibility() throws Exception {
FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean(); FieldRetrievingFactoryBean fr = new FieldRetrievingFactoryBean();
fr.setBeanName("test.beans.PackageLevelVisibleBean.CONSTANT"); fr.setBeanName("test.beans.PackageLevelVisibleBean.CONSTANT");
@ -107,9 +124,10 @@ public class FieldRetrievingFactoryBeanTests extends TestCase {
assertEquals("Wuby", fr.getObject()); assertEquals("Wuby", fr.getObject());
} }
@Test
public void testBeanNameSyntaxWithBeanFactory() throws Exception { public void testBeanNameSyntaxWithBeanFactory() throws Exception {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource("fieldRetrieving.xml", getClass())); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
TestBean testBean = (TestBean) bf.getBean("testBean"); TestBean testBean = (TestBean) bf.getBean("testBean");
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]);
assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]); assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]);

View File

@ -16,23 +16,28 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.beans.support.ArgumentConvertingMethodInvoker; import org.springframework.beans.support.ArgumentConvertingMethodInvoker;
import org.springframework.util.MethodInvoker; import org.springframework.util.MethodInvoker;
/** /**
* Unit tests for {@link MethodInvokingFactoryBean}.
*
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 21.11.2003 * @since 21.11.2003
*/ */
public class MethodInvokingFactoryBeanTests extends TestCase { public final class MethodInvokingFactoryBeanTests {
@Test
public void testParameterValidation() throws Exception { public void testParameterValidation() throws Exception {
String validationError = "improper validation of input properties"; String validationError = "improper validation of input properties";
@ -119,6 +124,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
} }
@Test
public void testGetObjectType() throws Exception { public void testGetObjectType() throws Exception {
TestClass1 tc1 = new TestClass1(); TestClass1 tc1 = new TestClass1();
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
@ -132,7 +138,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("voidRetvalMethod"); mcfb.setTargetMethod("voidRetvalMethod");
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
Class objType = mcfb.getObjectType(); Class<?> objType = mcfb.getObjectType();
assertTrue(objType.equals(void.class)); assertTrue(objType.equals(void.class));
// verify that we can call a method with args that are subtypes of the // verify that we can call a method with args that are subtypes of the
@ -141,7 +147,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes"); mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello"}); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello"});
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
mcfb.getObjectType(); mcfb.getObjectType();
@ -160,6 +166,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testGetObject() throws Exception { public void testGetObject() throws Exception {
// singleton, non-static // singleton, non-static
TestClass1 tc1 = new TestClass1(); TestClass1 tc1 = new TestClass1();
@ -217,16 +224,17 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes"); mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello"}); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello"});
// should pass // should pass
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
} }
@Test
public void testArgumentConversion() throws Exception { public void testArgumentConversion() throws Exception {
MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean(); MethodInvokingFactoryBean mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes"); mcfb.setTargetMethod("supertypes");
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", "bogus"}); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", "bogus"});
try { try {
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
fail("Matched method with wrong number of args"); fail("Matched method with wrong number of args");
@ -251,14 +259,14 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes2"); mcfb.setTargetMethod("supertypes2");
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), "hello", "bogus"}); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), "hello", "bogus"});
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
assertEquals("hello", mcfb.getObject()); assertEquals("hello", mcfb.getObject());
mcfb = new MethodInvokingFactoryBean(); mcfb = new MethodInvokingFactoryBean();
mcfb.setTargetClass(TestClass1.class); mcfb.setTargetClass(TestClass1.class);
mcfb.setTargetMethod("supertypes2"); mcfb.setTargetMethod("supertypes2");
mcfb.setArguments(new Object[] {new ArrayList(), new ArrayList(), new Object()}); mcfb.setArguments(new Object[] {new ArrayList<Object>(), new ArrayList<Object>(), new Object()});
try { try {
mcfb.afterPropertiesSet(); mcfb.afterPropertiesSet();
fail("Matched method when shouldn't have matched"); fail("Matched method when shouldn't have matched");
@ -268,6 +276,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
} }
} }
@Test
public void testInvokeWithNullArgument() throws Exception { public void testInvokeWithNullArgument() throws Exception {
MethodInvoker methodInvoker = new MethodInvoker(); MethodInvoker methodInvoker = new MethodInvoker();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
@ -277,6 +286,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
methodInvoker.invoke(); methodInvoker.invoke();
} }
@Test
public void testInvokeWithIntArgument() throws Exception { public void testInvokeWithIntArgument() throws Exception {
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
@ -293,6 +303,7 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
methodInvoker.invoke(); methodInvoker.invoke();
} }
@Test
public void testInvokeWithIntArguments() throws Exception { public void testInvokeWithIntArguments() throws Exception {
ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker(); ArgumentConvertingMethodInvoker methodInvoker = new ArgumentConvertingMethodInvoker();
methodInvoker.setTargetClass(TestClass1.class); methodInvoker.setTargetClass(TestClass1.class);
@ -363,23 +374,23 @@ public class MethodInvokingFactoryBeanTests extends TestCase {
public static void intArguments(int[] arg) { public static void intArguments(int[] arg) {
} }
public static String supertypes(Collection c, Integer i) { public static String supertypes(Collection<?> c, Integer i) {
return i.toString(); return i.toString();
} }
public static String supertypes(Collection c, List l, String s) { public static String supertypes(Collection<?> c, List<?> l, String s) {
return s; return s;
} }
public static String supertypes2(Collection c, List l, Integer i) { public static String supertypes2(Collection<?> c, List<?> l, Integer i) {
return i.toString(); return i.toString();
} }
public static String supertypes2(Collection c, List l, String s, Integer i) { public static String supertypes2(Collection<?> c, List<?> l, String s, Integer i) {
return s; return s;
} }
public static String supertypes2(Collection c, List l, String s, String s2) { public static String supertypes2(Collection<?> c, List<?> l, String s, String s2) {
return s; return s;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,65 +16,72 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Date; import java.util.Date;
import junit.framework.TestCase; import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource;
import org.easymock.MockControl;
/** /**
* Unit tests for the ObjectFactoryCreatingFactoryBean class. * Unit tests for {@link ObjectFactoryCreatingFactoryBean}.
* *
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Rick Evans * @author Rick Evans
* @author Chris Beams
* @since 2004-05-11 * @since 2004-05-11
*/ */
public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase { public final class ObjectFactoryCreatingFactoryBeanTests {
private static final Resource CONTEXT =
qualifiedResource(ObjectFactoryCreatingFactoryBeanTests.class, "context.xml");
private BeanFactory beanFactory; private BeanFactory beanFactory;
@Before
protected void setUp() throws Exception { public void setUp() {
this.beanFactory = new XmlBeanFactory(new ClassPathResource( this.beanFactory = new XmlBeanFactory(CONTEXT);
"ObjectFactoryCreatingFactoryBeanTests.xml", getClass()));
} }
@Test
public void testBasicOperation() throws BeansException { public void testBasicOperation() throws BeansException {
TestBean testBean = (TestBean) beanFactory.getBean("testBean"); TestBean testBean = (TestBean) beanFactory.getBean("testBean");
ObjectFactory objectFactory = testBean.getObjectFactory(); ObjectFactory<?> objectFactory = testBean.getObjectFactory();
Date date1 = (Date) objectFactory.getObject(); Date date1 = (Date) objectFactory.getObject();
Date date2 = (Date) objectFactory.getObject(); Date date2 = (Date) objectFactory.getObject();
assertTrue(date1 != date2); assertTrue(date1 != date2);
} }
@Test
public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exception { public void testDoesNotComplainWhenTargetBeanNameRefersToSingleton() throws Exception {
final String targetBeanName = "singleton"; final String targetBeanName = "singleton";
final String expectedSingleton = "Alicia Keys"; final String expectedSingleton = "Alicia Keys";
MockControl mock = MockControl.createControl(BeanFactory.class); BeanFactory beanFactory = createMock(BeanFactory.class);
BeanFactory beanFactory = (BeanFactory) mock.getMock(); expect(beanFactory.getBean(targetBeanName)).andReturn(expectedSingleton);
beanFactory.getBean(targetBeanName); replay(beanFactory);
mock.setReturnValue(expectedSingleton);
mock.replay();
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
factory.setTargetBeanName(targetBeanName); factory.setTargetBeanName(targetBeanName);
factory.setBeanFactory(beanFactory); factory.setBeanFactory(beanFactory);
factory.afterPropertiesSet(); factory.afterPropertiesSet();
ObjectFactory objectFactory = (ObjectFactory) factory.getObject(); ObjectFactory<?> objectFactory = (ObjectFactory<?>) factory.getObject();
Object actualSingleton = objectFactory.getObject(); Object actualSingleton = objectFactory.getObject();
assertSame(expectedSingleton, actualSingleton); assertSame(expectedSingleton, actualSingleton);
mock.verify(); verify(beanFactory);
} }
@Test
public void testWhenTargetBeanNameIsNull() throws Exception { public void testWhenTargetBeanNameIsNull() throws Exception {
try { try {
new ObjectFactoryCreatingFactoryBean().afterPropertiesSet(); new ObjectFactoryCreatingFactoryBean().afterPropertiesSet();
@ -83,6 +90,7 @@ public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase {
catch (IllegalArgumentException expected) {} catch (IllegalArgumentException expected) {}
} }
@Test
public void testWhenTargetBeanNameIsEmptyString() throws Exception { public void testWhenTargetBeanNameIsEmptyString() throws Exception {
try { try {
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
@ -93,6 +101,7 @@ public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase {
catch (IllegalArgumentException expected) {} catch (IllegalArgumentException expected) {}
} }
@Test
public void testWhenTargetBeanNameIsWhitespacedString() throws Exception { public void testWhenTargetBeanNameIsWhitespacedString() throws Exception {
try { try {
ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean(); ObjectFactoryCreatingFactoryBean factory = new ObjectFactoryCreatingFactoryBean();
@ -103,6 +112,7 @@ public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase {
catch (IllegalArgumentException expected) {} catch (IllegalArgumentException expected) {}
} }
@Test
public void testEnsureOFBFBReportsThatItActuallyCreatesObjectFactoryInstances() throws Exception { public void testEnsureOFBFBReportsThatItActuallyCreatesObjectFactoryInstances() throws Exception {
assertEquals("Must be reporting that it creates ObjectFactory instances (as per class contract).", assertEquals("Must be reporting that it creates ObjectFactory instances (as per class contract).",
ObjectFactory.class, new ObjectFactoryCreatingFactoryBean().getObjectType()); ObjectFactory.class, new ObjectFactoryCreatingFactoryBean().getObjectType());
@ -111,14 +121,14 @@ public final class ObjectFactoryCreatingFactoryBeanTests extends TestCase {
public static class TestBean { public static class TestBean {
public ObjectFactory objectFactory; public ObjectFactory<?> objectFactory;
public ObjectFactory getObjectFactory() { public ObjectFactory<?> getObjectFactory() {
return objectFactory; return objectFactory;
} }
public void setObjectFactory(ObjectFactory objectFactory) { public void setObjectFactory(ObjectFactory<?> objectFactory) {
this.objectFactory = objectFactory; this.objectFactory = objectFactory;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,40 +16,46 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.Properties; import java.util.Properties;
import junit.framework.TestCase; import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.JdkVersion;
import org.springframework.core.io.ClassPathResource;
/** /**
* Unit tests for {@link PropertiesFactoryBean}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 01.11.2003 * @since 01.11.2003
*/ */
public class PropertiesFactoryBeanTests extends TestCase { public final class PropertiesFactoryBeanTests {
private static final Class<?> CLASS = PropertiesFactoryBeanTests.class;
private static final Resource TEST_PROPS = qualifiedResource(CLASS, "test.properties");
private static final Resource TEST_PROPS_XML = qualifiedResource(CLASS, "test.properties.xml");
@Test
public void testWithPropertiesFile() throws Exception { public void testWithPropertiesFile() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); pfb.setLocation(TEST_PROPS);
pfb.afterPropertiesSet(); pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject(); Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age")); assertEquals("99", props.getProperty("tb.array[0].age"));
} }
@Test
public void testWithPropertiesXmlFile() throws Exception { public void testWithPropertiesXmlFile() throws Exception {
// ignore for JDK < 1.5
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
return;
}
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test-properties.xml")); pfb.setLocation(TEST_PROPS_XML);
pfb.afterPropertiesSet(); pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject(); Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age")); assertEquals("99", props.getProperty("tb.array[0].age"));
} }
@Test
public void testWithLocalProperties() throws Exception { public void testWithLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
Properties localProps = new Properties(); Properties localProps = new Properties();
@ -60,9 +66,10 @@ public class PropertiesFactoryBeanTests extends TestCase {
assertEquals("value2", props.getProperty("key2")); assertEquals("value2", props.getProperty("key2"));
} }
@Test
public void testWithPropertiesFileAndLocalProperties() throws Exception { public void testWithPropertiesFileAndLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); pfb.setLocation(TEST_PROPS);
Properties localProps = new Properties(); Properties localProps = new Properties();
localProps.setProperty("key2", "value2"); localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0"); localProps.setProperty("tb.array[0].age", "0");
@ -73,9 +80,10 @@ public class PropertiesFactoryBeanTests extends TestCase {
assertEquals("value2", props.getProperty("key2")); assertEquals("value2", props.getProperty("key2"));
} }
@Test
public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception { public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); pfb.setLocation(TEST_PROPS);
Properties props1 = new Properties(); Properties props1 = new Properties();
props1.setProperty("key2", "value2"); props1.setProperty("key2", "value2");
@ -101,9 +109,10 @@ public class PropertiesFactoryBeanTests extends TestCase {
assertEquals("man", props.getProperty("bat")); assertEquals("man", props.getProperty("bat"));
} }
@Test
public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception { public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); pfb.setLocation(TEST_PROPS);
Properties localProps = new Properties(); Properties localProps = new Properties();
localProps.setProperty("key2", "value2"); localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0"); localProps.setProperty("tb.array[0].age", "0");
@ -115,10 +124,11 @@ public class PropertiesFactoryBeanTests extends TestCase {
assertEquals("value2", props.getProperty("key2")); assertEquals("value2", props.getProperty("key2"));
} }
@Test
public void testWithPrototype() throws Exception { public void testWithPrototype() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean(); PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setSingleton(false); pfb.setSingleton(false);
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); pfb.setLocation(TEST_PROPS);
Properties localProps = new Properties(); Properties localProps = new Properties();
localProps.setProperty("key2", "value2"); localProps.setProperty("key2", "value2");
pfb.setProperties(localProps); pfb.setProperties(localProps);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,22 +16,30 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import junit.framework.TestCase; import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import org.junit.Test;
import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource;
import test.beans.ITestBean; import test.beans.ITestBean;
import test.beans.TestBean; import test.beans.TestBean;
/** /**
* Unit tests for {@link PropertyPathFactoryBean}.
*
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams
* @since 04.10.2004 * @since 04.10.2004
*/ */
public class PropertyPathFactoryBeanTests extends TestCase { public class PropertyPathFactoryBeanTests {
private static final Resource CONTEXT = qualifiedResource(PropertyPathFactoryBeanTests.class, "context.xml");
@Test
public void testPropertyPathFactoryBeanWithSingletonResult() { public void testPropertyPathFactoryBeanWithSingletonResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass())); XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
assertEquals(new Integer(12), xbf.getBean("propertyPath1")); assertEquals(new Integer(12), xbf.getBean("propertyPath1"));
assertEquals(new Integer(11), xbf.getBean("propertyPath2")); assertEquals(new Integer(11), xbf.getBean("propertyPath2"));
assertEquals(new Integer(10), xbf.getBean("tb.age")); assertEquals(new Integer(10), xbf.getBean("tb.age"));
@ -43,8 +51,9 @@ public class PropertyPathFactoryBeanTests extends TestCase {
assertEquals(99, ((TestBean) result1).getAge()); assertEquals(99, ((TestBean) result1).getAge());
} }
@Test
public void testPropertyPathFactoryBeanWithPrototypeResult() { public void testPropertyPathFactoryBeanWithPrototypeResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass())); XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
assertNull(xbf.getType("tb.spouse")); assertNull(xbf.getType("tb.spouse"));
assertEquals(TestBean.class, xbf.getType("propertyPath3")); assertEquals(TestBean.class, xbf.getType("propertyPath3"));
Object result1 = xbf.getBean("tb.spouse"); Object result1 = xbf.getBean("tb.spouse");
@ -61,14 +70,16 @@ public class PropertyPathFactoryBeanTests extends TestCase {
assertTrue(result2 != result3); assertTrue(result2 != result3);
} }
@Test
public void testPropertyPathFactoryBeanWithNullResult() { public void testPropertyPathFactoryBeanWithNullResult() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass())); XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
assertNull(xbf.getType("tb.spouse.spouse")); assertNull(xbf.getType("tb.spouse.spouse"));
assertNull(xbf.getBean("tb.spouse.spouse")); assertNull(xbf.getBean("tb.spouse.spouse"));
} }
@Test
public void testPropertyPathFactoryBeanAsInnerBean() { public void testPropertyPathFactoryBeanAsInnerBean() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("propertyPath.xml", getClass())); XmlBeanFactory xbf = new XmlBeanFactory(CONTEXT);
TestBean spouse = (TestBean) xbf.getBean("otb.spouse"); TestBean spouse = (TestBean) xbf.getBean("otb.spouse");
TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner"); TestBean tbWithInner = (TestBean) xbf.getBean("tbWithInner");
assertSame(spouse, tbWithInner.getSpouse()); assertSame(spouse, tbWithInner.getSpouse());

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties version="1.0">
<entry key="tb.array[0].age">99</entry>
<entry key="tb.list[1].name">test</entry>
</properties>

View File

@ -18,6 +18,7 @@ package org.springframework.beans.factory.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -38,7 +39,6 @@ import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet; import org.springframework.beans.factory.support.ManagedSet;
import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import test.beans.IndexedTestBean; import test.beans.IndexedTestBean;
@ -53,7 +53,12 @@ import test.beans.TestBean;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class PropertyResourceConfigurerTests { public final class PropertyResourceConfigurerTests {
private static final Class<?> CLASS = PropertyResourceConfigurerTests.class;
private static final Resource TEST_PROPS = qualifiedResource(CLASS, "test.properties");
private static final Resource XTEST_PROPS = qualifiedResource(CLASS, "xtest.properties"); // does not exist
private static final Resource TEST_PROPS_XML = qualifiedResource(CLASS, "test.properties.xml");
private DefaultListableBeanFactory factory; private DefaultListableBeanFactory factory;
@ -205,7 +210,7 @@ public class PropertyResourceConfigurerTests {
factory.registerBeanDefinition("tb", def); factory.registerBeanDefinition("tb", def);
PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer(); PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
poc.setLocation(new ClassPathResource("org/springframework/beans/factory/config/test.properties")); poc.setLocation(TEST_PROPS);
poc.postProcessBeanFactory(factory); poc.postProcessBeanFactory(factory);
IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
@ -221,11 +226,7 @@ public class PropertyResourceConfigurerTests {
factory.registerBeanDefinition("tb", def); factory.registerBeanDefinition("tb", def);
PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer(); PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
poc.setLocations(new Resource[] { poc.setLocations(new Resource[] { TEST_PROPS, XTEST_PROPS });
new ClassPathResource("org/springframework/beans/factory/config/test.properties"),
new ClassPathResource("org/springframework/beans/factory/config/xtest.properties")
}
);
poc.setIgnoreResourceNotFound(true); poc.setIgnoreResourceNotFound(true);
poc.postProcessBeanFactory(factory); poc.postProcessBeanFactory(factory);
@ -242,7 +243,7 @@ public class PropertyResourceConfigurerTests {
factory.registerBeanDefinition("tb", def); factory.registerBeanDefinition("tb", def);
PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer(); PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
poc.setLocation(new ClassPathResource("org/springframework/beans/factory/config/test-properties.xml")); poc.setLocation(TEST_PROPS_XML);
poc.postProcessBeanFactory(factory); poc.postProcessBeanFactory(factory);
IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb"); IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");

View File

@ -31,11 +31,13 @@ import org.springframework.core.NestedCheckedException;
import org.springframework.core.NestedRuntimeException; import org.springframework.core.NestedRuntimeException;
/** /**
* Unit tests for {@link ServiceLocatorFactoryBean}.
*
* @author Colin Sampaleanu * @author Colin Sampaleanu
* @author Rick Evans * @author Rick Evans
* @author Chris Beams * @author Chris Beams
*/ */
public class ServiceLocatorFactoryBeanTests { public final class ServiceLocatorFactoryBeanTests {
private DefaultListableBeanFactory bf; private DefaultListableBeanFactory bf;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2008 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,7 @@
package org.springframework.beans.factory.config; package org.springframework.beans.factory.config;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static test.util.TestResourceUtils.qualifiedResource;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -26,6 +27,7 @@ import org.junit.Test;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader; import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.core.io.Resource;
import test.beans.TestBean; import test.beans.TestBean;
@ -36,7 +38,9 @@ import test.beans.TestBean;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Chris Beams * @author Chris Beams
*/ */
public class SimpleScopeTests { public final class SimpleScopeTests {
private static final Resource CONTEXT = qualifiedResource(SimpleScopeTests.class, "context.xml");
private DefaultListableBeanFactory beanFactory; private DefaultListableBeanFactory beanFactory;
@ -65,7 +69,7 @@ public class SimpleScopeTests {
assertSame(scope, beanFactory.getRegisteredScope("myScope")); assertSame(scope, beanFactory.getRegisteredScope("myScope"));
XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory); XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
xbdr.loadBeanDefinitions("org/springframework/beans/factory/config/simpleScope.xml"); xbdr.loadBeanDefinitions(CONTEXT);
} }
@Test @Test

View File

@ -18,10 +18,17 @@ package org.springframework.beans.factory.config;
import org.springframework.beans.factory.ObjectFactory; import org.springframework.beans.factory.ObjectFactory;
/**
* Shared test types for this package.
*
* @author Chris Beams
*/
final class TestTypes {}
/** /**
* @author Juergen Hoeller * @author Juergen Hoeller
*/ */
public class NoOpScope implements Scope { class NoOpScope implements Scope {
public Object get(String name, ObjectFactory<?> objectFactory) { public Object get(String name, ObjectFactory<?> objectFactory) {
throw new UnsupportedOperationException(); throw new UnsupportedOperationException();

View File

@ -22,7 +22,7 @@
<util:property-path path="configuredBean.name"/> <util:property-path path="configuredBean.name"/>
</property> </property>
<property name="someProperties"> <property name="someProperties">
<util:properties location="classpath:/org/springframework/beans/factory/config/util.properties"/> <util:properties location="classpath:/org/springframework/beans/factory/xml/util.properties"/>
</property> </property>
</bean> </bean>
@ -147,35 +147,35 @@
</bean> </bean>
<util:properties id="myProperties" <util:properties id="myProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties"/> location="classpath:/org/springframework/beans/factory/xml/util.properties"/>
<util:properties id="myScopedProperties" <util:properties id="myScopedProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties" scope="prototype"/> location="classpath:/org/springframework/beans/factory/xml/util.properties" scope="prototype"/>
<util:properties id="myLocalProperties"> <util:properties id="myLocalProperties">
<prop key="foo2">bar2</prop> <prop key="foo2">bar2</prop>
</util:properties> </util:properties>
<util:properties id="myMergedProperties" <util:properties id="myMergedProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties"> location="classpath:/org/springframework/beans/factory/xml/util.properties">
<prop key="foo2">bar2</prop> <prop key="foo2">bar2</prop>
</util:properties> </util:properties>
<util:properties id="defaultLocalOverrideProperties" <util:properties id="defaultLocalOverrideProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties"> location="classpath:/org/springframework/beans/factory/xml/util.properties">
<prop key="foo">local</prop> <prop key="foo">local</prop>
<prop key="foo2">local2</prop> <prop key="foo2">local2</prop>
</util:properties> </util:properties>
<util:properties id="trueLocalOverrideProperties" <util:properties id="trueLocalOverrideProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties" location="classpath:/org/springframework/beans/factory/xml/util.properties"
local-override="true"> local-override="true">
<prop key="foo">local</prop> <prop key="foo">local</prop>
<prop key="foo2">local2</prop> <prop key="foo2">local2</prop>
</util:properties> </util:properties>
<util:properties id="falseLocalOverrideProperties" <util:properties id="falseLocalOverrideProperties"
location="classpath:/org/springframework/beans/factory/config/util.properties" location="classpath:/org/springframework/beans/factory/xml/util.properties"
local-override="false"> local-override="false">
<prop key="foo">local</prop> <prop key="foo">local</prop>
<prop key="foo2">local2</prop> <prop key="foo2">local2</prop>