INT-1856 added support for filename-generator-expression to outbound file adapters, added tests

This commit is contained in:
Oleg Zhurakousky
2011-04-07 12:39:40 -04:00
parent 66aba7b5a6
commit 46577add6b
5 changed files with 55 additions and 11 deletions

View File

@@ -18,11 +18,13 @@ package org.springframework.integration.file.config;
import org.w3c.dom.Element;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.integration.config.xml.AbstractOutboundChannelAdapterParser;
import org.springframework.integration.context.IntegrationContextUtils;
import org.springframework.util.StringUtils;
/**
* Parser for the <outbound-channel-adapter/> element of the 'file'
@@ -30,14 +32,37 @@ import org.springframework.integration.context.IntegrationContextUtils;
*
* @author Mark Fisher
* @author Iwein Fuld
* @author Oleg Zhurakousky
*/
public class FileOutboundChannelAdapterParser extends AbstractOutboundChannelAdapterParser {
@Override
protected AbstractBeanDefinition parseConsumer(Element element, ParserContext parserContext) {
BeanDefinitionBuilder builder = FileWritingMessageHandlerBeanDefinitionBuilder.configure(
BeanDefinitionBuilder handlerBuilder = FileWritingMessageHandlerBeanDefinitionBuilder.configure(
element, IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME, parserContext);
return (builder != null ? builder.getBeanDefinition() : null);
if (handlerBuilder != null){
String remoteFileNameGenerator = element.getAttribute("filename-generator");
String remoteFileNameGeneratorExpression = element.getAttribute("filename-generator-expression");
boolean hasRemoteFileNameGenerator = StringUtils.hasText(remoteFileNameGenerator);
boolean hasRemoteFileNameGeneratorExpression = StringUtils.hasText(remoteFileNameGeneratorExpression);
if (hasRemoteFileNameGenerator || hasRemoteFileNameGeneratorExpression) {
if (hasRemoteFileNameGenerator && hasRemoteFileNameGeneratorExpression) {
throw new BeanDefinitionStoreException("at most one of 'filename-generator-expression' or 'filename-generator' " +
"is allowed on file outbound adapter/gateway");
}
if (hasRemoteFileNameGenerator) {
handlerBuilder.addPropertyReference("fileNameGenerator", remoteFileNameGenerator);
}
else {
BeanDefinitionBuilder fileNameGeneratorBuilder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.integration.file.DefaultFileNameGenerator");
fileNameGeneratorBuilder.addPropertyValue("expression", remoteFileNameGeneratorExpression);
handlerBuilder.addPropertyValue("fileNameGenerator", fileNameGeneratorBuilder.getBeanDefinition());
}
}
}
return (handlerBuilder != null ? handlerBuilder.getBeanDefinition() : null);
}
}

View File

@@ -18,7 +18,8 @@
<file:outbound-channel-adapter id="simpleAdapter"
channel="testChannel"
directory="${java.io.tmpdir}"
temporary-file-suffix=".foo"/>
temporary-file-suffix=".foo"
filename-generator-expression="'foo.txt'"/>
<file:outbound-channel-adapter id="adapterWithCustomNameGenerator"
channel="testChannel"

View File

@@ -16,22 +16,27 @@
package org.springframework.integration.file.config;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.nio.charset.Charset;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.endpoint.EventDrivenConsumer;
import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.FileWritingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
import java.nio.charset.Charset;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
/**
* @author Mark Fisher
* @author Marius Bogoevici
@@ -66,7 +71,11 @@ public class FileOutboundChannelAdapterParserTests {
File actual = (File) handlerAccessor.getPropertyValue("destinationDirectory");
assertEquals(".foo", handler.getTemporaryFileSuffix());
assertThat(actual, is(expected));
assertThat(handlerAccessor.getPropertyValue("fileNameGenerator"), is(DefaultFileNameGenerator.class));
DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
assertNotNull(fileNameGenerator);
String expression = (String) TestUtils.getPropertyValue(fileNameGenerator, "expression");
assertNotNull(expression);
assertEquals("'foo.txt'", expression);
assertEquals(Boolean.FALSE, handlerAccessor.getPropertyValue("deleteSourceFiles"));
}

View File

@@ -17,7 +17,8 @@
request-channel="someChannel"
directory="${java.io.tmpdir}"
auto-startup="false"
order="777"/>
order="777"
filename-generator-expression="'foo.txt'"/>
<context:property-placeholder />

View File

@@ -21,11 +21,14 @@ import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.file.DefaultFileNameGenerator;
import org.springframework.integration.file.FileWritingMessageHandler;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
/**
* @author Mark Fisher
@@ -47,6 +50,11 @@ public class FileOutboundGatewayParserTests {
assertEquals(Boolean.FALSE, gatewayAccessor.getPropertyValue("autoStartup"));
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(777, handlerAccessor.getPropertyValue("order"));
DefaultFileNameGenerator fileNameGenerator = (DefaultFileNameGenerator) handlerAccessor.getPropertyValue("fileNameGenerator");
assertNotNull(fileNameGenerator);
String expression = (String) TestUtils.getPropertyValue(fileNameGenerator, "expression");
assertNotNull(expression);
assertEquals("'foo.txt'", expression);
}
}