From 9187dbd3624e7ccab95c6e1ab3830634c7677271 Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Tue, 8 Nov 2016 13:36:17 +0100 Subject: [PATCH 1/3] - added new camel example - added new camel context for junit test --- .../baeldung/camel/file/FileProcessor.java | 20 ++++++ .../com/baeldung/camel/file/FileRouter.java | 15 ++++ .../src/main/resources/camel-context-test.xml | 14 ++++ .../src/main/resources/camel-context.xml | 48 ++++++------- .../file/processor/FileProcessorTest.java | 68 +++++++++++++++++++ 5 files changed, 141 insertions(+), 24 deletions(-) create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/FileProcessor.java create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/file/FileRouter.java create mode 100644 spring-apache-camel/src/main/resources/camel-context-test.xml create mode 100644 spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileProcessor.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileProcessor.java new file mode 100644 index 0000000000..1ea2cad188 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileProcessor.java @@ -0,0 +1,20 @@ +package com.baeldung.camel.file; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class FileProcessor implements Processor { + + public void process(Exchange exchange) throws Exception { + String originalFileName = (String) exchange.getIn().getHeader(Exchange.FILE_NAME, String.class); + + Date date = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); + String changedFileName = dateFormat.format(date) + originalFileName; + exchange.getIn().setHeader(Exchange.FILE_NAME, changedFileName); + } + +} diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileRouter.java b/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileRouter.java new file mode 100644 index 0000000000..5216c9a595 --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/file/FileRouter.java @@ -0,0 +1,15 @@ +package com.baeldung.camel.file; + +import org.apache.camel.builder.RouteBuilder; + +public class FileRouter extends RouteBuilder { + + private static final String SOURCE_FOLDER = "src/test/source-folder"; + private static final String DESTINATION_FOLDER = "src/test/destination-folder"; + + @Override + public void configure() throws Exception { + from("file://" + SOURCE_FOLDER + "?delete=true").process(new FileProcessor()).to("file://" + DESTINATION_FOLDER); + } + +} diff --git a/spring-apache-camel/src/main/resources/camel-context-test.xml b/spring-apache-camel/src/main/resources/camel-context-test.xml new file mode 100644 index 0000000000..e6435db9e5 --- /dev/null +++ b/spring-apache-camel/src/main/resources/camel-context-test.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/spring-apache-camel/src/main/resources/camel-context.xml b/spring-apache-camel/src/main/resources/camel-context.xml index 0c10e0ecef..63ef406fdf 100644 --- a/spring-apache-camel/src/main/resources/camel-context.xml +++ b/spring-apache-camel/src/main/resources/camel-context.xml @@ -1,39 +1,39 @@ - + - + - - + + - - + + - - + + - - - ${body.toLowerCase()} - + + + ${body.toLowerCase()} + - - + + - - - .......... File content conversion completed .......... - - + + + .......... File content conversion completed .......... + + - + - + - + \ No newline at end of file diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java new file mode 100644 index 0000000000..3d63f614e0 --- /dev/null +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java @@ -0,0 +1,68 @@ +package org.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(); + + } +} \ No newline at end of file From e2608793bfbdef223f57ca62787308711a3a3289 Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Mon, 14 Nov 2016 19:43:29 +0100 Subject: [PATCH 2/3] - junit version in pom.xml is updated to the last one --- spring-apache-camel/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-apache-camel/pom.xml b/spring-apache-camel/pom.xml index fbea9b779d..973e3aafb1 100644 --- a/spring-apache-camel/pom.xml +++ b/spring-apache-camel/pom.xml @@ -12,7 +12,7 @@ 2.16.1 4.2.4.RELEASE 1.7 - 4.1 + 4.12 From d33c3868bcbb42163db7edae2955fae16e296e00 Mon Sep 17 00:00:00 2001 From: Ante Pocedulic Date: Mon, 14 Nov 2016 19:52:48 +0100 Subject: [PATCH 3/3] - changed names of jUnit tests due to Baeldung name convenction --- spring-apache-camel/pom.xml | 22 +++++++++++++++---- ...java => FileProcessorIntegrationTest.java} | 2 +- .../{AppTest.java => AppIntegrationTest.java} | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) rename spring-apache-camel/src/test/java/org/apache/camel/file/processor/{FileProcessorTest.java => FileProcessorIntegrationTest.java} (98%) rename spring-apache-camel/src/test/java/org/apache/camel/main/{AppTest.java => AppIntegrationTest.java} (95%) diff --git a/spring-apache-camel/pom.xml b/spring-apache-camel/pom.xml index 973e3aafb1..1e08a977c7 100644 --- a/spring-apache-camel/pom.xml +++ b/spring-apache-camel/pom.xml @@ -16,20 +16,20 @@ - + junit junit ${junit.version} test - + org.apache.camel camel-core ${env.camel.version} - + org.apache.camel camel-spring @@ -49,7 +49,7 @@ - + @@ -62,6 +62,20 @@ ${java.version} + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + diff --git a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorIntegrationTest.java similarity index 98% rename from spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java rename to spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorIntegrationTest.java index 3d63f614e0..e73ad1e4a4 100644 --- a/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorTest.java +++ b/spring-apache-camel/src/test/java/org/apache/camel/file/processor/FileProcessorIntegrationTest.java @@ -12,7 +12,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baeldung.camel.file.FileProcessor; -public class FileProcessorTest { +public class FileProcessorIntegrationTest { private static final long DURATION_MILIS = 10000; private static final String SOURCE_FOLDER = "src/test/source-folder"; diff --git a/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java b/spring-apache-camel/src/test/java/org/apache/camel/main/AppIntegrationTest.java similarity index 95% rename from spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java rename to spring-apache-camel/src/test/java/org/apache/camel/main/AppIntegrationTest.java index 87b20369f3..fc7fa9653b 100644 --- a/spring-apache-camel/src/test/java/org/apache/camel/main/AppTest.java +++ b/spring-apache-camel/src/test/java/org/apache/camel/main/AppIntegrationTest.java @@ -15,7 +15,7 @@ import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Paths; -public class AppTest extends TestCase { +public class AppIntegrationTest extends TestCase { private static final String FILE_NAME = "file.txt"; private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/";