moving unit tests from .testsuite -> .beans

This commit is contained in:
Chris Beams
2008-12-15 05:20:25 +00:00
parent b44d647dad
commit 248a7de73f
8 changed files with 299 additions and 124 deletions

View File

@@ -16,13 +16,14 @@
package org.springframework.beans.factory.support;
import static org.junit.Assert.*;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import junit.framework.TestCase;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.UnsatisfiedDependencyException;
@@ -35,14 +36,16 @@ import org.springframework.context.support.GenericApplicationContext;
/**
* @author Mark Fisher
* @author Juergen Hoeller
* @author Chris Beams
*/
public class QualifierAnnotationAutowireContextTests extends TestCase {
public class QualifierAnnotationAutowireContextTests {
private static final String JUERGEN = "juergen";
private static final String MARK = "mark";
@Test
public void testAutowiredFieldWithSingleNonQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -62,6 +65,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredMethodParameterWithSingleNonQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -81,6 +85,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredConstructorArgumentWithSingleNonQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -100,6 +105,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredFieldWithSingleQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -114,6 +120,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredMethodParameterWithSingleQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -130,6 +137,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredMethodParameterWithStaticallyQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -145,6 +153,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredConstructorArgumentWithSingleQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs = new ConstructorArgumentValues();
@@ -161,6 +170,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldWithMultipleNonQualifiedCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -184,6 +194,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredMethodParameterWithMultipleNonQualifiedCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -207,6 +218,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredConstructorArgumentWithMultipleNonQualifiedCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -230,6 +242,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredFieldResolvesQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -249,6 +262,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredMethodParameterResolvesQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -269,6 +283,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredConstructorArgumentResolvesQualifiedCandidate() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -289,6 +304,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldResolvesQualifiedCandidateWithDefaultValueAndNoValueOnBeanDefinition() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -310,6 +326,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldDoesNotResolveCandidateWithDefaultValueAndConflictingValueOnBeanDefinition() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -335,6 +352,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -356,6 +374,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(JUERGEN, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldResolvesWithMultipleQualifierValues() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -381,6 +400,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(MARK, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldDoesNotResolveWithMultipleQualifierValuesAndConflictingDefaultValue() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -411,6 +431,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredFieldResolvesWithMultipleQualifierValuesAndExplicitDefaultValue() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -437,6 +458,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(MARK, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldDoesNotResolveWithMultipleQualifierValuesAndMultipleMatchingCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -467,6 +489,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
}
}
@Test
public void testAutowiredFieldResolvesWithBaseQualifierAndDefaultValue() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -487,6 +510,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals(MARK, bean.getPerson().getName());
}
@Test
public void testAutowiredFieldResolvesWithBaseQualifierAndNonDefaultValue() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
@@ -508,6 +532,7 @@ public class QualifierAnnotationAutowireContextTests extends TestCase {
assertEquals("the real juergen", bean.getPerson().getName());
}
@Test
public void testAutowiredFieldDoesNotResolveWithBaseQualifierAndNonDefaultValueAndMultipleMatchingCandidates() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();

View File

@@ -1,64 +0,0 @@
/*
* 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.xml;
import junit.framework.TestCase;
import org.springframework.test.AssertThrows;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/**
* Unit tests for the {@link DelegatingEntityResolver} class.
*
* @author Rick Evans
*/
public final class DelegatingEntityResolverTests extends TestCase {
public void testCtorWhereDtdEntityResolverIsNull() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new DelegatingEntityResolver(null, new NoOpEntityResolver());
}
}.runTest();
}
public void testCtorWhereSchemaEntityResolverIsNull() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new DelegatingEntityResolver(new NoOpEntityResolver(), null);
}
}.runTest();
}
public void testCtorWhereEntityResolversAreBothNull() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new DelegatingEntityResolver(null, null);
}
}.runTest();
}
private static final class NoOpEntityResolver implements EntityResolver {
public InputSource resolveEntity(String publicId, String systemId) {
return null;
}
}
}

View File

@@ -1,325 +0,0 @@
/*
* 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.xml;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.mail.Session;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author Juergen Hoeller
*/
public class FactoryMethodTests extends TestCase {
public void testFactoryMethodsSingletonOnTargetClass() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
TestBean tb = (TestBean) xbf.getBean("defaultTestBean");
assertEquals("defaultInstance", tb.getName());
assertEquals(1, tb.getAge());
FactoryMethods fm = (FactoryMethods) xbf.getBean("default");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
assertEquals("defaultInstance", fm.getTestBean().getName());
assertEquals("setterString", fm.getStringValue());
fm = (FactoryMethods) xbf.getBean("testBeanOnly");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
// This comes from the test bean
assertEquals("Juergen", fm.getTestBean().getName());
fm = (FactoryMethods) xbf.getBean("full");
assertEquals(27, fm.getNum());
assertEquals("gotcha", fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full");
assertSame(fm, fm2);
xbf.destroySingletons();
assertTrue(tb.wasDestroyed());
}
public void testFactoryMethodsWithNullInstance() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("null");
assertNull(fm);
try {
xbf.getBean("nullWithProperty");
fail("Should have thrown BeanCreationException");
}
catch (BeanCreationException ex) {
// expected
}
}
public void testFactoryMethodsWithNullValue() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithNull");
assertEquals(27, fm.getNum());
assertEquals(null, fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
fm = (FactoryMethods) xbf.getBean("fullWithGenericNull");
assertEquals(27, fm.getNum());
assertEquals(null, fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
}
public void testFactoryMethodsWithAutowire() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("fullWithAutowire");
assertEquals(27, fm.getNum());
assertEquals("gotchaAutowired", fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
}
public void testProtectedFactoryMethod() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
TestBean tb = (TestBean) xbf.getBean("defaultTestBean.protected");
assertEquals(1, tb.getAge());
}
public void testPrivateFactoryMethod() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
TestBean tb = (TestBean) xbf.getBean("defaultTestBean.private");
assertEquals(1, tb.getAge());
}
public void testFactoryMethodsPrototypeOnTargetClass() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("defaultPrototype");
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("defaultPrototype");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
assertEquals("defaultInstance", fm.getTestBean().getName());
assertEquals("setterString", fm.getStringValue());
assertEquals(fm.getNum(), fm2.getNum());
assertEquals(fm.getStringValue(), fm2.getStringValue());
// The TestBean is created separately for each bean
assertNotSame(fm.getTestBean(), fm2.getTestBean());
assertNotSame(fm, fm2);
fm = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype");
fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
// This comes from the test bean
assertEquals("Juergen", fm.getTestBean().getName());
assertEquals(fm.getNum(), fm2.getNum());
assertEquals(fm.getStringValue(), fm2.getStringValue());
// The TestBean reference is resolved to a prototype in the factory
assertSame(fm.getTestBean(), fm2.getTestBean());
assertNotSame(fm, fm2);
fm = (FactoryMethods) xbf.getBean("fullPrototype");
fm2 = (FactoryMethods) xbf.getBean("fullPrototype");
assertEquals(27, fm.getNum());
assertEquals("gotcha", fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
assertEquals(fm.getNum(), fm2.getNum());
assertEquals(fm.getStringValue(), fm2.getStringValue());
// The TestBean reference is resolved to a prototype in the factory
assertSame(fm.getTestBean(), fm2.getTestBean());
assertNotSame(fm, fm2);
}
/**
* Tests where the static factory method is on a different class.
*/
public void testFactoryMethodsOnExternalClass() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs"));
assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs"));
String[] names = xbf.getBeanNamesForType(TestBean.class);
assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs"));
assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs"));
TestBean tb = (TestBean) xbf.getBean("externalFactoryMethodWithoutArgs");
assertEquals(2, tb.getAge());
assertEquals("Tristan", tb.getName());
tb = (TestBean) xbf.getBean("externalFactoryMethodWithArgs");
assertEquals(33, tb.getAge());
assertEquals("Rod", tb.getName());
assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithoutArgs"));
assertEquals(TestBean.class, xbf.getType("externalFactoryMethodWithArgs"));
names = xbf.getBeanNamesForType(TestBean.class);
assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithoutArgs"));
assertTrue(Arrays.asList(names).contains("externalFactoryMethodWithArgs"));
}
public void testInstanceFactoryMethodWithoutArgs() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
InstanceFactory.count = 0;
xbf.preInstantiateSingletons();
assertEquals(1, InstanceFactory.count);
FactoryMethods fm = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs");
assertEquals("instanceFactory", fm.getTestBean().getName());
assertEquals(1, InstanceFactory.count);
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("instanceFactoryMethodWithoutArgs");
assertEquals("instanceFactory", fm2.getTestBean().getName());
assertSame(fm2, fm);
assertEquals(1, InstanceFactory.count);
}
public void testFactoryMethodNoMatchingStaticMethod() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
try {
xbf.getBean("noMatchPrototype");
fail("No static method matched");
}
catch (BeanCreationException ex) {
// Ok
}
}
public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
TestBean tbArg = new TestBean();
tbArg.setName("arg1");
TestBean tbArg2 = new TestBean();
tbArg2.setName("arg2");
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[] {tbArg});
assertEquals(0, fm1.getNum());
assertEquals("default", fm1.getName());
// This comes from the test bean
assertEquals("arg1", fm1.getTestBean().getName());
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[] {tbArg2});
assertEquals("arg2", fm2.getTestBean().getName());
assertEquals(fm1.getNum(), fm2.getNum());
assertEquals(fm2.getStringValue(), "testBeanOnlyPrototypeDISetterString");
assertEquals(fm2.getStringValue(), fm2.getStringValue());
// The TestBean reference is resolved to a prototype in the factory
assertSame(fm2.getTestBean(), fm2.getTestBean());
assertNotSame(fm1, fm2);
FactoryMethods fm3 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[] {tbArg2, new Integer(1), "myName"});
assertEquals(1, fm3.getNum());
assertEquals("myName", fm3.getName());
assertEquals("arg2", fm3.getTestBean().getName());
FactoryMethods fm4 = (FactoryMethods) xbf.getBean("testBeanOnlyPrototype", new Object[] {tbArg});
assertEquals(0, fm4.getNum());
assertEquals("default", fm4.getName());
assertEquals("arg1", fm4.getTestBean().getName());
}
public void testCannotSpecifyFactoryMethodArgumentsOnSingleton() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
try {
xbf.getBean("testBeanOnly", new Object[] {new TestBean()});
fail("Shouldn't allow args to be passed to a singleton");
}
catch (BeanDefinitionStoreException ex) {
// OK
}
}
public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
xbf.getBean("testBeanOnly");
try {
xbf.getBean("testBeanOnly", new Object[] {new TestBean()});
fail("Shouldn't allow args to be passed to a singleton");
}
catch (BeanDefinitionStoreException ex) {
// OK
}
}
public void testFactoryMethodWithDifferentReturnType() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
// Check that listInstance is not considered a bean of type FactoryMethods.
assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance")));
String[] names = xbf.getBeanNamesForType(FactoryMethods.class);
assertTrue(!Arrays.asList(names).contains("listInstance"));
names = xbf.getBeanNamesForType(List.class);
assertTrue(Arrays.asList(names).contains("listInstance"));
xbf.preInstantiateSingletons();
assertTrue(List.class.isAssignableFrom(xbf.getType("listInstance")));
names = xbf.getBeanNamesForType(FactoryMethods.class);
assertTrue(!Arrays.asList(names).contains("listInstance"));
names = xbf.getBeanNamesForType(List.class);
assertTrue(Arrays.asList(names).contains("listInstance"));
List list = (List) xbf.getBean("listInstance");
assertEquals(Collections.EMPTY_LIST, list);
}
public void testFactoryMethodForJavaMailSession() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
Session session = (Session) xbf.getBean("javaMailSession");
assertEquals("someuser", session.getProperty("mail.smtp.user"));
assertEquals("somepw", session.getProperty("mail.smtp.password"));
}
}

