moving unit tests from .testsuite -> .beans and .context
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* 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.context.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.context.support.StaticApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests the interaction between {@link ApplicationContext} implementations and
|
||||
* any registered {@link BeanFactoryPostProcessor} implementations. Specifically
|
||||
* {@link StaticApplicationContext} is used for the tests, but what's represented
|
||||
* here is any {@link AbstractApplicationContext} implementation.
|
||||
*
|
||||
* @author Colin Sampaleanu
|
||||
* @author Juergen Hoeller
|
||||
* @author Chris Beams
|
||||
* @since 02.10.2003
|
||||
*/
|
||||
public class BeanFactoryPostProcessorTests {
|
||||
|
||||
@Test
|
||||
public void testRegisteredBeanFactoryPostProcessor() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
ac.registerSingleton("tb1", TestBean.class);
|
||||
ac.registerSingleton("tb2", TestBean.class);
|
||||
TestBeanFactoryPostProcessor bfpp = new TestBeanFactoryPostProcessor();
|
||||
ac.addBeanFactoryPostProcessor(bfpp);
|
||||
assertFalse(bfpp.wasCalled);
|
||||
ac.refresh();
|
||||
assertTrue(bfpp.wasCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefinedBeanFactoryPostProcessor() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
ac.registerSingleton("tb1", TestBean.class);
|
||||
ac.registerSingleton("tb2", TestBean.class);
|
||||
ac.registerSingleton("bfpp", TestBeanFactoryPostProcessor.class);
|
||||
ac.refresh();
|
||||
TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp");
|
||||
assertTrue(bfpp.wasCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleDefinedBeanFactoryPostProcessors() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
ac.registerSingleton("tb1", TestBean.class);
|
||||
ac.registerSingleton("tb2", TestBean.class);
|
||||
MutablePropertyValues pvs1 = new MutablePropertyValues();
|
||||
pvs1.addPropertyValue("initValue", "${key}");
|
||||
ac.registerSingleton("bfpp1", TestBeanFactoryPostProcessor.class, pvs1);
|
||||
MutablePropertyValues pvs2 = new MutablePropertyValues();
|
||||
pvs2.addPropertyValue("properties", "key=value");
|
||||
ac.registerSingleton("bfpp2", PropertyPlaceholderConfigurer.class, pvs2);
|
||||
ac.refresh();
|
||||
TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) ac.getBean("bfpp1");
|
||||
assertEquals("value", bfpp.initValue);
|
||||
assertTrue(bfpp.wasCalled);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanFactoryPostProcessorNotExecutedByBeanFactory() {
|
||||
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
|
||||
bf.registerBeanDefinition("tb1", new RootBeanDefinition(TestBean.class));
|
||||
bf.registerBeanDefinition("tb2", new RootBeanDefinition(TestBean.class));
|
||||
bf.registerBeanDefinition("bfpp", new RootBeanDefinition(TestBeanFactoryPostProcessor.class));
|
||||
TestBeanFactoryPostProcessor bfpp = (TestBeanFactoryPostProcessor) bf.getBean("bfpp");
|
||||
assertFalse(bfpp.wasCalled);
|
||||
}
|
||||
|
||||
|
||||
public static class TestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
|
||||
|
||||
public String initValue;
|
||||
|
||||
public void setInitValue(String initValue) {
|
||||
this.initValue = initValue;
|
||||
}
|
||||
|
||||
public boolean wasCalled = false;
|
||||
|
||||
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
|
||||
wasCalled = true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* 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.context.support;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.TestBean;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
|
||||
import org.springframework.beans.factory.config.PropertyResourceConfigurer;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Integration tests for {@link PropertyResourceConfigurer} implementations requiring
|
||||
* interaction with an {@link ApplicationContext}. For example, a {@link PropertyPlaceholderConfigurer}
|
||||
* that contains ${..} tokens in its 'location' property requires being tested through an ApplicationContext
|
||||
* as opposed to using only a BeanFactory during testing.
|
||||
*
|
||||
* @see org.springframework.beans.factory.config.PropertyResourceConfigurerTests;
|
||||
*
|
||||
* @author Chris Beams
|
||||
*/
|
||||
public class PropertyResourceConfigurerIntegrationTests {
|
||||
|
||||
private DefaultListableBeanFactory factory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
factory = new DefaultListableBeanFactory();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithSystemPropertyInLocation() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
ac.registerSingleton("tb", TestBean.class, pvs);
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("location", "${user.dir}/test");
|
||||
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanInitializationException");
|
||||
}
|
||||
catch (BeanInitializationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof FileNotFoundException);
|
||||
// slight hack for Linux/Unix systems
|
||||
String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
|
||||
if (userDir.startsWith("/")) {
|
||||
userDir = userDir.substring(1);
|
||||
}
|
||||
assertTrue(ex.getMessage().indexOf(userDir) != -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithSystemPropertiesInLocation() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
ac.registerSingleton("tb", TestBean.class, pvs);
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("location", "${user.dir}/test/${user.dir}");
|
||||
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanInitializationException");
|
||||
}
|
||||
catch (BeanInitializationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof FileNotFoundException);
|
||||
// slight hack for Linux/Unix systems
|
||||
String userDir = StringUtils.cleanPath(System.getProperty("user.dir"));
|
||||
if (userDir.startsWith("/")) {
|
||||
userDir = userDir.substring(1);
|
||||
}
|
||||
/* the above hack doesn't work since the exception message is created without
|
||||
the leading / stripped so the test fails. Changed 17/11/04. DD */
|
||||
//assertTrue(ex.getMessage().indexOf(userDir + "/test/" + userDir) != -1);
|
||||
assertTrue(ex.getMessage().indexOf(userDir + "/test/" + userDir) != -1 ||
|
||||
ex.getMessage().indexOf(userDir + "/test//" + userDir) != -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithUnresolvableSystemPropertiesInLocation() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("spouse", new RuntimeBeanReference("${ref}"));
|
||||
ac.registerSingleton("tb", TestBean.class, pvs);
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("location", "${myprop}/test/${myprop}");
|
||||
ac.registerSingleton("configurer", PropertyPlaceholderConfigurer.class, pvs);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanInitializationException ex) {
|
||||
// expected
|
||||
assertTrue(ex.getCause() instanceof FileNotFoundException);
|
||||
assertTrue(ex.getMessage().indexOf("myprop") != -1);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithMultiLevelCircularReference() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("name", "name${var}");
|
||||
ac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "var=${m}var\nm=${var2}\nvar2=${var}");
|
||||
ac.registerSingleton("configurer1", PropertyPlaceholderConfigurer.class, pvs);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithNestedCircularReference() {
|
||||
StaticApplicationContext ac = new StaticApplicationContext();
|
||||
MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("name", "name${var}");
|
||||
ac.registerSingleton("tb1", TestBean.class, pvs);
|
||||
pvs = new MutablePropertyValues();
|
||||
pvs.addPropertyValue("properties", "var=${m}var\nm=${var2}\nvar2=${m}");
|
||||
ac.registerSingleton("configurer1", PropertyPlaceholderConfigurer.class, pvs);
|
||||
try {
|
||||
ac.refresh();
|
||||
fail("Should have thrown BeanDefinitionStoreException");
|
||||
}
|
||||
catch (BeanDefinitionStoreException ex) {
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
@Ignore // this test was breaking after the 3.0 repackaging
|
||||
@Test
|
||||
public void testPropertyPlaceholderConfigurerWithAutowireByType() {
|
||||
// StaticApplicationContext ac = new StaticApplicationContext();
|
||||
// MutablePropertyValues pvs = new MutablePropertyValues();
|
||||
// pvs.addPropertyValue("touchy", "${test}");
|
||||
// ac.registerSingleton("tb", TestBean.class, pvs);
|
||||
// pvs = new MutablePropertyValues();
|
||||
// pvs.addPropertyValue("target", new RuntimeBeanReference("tb"));
|
||||
// // uncomment when fixing this test
|
||||
// // ac.registerSingleton("tbProxy", org.springframework.aop.framework.ProxyFactoryBean.class, pvs);
|
||||
// pvs = new MutablePropertyValues();
|
||||
// Properties props = new Properties();
|
||||
// props.put("test", "mytest");
|
||||
// pvs.addPropertyValue("properties", new Properties(props));
|
||||
// RootBeanDefinition ppcDef = new RootBeanDefinition(PropertyPlaceholderConfigurer.class, pvs);
|
||||
// ppcDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
|
||||
// ac.registerBeanDefinition("configurer", ppcDef);
|
||||
// ac.refresh();
|
||||
// TestBean tb = (TestBean) ac.getBean("tb");
|
||||
// assertEquals("mytest", tb.getTouchy());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user