Clean up Apache Camel example

This commit is contained in:
David Morley 2016-02-18 21:31:51 -06:00
parent 9d6f56c40a
commit 54a1235d17
7 changed files with 147 additions and 148 deletions

View File

@ -23,6 +23,6 @@ To build this application execute following maven command in ApacheCamelFileProc
<code>mvn clean install</code>
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:
<code>mvn exec:java -Dexec.mainClass="org.apache.camel.main.App"</code>
<code>mvn exec:java -Dexec.mainClass="App"</code>

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -1,40 +1,39 @@
<?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-3.1.xsd
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-3.1.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route customId="true" id="route1">
<route customId="true" id="route1">
<!-- Read files from input directory -->
<from uri="file://src/test/data/input" />
<!-- Read files from input directory -->
<from uri="file://src/test/data/input"/>
<!-- Transform content to UpperCase -->
<process ref="myFileProcessor" />
<!-- Transform content to UpperCase -->
<process ref="myFileProcessor"/>
<!-- Write converted file content -->
<to uri="file://src/test/data/outputUpperCase" />
<!-- Write converted file content -->
<to uri="file://src/test/data/outputUpperCase"/>
<!-- Transform content to LowerCase -->
<transform>
<simple>${body.toLowerCase()}</simple>
</transform>
<!-- Transform content to LowerCase -->
<transform>
<simple>${body.toLowerCase()}</simple>
</transform>
<!-- Write converted file content -->
<to uri="file://src/test/data/outputLowerCase" />
<!-- Write converted file content -->
<to uri="file://src/test/data/outputLowerCase"/>
<!-- Display process completion message on console -->
<transform>
<simple> .......... File content conversion completed ..........
</simple>
</transform>
<to uri="stream:out" />
<!-- Display process completion message on console -->
<transform>
<simple>.......... File content conversion completed ..........</simple>
</transform>
<to uri="stream:out"/>
</route>
</route>
</camelContext>
</camelContext>
<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />
<bean id="myFileProcessor" class="com.baeldung.camel.processor.FileProcessor"/>
</beans>

View File

@ -1 +1 @@
THIS IS UPPERCASE CONTENT. this is lowercase content.
This is data that will be processed by a Camel route!

View File

@ -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();
}
}
}
}