This commit is contained in:
Jonathan Cook
2019-10-23 15:01:44 +02:00
parent db85c8f275
commit 684ec0d2e3
20486 changed files with 1642483 additions and 0 deletions
@@ -0,0 +1,71 @@
package com.apache.camel.file.processor;
import java.io.File;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.baeldung.camel.file.cfg.ContentBasedFileRouterConfig;
@RunWith(JUnit4.class)
public class ContentBasedFileRouterIntegrationTest {
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_TXT = "src/test/destination-folder-txt";
private static final String DESTINATION_FOLDER_OTHER = "src/test/destination-folder-other";
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderTxt = new File(DESTINATION_FOLDER_TXT);
File destinationFolderOther = new File(DESTINATION_FOLDER_OTHER);
cleanFolder(sourceFolder);
cleanFolder(destinationFolderTxt);
cleanFolder(destinationFolderOther);
sourceFolder.mkdirs();
File file1 = new File(SOURCE_FOLDER + "/File1.txt");
File file2 = new File(SOURCE_FOLDER + "/File2.csv");
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
@Ignore
public void routeWithXMLConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"camel-context-ContentBasedFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
@Test
@Ignore
public void routeWithJavaConfigTest() throws InterruptedException {
AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(
ContentBasedFileRouterConfig.class);
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@@ -0,0 +1,43 @@
package com.apache.camel.file.processor;
import java.io.File;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DeadLetterChannelFileRouterIntegrationTest {
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
cleanFolder(sourceFolder);
sourceFolder.mkdirs();
File file = new File(SOURCE_FOLDER + "/File.txt");
file.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 routeTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-DeadLetterChannelFileRouter.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@@ -0,0 +1,68 @@
package com.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 FileProcessorIntegrationTest {
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();
}
}
@@ -0,0 +1,50 @@
package com.apache.camel.file.processor;
import java.io.File;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageTranslatorFileRouterIntegrationTest {
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
@Ignore
public void routeTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-MessageTranslatorFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@@ -0,0 +1,53 @@
package com.apache.camel.file.processor;
import java.io.File;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MulticastFileRouterIntegrationTest {
private static final long DURATION_MILIS = 10000;
private static final String SOURCE_FOLDER = "src/test/source-folder";
private static final String DESTINATION_FOLDER_WORLD = "src/test/destination-folder-world";
private static final String DESTINATION_FOLDER_HELLO = "src/test/destination-folder-hello";
@Before
public void setUp() throws Exception {
File sourceFolder = new File(SOURCE_FOLDER);
File destinationFolderWorld = new File(DESTINATION_FOLDER_WORLD);
File destinationFolderHello = new File(DESTINATION_FOLDER_HELLO);
cleanFolder(sourceFolder);
cleanFolder(destinationFolderWorld);
cleanFolder(destinationFolderHello);
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
@Ignore
public void routeTest() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-MulticastFileRouterTest.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@@ -0,0 +1,52 @@
package com.apache.camel.file.processor;
import java.io.File;
import java.io.FileWriter;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SplitterFileRouterIntegrationTest {
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 file = new File(SOURCE_FOLDER + "/File.txt");
FileWriter fileWriter = new FileWriter(file, false);
fileWriter.write("Hello\nWorld");
file.createNewFile();
fileWriter.close();
}
private void cleanFolder(File folder) {
File[] files = folder.listFiles();
if (files != null) {
for (File file : files) {
if (file.isFile()) {
file.delete();
}
}
}
}
@Test
@Ignore
public void routeTests() throws InterruptedException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("camel-context-SplitterFileRouter.xml");
Thread.sleep(DURATION_MILIS);
applicationContext.close();
}
}
@@ -0,0 +1,117 @@
package com.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;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
public class AppIntegrationTest 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 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();
}
@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);
String inputFileContent = readFileContent(SAMPLE_INPUT_DIR + FILE_NAME);
String outputUpperCase = readFileContent(UPPERCASE_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();
}
}
}
}
@@ -0,0 +1,13 @@
package org.baeldung;
import org.junit.Test;
import com.baeldung.camel.main.App;
public class SpringContextIntegrationTest {
@Test
public final void testMain() throws Exception {
App.main(null);
}
}
@@ -0,0 +1,13 @@
package org.baeldung;
import org.junit.Test;
import com.baeldung.camel.main.App;
public class SpringContextTest {
@Test
public final void testMain() throws Exception {
App.main(null);
}
}