Skip to content

Commit aedffc1

Browse files
committed
data folder moved under test/data. Test cases added. Code & POM updated.
1 parent c385d23 commit aedffc1

File tree

10 files changed

+163
-31
lines changed

10 files changed

+163
-31
lines changed

spring-apache-camel/data/file1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-apache-camel/data/input/.camel/file1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-apache-camel/data/outputLowerCase/file1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-apache-camel/data/outputUppperCase/file1.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spring-apache-camel/pom.xml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
<properties>
1212
<env.camel.version>2.16.1</env.camel.version>
1313
<env.spring.version>4.2.4.RELEASE</env.spring.version>
14+
<java.version>1.7</java.version>
15+
<junit.version>4.1</junit.version>
1416
</properties>
1517

1618
<dependencies>
17-
19+
20+
<dependency>
21+
<groupId>junit</groupId>
22+
<artifactId>junit</artifactId>
23+
<version>${junit.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
1827
<dependency>
1928
<groupId>org.apache.camel</groupId>
2029
<artifactId>camel-core</artifactId>
@@ -40,4 +49,19 @@
4049
</dependency>
4150

4251
</dependencies>
52+
53+
<build>
54+
<plugins>
55+
<plugin>
56+
<artifactId>maven-compiler-plugin</artifactId>
57+
<configuration>
58+
<verbose>true</verbose>
59+
<fork>true</fork>
60+
<compilerVersion>${java.version}</compilerVersion>
61+
<source>${java.version}</source>
62+
<target>${java.version}</target>
63+
</configuration>
64+
</plugin>
65+
</plugins>
66+
</build>
4367
</project>

spring-apache-camel/src/main/java/org/apache/camel/main/App.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
public class App {
66
public static void main(final String[] args) throws Exception {
7-
//Load application context
87
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context.xml");
9-
Thread.sleep(5000);
8+
// Keep main thread alive for some time to let application finish processing the input files.
9+
Thread.sleep(5000);
1010
applicationContext.close();
1111
}
1212
}

spring-apache-camel/src/main/java/org/apache/camel/processor/FileProcessor.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
public class FileProcessor implements Processor {
77

88
public void process(Exchange exchange) throws Exception {
9-
//Read file content
10-
String originalFileContent = (String)exchange.getIn().getBody(String.class);
11-
12-
//Convert file content to upper case.
9+
String originalFileContent = exchange.getIn().getBody(String.class);
1310
String upperCaseFileContent = originalFileContent.toUpperCase();
14-
15-
//Update file content.
1611
exchange.getIn().setBody(upperCaseFileContent);
1712
}
1813

spring-apache-camel/src/main/resources/camel-context.xml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<beans xmlns="http://www.springframework.org/schema/beans"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel="http://camel.apache.org/schema/spring"
4-
xmlns:util="http://www.springframework.org/schema/util"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
6-
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
7-
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
5+
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
86

97
<camelContext xmlns="http://camel.apache.org/schema/spring">
10-
11-
<route customId="true" id="rout1">
12-
8+
9+
<route customId="true" id="route1">
10+
1311
<!-- Read files from input directory -->
14-
<from uri="file://data/input" />
15-
12+
<from uri="file://src/test/data/input" />
13+
1614
<!-- Transform content to UpperCase -->
1715
<process ref="myFileProcessor" />
18-
16+
1917
<!-- Write converted file content -->
20-
<to uri="file://data/outputUppperCase" />
21-
18+
<to uri="file://src/test/data/outputUpperCase" />
19+
2220
<!-- Transform content to LowerCase -->
2321
<transform>
2422
<simple>${body.toLowerCase()}</simple>
2523
</transform>
26-
24+
2725
<!-- Write converted file content -->
28-
<to uri="file://data/outputLowerCase" />
29-
26+
<to uri="file://src/test/data/outputLowerCase" />
27+
3028
<!-- Display process completion message on console -->
3129
<transform>
32-
<simple> .......... File content conversion completed ..........</simple>
30+
<simple> .......... File content conversion completed ..........
31+
</simple>
3332
</transform>
3433
<to uri="stream:out" />
35-
34+
3635
</route>
37-
36+
3837
</camelContext>
3938

4039
<bean id="myFileProcessor" class="org.apache.camel.processor.FileProcessor" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
THIS IS UPPERCASE CONTENT. this is lowercase content.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package org.apache.camel.main;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.file.Files;
9+
import java.nio.file.Paths;
10+
11+
import junit.framework.TestCase;
12+
13+
import org.apache.camel.util.FileUtil;
14+
import org.junit.After;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
18+
public class AppTest extends TestCase {
19+
20+
private static final String FILE_NAME = "file.txt";
21+
private static final String SAMPLE_INPUT_DIR = "src/test/data/sampleInputFile/";
22+
private static final String TEST_INPUT_DIR = "src/test/data/input/";
23+
private static final String UPPERCARE_OUTPUT_DIR = "src/test/data/outputUpperCase/";
24+
private static final String LOWERCASE_OUTPUT_DIR = "src/test/data/outputLowerCase/";
25+
26+
@Before
27+
public void setUp() throws Exception {
28+
// Prepare input file for test
29+
copySampleFileToInputDirectory();
30+
}
31+
32+
@After
33+
public void tearDown() throws Exception {
34+
System.out.println("Deleting the test input and output files...");
35+
deleteFile(TEST_INPUT_DIR);
36+
deleteFile(LOWERCASE_OUTPUT_DIR);
37+
deleteFile(UPPERCARE_OUTPUT_DIR);
38+
}
39+
40+
@Test
41+
public final void testMain() throws Exception {
42+
App.main(null);
43+
44+
String inputFileContent = readFileContent(SAMPLE_INPUT_DIR+FILE_NAME);
45+
String outputUpperCase = readFileContent(UPPERCARE_OUTPUT_DIR+FILE_NAME);
46+
String outputLowerCase = readFileContent(LOWERCASE_OUTPUT_DIR+FILE_NAME);
47+
48+
System.out.println("Input File content = ["+inputFileContent+"]");
49+
System.out.println("UpperCaseOutput file content = ["+outputUpperCase+"]");
50+
System.out.println("LowerCaseOtput file content = ["+outputLowerCase+"]");
51+
52+
assertEquals(inputFileContent.toUpperCase(), outputUpperCase);
53+
assertEquals(inputFileContent.toLowerCase(), outputLowerCase);
54+
}
55+
56+
private String readFileContent(String path) throws IOException {
57+
byte[] encoded = Files.readAllBytes(Paths.get(path));
58+
return new String(encoded);
59+
}
60+
61+
private void deleteFile(String path) {
62+
try {
63+
FileUtil.removeDir(new File(path));
64+
} catch (Exception e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
69+
/**
70+
* Copy sample input file to input directory.
71+
*/
72+
private void copySampleFileToInputDirectory() {
73+
File sourceFile = new File(SAMPLE_INPUT_DIR + FILE_NAME);
74+
File destFile = new File(TEST_INPUT_DIR + FILE_NAME);
75+
76+
if (!sourceFile.exists()) {
77+
System.out.println("Sample input file not found at location = [" + SAMPLE_INPUT_DIR + FILE_NAME + "]. Please provide this file.");
78+
}
79+
80+
if (!destFile.exists()) {
81+
try {
82+
System.out.println("Creating input file = [" + TEST_INPUT_DIR + FILE_NAME + "]");
83+
84+
File destDir = new File(TEST_INPUT_DIR);
85+
if(!destDir.exists()) {
86+
destDir.mkdir();
87+
}
88+
destFile.createNewFile();
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
}
93+
FileChannel source = null;
94+
FileChannel destination = null;
95+
96+
try {
97+
source = new FileInputStream(sourceFile).getChannel();
98+
destination = new FileOutputStream(destFile).getChannel();
99+
if (destination != null && source != null) {
100+
destination.transferFrom(source, 0, source.size());
101+
}
102+
} catch (Exception e) {
103+
e.printStackTrace();
104+
} finally {
105+
try {
106+
source.close();
107+
} catch (Exception e) {
108+
e.printStackTrace();
109+
}
110+
try {
111+
destination.close();
112+
} catch (Exception e) {
113+
e.printStackTrace();
114+
}
115+
}
116+
}
117+
}

0 commit comments

Comments
 (0)