moving unit tests from .testsuite -> .beans

added <?> wildcard to Scope methods that accept ObjectFactory
This commit is contained in:
Chris Beams 2008-12-15 03:39:13 +00:00
parent 6cb71bbb71
commit afa4231751
13 changed files with 204 additions and 259 deletions

View File

@ -69,7 +69,7 @@ public interface Scope {
* object if it is not present in the underlying storage mechanism
* @return the desired object (never <code>null</code>)
*/
Object get(String name, ObjectFactory objectFactory);
Object get(String name, ObjectFactory<?> objectFactory);
/**
* Remove the object with the given <code>name</code> from the underlying scope.

View File

@ -16,36 +16,38 @@
package org.springframework.beans.factory.config;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.apache.commons.logging.Log;
import org.springframework.test.AssertThrows;
import org.junit.Test;
/**
* Unit tests for the {@link CommonsLogFactoryBean} class.
*
* @author Rick Evans
* @author Chris Beams
*/
public final class CommonsLogFactoryBeanTests extends TestCase {
public final class CommonsLogFactoryBeanTests {
@Test
public void testIsSingleton() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
assertTrue(factory.isSingleton());
}
@Test
public void testGetObjectTypeDefaultsToPlainResourceInterfaceifLookupResourceIsNotSupplied() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
assertEquals(Log.class, factory.getObjectType());
}
@Test(expected=IllegalArgumentException.class)
public void testWhenLogNameIsMissing() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
factory.afterPropertiesSet();
}
}.runTest();
}
@Test
public void testSunnyDayPath() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
factory.setLogName("The Tin Drum");

View File

@ -0,0 +1,113 @@
/*
* 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.config;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
/**
* @author Rick Evans
* @author Juergen Hoeller
* @author Chris Beams
*/
public class CustomScopeConfigurerTests {
private static final String FOO_SCOPE = "fooScope";
private ConfigurableListableBeanFactory factory;
@Before
public void setUp() {
factory = new DefaultListableBeanFactory();
}
@Test
public void testWithNoScopes() throws Exception {
Scope scope = createMock(Scope.class);
replay(scope);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.postProcessBeanFactory(factory);
verify(scope);
}
@Test
public void testSunnyDayWithBonaFideScopeInstance() throws Exception {
Scope scope = createMock(Scope.class);
replay(scope);
factory.registerScope(FOO_SCOPE, scope);
Map<String, Object> scopes = new HashMap<String, Object>();
scopes.put(FOO_SCOPE, scope);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
verify(scope);
}
@Test
public void testSunnyDayWithBonaFideScopeClass() throws Exception {
Map<String, Object> scopes = new HashMap<String, Object>();
scopes.put(FOO_SCOPE, NoOpScope.class);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
}
@Test
public void testSunnyDayWithBonaFideScopeClassname() throws Exception {
Map<String, Object> scopes = new HashMap<String, Object>();
scopes.put(FOO_SCOPE, NoOpScope.class.getName());
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
}
@Test(expected=IllegalArgumentException.class)
public void testWhereScopeMapHasNullScopeValueInEntrySet() throws Exception {
Map<String, Object> scopes = new HashMap<String, Object>();
scopes.put(FOO_SCOPE, null);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
@Test(expected=IllegalArgumentException.class)
public void testWhereScopeMapHasNonScopeInstanceInEntrySet() throws Exception {
Map<String, Object> scopes = new HashMap<String, Object>();
scopes.put(FOO_SCOPE, this); // <-- not a valid value...
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
@SuppressWarnings("unchecked")
@Test(expected=ClassCastException.class)
public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() throws Exception {
Map scopes = new HashMap();
scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)...
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
}

View File

@ -23,7 +23,7 @@ import org.springframework.beans.factory.ObjectFactory;
*/
public class NoOpScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
public Object get(String name, ObjectFactory<?> objectFactory) {
throw new UnsupportedOperationException();
}

View File

@ -16,35 +16,39 @@
package org.springframework.beans.factory.config;
import static org.junit.Assert.*;
import java.util.LinkedList;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.support.GenericApplicationContext;
/**
* Simple test to illustrate and verify scope usage.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Chris Beams
*/
public class SimpleScopeTests extends TestCase {
public class SimpleScopeTests {
private GenericApplicationContext applicationContext;
private DefaultListableBeanFactory beanFactory;
protected void setUp() {
applicationContext = new GenericApplicationContext();
@Before
public void setUp() {
beanFactory = new DefaultListableBeanFactory();
Scope scope = new NoOpScope() {
private int index;
private List objects = new LinkedList(); {
private List<TestBean> objects = new LinkedList<TestBean>(); {
objects.add(new TestBean());
objects.add(new TestBean());
}
public Object get(String name, ObjectFactory objectFactory) {
public Object get(String name, ObjectFactory<?> objectFactory) {
if (index >= objects.size()) {
index = 0;
}
@ -52,24 +56,23 @@ public class SimpleScopeTests extends TestCase {
}
};
applicationContext.getBeanFactory().registerScope("myScope", scope);
beanFactory.registerScope("myScope", scope);
String[] scopeNames = applicationContext.getBeanFactory().getRegisteredScopeNames();
String[] scopeNames = beanFactory.getRegisteredScopeNames();
assertEquals(1, scopeNames.length);
assertEquals("myScope", scopeNames[0]);
assertSame(scope, applicationContext.getBeanFactory().getRegisteredScope("myScope"));
assertSame(scope, beanFactory.getRegisteredScope("myScope"));
XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(applicationContext);
XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
xbdr.loadBeanDefinitions("org/springframework/beans/factory/config/simpleScope.xml");
applicationContext.refresh();
}
@Test
public void testCanGetScopedObject() {
TestBean tb1 = (TestBean) applicationContext.getBean("usesScope");
TestBean tb2 = (TestBean) applicationContext.getBean("usesScope");
TestBean tb1 = (TestBean) beanFactory.getBean("usesScope");
TestBean tb2 = (TestBean) beanFactory.getBean("usesScope");
assertNotSame(tb1, tb2);
TestBean tb3 = (TestBean) applicationContext.getBean("usesScope");
TestBean tb3 = (TestBean) beanFactory.getBean("usesScope");
assertSame(tb3, tb1);
}

View File

@ -16,22 +16,19 @@
package org.springframework.beans.factory.parsing;
import junit.framework.TestCase;
import org.springframework.test.AssertThrows;
import org.junit.Test;
/**
* Unit tests for the {@link ConstructorArgumentEntry} class.
*
* @author Rick Evans
* @author Chris Beams
*/
public final class ConstructorArgumentEntryTests extends TestCase {
public final class ConstructorArgumentEntryTests {
public void testCtorBailsOnNegativeCtorIndexArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
@Test(expected=IllegalArgumentException.class)
public void testCtorBailsOnNegativeCtorIndexArgument() {
new ConstructorArgumentEntry(-1);
}
}.runTest();
}
}

View File

@ -16,53 +16,40 @@
package org.springframework.beans.factory.parsing;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.easymock.AbstractMatcher;
import org.easymock.MockControl;
import static org.easymock.EasyMock.*;
import org.apache.commons.logging.Log;
import org.junit.Test;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.test.AssertThrows;
/**
* @author Rick Evans
* @author Juergen Hoeller
* @author Chris Beams
*/
public final class FailFastProblemReporterTests extends TestCase {
public final class FailFastProblemReporterTests {
@Test(expected=BeanDefinitionParsingException.class)
public void testError() throws Exception {
new AssertThrows(BeanDefinitionParsingException.class) {
public void test() throws Exception {
FailFastProblemReporter reporter = new FailFastProblemReporter();
reporter.error(new Problem("VGER", new Location(new DescriptiveResource("here")),
null, new IllegalArgumentException()));
}
}.runTest();
}
@Test
public void testWarn() throws Exception {
IllegalArgumentException rootCause = new IllegalArgumentException();
Problem problem = new Problem("VGER", new Location(new DescriptiveResource("here")),
null, new IllegalArgumentException());
MockControl mockLog = MockControl.createControl(Log.class);
Log log = (Log) mockLog.getMock();
log.warn(null, rootCause);
mockLog.setMatcher(new AbstractMatcher() {
public boolean matches(Object[] expected, Object[] actual) {
Assert.assertEquals(2, actual.length);
Assert.assertTrue(actual[1] instanceof IllegalArgumentException);
return true;
}
});
mockLog.replay();
Log log = createMock(Log.class);
log.warn(anyObject(), isA(IllegalArgumentException.class));
replay(log);
FailFastProblemReporter reporter = new FailFastProblemReporter();
reporter.setLogger(log);
reporter.warning(problem);
mockLog.verify();
verify(log);
}
}

View File

@ -0,0 +1,28 @@
package org.springframework.beans.factory.parsing;
import org.junit.Test;
/**
* Unit tests for the {@link PropertyEntry} class.
*
* @author Rick Evans
* @author Chris Beams
*/
public final class PropertyEntryTests {
@Test(expected=IllegalArgumentException.class)
public void testCtorBailsOnNullPropertyNameArgument() throws Exception {
new PropertyEntry(null);
}
@Test(expected=IllegalArgumentException.class)
public void testCtorBailsOnEmptyPropertyNameArgument() throws Exception {
new PropertyEntry("");
}
@Test(expected=IllegalArgumentException.class)
public void testCtorBailsOnWhitespacedPropertyNameArgument() throws Exception {
new PropertyEntry("\t ");
}
}

View File

@ -16,8 +16,9 @@
package org.springframework.beans.factory.config;
import junit.framework.TestCase;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
@ -27,10 +28,12 @@ import org.springframework.context.support.StaticApplicationContext;
/**
* @author Colin Sampaleanu
* @author Juergen Hoeller
* @author Chris Beams
* @since 02.10.2003
*/
public class BeanFactoryPostProcessorTests extends TestCase {
public class BeanFactoryPostProcessorTests {
@Test
public void testRegisteredBeanFactoryPostProcessor() {
StaticApplicationContext ac = new StaticApplicationContext();
ac.registerSingleton("tb1", TestBean.class);
@ -42,6 +45,7 @@ public class BeanFactoryPostProcessorTests extends TestCase {
assertTrue(bfpp.wasCalled);
}
@Test
public void testDefinedBeanFactoryPostProcessor() {
StaticApplicationContext ac = new StaticApplicationContext();
ac.registerSingleton("tb1", TestBean.class);
@ -52,6 +56,7 @@ public class BeanFactoryPostProcessorTests extends TestCase {
assertTrue(bfpp.wasCalled);
}
@Test
public void testMultipleDefinedBeanFactoryPostProcessors() {
StaticApplicationContext ac = new StaticApplicationContext();
ac.registerSingleton("tb1", TestBean.class);
@ -68,6 +73,7 @@ public class BeanFactoryPostProcessorTests extends TestCase {
assertTrue(bfpp.wasCalled);
}
@Test
public void testBeanFactoryPostProcessorNotExecutedByBeanFactory() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class));

View File

@ -1,155 +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.config;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.easymock.MockControl;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.mock.easymock.AbstractScalarMockTemplate;
import org.springframework.test.AssertThrows;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public class CustomScopeConfigurerTests extends TestCase {
private static final String FOO_SCOPE = "fooScope";
public void testWithNoScopes() throws Exception {
new ConfigurableListableBeanFactoryMockTemplate() {
protected void doTest(ConfigurableListableBeanFactory factory) {
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.postProcessBeanFactory(factory);
}
}.test();
}
public void testSunnyDayWithBonaFideScopeInstance() throws Exception {
MockControl mockScope = MockControl.createControl(Scope.class);
final Scope scope = (Scope) mockScope.getMock();
mockScope.replay();
new ConfigurableListableBeanFactoryMockTemplate() {
public void setupExpectations(MockControl mockControl, ConfigurableListableBeanFactory factory) {
factory.registerScope(FOO_SCOPE, scope);
}
protected void doTest(ConfigurableListableBeanFactory factory) {
Map scopes = new HashMap();
scopes.put(FOO_SCOPE, scope);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
}.test();
mockScope.verify();
}
public void testSunnyDayWithBonaFideScopeClass() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
Map scopes = new HashMap();
scopes.put(FOO_SCOPE, NoOpScope.class);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
}
public void testSunnyDayWithBonaFideScopeClassname() throws Exception {
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
Map scopes = new HashMap();
scopes.put(FOO_SCOPE, NoOpScope.class.getName());
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
assertTrue(factory.getRegisteredScope(FOO_SCOPE) instanceof NoOpScope);
}
public void testWhereScopeMapHasNullScopeValueInEntrySet() throws Exception {
new ConfigurableListableBeanFactoryMockTemplate() {
protected void doTest(final ConfigurableListableBeanFactory factory) {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
Map scopes = new HashMap();
scopes.put(FOO_SCOPE, null);
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
}.runTest();
}
}.test();
}
public void testWhereScopeMapHasNonScopeInstanceInEntrySet() throws Exception {
new ConfigurableListableBeanFactoryMockTemplate() {
protected void doTest(final ConfigurableListableBeanFactory factory) {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
Map scopes = new HashMap();
scopes.put(FOO_SCOPE, this); // <-- not a valid value...
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
}.runTest();
}
}.test();
}
public void testWhereScopeMapHasNonStringTypedScopeNameInKeySet() throws Exception {
new ConfigurableListableBeanFactoryMockTemplate() {
protected void doTest(final ConfigurableListableBeanFactory factory) {
new AssertThrows(ClassCastException.class) {
public void test() throws Exception {
Map scopes = new HashMap();
scopes.put(this, new NoOpScope()); // <-- not a valid value (the key)...
CustomScopeConfigurer figurer = new CustomScopeConfigurer();
figurer.setScopes(scopes);
figurer.postProcessBeanFactory(factory);
}
}.runTest();
}
}.test();
}
private abstract class ConfigurableListableBeanFactoryMockTemplate extends AbstractScalarMockTemplate {
public ConfigurableListableBeanFactoryMockTemplate() {
super(ConfigurableListableBeanFactory.class);
}
public final void setupExpectations(MockControl mockControl, Object mockObject) throws Exception {
setupExpectations(mockControl, (ConfigurableListableBeanFactory) mockObject);
}
public final void doTest(Object mockObject) throws Exception {
doTest((ConfigurableListableBeanFactory) mockObject);
}
public void setupExpectations(MockControl mockControl, ConfigurableListableBeanFactory factory) throws Exception {
}
protected abstract void doTest(ConfigurableListableBeanFactory factory) throws Exception;
}
}

View File

@ -37,6 +37,7 @@ import org.springframework.beans.TestBean;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.beans.factory.support.ManagedSet;

View File

@ -1,37 +0,0 @@
package org.springframework.beans.factory.parsing;
import junit.framework.TestCase;
import org.springframework.test.AssertThrows;
/**
* Unit tests for the {@link PropertyEntry} class.
*
* @author Rick Evans
*/
public final class PropertyEntryTests extends TestCase {
public void testCtorBailsOnNullPropertyNameArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new PropertyEntry(null);
}
}.runTest();
}
public void testCtorBailsOnEmptyPropertyNameArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new PropertyEntry("");
}
}.runTest();
}
public void testCtorBailsOnWhitespacedPropertyNameArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new PropertyEntry("\t ");
}
}.runTest();
}
}