From 64aa2620bc320f58e8fd8cbbd7d27d907e30b5e4 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sun, 14 Dec 2008 02:50:58 +0000 Subject: [PATCH] added missing base classes, eliminated unused *Bean classes --- .../beans/AbstractPropertyValuesTests.java | 54 +++ .../factory/AbstractBeanFactoryTests.java | 329 ++++++++++++++++++ .../AbstractListableBeanFactoryTests.java | 86 +++++ .../support/DerivedFromProtectedBaseBean.java | 25 -- .../beans/support/ProtectedBaseBean.java | 35 -- 5 files changed, 469 insertions(+), 60 deletions(-) create mode 100644 org.springframework.testsuite/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java create mode 100644 org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractBeanFactoryTests.java create mode 100644 org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractListableBeanFactoryTests.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java delete mode 100644 org.springframework.testsuite/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java diff --git a/org.springframework.testsuite/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java b/org.springframework.testsuite/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java new file mode 100644 index 00000000000..dfbf6cc4c75 --- /dev/null +++ b/org.springframework.testsuite/src/test/java/org/springframework/beans/AbstractPropertyValuesTests.java @@ -0,0 +1,54 @@ +/* + * 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; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +/** + * @author Rod Johnson + */ +public abstract class AbstractPropertyValuesTests extends TestCase { + + /** + * Must contain: forname=Tony surname=Blair age=50 + */ + protected void doTestTony(PropertyValues pvs) throws Exception { + assertTrue("Contains 3", pvs.getPropertyValues().length == 3); + assertTrue("Contains forname", pvs.contains("forname")); + assertTrue("Contains surname", pvs.contains("surname")); + assertTrue("Contains age", pvs.contains("age")); + assertTrue("Doesn't contain tory", !pvs.contains("tory")); + + PropertyValue[] ps = pvs.getPropertyValues(); + Map m = new HashMap(); + m.put("forname", "Tony"); + m.put("surname", "Blair"); + m.put("age", "50"); + for (int i = 0; i < ps.length; i++) { + Object val = m.get(ps[i].getName()); + assertTrue("Can't have unexpected value", val != null); + assertTrue("Val i string", val instanceof String); + assertTrue("val matches expected", val.equals(ps[i].getValue())); + m.remove(ps[i].getName()); + } + assertTrue("Map size is 0", m.size() == 0); + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractBeanFactoryTests.java b/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractBeanFactoryTests.java new file mode 100644 index 00000000000..98bcb441d5c --- /dev/null +++ b/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractBeanFactoryTests.java @@ -0,0 +1,329 @@ +/* + * 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; + +import java.beans.PropertyEditorSupport; +import java.util.StringTokenizer; + +import junit.framework.TestCase; +import junit.framework.Assert; + +import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyBatchUpdateException; +import org.springframework.beans.TestBean; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; + +/** + * Subclasses must implement setUp() to initialize bean factory + * and any other variables they need. + * + * @author Rod Johnson + * @author Juergen Hoeller + */ +public abstract class AbstractBeanFactoryTests extends TestCase { + + protected abstract BeanFactory getBeanFactory(); + + /** + * Roderick beans inherits from rod, overriding name only. + */ + public void testInheritance() { + assertTrue(getBeanFactory().containsBean("rod")); + assertTrue(getBeanFactory().containsBean("roderick")); + TestBean rod = (TestBean) getBeanFactory().getBean("rod"); + TestBean roderick = (TestBean) getBeanFactory().getBean("roderick"); + assertTrue("not == ", rod != roderick); + assertTrue("rod.name is Rod", rod.getName().equals("Rod")); + assertTrue("rod.age is 31", rod.getAge() == 31); + assertTrue("roderick.name is Roderick", roderick.getName().equals("Roderick")); + assertTrue("roderick.age was inherited", roderick.getAge() == rod.getAge()); + } + + public void testGetBeanWithNullArg() { + try { + getBeanFactory().getBean(null); + fail("Can't get null bean"); + } + catch (IllegalArgumentException ex) { + // OK + } + } + + /** + * Test that InitializingBean objects receive the afterPropertiesSet() callback + */ + public void testInitializingBeanCallback() { + MustBeInitialized mbi = (MustBeInitialized) getBeanFactory().getBean("mustBeInitialized"); + // The dummy business method will throw an exception if the + // afterPropertiesSet() callback wasn't invoked + mbi.businessMethod(); + } + + /** + * Test that InitializingBean/BeanFactoryAware/DisposableBean objects receive the + * afterPropertiesSet() callback before BeanFactoryAware callbacks + */ + public void testLifecycleCallbacks() { + LifecycleBean lb = (LifecycleBean) getBeanFactory().getBean("lifecycle"); + Assert.assertEquals("lifecycle", lb.getBeanName()); + // The dummy business method will throw an exception if the + // necessary callbacks weren't invoked in the right order. + lb.businessMethod(); + assertTrue("Not destroyed", !lb.isDestroyed()); + } + + public void testFindsValidInstance() { + try { + Object o = getBeanFactory().getBean("rod"); + assertTrue("Rod bean is a TestBean", o instanceof TestBean); + TestBean rod = (TestBean) o; + assertTrue("rod.name is Rod", rod.getName().equals("Rod")); + assertTrue("rod.age is 31", rod.getAge() == 31); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance"); + } + } + + public void testGetInstanceByMatchingClass() { + try { + Object o = getBeanFactory().getBean("rod", TestBean.class); + assertTrue("Rod bean is a TestBean", o instanceof TestBean); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance with matching class"); + } + } + + public void testGetInstanceByNonmatchingClass() { + try { + Object o = getBeanFactory().getBean("rod", BeanFactory.class); + fail("Rod bean is not of type BeanFactory; getBeanInstance(rod, BeanFactory.class) should throw BeanNotOfRequiredTypeException"); + } + catch (BeanNotOfRequiredTypeException ex) { + // So far, so good + assertTrue("Exception has correct bean name", ex.getBeanName().equals("rod")); + assertTrue("Exception requiredType must be BeanFactory.class", ex.getRequiredType().equals(BeanFactory.class)); + assertTrue("Exception actualType as TestBean.class", TestBean.class.isAssignableFrom(ex.getActualType())); + assertTrue("Actual type is correct", ex.getActualType() == getBeanFactory().getBean("rod").getClass()); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance"); + } + } + + public void testGetSharedInstanceByMatchingClass() { + try { + Object o = getBeanFactory().getBean("rod", TestBean.class); + assertTrue("Rod bean is a TestBean", o instanceof TestBean); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance with matching class"); + } + } + + public void testGetSharedInstanceByMatchingClassNoCatch() { + Object o = getBeanFactory().getBean("rod", TestBean.class); + assertTrue("Rod bean is a TestBean", o instanceof TestBean); + } + + public void testGetSharedInstanceByNonmatchingClass() { + try { + Object o = getBeanFactory().getBean("rod", BeanFactory.class); + fail("Rod bean is not of type BeanFactory; getBeanInstance(rod, BeanFactory.class) should throw BeanNotOfRequiredTypeException"); + } + catch (BeanNotOfRequiredTypeException ex) { + // So far, so good + assertTrue("Exception has correct bean name", ex.getBeanName().equals("rod")); + assertTrue("Exception requiredType must be BeanFactory.class", ex.getRequiredType().equals(BeanFactory.class)); + assertTrue("Exception actualType as TestBean.class", TestBean.class.isAssignableFrom(ex.getActualType())); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance"); + } + } + + public void testSharedInstancesAreEqual() { + try { + Object o = getBeanFactory().getBean("rod"); + assertTrue("Rod bean1 is a TestBean", o instanceof TestBean); + Object o1 = getBeanFactory().getBean("rod"); + assertTrue("Rod bean2 is a TestBean", o1 instanceof TestBean); + assertTrue("Object equals applies", o == o1); + } + catch (Exception ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on getting valid instance"); + } + } + + public void testPrototypeInstancesAreIndependent() { + TestBean tb1 = (TestBean) getBeanFactory().getBean("kathy"); + TestBean tb2 = (TestBean) getBeanFactory().getBean("kathy"); + assertTrue("ref equal DOES NOT apply", tb1 != tb2); + assertTrue("object equal true", tb1.equals(tb2)); + tb1.setAge(1); + tb2.setAge(2); + assertTrue("1 age independent = 1", tb1.getAge() == 1); + assertTrue("2 age independent = 2", tb2.getAge() == 2); + assertTrue("object equal now false", !tb1.equals(tb2)); + } + + public void testNotThere() { + assertFalse(getBeanFactory().containsBean("Mr Squiggle")); + try { + Object o = getBeanFactory().getBean("Mr Squiggle"); + fail("Can't find missing bean"); + } + catch (BeansException ex) { + //ex.printStackTrace(); + //fail("Shouldn't throw exception on getting valid instance"); + } + } + + public void testValidEmpty() { + try { + Object o = getBeanFactory().getBean("validEmpty"); + assertTrue("validEmpty bean is a TestBean", o instanceof TestBean); + TestBean ve = (TestBean) o; + assertTrue("Valid empty has defaults", ve.getName() == null && ve.getAge() == 0 && ve.getSpouse() == null); + } + catch (BeansException ex) { + ex.printStackTrace(); + fail("Shouldn't throw exception on valid empty"); + } + } + + public void xtestTypeMismatch() { + try { + Object o = getBeanFactory().getBean("typeMismatch"); + fail("Shouldn't succeed with type mismatch"); + } + catch (BeanCreationException wex) { + assertEquals("typeMismatch", wex.getBeanName()); + assertTrue(wex.getCause() instanceof PropertyBatchUpdateException); + PropertyBatchUpdateException ex = (PropertyBatchUpdateException) wex.getCause(); + // Further tests + assertTrue("Has one error ", ex.getExceptionCount() == 1); + assertTrue("Error is for field age", ex.getPropertyAccessException("age") != null); + assertTrue("We have rejected age in exception", ex.getPropertyAccessException("age").getPropertyChangeEvent().getNewValue().equals("34x")); + } + } + + public void testGrandparentDefinitionFoundInBeanFactory() throws Exception { + TestBean dad = (TestBean) getBeanFactory().getBean("father"); + assertTrue("Dad has correct name", dad.getName().equals("Albert")); + } + + public void testFactorySingleton() throws Exception { + assertTrue(getBeanFactory().isSingleton("&singletonFactory")); + assertTrue(getBeanFactory().isSingleton("singletonFactory")); + TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory"); + assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME)); + DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); + TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory"); + assertTrue("Singleton references ==", tb == tb2); + assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null); + } + + public void testFactoryPrototype() throws Exception { + assertTrue(getBeanFactory().isSingleton("&prototypeFactory")); + assertFalse(getBeanFactory().isSingleton("prototypeFactory")); + TestBean tb = (TestBean) getBeanFactory().getBean("prototypeFactory"); + assertTrue(!tb.getName().equals(DummyFactory.SINGLETON_NAME)); + TestBean tb2 = (TestBean) getBeanFactory().getBean("prototypeFactory"); + assertTrue("Prototype references !=", tb != tb2); + } + + /** + * Check that we can get the factory bean itself. + * This is only possible if we're dealing with a factory + * @throws Exception + */ + public void testGetFactoryItself() throws Exception { + DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); + assertTrue(factory != null); + } + + /** + * Check that afterPropertiesSet gets called on factory + * @throws Exception + */ + public void testFactoryIsInitialized() throws Exception { + TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory"); + DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory"); + assertTrue("Factory was initialized because it implemented InitializingBean", factory.wasInitialized()); + } + + /** + * It should be illegal to dereference a normal bean + * as a factory + */ + public void testRejectsFactoryGetOnNormalBean() { + try { + getBeanFactory().getBean("&rod"); + fail("Shouldn't permit factory get on normal bean"); + } + catch (BeanIsNotAFactoryException ex) { + // Ok + } + } + + // TODO: refactor in AbstractBeanFactory (tests for AbstractBeanFactory) + // and rename this class + public void testAliasing() { + BeanFactory bf = getBeanFactory(); + if (!(bf instanceof ConfigurableBeanFactory)) { + return; + } + ConfigurableBeanFactory cbf = (ConfigurableBeanFactory) bf; + + String alias = "rods alias"; + try { + cbf.getBean(alias); + fail("Shouldn't permit factory get on normal bean"); + } + catch (NoSuchBeanDefinitionException ex) { + // Ok + assertTrue(alias.equals(ex.getBeanName())); + } + + // Create alias + cbf.registerAlias("rod", alias); + Object rod = getBeanFactory().getBean("rod"); + Object aliasRod = getBeanFactory().getBean(alias); + assertTrue(rod == aliasRod); + } + + + public static class TestBeanEditor extends PropertyEditorSupport { + + public void setAsText(String text) { + TestBean tb = new TestBean(); + StringTokenizer st = new StringTokenizer(text, "_"); + tb.setName(st.nextToken()); + tb.setAge(Integer.parseInt(st.nextToken())); + setValue(tb); + } + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractListableBeanFactoryTests.java b/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractListableBeanFactoryTests.java new file mode 100644 index 00000000000..d683491a1f6 --- /dev/null +++ b/org.springframework.testsuite/src/test/java/org/springframework/beans/factory/AbstractListableBeanFactoryTests.java @@ -0,0 +1,86 @@ +/* + * Copyright 2002-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory; + +import junit.framework.Assert; + +import org.springframework.beans.TestBean; + +/** + * @author Rod Johnson + * @author Juergen Hoeller + */ +public abstract class AbstractListableBeanFactoryTests extends AbstractBeanFactoryTests { + + /** Subclasses must initialize this */ + protected ListableBeanFactory getListableBeanFactory() { + BeanFactory bf = getBeanFactory(); + if (!(bf instanceof ListableBeanFactory)) { + throw new IllegalStateException("ListableBeanFactory required"); + } + return (ListableBeanFactory) bf; + } + + /** + * Subclasses can override this. + */ + public void testCount() { + assertCount(13); + } + + protected final void assertCount(int count) { + String[] defnames = getListableBeanFactory().getBeanDefinitionNames(); + Assert.assertTrue("We should have " + count + " beans, not " + defnames.length, defnames.length == count); + } + + public void assertTestBeanCount(int count) { + String[] defNames = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, false); + Assert.assertTrue("We should have " + count + " beans for class org.springframework.beans.TestBean, not " + + defNames.length, defNames.length == count); + + int countIncludingFactoryBeans = count + 2; + String[] names = getListableBeanFactory().getBeanNamesForType(TestBean.class, true, true); + Assert.assertTrue("We should have " + countIncludingFactoryBeans + + " beans for class org.springframework.beans.TestBean, not " + names.length, + names.length == countIncludingFactoryBeans); + } + + public void testGetDefinitionsForNoSuchClass() { + String[] defnames = getListableBeanFactory().getBeanNamesForType(String.class); + Assert.assertTrue("No string definitions", defnames.length == 0); + } + + /** + * Check that count refers to factory class, not bean class. (We don't know + * what type factories may return, and it may even change over time.) + */ + public void testGetCountForFactoryClass() { + Assert.assertTrue("Should have 2 factories, not " + + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length, + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2); + + Assert.assertTrue("Should have 2 factories, not " + + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length, + getListableBeanFactory().getBeanNamesForType(FactoryBean.class).length == 2); + } + + public void testContainsBeanDefinition() { + Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("rod")); + Assert.assertTrue(getListableBeanFactory().containsBeanDefinition("roderick")); + } + +} \ No newline at end of file diff --git a/org.springframework.testsuite/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java b/org.springframework.testsuite/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java deleted file mode 100644 index fc41a86301f..00000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/beans/support/DerivedFromProtectedBaseBean.java +++ /dev/null @@ -1,25 +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.support; - -/** - * @author Juergen Hoeller - * @since 29.07.2004 - */ -public class DerivedFromProtectedBaseBean extends ProtectedBaseBean { - -} diff --git a/org.springframework.testsuite/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java b/org.springframework.testsuite/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java deleted file mode 100644 index 84c8e675a0b..00000000000 --- a/org.springframework.testsuite/src/test/java/org/springframework/beans/support/ProtectedBaseBean.java +++ /dev/null @@ -1,35 +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.support; - -/** - * @author Juergen Hoeller - * @since 29.07.2004 - */ -class ProtectedBaseBean { - - private String someProperty; - - public void setSomeProperty(String someProperty) { - this.someProperty = someProperty; - } - - public String getSomeProperty() { - return someProperty; - } - -}