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

@@ -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,59 +0,0 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.springframework.test.AssertThrows;
/**
* Unit tests for the {@link CommonsLogFactoryBean} class.
*
* @author Rick Evans
*/
public final class CommonsLogFactoryBeanTests extends TestCase {
public void testIsSingleton() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
assertTrue(factory.isSingleton());
}
public void testGetObjectTypeDefaultsToPlainResourceInterfaceifLookupResourceIsNotSupplied() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
assertEquals(Log.class, factory.getObjectType());
}
public void testWhenLogNameIsMissing() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
factory.afterPropertiesSet();
}
}.runTest();
}
public void testSunnyDayPath() throws Exception {
CommonsLogFactoryBean factory = new CommonsLogFactoryBean();
factory.setLogName("The Tin Drum");
factory.afterPropertiesSet();
Object object = factory.getObject();
assertNotNull("As per FactoryBean contract, the return value of getObject() cannot be null.", object);
assertTrue("Obviously not getting a Log back", Log.class.isAssignableFrom(object.getClass()));
}
}

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

@@ -1,45 +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 org.springframework.beans.factory.ObjectFactory;
/**
* @author Juergen Hoeller
*/
public class NoOpScope implements Scope {
public Object get(String name, ObjectFactory objectFactory) {
throw new UnsupportedOperationException();
}
public Object remove(String name) {
throw new UnsupportedOperationException();
}
public void registerDestructionCallback(String name, Runnable callback) {
}
public Object resolveContextualObject(String key) {
return null;
}
public String getConversationId() {
return null;
}
}

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,76 +0,0 @@
/*
* 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.config;
import java.util.LinkedList;
import java.util.List;
import junit.framework.TestCase;
import org.springframework.beans.TestBean;
import org.springframework.beans.factory.ObjectFactory;
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
*/
public class SimpleScopeTests extends TestCase {
private GenericApplicationContext applicationContext;
protected void setUp() {
applicationContext = new GenericApplicationContext();
Scope scope = new NoOpScope() {
private int index;
private List objects = new LinkedList(); {
objects.add(new TestBean());
objects.add(new TestBean());
}
public Object get(String name, ObjectFactory objectFactory) {
if (index >= objects.size()) {
index = 0;
}
return objects.get(index++);
}
};
applicationContext.getBeanFactory().registerScope("myScope", scope);
String[] scopeNames = applicationContext.getBeanFactory().getRegisteredScopeNames();
assertEquals(1, scopeNames.length);
assertEquals("myScope", scopeNames[0]);
assertSame(scope, applicationContext.getBeanFactory().getRegisteredScope("myScope"));
XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(applicationContext);
xbdr.loadBeanDefinitions("org/springframework/beans/factory/config/simpleScope.xml");
applicationContext.refresh();
}
public void testCanGetScopedObject() {
TestBean tb1 = (TestBean) applicationContext.getBean("usesScope");
TestBean tb2 = (TestBean) applicationContext.getBean("usesScope");
assertNotSame(tb1, tb2);
TestBean tb3 = (TestBean) applicationContext.getBean("usesScope");
assertSame(tb3, tb1);
}
}

View File

@@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<bean id="usesScope" class="org.springframework.beans.TestBean" scope="myScope"/>
</beans>

View File

@@ -1,37 +0,0 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.parsing;
import junit.framework.TestCase;
import org.springframework.test.AssertThrows;
/**
* Unit tests for the {@link ConstructorArgumentEntry} class.
*
* @author Rick Evans
*/
public final class ConstructorArgumentEntryTests extends TestCase {
public void testCtorBailsOnNegativeCtorIndexArgument() throws Exception {
new AssertThrows(IllegalArgumentException.class) {
public void test() throws Exception {
new ConstructorArgumentEntry(-1);
}
}.runTest();
}
}

View File

@@ -1,68 +0,0 @@
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.parsing;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.commons.logging.Log;
import org.easymock.AbstractMatcher;
import org.easymock.MockControl;
import org.springframework.core.io.DescriptiveResource;
import org.springframework.test.AssertThrows;
/**
* @author Rick Evans
* @author Juergen Hoeller
*/
public final class FailFastProblemReporterTests extends TestCase {
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();
}
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();
FailFastProblemReporter reporter = new FailFastProblemReporter();
reporter.setLogger(log);
reporter.warning(problem);
mockLog.verify();
}
}

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();
}
}