View File

@@ -1,62 +0,0 @@
/*
* Copyright 2002-2005 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.xml;
import org.springframework.beans.TestBean;
/**
* Test class for Spring's ability to create objects using
* static factory methods, rather than constructors.
*
* @author Rod Johnson
*/
public class InstanceFactory {
protected static int count = 0;
private String factoryBeanProperty;
public InstanceFactory() {
count++;
}
public void setFactoryBeanProperty(String s) {
this.factoryBeanProperty = s;
}
public String getFactoryBeanProperty() {
return this.factoryBeanProperty;
}
public FactoryMethods defaultInstance() {
TestBean tb = new TestBean();
tb.setName(this.factoryBeanProperty);
return FactoryMethods.newInstance(tb);
}
/**
* Note that overloaded methods are supported.
*/
public FactoryMethods newInstance(TestBean tb) {
return FactoryMethods.newInstance(tb);
}
public FactoryMethods newInstance(TestBean tb, int num, String name) {
return FactoryMethods.newInstance(tb, num, name);
}
}

View File

@@ -1,43 +0,0 @@
/*
* Copyright 2002-2005 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.xml;
import org.springframework.beans.TestBean;
/**
* Test class for Spring's ability to create
* objects using static factory methods, rather
* than constructors.
* @author Rod Johnson
*/
public class TestBeanCreator {
public static TestBean createTestBean(String name, int age) {
TestBean tb = new TestBean();
tb.setName(name);
tb.setAge(age);
return tb;
}
public static TestBean createTestBean() {
TestBean tb = new TestBean();
tb.setName("Tristan");
tb.setAge(2);
return tb;
}
}

View File

