From 54a1235d175827af25d7089f644549365fbb9ff9 Mon Sep 17 00:00:00 2001 From: David Morley Date: Thu, 18 Feb 2016 21:31:51 -0600 Subject: [PATCH] Clean up Apache Camel example --- spring-apache-camel/README.md | 4 +- .../baeldung}/camel/main/App.java | 22 +- .../camel/processor/FileProcessor.java | 14 ++ .../apache/camel/processor/FileProcessor.java | 14 -- .../src/main/resources/camel-context.xml | 49 +++-- .../src/test/data/sampleInputFile/file.txt | 2 +- .../java/org/apache/camel/main/AppTest.java | 190 +++++++++--------- 7 files changed, 147 insertions(+), 148 deletions(-) rename spring-apache-camel/src/main/java/{org/apache => com/baeldung}/camel/main/App.java (63%) create mode 100644 spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java delete mode 100644 spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java diff --git a/spring-apache-camel/README.md b/spring-apache-camel/README.md index 28b95bdf89..f6ae77b2a5 100644 --- a/spring-apache-camel/README.md +++ b/spring-apache-camel/README.md @@ -23,6 +23,6 @@ To build this application execute following maven command in ApacheCamelFileProc mvn clean install -To run this application you can either run our main class org.apache.camel.main.App from your IDE or you can execute following maven command: +To run this application you can either run our main class App from your IDE or you can execute following maven command: -mvn exec:java -Dexec.mainClass="org.apache.camel.main.App" \ No newline at end of file +mvn exec:java -Dexec.mainClass="App" \ No newline at end of file diff --git a/spring-apache-camel/src/main/java/org/apache/camel/main/App.java b/spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java similarity index 63% rename from spring-apache-camel/src/main/java/org/apache/camel/main/App.java rename to spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java index 8674caefca..ac0605a215 100644 --- a/spring-apache-camel/src/main/java/org/apache/camel/main/App.java +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/main/App.java @@ -1,12 +1,12 @@ -package org.apache.camel.main; - -import org.springframework.context.support.ClassPathXmlApplicationContext; - -public class App { - public static void main(final String[] args) throws Exception { - ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml"); - // Keep main thread alive for some time to let application finish processing the input files. - Thread.sleep(5000); - applicationContext.close(); - } +package com.baeldung.camel.main; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class App { + public static void main(final String[] args) throws Exception { + ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml"); + // Keep main thread alive for some time to let application finish processing the input files. + Thread.sleep(5000); + applicationContext.close(); + } } \ No newline at end of file diff --git a/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java b/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java new file mode 100644 index 0000000000..971dd206cd --- /dev/null +++ b/spring-apache-camel/src/main/java/com/baeldung/camel/processor/FileProcessor.java @@ -0,0 +1,14 @@ +package com.baeldung.camel.processor; + +import org.apache.camel.Exchange; +import org.apache.camel.Processor; + +public class FileProcessor implements Processor { + + public void process(Exchange exchange) throws Exception { + String originalFileContent = exchange.getIn().getBody(String.class); + String upperCaseFileContent = originalFileContent.toUpperCase(); + exchange.getIn().setBody(upperCaseFileContent); + } + +} diff --git a/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java b/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java deleted file mode 100644 index a98c77feb8..0000000000 --- a/spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.apache.camel.processor; - -import org.apache.camel.Exchange; -import org.apache.camel.Processor; - -public class FileProcessor implements Processor { - - public void process(Exchange exchange) throws Exception { - String originalFileContent = exchange.getIn().getBody(String.class); - String upperCaseFileContent = originalFileContent.toUpperCase(); - exchange.getIn().setBody(upperCaseFileContent); - } - -} diff --git a/spring-apache-camel/src/main/resources/camel-context.xml b/spring-apache-camel/src/main/resources/camel-context.xml index 75e2a61711..0c10e0ecef 100644 --- a/spring-apache-camel/src/main/resources/camel-context.xml +++ b/spring-apache-camel/src/main/resources/camel-context.xml @@ -1,40 +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/data/sampleInputFile/file.txt b/spring-apache-camel/src/test/data/sampleInputFile/file.txt index c1df3791c9..e057427864 100644 --- a/spring-apache-camel/src/test/data/sampleInputFile/file.txt +++ b/spring-apache-camel/src/test/data/sampleInputFile/file.txt @@ -1 +1 @@ -THIS IS UPPERCASE CONTENT. this is lowercase content. \ No newline at end of file +This is data that will be processed by a Camel route! \ No newline at end of file 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/AppTest.java index 75a161d8cc..87b20369f3 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/AppTest.java @@ -1,5 +1,12 @@ package org.apache.camel.main; +import com.baeldung.camel.main.App; +import junit.framework.TestCase; +import org.apache.camel.util.FileUtil; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -8,110 +15,103 @@ import java.nio.channels.FileChannel; import java.nio.file.Files; import java.nio.file.Paths; -import junit.framework.TestCase; - -import org.apache.camel.util.FileUtil; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - public class AppTest extends TestCase { - private static final String FILE_NAME = "file.txt"; - private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/"; - private static final String TEST_INPUT_DIR = "src/test/data/input/"; - private static final String UPPERCARE_OUTPUT_DIR = "src/test/data/outputUpperCase/"; - private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/"; + private static final String FILE_NAME = "file.txt"; + private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/"; + private static final String TEST_INPUT_DIR = "src/test/data/input/"; + private static final String UPPERCASE_OUTPUT_DIR = "src/test/data/outputUpperCase/"; + private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/"; - @Before - public void setUp() throws Exception { - // Prepare input file for test - copySampleFileToInputDirectory(); - } + @Before + public void setUp() throws Exception { + // Prepare input file for test + copySampleFileToInputDirectory(); + } - @After - public void tearDown() throws Exception { - System.out.println("Deleting the test input and output files..."); - deleteFile(TEST_INPUT_DIR); - deleteFile(LOWERCASE_OUTPUT_DIR); - deleteFile(UPPERCARE_OUTPUT_DIR); - } + @After + public void tearDown() throws Exception { + System.out.println("Deleting the test input and output files..."); + deleteFile(TEST_INPUT_DIR); + deleteFile(LOWERCASE_OUTPUT_DIR); + deleteFile(UPPERCASE_OUTPUT_DIR); + } - @Test - public final void testMain() throws Exception { - App.main(null); + @Test + public final void testMain() throws Exception { + App.main(null); - String inputFileContent = readFileContent(SAMPLE_INPUT_DIR+FILE_NAME); - String outputUpperCase = readFileContent(UPPERCARE_OUTPUT_DIR+FILE_NAME); - String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR+FILE_NAME); - - System.out.println("Input File content = ["+inputFileContent+"]"); - System.out.println("UpperCaseOutput file content = ["+outputUpperCase+"]"); - System.out.println("LowerCaseOtput file content = ["+outputLowerCase+"]"); - - assertEquals(inputFileContent.toUpperCase(), outputUpperCase); - assertEquals(inputFileContent.toLowerCase(), outputLowerCase); - } + String inputFileContent = readFileContent(SAMPLE_INPUT_DIR + FILE_NAME); + String outputUpperCase = readFileContent(UPPERCASE_OUTPUT_DIR + FILE_NAME); + String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR + FILE_NAME); - private String readFileContent(String path) throws IOException { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded); - } + System.out.println("Input File content = [" + inputFileContent + "]"); + System.out.println("UpperCaseOutput file content = [" + outputUpperCase + "]"); + System.out.println("LowerCaseOtput file content = [" + outputLowerCase + "]"); - private void deleteFile(String path) { - try { - FileUtil.removeDir(new File(path)); - } catch (Exception e) { - e.printStackTrace(); - } - } + assertEquals(inputFileContent.toUpperCase(), outputUpperCase); + assertEquals(inputFileContent.toLowerCase(), outputLowerCase); + } - /** - * Copy sample input file to input directory. - */ - private void copySampleFileToInputDirectory() { - File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME); - File destFile = new File(TEST_INPUT_DIR + FILE_NAME); - - if (!sourceFile.exists()) { - System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file."); - } + private String readFileContent(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded); + } - if (!destFile.exists()) { - try { - System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]"); - - File destDir = new File(TEST_INPUT_DIR); - if(!destDir.exists()) { - destDir.mkdir(); - } - destFile.createNewFile(); - } catch (IOException e) { - e.printStackTrace(); - } - } - FileChannel source = null; - FileChannel destination = null; + private void deleteFile(String path) { + try { + FileUtil.removeDir(new File(path)); + } catch (Exception e) { + e.printStackTrace(); + } + } - try { - source = new FileInputStream(sourceFile).getChannel(); - destination = new FileOutputStream(destFile).getChannel(); - if (destination != null && source != null) { - destination.transferFrom(source, 0, source.size()); - } - } catch (Exception e) { - e.printStackTrace(); - } finally { - try { - source.close(); - } catch (Exception e) { - e.printStackTrace(); - } - try { - destination.close(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } + /** + * Copy sample input file to input directory. + */ + private void copySampleFileToInputDirectory() { + File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME); + File destFile = new File(TEST_INPUT_DIR + FILE_NAME); + + if (!sourceFile.exists()) { + System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file."); + } + + if (!destFile.exists()) { + try { + System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]"); + + File destDir = new File(TEST_INPUT_DIR); + if (!destDir.exists()) { + destDir.mkdir(); + } + destFile.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + } + FileChannel source = null; + FileChannel destination = null; + + try { + source = new FileInputStream(sourceFile).getChannel(); + destination = new FileOutputStream(destFile).getChannel(); + if (destination != null && source != null) { + destination.transferFrom(source, 0, source.size()); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + source.close(); + } catch (Exception e) { + e.printStackTrace(); + } + try { + destination.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } }