moving unit tests from .testsuite -> .beans
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.junit.Test;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.InputSource;
|
||||
|
||||
/**
|
||||
* Unit tests for the {@link DelegatingEntityResolver} class.
|
||||
*
|
||||
* @author Rick Evans
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public final class DelegatingEntityResolverTests {
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWhereDtdEntityResolverIsNull() throws Exception {
|
||||
new DelegatingEntityResolver(null, new NoOpEntityResolver());
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWhereSchemaEntityResolverIsNull() throws Exception {
|
||||
new DelegatingEntityResolver(new NoOpEntityResolver(), null);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void testCtorWhereEntityResolversAreBothNull() throws Exception {
|
||||
new DelegatingEntityResolver(null, null);
|
||||
}
|
||||
|
||||
|
||||
private static final class NoOpEntityResolver implements EntityResolver {
|
||||
public InputSource resolveEntity(String publicId, String systemId) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
/*
|
||||
* 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 static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Test;
|
||||
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
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class FactoryMethodTests {
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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.
|
||||
*/
|
||||
@Test
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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());
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFactoryMethodForJavaMailSession() {
|
||||
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
|
||||
|
||||
MailSession session = (MailSession) xbf.getBean("javaMailSession");
|
||||
assertEquals("someuser", session.getProperty("mail.smtp.user"));
|
||||
assertEquals("somepw", session.getProperty("mail.smtp.password"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MailSession {
|
||||
private Properties props;
|
||||
|
||||
private MailSession() {
|
||||
}
|
||||
|
||||
public void setProperties(Properties props) {
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
public static MailSession getDefaultInstance(Properties props) {
|
||||
MailSession session = new MailSession();
|
||||
session.setProperties(props);
|
||||
return session;
|
||||
}
|
||||
|
||||
public Object getProperty(String key) {
|
||||
return props.get(key);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* 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 java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.TestBean;
|
||||
|
||||
/**
|
||||
* Test class for Spring's ability to create objects using static
|
||||
* factory methods, rather than constructors.
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
*/
|
||||
public class FactoryMethods {
|
||||
|
||||
public static FactoryMethods nullInstance() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static FactoryMethods defaultInstance() {
|
||||
TestBean tb = new TestBean();
|
||||
tb.setName("defaultInstance");
|
||||
return new FactoryMethods(tb, "default", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note that overloaded methods are supported.
|
||||
*/
|
||||
public static FactoryMethods newInstance(TestBean tb) {
|
||||
return new FactoryMethods(tb, "default", 0);
|
||||
}
|
||||
|
||||
protected static FactoryMethods newInstance(TestBean tb, int num, String name) {
|
||||
if (name == null) {
|
||||
throw new IllegalStateException("Should never be called with null value");
|
||||
}
|
||||
return new FactoryMethods(tb, name, num);
|
||||
}
|
||||
|
||||
static FactoryMethods newInstance(TestBean tb, int num, Integer something) {
|
||||
if (something != null) {
|
||||
throw new IllegalStateException("Should never be called with non-null value");
|
||||
}
|
||||
return new FactoryMethods(tb, null, num);
|
||||
}
|
||||
|
||||
private static List listInstance() {
|
||||
return Collections.EMPTY_LIST;
|
||||
}
|
||||
|
||||
|
||||
private int num = 0;
|
||||
private String name = "default";
|
||||
private TestBean tb;
|
||||
private String stringValue;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor is private: not for use outside this class,
|
||||
* even by IoC container.
|
||||
*/
|
||||
private FactoryMethods(TestBean tb, String name, int num) {
|
||||
this.tb = tb;
|
||||
this.name = name;
|
||||
this.num = num;
|
||||
}
|
||||
|
||||
public void setStringValue(String stringValue) {
|
||||
this.stringValue = stringValue;
|
||||
}
|
||||
|
||||
public String getStringValue() {
|
||||
return this.stringValue;
|
||||
}
|
||||
|
||||
public TestBean getTestBean() {
|
||||
return this.tb;
|
||||
}
|
||||
|
||||
protected TestBean protectedGetTestBean() {
|
||||
return this.tb;
|
||||
}
|
||||
|
||||
private TestBean privateGetTestBean() {
|
||||
return this.tb;
|
||||
}
|
||||
|
||||
public int getNum() {
|
||||
return num;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set via Setter Injection once instance is created.
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
<?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="org.springframework.beans.factory.xml.MailSession"
|
||||
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>
|
||||
Reference in New Issue
Block a user