Skip to content

Instantly share code, notes, and snippets.

@dparoulek
Created February 6, 2012 17:25
Show Gist options
  • Save dparoulek/1753480 to your computer and use it in GitHub Desktop.
Save dparoulek/1753480 to your computer and use it in GitHub Desktop.
Manually instantiate spring-integration objects inside junit test
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-2.0.xsd
http://www.springframework.org/schema/integration/file http://www.springframework.org/schema/integration/file/spring-integration-file-2.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream-2.0.xsd"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:si="http://www.springframework.org/schema/integration"
xmlns:file="http://www.springframework.org/schema/integration/file"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:stream="http://www.springframework.org/schema/integration/stream"
xmlns:p="http://www.springframework.org/schema/p">
<si:channel id="fileHandler">
</si:channel>
</beans>
package foo.input.file;
import foo.input.file.FileResponder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.Message;
import org.springframework.integration.MessageChannel;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.SourcePollingChannelAdapterFactoryBean;
import org.springframework.integration.config.xml.*;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.file.FileReadingMessageSource;
import org.springframework.integration.file.config.FileInboundChannelAdapterParser;
import org.springframework.integration.file.filters.AcceptOnceFileListFilter;
import org.springframework.integration.file.filters.CompositeFileListFilter;
import org.springframework.integration.file.filters.FileListFilter;
import org.springframework.integration.file.filters.RegexPatternFileListFilter;
import org.springframework.integration.handler.LoggingHandler;
import org.springframework.integration.handler.MessageHandlerChain;
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import static org.junit.Assert.*;
@ContextConfiguration(locations = {"classpath: foo/engine/input/file/test-context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class InputFileScanTest {
@Autowired
@Qualifier("fileHandler")
MessageChannel fileHandler;
@Autowired
ApplicationContext applicationContext;
@Test
public void findFileByRegex() {
assertNotNull(applicationContext);
// STEP 1: set up objects that watch a directory for files with filenames that match regex
//Directory to watch
File dir1 = new File("unit-test/dir1");
if(!dir1.exists()){
dir1.mkdirs();
}
AcceptOnceFileListFilter acceptOnceFileListFilter = new AcceptOnceFileListFilter();
FileListFilter fileListFilter = new RegexPatternFileListFilter("^.*\\.(TXT|csv|test)$");
CompositeFileListFilter compositeFileListFilter = new CompositeFileListFilter();
compositeFileListFilter.addFilter(acceptOnceFileListFilter);
compositeFileListFilter.addFilter(fileListFilter);
FileReadingMessageSource fileReadingMessageSource = new FileReadingMessageSource();
fileReadingMessageSource.setDirectory(dir1);
fileReadingMessageSource.setFilter(compositeFileListFilter);
/**
* chain element is parsed by ChainParser
*/
MessageHandlerChain messageHandlerChain = new MessageHandlerChain();
messageHandlerChain.setBeanFactory(applicationContext);
//Complaining about ERROR org.springframework.integration.handler.LoggingHandler - org.springframework.integration.MessageDeliveryException: Dispatcher has no subscribers.
//LoggingChannelAdapterParser
//LoggingChannelAdapterParser foo;
LoggingHandler loggingHandler = new LoggingHandler("INFO");
loggingHandler.setShouldLogFullMessage(true);
loggingHandler.setBeanFactory(applicationContext);
List fileHandlers = new ArrayList();
FileResponder fileResponder = new FileResponder();
fileHandlers.add(fileResponder);
//fileHandlers.add(loggingHandler);
messageHandlerChain.setHandlers(fileHandlers);
((DirectChannel) fileHandler).subscribe(messageHandlerChain);
//((DirectChannel) fileHandler).subscribe(fileResponder);
/**
* inbound-channel-adaptor parsed by AbstractPollingChannelAdapterParser?
* Looks like maybe we can use FileInboundChannelAdapterParser from "file" xml namespace?
* has a poller element which is a basePollerType parsed by PollerParser
* poller must have a trigger
* in this case, fixed-rate attribute means org.springframework.scheduling.support.PeriodicTrigger
*
*/
PeriodicTrigger periodicTrigger = new PeriodicTrigger(50);
// SourcePollingChannelAdapterFactoryBean sourcePollingChannelAdapterFactoryBean = new SourcePollingChannelAdapterFactoryBean();
// sourcePollingChannelAdapterFactoryBean.setSource(fileReadingMessageSource);
// sourcePollingChannelAdapterFactoryBean.setOutputChannel(fileHandler);
//
// PollerMetadata pollerMetadata = new PollerMetadata();
// pollerMetadata.setTrigger(periodicTrigger);
//
// sourcePollingChannelAdapterFactoryBean.setPollerMetadata(pollerMetadata);
// //sourcePollingChannelAdapterFactoryBean.start();
// sourcePollingChannelAdapterFactoryBean.setAutoStartup(true);
// sourcePollingChannelAdapterFactoryBean.start();
// assertTrue(sourcePollingChannelAdapterFactoryBean.isRunning());
SourcePollingChannelAdapter sourcePollingChannelAdapter = new SourcePollingChannelAdapter();
sourcePollingChannelAdapter.setSource(fileReadingMessageSource);
sourcePollingChannelAdapter.setTrigger(periodicTrigger);
sourcePollingChannelAdapter.setAutoStartup(false);
sourcePollingChannelAdapter.setOutputChannel(fileHandler);
sourcePollingChannelAdapter.setBeanFactory(applicationContext);
FileResponder responder = new FileResponder();
sourcePollingChannelAdapter.start();
assertTrue(sourcePollingChannelAdapter.isRunning());
//applicationContext.
// STEP 2: Associate the file watching stuff with a MessageChannel.
// I think the message channel must be a bean registered with spring and that's how the stuff in step 3 picks it up, maybe?
/**
* PointToPointChannelParser parses <channel>.
* If <channel> contains a <queue> it's a QueueChannel.
* If <channel> contains a <priority-queue> it's a PriorityChannel.
* If <channel> contains a <rendevous-queue> it's a RendevousChannel.
* If <channel> has a "dispatcher" attribute, it's a DirectChannel.
* If <channel> has "task-executor" attribute, it's an ExecutorChannel.
* Otherwise, it looks like it defaults to DirectChannel with a round robin load-balancing strategy
*/
// injected by spring
// MessageChannel fileHandler = new DirectChannel();
assertNotNull(fileHandler);
//sourcePollingChannelAdapter.setOutputChannel(fileHandler);
// STEP 3: Create a file inside the directory and test to make sure the file gets picked up
File newFile = new File(dir1.getAbsolutePath()+"/test1.csv");
if(!newFile.exists()){
try {
newFile.createNewFile();
} catch (IOException e) {
throw new AssertionError("unable to create file");
}
}
assertTrue(newFile.exists());
assertEquals(newFile.getAbsolutePath(), "/Users/dparoulek/code/foo/unit-test/dir1/test1.csv");
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// throw new AssertionError();
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment