Skip to content

Commit

Permalink
Advanced Camel article (eugenp#905)
Browse files Browse the repository at this point in the history
* - changed test package name from org.baeldung to com.baeldung
- streams are added where neccessary
- format fixes

* Adding Java config for Content Based File Router

* Adding Java config for Content Based File Router
  • Loading branch information
ante003 authored and pivovarit committed Dec 21, 2016
1 parent 42067af commit acdea70
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 42 deletions.
8 changes: 7 additions & 1 deletion spring-apache-camel/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<url>http://maven.apache.org</url>

<properties>
<env.camel.version>2.16.1</env.camel.version>
<env.camel.version>2.18.1</env.camel.version>
<env.spring.version>4.3.4.RELEASE</env.spring.version>
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
<java.version>1.8</java.version>
Expand Down Expand Up @@ -53,6 +53,12 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-javaconfig</artifactId>
<version>${env.camel.version}</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.baeldung.camel.file.cfg;

import java.util.Arrays;
import java.util.List;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spring.javaconfig.CamelConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baeldung.camel.file.ContentBasedFileRouter;

@Configuration
public class ContentBasedFileRouterConfig extends CamelConfiguration {

@Bean
ContentBasedFileRouter getContentBasedFileRouter() {
return new ContentBasedFileRouter();
}

@Override
public List<RouteBuilder> routes() {
return Arrays.asList(getContentBasedFileRouter());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,65 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baeldung.camel.file.cfg.ContentBasedFileRouterConfig;

@RunWith(JUnit4.class)
public class ContentBasedFileRouterIntegrationTest {

private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";

@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);

cleanFolder(sourceFolder);
cleanFolder(destinationFolderTxt);
cleanFolder(destinationFolderOther);

sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
file1.createNewFile();
file2.createNewFile();
}

private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}

@Test
@Ignore
public void routeTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-ContentBasedFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();

}
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";

@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);

cleanFolder(sourceFolder);
cleanFolder(destinationFolderTxt);
cleanFolder(destinationFolderOther);

sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
file1.createNewFile();
file2.createNewFile();
}

private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}

@Test
@Ignore
public void routeWithXMLConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"camel-context-ContentBasedFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();

}

@Test
@Ignore
public void routeWithJavaConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(
ContentBasedFileRouterConfig.class);
Thread.sleep(DURATION_MILIS);
applicationContext.close();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.apache.camel.file.processor;

import java.io.File;

import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.baeldung.camel.file.FileProcessor;


public class FileProcessorTest {

private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER = "src/test/destination-folder";

@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolder = new File(DESTINATION_FOLDER);

cleanFolder(sourceFolder);
cleanFolder(destinationFolder);

sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.txt");
file1.createNewFile();
file2.createNewFile();
}

private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}

@Test
public void moveFolderContentJavaDSLTest() throws Exception {
final CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER);
}
});
camelContext.start();
Thread.sleep(DURATION_MILIS);
camelContext.stop();
}

@Test
public void moveFolderContentSpringDSLTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-test.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();

}
}

0 comments on commit acdea70

Please sign in to comment.