data folder moved under test/data. Test cases added. Code & POM updated.

This commit is contained in:
ypatil 2016-02-15 15:29:13 +05:30
parent c385d23cd5
commit aedffc1d13
10 changed files with 163 additions and 31 deletions

View File

@ -1 +0,0 @@
THIS IS UPPERCASE CONTENT. this is lowecase content.

View File

@ -1 +0,0 @@
THIS IS UPPERCASE CONTENT. this is lowecase content.

View File

@ -1 +0,0 @@
this is uppercase content. this is lowecase content.

View File

@ -1 +0,0 @@
THIS IS UPPERCASE CONTENT. THIS IS LOWECASE CONTENT.

View File

@ -11,10 +11,19 @@
<properties>
<env.camel.version>2.16.1</env.camel.version>
<env.spring.version>4.2.4.RELEASE</env.spring.version>
<java.version>1.7</java.version>
<junit.version>4.1</junit.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
@ -40,4 +49,19 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<verbose>true</verbose>
<fork>true</fork>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -4,9 +4,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(final String[] args) throws Exception {
//Load application context
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml");
Thread.sleep(5000);
// Keep main thread alive for some time to let application finish processing the input files.
Thread.sleep(5000);
applicationContext.close();
}
}

View File

@ -6,13 +6,8 @@ import org.apache.camel.Processor;
public class FileProcessor implements Processor {
public void process(Exchange exchange) throws Exception {
//Read file content
String originalFileContent = (String)exchange.getIn().getBody(String.class);
//Convert file content to upper case.
String originalFileContent = exchange.getIn().getBody(String.class);
String upperCaseFileContent = originalFileContent.toUpperCase();
//Update file content.
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" xmlns:camel="http://camel.apache.org/schema/spring"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-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">
<route customId="true" id="rout1">
<route customId="true" id="route1">
<!-- Read files from input directory -->
<from uri="file://data/input" />
<from uri="file://src/test/data/input" />
<!-- Transform content to UpperCase -->
<process ref="myFileProcessor" />
<!-- Write converted file content -->
<to uri="file://data/outputUppperCase" />
<to uri="file://src/test/data/outputUpperCase" />
<!-- Transform content to LowerCase -->
<transform>
<simple>${body.toLowerCase()}</simple>
</transform>
<!-- Write converted file content -->
<to uri="file://data/outputLowerCase" />
<to uri="file://src/test/data/outputLowerCase" />
<!-- Display process completion message on console -->
<transform>
<simple> .......... File content conversion completed ..........</simple>
<simple> .......... File content conversion completed ..........
</simple>
</transform>
<to uri="stream:out" />
</route>
</camelContext>
<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />

View File

@ -0,0 +1 @@
THIS IS UPPERCASE CONTENT. this is lowercase content.

View File

@ -0,0 +1,117 @@
package org.apache.camel.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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/";
@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);
}
@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);
}
private String readFileContent(String path) throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded);
}
private void deleteFile(String path) {
try {
FileUtil.removeDir(new File(path));
} 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();
}
}
}
}