@@ -16,6 +16,9 @@
package org.springframework.beans.factory.xml;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
@@ -27,11 +30,8 @@ import java.util.Map;
import javax.servlet.ServletException;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.InputSource;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.BeansException;
@@ -65,6 +65,7 @@ import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.SerializationTestUtils;
import org.springframework.util.StopWatch;
import org.xml.sax.InputSource;
/**
* Miscellaneous tests for XML bean definitions.
@@ -72,19 +73,20 @@ import org.springframework.util.StopWatch;
* @author Juergen Hoeller
* @author Rod Johnson
* @author Rick Evans
* @author Chris Beams
*/
public class XmlBeanFactoryTests extends TestCase {
public class XmlBeanFactoryTests {
/*
* http://opensource.atlassian.com/projects/spring/browse/SPR-2368
*/
public void testCollectionsReferredToAsRefLocals() throws Exception {
public @Test void testCollectionsReferredToAsRefLocals() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(
new ClassPathResource("local-collections-using-XSD.xml", getClass()));
factory.preInstantiateSingletons();
}
public void testRefToSeparatePrototypeInstances() throws Exception {
public @Test void testRefToSeparatePrototypeInstances() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -102,7 +104,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks")));
}
public void testRefToSingleton() throws Exception {
public @Test void testRefToSingleton() throws Exception {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -118,7 +120,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue("1 jen instance", davesJen == jen);
}
public void testInnerBeans() throws IOException {
public @Test void testInnerBeans() throws IOException {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
@@ -181,7 +183,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inner5.wasDestroyed());
}
public void testInnerBeansWithoutDestroy() {
public @Test void testInnerBeansWithoutDestroy() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -214,7 +216,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals("innerBean", inner5.getBeanName());
}
public void testFailsOnInnerBean() {
public @Test void testFailsOnInnerBean() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -241,7 +243,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testSingletonInheritanceFromParentFactorySingleton() throws Exception {
public @Test void testSingletonInheritanceFromParentFactorySingleton() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
assertEquals(TestBean.class, child.getType("inheritsFromParentFactory"));
@@ -254,7 +256,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits2 == inherits);
}
public void testInheritanceWithDifferentClass() throws Exception {
public @Test void testInheritanceWithDifferentClass() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass"));
@@ -266,7 +268,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits.wasInitialized());
}
public void testInheritanceWithClass() throws Exception {
public @Test void testInheritanceWithClass() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
assertEquals(DerivedTestBean.class, child.getType("inheritsWithClass"));
@@ -278,7 +280,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits.wasInitialized());
}
public void testPrototypeInheritanceFromParentFactoryPrototype() throws Exception {
public @Test void testPrototypeInheritanceFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
assertEquals(TestBean.class, child.getType("prototypeInheritsFromParentFactoryPrototype"));
@@ -295,7 +297,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits.getAge() == 2);
}
public void testPrototypeInheritanceFromParentFactorySingleton() throws Exception {
public @Test void testPrototypeInheritanceFromParentFactorySingleton() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
@@ -311,7 +313,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits.getAge() == 1);
}
public void testAutowireModeNotInherited() {
public @Test void testAutowireModeNotInherited() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass()));
@@ -325,13 +327,13 @@ public class XmlBeanFactoryTests extends TestCase {
assertNull("autowiring not propagated along child relationships", derivedDavid.getSpouse());
}
public void testAbstractParentBeans() {
public @Test void testAbstractParentBeans() {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
parent.preInstantiateSingletons();
assertTrue(parent.isSingleton("inheritedTestBeanWithoutClass"));
// abstract beans should not match
Map tbs = parent.getBeansOfType(TestBean.class);
Map<?, ?> tbs = parent.getBeansOfType(TestBean.class);
assertEquals(2, tbs.size());
assertTrue(tbs.containsKey("inheritedTestBeanPrototype"));
assertTrue(tbs.containsKey("inheritedTestBeanSingleton"));
@@ -349,7 +351,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(parent.getBean("inheritedTestBeanPrototype") instanceof TestBean);
}
public void testDependenciesMaterializeThis() throws Exception {
public @Test void testDependenciesMaterializeThis() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("dependenciesMaterializeThis.xml", getClass()));
assertEquals(2, xbf.getBeansOfType(DummyBo.class, true, false).size());
@@ -366,7 +368,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(bos.dao == bop.dao);
}
public void testChildOverridesParentBean() throws Exception {
public @Test void testChildOverridesParentBean() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
TestBean inherits = (TestBean) child.getBean("inheritedTestBean");
@@ -382,7 +384,7 @@ public class XmlBeanFactoryTests extends TestCase {
* Check that a prototype can't inherit from a bogus parent.
* If a singleton does this the factory will fail to load.
*/
public void testBogusParentageFromParentFactory() throws Exception {
public @Test void testBogusParentageFromParentFactory() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
try {
@@ -401,7 +403,7 @@ public class XmlBeanFactoryTests extends TestCase {
* It's possible for a subclass singleton not to return independent
* instances even if derived from a prototype
*/
public void testSingletonInheritsFromParentFactoryPrototype() throws Exception {
public @Test void testSingletonInheritsFromParentFactoryPrototype() throws Exception {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
TestBean inherits = (TestBean) child.getBean("singletonInheritsFromParentFactoryPrototype");
@@ -413,7 +415,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(inherits2 == inherits);
}
public void testSingletonFromParent() {
public @Test void testSingletonFromParent() {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
TestBean beanFromParent = (TestBean) parent.getBean("inheritedTestBeanSingleton");
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
@@ -421,14 +423,14 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue("singleton from parent and child is the same", beanFromParent == beanFromChild);
}
public void testNestedPropertyValue() {
public @Test void testNestedPropertyValue() {
XmlBeanFactory parent = new XmlBeanFactory(new ClassPathResource("parent.xml", getClass()));
XmlBeanFactory child = new XmlBeanFactory(new ClassPathResource("child.xml", getClass()), parent);
IndexedTestBean bean = (IndexedTestBean) child.getBean("indexedTestBean");
assertEquals("name applied correctly", "myname", bean.getArray()[0].getName());
}
public void testCircularReferences() {
public @Test void testCircularReferences() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -445,17 +447,17 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue("Correct circular reference", complexEgo.getSpouse().getSpouse() == complexEgo);
}
public void testCircularReferenceWithFactoryBeanFirst() {
public @Test void testCircularReferenceWithFactoryBeanFirst() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.loadBeanDefinitions(new ClassPathResource("reftypes.xml", getClass()));
TestBean egoBridge = (TestBean) xbf.getBean("egoBridge");
xbf.getBean("egoBridge");
TestBean complexEgo = (TestBean) xbf.getBean("complexEgo");
assertTrue("Correct circular reference", complexEgo.getSpouse().getSpouse() == complexEgo);
}
public void testCircularReferenceWithTwoFactoryBeans() {
public @Test void testCircularReferenceWithTwoFactoryBeans() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -466,7 +468,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue("Correct circular reference", ego3.getSpouse().getSpouse() == ego3);
}
public void testCircularReferencesWithNotAllowed() {
public @Test void testCircularReferencesWithNotAllowed() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
xbf.setAllowCircularReferences(false);
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
@@ -481,7 +483,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testCircularReferencesWithWrapping() {
public @Test void testCircularReferencesWithWrapping() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
@@ -496,7 +498,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testCircularReferencesWithWrappingAndRawInjectionAllowed() {
public @Test void testCircularReferencesWithWrappingAndRawInjectionAllowed() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
xbf.setAllowRawInjectionDespiteWrapping(true);
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
@@ -516,19 +518,19 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(!AopUtils.isAopProxy(david.getSpouse()));
}
public void testFactoryReferenceCircle() {
public @Test void testFactoryReferenceCircle() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("factoryCircle.xml", getClass()));
TestBean tb = (TestBean) xbf.getBean("singletonFactory");
DummyFactory db = (DummyFactory) xbf.getBean("&singletonFactory");
assertTrue(tb == db.getOtherTestBean());
}
public void testFactoryReferenceWithDoublePrefix() {
public @Test void testFactoryReferenceWithDoublePrefix() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("factoryCircle.xml", getClass()));
DummyFactory db = (DummyFactory) xbf.getBean("&&singletonFactory");
assertThat(xbf.getBean("&&singletonFactory"), instanceOf(DummyFactory.class));
}
public void testComplexFactoryReferenceCircle() {
public @Test void testComplexFactoryReferenceCircle() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("complexFactoryCircle.xml", getClass()));
xbf.getBean("proxy1");
// check that unused instances from autowiring got removed
@@ -538,7 +540,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals(7, xbf.getSingletonCount());
}
public void testNoSuchFactoryBeanMethod() {
public @Test void testNoSuchFactoryBeanMethod() {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("no-such-factory-method.xml", getClass()));
assertNotNull(xbf.getBean("defaultTestBean"));
@@ -549,7 +551,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testInitMethodIsInvoked() throws Exception {
public @Test void testInitMethodIsInvoked() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
DoubleInitializer in = (DoubleInitializer) xbf.getBean("init-method1");
// Initializer should have doubled value
@@ -559,7 +561,7 @@ public class XmlBeanFactoryTests extends TestCase {
/**
* Test that if a custom initializer throws an exception, it's handled correctly
*/
public void testInitMethodThrowsException() {
public @Test void testInitMethodThrowsException() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
try {
xbf.getBean("init-method2");
@@ -572,7 +574,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testNoSuchInitMethod() throws Exception {
public @Test void testNoSuchInitMethod() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
try {
xbf.getBean("init-method3");
@@ -589,7 +591,7 @@ public class XmlBeanFactoryTests extends TestCase {
/**
* Check that InitializingBean method is called first.
*/
public void testInitializingBeanAndInitMethod() throws Exception {
public @Test void testInitializingBeanAndInitMethod() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
assertFalse(InitAndIB.constructed);
@@ -608,7 +610,7 @@ public class XmlBeanFactoryTests extends TestCase {
/**
* Check that InitializingBean method is not called twice.
*/
public void testInitializingBeanAndSameInitMethod() throws Exception {
public @Test void testInitializingBeanAndSameInitMethod() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("initializers.xml", getClass()));
assertFalse(InitAndIB.constructed);
@@ -624,7 +626,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(iib.destroyed && !iib.customDestroyed);
}
public void testDefaultLazyInit() throws Exception {
public @Test void testDefaultLazyInit() throws Exception {
InitAndIB.constructed = false;
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("default-lazy-init.xml", getClass()));
assertFalse(InitAndIB.constructed);
@@ -638,7 +640,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testNoSuchXmlFile() throws Exception {
public @Test void testNoSuchXmlFile() throws Exception {
try {
new XmlBeanFactory(new ClassPathResource("missing.xml", getClass()));
fail("Must not create factory from missing XML");
@@ -647,7 +649,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testInvalidXmlFile() throws Exception {
public @Test void testInvalidXmlFile() throws Exception {
try {
new XmlBeanFactory(new ClassPathResource("invalid.xml", getClass()));
fail("Must not create factory from invalid XML");
@@ -656,7 +658,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testUnsatisfiedObjectDependencyCheck() throws Exception {
public @Test void testUnsatisfiedObjectDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedObjectDependencyCheck.xml", getClass()));
xbf.getBean("a", DependenciesBean.class);
@@ -666,7 +668,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testUnsatisfiedSimpleDependencyCheck() throws Exception {
public @Test void testUnsatisfiedSimpleDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedSimpleDependencyCheck.xml", getClass()));
xbf.getBean("a", DependenciesBean.class);
@@ -676,20 +678,20 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testSatisfiedObjectDependencyCheck() throws Exception {
public @Test void testSatisfiedObjectDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedObjectDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertNotNull(a.getSpouse());
assertEquals(xbf, a.getBeanFactory());
}
public void testSatisfiedSimpleDependencyCheck() throws Exception {
public @Test void testSatisfiedSimpleDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedSimpleDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
}
public void testUnsatisfiedAllDependencyCheck() throws Exception {
public @Test void testUnsatisfiedAllDependencyCheck() throws Exception {
try {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("unsatisfiedAllDependencyCheckMissingObjects.xml", getClass()));
xbf.getBean("a", DependenciesBean.class);
@@ -699,7 +701,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testSatisfiedAllDependencyCheck() throws Exception {
public @Test void testSatisfiedAllDependencyCheck() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("satisfiedAllDependencyCheck.xml", getClass()));
DependenciesBean a = (DependenciesBean) xbf.getBean("a");
assertEquals(a.getAge(), 33);
@@ -707,14 +709,14 @@ public class XmlBeanFactoryTests extends TestCase {
assertNotNull(a.getSpouse());
}
public void testAutowire() throws Exception {
public @Test void testAutowire() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass()));
TestBean spouse = new TestBean("kerry", 0);
xbf.registerSingleton("spouse", spouse);
doTestAutowire(xbf);
}
public void testAutowireWithParent() throws Exception {
public @Test void testAutowireWithParent() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("autowire.xml", getClass()));
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
MutablePropertyValues pvs = new MutablePropertyValues();
@@ -771,7 +773,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(appCtx.containsBean("jenny"));
}
public void testAutowireWithDefault() throws Exception {
public @Test void testAutowireWithDefault() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("default-autowire.xml", getClass()));
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
@@ -792,7 +794,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testAutowireByConstructor() throws Exception {
public @Test void testAutowireByConstructor() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("kerry2");
@@ -828,7 +830,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals(null, rod.getName());
}
public void testAutowireByConstructorWithSimpleValues() throws Exception {
public @Test void testAutowireByConstructorWithSimpleValues() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5");
@@ -856,7 +858,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(rod6.destroyed);
}
public void testRelatedCausesFromConstructorResolution() {
public @Test void testRelatedCausesFromConstructorResolution() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
try {
@@ -869,7 +871,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testConstructorArgResolution() {
public @Test void testConstructorArgResolution() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
@@ -911,13 +913,13 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals(29, rod16.getAge());
}
public void testConstructorArgWithSingleMatch() {
public @Test void testConstructorArgWithSingleMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
File file = (File) xbf.getBean("file");
assertEquals(File.separator + "test", file.getPath());
}
public void testThrowsExceptionOnTooManyArguments() throws Exception {
public @Test void testThrowsExceptionOnTooManyArguments() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
try {
xbf.getBean("rod7", ConstructorDependenciesBean.class);
@@ -927,7 +929,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testThrowsExceptionOnAmbiguousResolution() throws Exception {
public @Test void testThrowsExceptionOnAmbiguousResolution() throws Exception {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
try {
xbf.getBean("rod8", ConstructorDependenciesBean.class);
@@ -937,43 +939,43 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testDependsOn() {
public @Test void testDependsOn() {
doTestDependencies("dependencies-dependsOn.xml", 1);
}
public void testDependsOnInInnerBean() {
public @Test void testDependsOnInInnerBean() {
doTestDependencies("dependencies-dependsOn-inner.xml", 4);
}
public void testDependenciesThroughConstructorArguments() {
public @Test void testDependenciesThroughConstructorArguments() {
doTestDependencies("dependencies-carg.xml", 1);
}
public void testDependenciesThroughConstructorArgumentAutowiring() {
public @Test void testDependenciesThroughConstructorArgumentAutowiring() {
doTestDependencies("dependencies-carg-autowire.xml", 1);
}
public void testDependenciesThroughConstructorArgumentsInInnerBean() {
public @Test void testDependenciesThroughConstructorArgumentsInInnerBean() {
doTestDependencies("dependencies-carg-inner.xml", 1);
}
public void testDependenciesThroughProperties() {
public @Test void testDependenciesThroughProperties() {
doTestDependencies("dependencies-prop.xml", 1);
}
public void testDependenciesThroughPropertiesWithInTheMiddle() {
public @Test void testDependenciesThroughPropertiesWithInTheMiddle() {
doTestDependencies("dependencies-prop-inTheMiddle.xml", 1);
}
public void testDependenciesThroughPropertyAutowiringByName() {
public @Test void testDependenciesThroughPropertyAutowiringByName() {
doTestDependencies("dependencies-prop-autowireByName.xml", 1);
}
public void testDependenciesThroughPropertyAutowiringByType() {
public @Test void testDependenciesThroughPropertyAutowiringByType() {
doTestDependencies("dependencies-prop-autowireByType.xml", 1);
}
public void testDependenciesThroughPropertiesInInnerBean() {
public @Test void testDependenciesThroughPropertiesInInnerBean() {
doTestDependencies("dependencies-prop-inner.xml", 1);
}
@@ -1003,7 +1005,7 @@ public class XmlBeanFactoryTests extends TestCase {
* bean def is being parsed, 'cos everything on a bean def is now lazy, but
* must rather only be picked up when the bean is instantiated.
*/
public void testClassNotFoundWithDefaultBeanClassLoader() {
public @Test void testClassNotFoundWithDefaultBeanClassLoader() {
BeanFactory factory = new XmlBeanFactory(new ClassPathResource("classNotFound.xml", getClass()));
// cool, no errors, so the rubbish class name in the bean def was not resolved
try {
@@ -1017,7 +1019,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testClassNotFoundWithNoBeanClassLoader() {
public @Test void testClassNotFoundWithNoBeanClassLoader() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(bf);
reader.setBeanClassLoader(null);
@@ -1025,7 +1027,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals("WhatALotOfRubbish", bf.getBeanDefinition("classNotFound").getBeanClassName());
}
public void testResourceAndInputStream() throws IOException {
public @Test void testResourceAndInputStream() throws IOException {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("resource.xml", getClass()));
// comes from "resourceImport.xml"
ResourceTestBean resource1 = (ResourceTestBean) xbf.getBean("resource1");
@@ -1047,7 +1049,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals("test", writer.toString());
}
public void testClassPathResourceWithImport() {
public @Test void testClassPathResourceWithImport() {
XmlBeanFactory xbf = new XmlBeanFactory(
new ClassPathResource("org/springframework/beans/factory/xml/resource.xml"));
// comes from "resourceImport.xml"
@@ -1056,7 +1058,7 @@ public class XmlBeanFactoryTests extends TestCase {
xbf.getBean("resource2", ResourceTestBean.class);
}
public void testUrlResourceWithImport() {
public @Test void testUrlResourceWithImport() {
URL url = getClass().getResource("resource.xml");
XmlBeanFactory xbf = new XmlBeanFactory(new UrlResource(url));
// comes from "resourceImport.xml"
@@ -1065,7 +1067,7 @@ public class XmlBeanFactoryTests extends TestCase {
xbf.getBean("resource2", ResourceTestBean.class);
}
public void testFileSystemResourceWithImport() {
public @Test void testFileSystemResourceWithImport() {
String file = getClass().getResource("resource.xml").getFile();
XmlBeanFactory xbf = new XmlBeanFactory(new FileSystemResource(file));
// comes from "resourceImport.xml"
@@ -1074,10 +1076,9 @@ public class XmlBeanFactoryTests extends TestCase {
xbf.getBean("resource2", ResourceTestBean.class);
}
public void testRecursiveImport() {
public @Test void testRecursiveImport() {
try {
XmlBeanFactory xbf = new XmlBeanFactory(
new ClassPathResource("org/springframework/beans/factory/xml/recursiveImport.xml"));
new XmlBeanFactory(new ClassPathResource("org/springframework/beans/factory/xml/recursiveImport.xml"));
fail("Should have thrown BeanDefinitionStoreException");
}
catch (BeanDefinitionStoreException ex) {
@@ -1087,7 +1088,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
public void testLookupOverrideMethodsWithSetterInjection() {
public @Test void testLookupOverrideMethodsWithSetterInjection() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass()));
@@ -1152,7 +1153,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertSame(dave1, dave2);
}
public void testReplaceMethodOverrideWithSetterInjection() {
public @Test void testReplaceMethodOverrideWithSetterInjection() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("delegationOverrides.xml", getClass()));
@@ -1199,7 +1200,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals(s2, dos.lastArg);
}
public void testLookupOverrideOneMethodWithConstructorInjection() {
public @Test void testLookupOverrideOneMethodWithConstructorInjection() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("constructorOverrides.xml", getClass()));
@@ -1222,7 +1223,7 @@ public class XmlBeanFactoryTests extends TestCase {
fm1.getTestBean(), fm2.getTestBean());
}
public void testRejectsOverrideOfBogusMethodName() {
public @Test void testRejectsOverrideOfBogusMethodName() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
try {
@@ -1239,12 +1240,12 @@ public class XmlBeanFactoryTests extends TestCase {
/**
* Assert the presence of this bug until we resolve it.
*/
public void testSerializabilityOfMethodReplacer() throws Exception {
public @Test void testSerializabilityOfMethodReplacer() throws Exception {
try {
BUGtestSerializableMethodReplacerAndSuperclass();
fail();
}
catch (AssertionFailedError ex) {
catch (AssertionError ex) {
System.err.println("****** SPR-356: Objects with MethodReplace overrides are not serializable");
}
}
@@ -1262,7 +1263,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals("Method replace still works after serialization and deserialization", backwards, s.replaceMe(forwards));
}
public void testInnerBeanInheritsScopeFromConcreteChildDefinition() {
public @Test void testInnerBeanInheritsScopeFromConcreteChildDefinition() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("overrides.xml", getClass()));
@@ -1271,7 +1272,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(jenny.getFriends().iterator().next() instanceof TestBean);
}
public void testConstructorArgWithSingleSimpleTypeMatch() {
public @Test void testConstructorArgWithSingleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBoolean");
@@ -1281,7 +1282,7 @@ public class XmlBeanFactoryTests extends TestCase {
assertTrue(bean2.isSingleBoolean());
}
public void testConstructorArgWithDoubleSimpleTypeMatch() {
public @Test void testConstructorArgWithDoubleSimpleTypeMatch() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
SingleSimpleTypeConstructorBean bean = (SingleSimpleTypeConstructorBean) xbf.getBean("beanWithBooleanAndString");
@@ -1293,21 +1294,21 @@ public class XmlBeanFactoryTests extends TestCase {
assertEquals("A String", bean2.getTestString());
}
public void testDoubleBooleanAutowire() {
public @Test void testDoubleBooleanAutowire() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBoolean");
assertEquals(Boolean.TRUE, bean.boolean1);
assertEquals(Boolean.FALSE, bean.boolean2);
}
public void testDoubleBooleanAutowireWithIndex() {
public @Test void testDoubleBooleanAutowireWithIndex() {
XmlBeanFactory xbf = new XmlBeanFactory(new ClassPathResource("constructor-arg.xml", getClass()));
DoubleBooleanConstructorBean bean = (DoubleBooleanConstructorBean) xbf.getBean("beanWithDoubleBooleanAndIndex");
assertEquals(Boolean.FALSE, bean.boolean1);
assertEquals(Boolean.TRUE, bean.boolean2);
}
public void testWithDuplicateName() throws Exception {
public @Test void testWithDuplicateName() throws Exception {
try {
new XmlBeanFactory(new ClassPathResource("testWithDuplicateNames.xml", getClass()));
fail("Duplicate name not detected");
@@ -1317,7 +1318,7 @@ public class XmlBeanFactoryTests extends TestCase {
}
}
public void testWithDuplicateNameInAlias() throws Exception {
public @Test void testWithDuplicateNameInAlias() throws Exception {
try {
new XmlBeanFactory(new ClassPathResource("testWithDuplicateNameInAlias.xml", getClass()));
fail("Duplicate name not detected");

View File

@@ -1,145 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="default" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="defaultInstance">
<!-- No constructor-arg elements -->
<property name="stringValue"><value>setterString</value></property>
</bean>
<bean id="defaultTestBean" factory-bean="default" factory-method="getTestBean"
init-method="haveBirthday" destroy-method="destroy"/>
<bean id="defaultTestBean.protected" factory-bean="default" factory-method="protectedGetTestBean"
init-method="haveBirthday" destroy-method="destroy"/>
<bean id="defaultTestBean.private" factory-bean="default" factory-method="privateGetTestBean"
init-method="haveBirthday" destroy-method="destroy"/>
<bean id="testBeanOnly" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance">
<constructor-arg><ref local="juergen"/></constructor-arg>
</bean>
<bean id="null" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="nullInstance"/>
<bean id="nullWithProperty" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="nullInstance" init-method="getName" scope="prototype">
<property name="stringValue"><value>setterString</value></property>
</bean>
<bean id="full" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance">
<constructor-arg index="0"><ref local="juergen"/></constructor-arg>
<constructor-arg index="1"><value>27</value></constructor-arg>
<constructor-arg index="2"><value>gotcha</value></constructor-arg>
</bean>
<bean id="fullWithNull" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance">
<constructor-arg index="0"><ref local="juergen"/></constructor-arg>
<constructor-arg index="1"><value>27</value></constructor-arg>
<constructor-arg index="2" type="java.lang.Integer"><null/></constructor-arg>
</bean>
<bean id="fullWithGenericNull" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance">
<constructor-arg><ref local="juergen"/></constructor-arg>
<constructor-arg type="int"><value>27</value></constructor-arg>
<constructor-arg type="java.lang.Integer"><null/></constructor-arg>
</bean>
<bean id="fullWithAutowire" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance" autowire="constructor">
<constructor-arg index="0" ref="juergen"/>
<constructor-arg index="1" value="27"/>
</bean>
<bean id="stringToBeAutowired" class="java.lang.String">
<constructor-arg value="gotchaAutowired"/>
</bean>
<bean id="defaultPrototype" class="org.springframework.beans.factory.xml.FactoryMethods"
scope="prototype" factory-method="defaultInstance">
<!-- No constructor-arg elements -->
<property name="stringValue"><value>setterString</value></property>
</bean>
<bean id="testBeanOnlyPrototype" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance" scope="prototype">
<constructor-arg><ref local="juergen"/></constructor-arg>
<property name="stringValue"><value>testBeanOnlyPrototypeDISetterString</value></property>
</bean>
<bean id="fullPrototype" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance" scope="prototype">
<constructor-arg type="int"><value>27</value></constructor-arg>
<constructor-arg><value>gotcha</value></constructor-arg>
<constructor-arg><ref local="juergen"/></constructor-arg>
</bean>
<bean id="noMatchPrototype" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="newInstance" scope="prototype">
<constructor-arg index="0"><ref local="juergen"/></constructor-arg>
<constructor-arg index="1"><value>27</value></constructor-arg>
<constructor-arg index="2"><value>gotcha</value></constructor-arg>
<constructor-arg index="3"><value>bogus</value></constructor-arg>
</bean>
<bean id="listInstance" class="org.springframework.beans.factory.xml.FactoryMethods"
factory-method="listInstance"/>
<bean id="juergen" class="org.springframework.beans.TestBean">
<property name="name"><value>Juergen</value></property>
</bean>
<!--
The class is the factory class, not the created class.
-->
<bean id="externalFactoryMethodWithoutArgs"
class="org.springframework.beans.factory.xml.TestBeanCreator"
factory-method="createTestBean">
</bean>
<bean id="externalFactoryMethodWithArgs" class="org.springframework.beans.factory.xml.TestBeanCreator"
factory-method="createTestBean">
<constructor-arg index="0"><value>Rod</value></constructor-arg>
<constructor-arg><value type="java.lang.Integer">33</value></constructor-arg>
</bean>
<bean id="instanceFactoryMethodWithoutArgs"
factory-bean="instanceFactory"
factory-method="defaultInstance"/>
<!-- Unnamed bean with factory-bean declaration -->
<bean factory-bean="instanceFactory" factory-method="defaultInstance"/>
<bean id="testBeanWithInnerFactoryMethod" class="org.springframework.beans.TestBean">
<property name="friends">
<list>
<!-- Unnamed bean with factory-bean declaration -->
<bean factory-bean="instanceFactory" factory-method="defaultInstance"/>
</list>
</property>
</bean>
<bean id="instanceFactory" class="org.springframework.beans.factory.xml.InstanceFactory" scope="singleton">
<property name="factoryBeanProperty"><value>instanceFactory</value></property>
</bean>
<bean name="javaMailSession" class="javax.mail.Session" factory-method="getDefaultInstance">
<constructor-arg>
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.host"></prop>
<prop key="mail.smtp.port"></prop>
<prop key="mail.smtp.user">someuser</prop>
<prop key="mail.smtp.password">somepw</prop>
</props>
</constructor-arg>
</bean>
</beans>