提交 6cb71bbb 编写于 作者: C Chris Beams

moving unit tests from .testsuite -> .beans

上级 8977ad40
......@@ -14,17 +14,23 @@
* limitations under the License.
*/
package org.springframework.beans.factory.annotation;
package org.springframework.beans.annotation;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.AnnotationBeanWiringInfoResolver;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.wiring.BeanWiringInfo;
/**
* @author Rick Evans
* @author Chris Beams
*/
public class AnnotationBeanWiringInfoResolverTests extends TestCase {
public class AnnotationBeanWiringInfoResolverTests {
@Test
public void testResolveWiringInfo() throws Exception {
try {
new AnnotationBeanWiringInfoResolver().resolveWiringInfo(null);
......@@ -34,18 +40,21 @@ public class AnnotationBeanWiringInfoResolverTests extends TestCase {
}
}
@Test
public void testResolveWiringInfoWithAnInstanceOfANonAnnotatedClass() {
AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver();
BeanWiringInfo info = resolver.resolveWiringInfo("java.lang.String is not @Configurable");
assertNull("Must be returning null for a non-@Configurable class instance", info);
}
@Test
public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClass() {
AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver();
BeanWiringInfo info = resolver.resolveWiringInfo(new Soap());
assertNotNull("Must *not* be returning null for a non-@Configurable class instance", info);
}
@Test
public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClassWithAutowiringTurnedOffExplicitly() {
AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver();
BeanWiringInfo info = resolver.resolveWiringInfo(new WirelessSoap());
......@@ -54,6 +63,7 @@ public class AnnotationBeanWiringInfoResolverTests extends TestCase {
assertEquals(WirelessSoap.class.getName(), info.getBeanName());
}
@Test
public void testResolveWiringInfoWithAnInstanceOfAnAnnotatedClassWithAutowiringTurnedOffExplicitlyAndCustomBeanName() {
AnnotationBeanWiringInfoResolver resolver = new AnnotationBeanWiringInfoResolver();
BeanWiringInfo info = resolver.resolveWiringInfo(new NamedWirelessSoap());
......
......@@ -16,12 +16,16 @@
package org.springframework.beans.factory;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import junit.framework.TestCase;
import net.sf.cglib.proxy.NoOp;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.ITestBean;
import org.springframework.beans.IndexedTestBean;
import org.springframework.beans.TestBean;
......@@ -30,20 +34,21 @@ import org.springframework.beans.factory.support.StaticListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerAdapter;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
* @since 04.07.2003
*/
public class BeanFactoryUtilsTests extends TestCase {
public class BeanFactoryUtilsTests {
private ConfigurableListableBeanFactory listableBeanFactory;
private ConfigurableListableBeanFactory dependentBeansBF;
protected void setUp() {
@Before
public void setUp() {
// Interesting hierarchical factory to test counts.
// Slow to read so we cache it.
XmlBeanFactory grandParent = new XmlBeanFactory(new ClassPathResource("root.xml", getClass()));
......@@ -54,6 +59,7 @@ public class BeanFactoryUtilsTests extends TestCase {
this.listableBeanFactory = child;
}
@Test
public void testHierarchicalCountBeansWithNonHierarchicalFactory() {
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
lbf.addBean("t1", new TestBean());
......@@ -64,6 +70,7 @@ public class BeanFactoryUtilsTests extends TestCase {
/**
* Check that override doesn't count as two separate beans.
*/
@Test
public void testHierarchicalCountBeansWithOverride() throws Exception {
// Leaf count
assertTrue(this.listableBeanFactory.getBeanDefinitionCount() == 1);
......@@ -73,14 +80,16 @@ public class BeanFactoryUtilsTests extends TestCase {
BeanFactoryUtils.countBeansIncludingAncestors(this.listableBeanFactory) == 7);
}
@Test
public void testHierarchicalNamesWithNoMatch() throws Exception {
List names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
HandlerAdapter.class));
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
NoOp.class));
assertEquals(0, names.size());
}
@Test
public void testHierarchicalNamesWithMatchOnlyInRoot() throws Exception {
List names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
IndexedTestBean.class));
assertEquals(1, names.size());
assertTrue(names.contains("indexedBean"));
......@@ -88,8 +97,9 @@ public class BeanFactoryUtilsTests extends TestCase {
assertTrue(listableBeanFactory.getBeanNamesForType(IndexedTestBean.class).length == 0);
}
@Test
public void testGetBeanNamesForTypeWithOverride() throws Exception {
List names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
List<String> names = Arrays.asList(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(this.listableBeanFactory,
ITestBean.class));
// includes 2 TestBeans from FactoryBeans (DummyFactory definitions)
assertEquals(4, names.size());
......@@ -99,13 +109,15 @@ public class BeanFactoryUtilsTests extends TestCase {
assertTrue(names.contains("testFactory2"));
}
@Test
public void testNoBeansOfType() {
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
lbf.addBean("foo", new Object());
Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
assertTrue(beans.isEmpty());
}
@Test
public void testFindsBeansOfTypeWithStaticFactory() {
StaticListableBeanFactory lbf = new StaticListableBeanFactory();
TestBean t1 = new TestBean();
......@@ -118,7 +130,7 @@ public class BeanFactoryUtilsTests extends TestCase {
lbf.addBean("t3", t3);
lbf.addBean("t4", t4);
Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, ITestBean.class, true, false);
assertEquals(2, beans.size());
assertEquals(t1, beans.get("t1"));
assertEquals(t2, beans.get("t2"));
......@@ -147,6 +159,7 @@ public class BeanFactoryUtilsTests extends TestCase {
assertEquals(t4, beans.get("&t4"));
}
@Test
public void testFindsBeansOfTypeWithDefaultFactory() {
Object test3 = this.listableBeanFactory.getBean("test3");
Object test = this.listableBeanFactory.getBean("test");
......@@ -161,7 +174,7 @@ public class BeanFactoryUtilsTests extends TestCase {
this.listableBeanFactory.registerSingleton("t3", t3);
this.listableBeanFactory.registerSingleton("t4", t4);
Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
false);
assertEquals(6, beans.size());
assertEquals(test3, beans.get("test3"));
......@@ -212,11 +225,12 @@ public class BeanFactoryUtilsTests extends TestCase {
assertEquals(t4, beans.get("&t4"));
}
@Test
public void testHierarchicalResolutionWithOverride() throws Exception {
Object test3 = this.listableBeanFactory.getBean("test3");
Object test = this.listableBeanFactory.getBean("test");
Map beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
Map<?, ?> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(this.listableBeanFactory, ITestBean.class, true,
false);
assertEquals(2, beans.size());
assertEquals(test3, beans.get("test3"));
......@@ -250,21 +264,25 @@ public class BeanFactoryUtilsTests extends TestCase {
assertEquals(this.listableBeanFactory.getBean("&testFactory2"), beans.get("&testFactory2"));
}
@Test
public void testADependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("a");
assertTrue(ObjectUtils.isEmpty(deps));
}
@Test
public void testBDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("b");
assertTrue(Arrays.equals(new String[] { "c" }, deps));
}
@Test
public void testCDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("c");
assertTrue(Arrays.equals(new String[] { "int", "long" }, deps));
}
@Test
public void testIntDependencies() {
String[] deps = this.dependentBeansBF.getDependentBeans("int");
assertTrue(Arrays.equals(new String[] { "buffer" }, deps));
......
......@@ -16,28 +16,36 @@
package org.springframework.beans.factory;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.Assert;
/**
* @author Rob Harrop
* @author Juergen Hoeller
* @author Chris Beams
*/
public class FactoryBeanTests extends TestCase {
public class FactoryBeanTests {
@Test
public void testFactoryBeanReturnsNull() throws Exception {
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("factoryBeanReturnsNull.xml", getClass()));
Object result = factory.getBean("factoryBean");
assertNull(result);
}
@Test
public void testFactoryBeansWithAutowiring() throws Exception {
ClassPathXmlApplicationContext factory =
new ClassPathXmlApplicationContext("factoryBeansWithAutowiring.xml", getClass());
XmlBeanFactory factory =
new XmlBeanFactory(new ClassPathResource("factoryBeansWithAutowiring.xml", getClass()));
BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
ppc.postProcessBeanFactory(factory);
Alpha alpha = (Alpha) factory.getBean("alpha");
Beta beta = (Beta) factory.getBean("beta");
Gamma gamma = (Gamma) factory.getBean("gamma");
......@@ -48,9 +56,14 @@ public class FactoryBeanTests extends TestCase {
assertEquals("yourName", beta.getName());
}
@Test
public void testFactoryBeansWithIntermediateFactoryBeanAutowiringFailure() throws Exception {
ClassPathXmlApplicationContext factory =
new ClassPathXmlApplicationContext("factoryBeansWithAutowiring.xml", getClass());
XmlBeanFactory factory =
new XmlBeanFactory(new ClassPathResource("factoryBeansWithAutowiring.xml", getClass()));
BeanFactoryPostProcessor ppc = (BeanFactoryPostProcessor) factory.getBean("propertyPlaceholderConfigurer");
ppc.postProcessBeanFactory(factory);
Beta beta = (Beta) factory.getBean("beta");
Alpha alpha = (Alpha) factory.getBean("alpha");
Gamma gamma = (Gamma) factory.getBean("gamma");
......@@ -59,13 +72,13 @@ public class FactoryBeanTests extends TestCase {
}
public static class NullReturningFactoryBean implements FactoryBean {
public static class NullReturningFactoryBean implements FactoryBean<Object> {
public Object getObject() {
return null;
}
public Class getObjectType() {
public Class<?> getObjectType() {
return null;
}
......@@ -125,7 +138,7 @@ public class FactoryBeanTests extends TestCase {
}
public static class BetaFactoryBean implements FactoryBean {
public static class BetaFactoryBean implements FactoryBean<Object> {
private Beta beta;
......@@ -137,7 +150,7 @@ public class FactoryBeanTests extends TestCase {
return this.beta;
}
public Class getObjectType() {
public Class<?> getObjectType() {
return null;
}
......
......@@ -3,24 +3,11 @@
<beans default-lazy-init="true">
<bean id="abstractBeta" class="org.springframework.aop.framework.ProxyFactoryBean"
abstract="true"/>
<bean name="beta" parent="abstractBeta">
<property name="target">
<bean class="org.springframework.beans.factory.FactoryBeanTests$Beta" autowire="byType">
<bean name="beta" class="org.springframework.beans.factory.FactoryBeanTests$Beta" autowire="byType">
<property name="name" value="${myName}"/>
</bean>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
<bean id="alpha" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<bean class="org.springframework.beans.factory.FactoryBeanTests$Alpha" autowire="byType"/>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
<bean id="alpha" class="org.springframework.beans.factory.FactoryBeanTests$Alpha" autowire="byType"/>
<bean id="gamma" class="org.springframework.beans.factory.FactoryBeanTests$Gamma"/>
......@@ -30,7 +17,7 @@
<bean id="gammaFactory" factory-bean="betaFactory" factory-method="getGamma"/>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<props>
<prop key="myName">yourName</prop>
......
......@@ -6,7 +6,7 @@
<!--
Just included for the count: not to mean anything in particular
-->
<bean id="something" class="org.springframework.aop.support.DefaultPointcutAdvisor"/>
<bean id="something" class="org.springframework.beans.GenericIntegerBean"/>
<bean id="indexedBean" class="org.springframework.beans.IndexedTestBean"/>
......
/*
* 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 java.io.Serializable;
import org.springframework.beans.IndexedTestBean;
import org.springframework.beans.TestBean;
/**
* Simple bean used to check constructor dependency checking.
*
* @author Juergen Hoeller
* @since 09.11.2003
*/
public class ConstructorDependenciesBean implements Serializable {
private int age;
private String name;
private TestBean spouse1;
private TestBean spouse2;
private IndexedTestBean other;
public ConstructorDependenciesBean(int age) {
this.age = age;
}
public ConstructorDependenciesBean(String name) {
this.name = name;
}
public ConstructorDependenciesBean(TestBean spouse1) {
this.spouse1 = spouse1;
}
public ConstructorDependenciesBean(TestBean spouse1, TestBean spouse2) {
this.spouse1 = spouse1;
this.spouse2 = spouse2;
}
public ConstructorDependenciesBean(TestBean spouse1, TestBean spouse2, int age) {
this.spouse1 = spouse1;
this.spouse2 = spouse2;
this.age = age;
}
public ConstructorDependenciesBean(TestBean spouse1, TestBean spouse2, IndexedTestBean other) {
this.spouse1 = spouse1;
this.spouse2 = spouse2;
this.other = other;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
public TestBean getSpouse1() {
return spouse1;
}
public TestBean getSpouse2() {
return spouse2;
}
public IndexedTestBean getOther() {
return other;
}
public void setAge(int age) {
this.age = age;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Copyright 2002-2006 the original author or authors.
*
* 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.
......@@ -14,51 +14,59 @@
* limitations under the License.
*/
package org.springframework.beans.factory;
package org.springframework.beans.factory.xml;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
/**
* @author Juergen Hoeller
* Simple bean used to test dependency checking.
*
* @author Rod Johnson
* @since 04.09.2003
*/
public class CountingFactory implements FactoryBean {
public class DependenciesBean implements BeanFactoryAware {
private int age;
private static int factoryBeanInstanceCount = 0;
private String name;
private TestBean spouse;
private BeanFactory beanFactory;
/**
* Clear static state.
*/
public static void reset() {
factoryBeanInstanceCount = 0;
}
public static int getFactoryBeanInstanceCount() {
return factoryBeanInstanceCount;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
public CountingFactory() {
factoryBeanInstanceCount++;
public void setName(String name) {
this.name = name;
}
public void setTestBean(TestBean tb) {
if (tb.getSpouse() == null) {
throw new IllegalStateException("TestBean needs to have spouse");
}
public String getName() {
return name;
}
public void setSpouse(TestBean spouse) {
this.spouse = spouse;
}
public Object getObject() {
return "myString";
public TestBean getSpouse() {
return spouse;
}
public Class getObjectType() {
return String.class;
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public boolean isSingleton() {
return true;
public BeanFactory getBeanFactory() {
return beanFactory;
}
}
package org.springframework.beans.factory.xml;
/**
* Bean that changes state on a business invocation, so that
* we can check whether it's been invoked
* @author Rod Johnson
*/
public class SideEffectBean {
private int count;
public void setCount(int count) {
this.count = count;
}
public int getCount() {
return this.count;
}
public void doWork() {
++count;
}
}
\ No newline at end of file
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory;
/**
* Used in the tests for the FieldRetrievingFactoryBean class
* (c.f. FieldRetrievingFactoryBeanTests)
*
* @author Rick Evans
*/
class PackageLevelVisibleBean {
public static final String CONSTANT = "Wuby";
}
<?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="rob" class="org.springframework.beans.TestBean" autowire="byType"/>
<bean id="sally" class="org.springframework.beans.TestBean"/>
<bean id="props1" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<value>name=props1</value>
</property>
</bean>
<bean id="props2" class="org.springframework.beans.factory.config.PropertiesFactoryBean" autowire-candidate="false">
<property name="properties">
<value>name=props2</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.CountingFactory">
<property name="testBean" ref="rob"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire-candidates="">
<bean id="rob" class="org.springframework.beans.TestBean" autowire="byType"/>
<bean id="sally" class="org.springframework.beans.TestBean" autowire-candidate="true"/>
<bean id="props1" class="org.springframework.beans.factory.config.PropertiesFactoryBean" autowire-candidate="true">
<property name="properties">
<value>name=props1</value>
</property>
</bean>
<bean id="props2" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<value>name=props2</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.CountingFactory">
<property name="testBean" ref="rob"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"
default-autowire-candidates="props*,*ly">
<bean id="rob" class="org.springframework.beans.TestBean" autowire="byType"/>
<bean id="sally" class="org.springframework.beans.TestBean"/>
<bean id="props1" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<value>name=props1</value>
</property>
</bean>
<bean id="props2" class="org.springframework.beans.factory.config.PropertiesFactoryBean" autowire-candidate="false">
<property name="properties">
<value>name=props2</value>
</property>
</bean>
<bean id="someProps" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<value>name=someProps</value>
</property>
</bean>
<bean class="org.springframework.beans.factory.CountingFactory">
<property name="testBean" ref="rob"/>
</bean>
</beans>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册