INT-647 removed duplicated TestUtils and fixed resulting errors. It would be good to start using JavaConfig finally

This commit is contained in:
Iwein Fuld
2009-09-18 16:12:17 +00:00
parent 28c57bac04
commit 235c0220f5
36 changed files with 411 additions and 709 deletions

View File

@@ -1,55 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java" />
<classpathentry kind="src" output="target/test-classes" path="src/test/java" />
<classpathentry kind="src" path="src/main/resources" />
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" />
<classpathentry kind="var" path="IVY_CACHE/org.aspectj/com.springsource.org.aspectj.weaver/1.6.5.RELEASE/com.springsource.org.aspectj.weaver-1.6.5.RELEASE.jar"
sourcepath="/IVY_CACHE/org.aspectj/com.springsource.org.aspectj.weaver/1.6.5.RELEASE/com.springsource.org.aspectj.weaver-1.6.5.RELEASE.jar" />
<classpathentry kind="var"
path="IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-2.1.3.jar"
sourcepath="/IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-sources-2.1.3.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0.jar"
sourcepath="/IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-sources-1.0.0.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.junit/com.springsource.org.junit/4.6.0/com.springsource.org.junit-4.6.0.jar"
sourcepath="/IVY_CACHE/org.junit/com.springsource.org.junit/4.6.0/com.springsource.org.junit-sources-4.6.0.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.aop/3.0.0.M4/org.springframework.aop-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.aop/3.0.0.M4/org.springframework.aop-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.asm/3.0.0.M4/org.springframework.asm-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.asm/3.0.0.M4/org.springframework.asm-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.beans/3.0.0.M4/org.springframework.beans-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.beans/3.0.0.M4/org.springframework.beans-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.context/3.0.0.M4/org.springframework.context-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.context/3.0.0.M4/org.springframework.context-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.context.support/3.0.0.M4/org.springframework.context.support-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.context.support/3.0.0.M4/org.springframework.context.support-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.core/3.0.0.M4/org.springframework.core-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.core/3.0.0.M4/org.springframework.core-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.expression/3.0.0.M4/org.springframework.expression-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.expression/3.0.0.M4/org.springframework.expression-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.transaction/3.0.0.M4/org.springframework.transaction-3.0.0.M4.jar"
sourcepath="/IVY_CACHE/org.springframework/org.springframework.transaction/3.0.0.M4/org.springframework.transaction-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.springframework/org.springframework.test/3.0.0.M4/org.springframework.test-3.0.0.M4.jar"
sourcepath="IVY_CACHE/org.springframework/org.springframework.test/3.0.0.M4/org.springframework.test-sources-3.0.0.M4.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.mockito/com.springsource.org.mockito/1.6.0/com.springsource.org.mockito-1.6.0.jar"
sourcepath="/IVY_CACHE/org.mockito/com.springsource.org.mockito/1.6.0/com.springsource.org.mockito-sources-1.6.0.jar" />
<classpathentry kind="var"
path="IVY_CACHE/org.objenesis/com.springsource.org.objenesis/1.0.0/com.springsource.org.objenesis-1.0.0.jar"
sourcepath="/IVY_CACHE/org.objenesis/com.springsource.org.objenesis/1.0.0/com.springsource.org.objenesis-sources-1.0.0.jar" />
<classpathentry combineaccessrules="false" kind="src"
path="/org.springframework.integration" />
<classpathentry kind="output" path="target/classes" />
<classpathentry kind="src" path="src/main/java" />
<classpathentry kind="src" path="src/main/resources" />
<classpathentry kind="src" output="target/test-classes" path="src/test/java" />
<classpathentry kind="src" path="src/test/resources" />
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.aspectj/com.springsource.org.aspectj.weaver/1.6.5.RELEASE/com.springsource.org.aspectj.weaver-1.6.5.RELEASE.jar" sourcepath="/SANDBOX_IVY_CACHE/org.aspectj/com.springsource.org.aspectj.weaver/1.6.5.RELEASE/com.springsource.org.aspectj.weaver-1.6.5.RELEASE.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-2.1.3.jar" sourcepath="/SANDBOX_IVY_CACHE/net.sourceforge.cglib/com.springsource.net.sf.cglib/2.1.3/com.springsource.net.sf.cglib-sources-2.1.3.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-1.0.0.jar" sourcepath="/SANDBOX_IVY_CACHE/org.aopalliance/com.springsource.org.aopalliance/1.0.0/com.springsource.org.aopalliance-sources-1.0.0.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.junit/com.springsource.org.junit/4.6.0/com.springsource.org.junit-4.6.0.jar" sourcepath="/SANDBOX_IVY_CACHE/org.junit/com.springsource.org.junit/4.6.0/com.springsource.org.junit-sources-4.6.0.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.aop/3.0.0.M4/org.springframework.aop-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.aop/3.0.0.M4/org.springframework.aop-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.asm/3.0.0.M4/org.springframework.asm-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.asm/3.0.0.M4/org.springframework.asm-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.beans/3.0.0.M4/org.springframework.beans-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.beans/3.0.0.M4/org.springframework.beans-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.context/3.0.0.M4/org.springframework.context-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.context/3.0.0.M4/org.springframework.context-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.context.support/3.0.0.M4/org.springframework.context.support-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.context.support/3.0.0.M4/org.springframework.context.support-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.core/3.0.0.M4/org.springframework.core-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.core/3.0.0.M4/org.springframework.core-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.expression/3.0.0.M4/org.springframework.expression-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.expression/3.0.0.M4/org.springframework.expression-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.transaction/3.0.0.M4/org.springframework.transaction-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.transaction/3.0.0.M4/org.springframework.transaction-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.springframework/org.springframework.test/3.0.0.M4/org.springframework.test-3.0.0.M4.jar" sourcepath="/SANDBOX_IVY_CACHE/org.springframework/org.springframework.test/3.0.0.M4/org.springframework.test-sources-3.0.0.M4.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.mockito/com.springsource.org.mockito/1.6.0/com.springsource.org.mockito-1.6.0.jar" sourcepath="/SANDBOX_IVY_CACHE/org.mockito/com.springsource.org.mockito/1.6.0/com.springsource.org.mockito-sources-1.6.0.jar" />
<classpathentry kind="var" path="SANDBOX_IVY_CACHE/org.objenesis/com.springsource.org.objenesis/1.0.0/com.springsource.org.objenesis-1.0.0.jar" sourcepath="/SANDBOX_IVY_CACHE/org.objenesis/com.springsource.org.objenesis/1.0.0/com.springsource.org.objenesis-sources-1.0.0.jar" />
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.integration" />
<classpathentry kind="output" path="target/classes" />
</classpath>

View File

@@ -16,8 +16,9 @@
package org.springframework.integration.test.util;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import static org.hamcrest.CoreMatchers.is;
import org.hamcrest.Matcher;
import static org.junit.Assert.assertThat;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.FatalBeanException;
import org.springframework.beans.factory.BeanFactory;
@@ -26,117 +27,133 @@ import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.integration.channel.BeanFactoryChannelResolver;
import org.springframework.integration.channel.MessagePublishingErrorHandler;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.integration.core.Message;
import org.springframework.integration.core.MessageChannel;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.AbstractPollingEndpoint;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessageHandler;
import org.springframework.integration.message.MessageHandlingException;
import org.springframework.integration.message.MessageRejectedException;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.ErrorHandler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.util.Assert;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
/**
* @author Mark Fisher
* @author Iwein Fuld
*/
public abstract class TestUtils {
public static Object getPropertyValue(Object root, String propertyPath) {
Object value = null;
DirectFieldAccessor accessor = new DirectFieldAccessor(root);
String[] tokens = propertyPath.split("\\.");
for (int i = 0; i < tokens.length; i++) {
value = accessor.getPropertyValue(tokens[i]);
if (value != null) {
accessor = new DirectFieldAccessor(value);
}
else if (i == tokens.length - 1) {
return null;
}
else {
throw new IllegalArgumentException(
"intermediate property '" + tokens[i] + "' is null");
}
}
return value;
}
public static Object getPropertyValue(Object root, String propertyPath) {
Object value = null;
DirectFieldAccessor accessor = new DirectFieldAccessor(root);
String[] tokens = propertyPath.split("\\.");
for (int i = 0; i < tokens.length; i++) {
value = accessor.getPropertyValue(tokens[i]);
if (value != null) {
accessor = new DirectFieldAccessor(value);
} else if (i == tokens.length - 1) {
return null;
} else {
throw new IllegalArgumentException(
"intermediate property '" + tokens[i] + "' is null");
}
}
return value;
}
@SuppressWarnings("unchecked")
public static <T> T getPropertyValue(Object root, String propertyPath, Class<T> type) {
Object value = getPropertyValue(root, propertyPath);
Assert.isAssignable(type, value.getClass());
return (T) value;
}
@SuppressWarnings("unchecked")
public static <T> T getPropertyValue(Object root, String propertyPath, Class<T> type) {
Object value = getPropertyValue(root, propertyPath);
Assert.isAssignable(type, value.getClass());
return (T) value;
}
public static TestApplicationContext createTestApplicationContext() {
TestApplicationContext context = new TestApplicationContext();
registerBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, createTaskScheduler(10), context);
return context;
}
public static TestApplicationContext createTestApplicationContext() {
TestApplicationContext context = new TestApplicationContext();
ErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(context));
ThreadPoolTaskScheduler scheduler = createTaskScheduler(10);
scheduler.setErrorHandler(errorHandler);
registerBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, scheduler, context);
return context;
}
public static TaskScheduler createTaskScheduler(int poolSize) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(poolSize);
scheduler.setRejectedExecutionHandler(new CallerRunsPolicy());
scheduler.afterPropertiesSet();
return scheduler;
}
public static ThreadPoolTaskScheduler createTaskScheduler(int poolSize) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(poolSize);
scheduler.setRejectedExecutionHandler(new CallerRunsPolicy());
scheduler.afterPropertiesSet();
return scheduler;
}
private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) {
Assert.notNull(beanName, "bean name must not be null");
ConfigurableListableBeanFactory configurableListableBeanFactory = null;
if (beanFactory instanceof ConfigurableListableBeanFactory) {
configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
else if (beanFactory instanceof GenericApplicationContext) {
configurableListableBeanFactory = ((GenericApplicationContext) beanFactory).getBeanFactory();
}
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(beanFactory);
}
if (bean instanceof InitializingBean) {
try {
((InitializingBean) bean).afterPropertiesSet();
}
catch (Exception e) {
throw new FatalBeanException("failed to register bean with test context", e);
}
}
configurableListableBeanFactory.registerSingleton(beanName, bean);
}
private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) {
Assert.notNull(beanName, "bean name must not be null");
ConfigurableListableBeanFactory configurableListableBeanFactory = null;
if (beanFactory instanceof ConfigurableListableBeanFactory) {
configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
} else if (beanFactory instanceof GenericApplicationContext) {
configurableListableBeanFactory = ((GenericApplicationContext) beanFactory).getBeanFactory();
}
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(beanFactory);
}
if (bean instanceof InitializingBean) {
try {
((InitializingBean) bean).afterPropertiesSet();
}
catch (Exception e) {
throw new FatalBeanException("failed to register bean with test context", e);
}
}
configurableListableBeanFactory.registerSingleton(beanName, bean);
}
public static class TestApplicationContext extends GenericApplicationContext {
public static class TestApplicationContext extends GenericApplicationContext {
private TestApplicationContext() {
super();
}
private TestApplicationContext() {
super();
}
public void registerChannel(String channelName, MessageChannel channel) {
if (channel.getName() != null) {
if (channelName == null) {
Assert.notNull(channel.getName(), "channel name must not be null");
channelName = channel.getName();
}
else {
Assert.isTrue(channel.getName().equals(channelName),
"channel name has already been set with a conflicting value");
}
}
registerBean(channelName, channel, this);
}
public void registerChannel(String channelName, MessageChannel channel) {
if (channel.getName() != null) {
if (channelName == null) {
Assert.notNull(channel.getName(), "channel name must not be null");
channelName = channel.getName();
} else {
Assert.isTrue(channel.getName().equals(channelName),
"channel name has already been set with a conflicting value");
}
}
registerBean(channelName, channel, this);
}
public void registerEndpoint(String endpointName, AbstractEndpoint endpoint) {
if (endpoint instanceof AbstractPollingEndpoint) {
DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
if (accessor.getPropertyValue("trigger") == null) {
((AbstractPollingEndpoint) endpoint).setTrigger(new PeriodicTrigger(10));
}
}
registerBean(endpointName, endpoint, this);
}
}
public void registerEndpoint(String endpointName, AbstractEndpoint endpoint) {
if (endpoint instanceof AbstractPollingEndpoint) {
DirectFieldAccessor accessor = new DirectFieldAccessor(endpoint);
if (accessor.getPropertyValue("trigger") == null) {
((AbstractPollingEndpoint) endpoint).setTrigger(new PeriodicTrigger(10));
}
}
registerBean(endpointName, endpoint, this);
}
}
public static MessageHandler handlerExpecting(final Matcher<Message> messageMatcher) {
return new MessageHandler() {
public void handleMessage(Message<?> message) throws MessageRejectedException, MessageHandlingException, MessageDeliveryException {
assertThat(message, is(messageMatcher));
}
};
}
}