Hey,
I am trying to use the Java DSL and a properties file using the PropertiesComponent. I have created a UnitTest which works if I use Strings for the directories instead of Properties.
If I want to use properties, a Nullpointer occurs in the setup() method. The context is null there! The location is set correctly in the overridden method "createCamelContext". The file exists as in your source code example in /src/test/resources/firstTest-test.properties and has this content:
file.inbox=target/inbox
file.outbox=target/outbox
So, everything should be fine. But I cannot find out why it does not work
Can you please give me a hint? Thank you.
Best regards,
Kai
package cameltest;
import java.io.File;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.properties.PropertiesComponent;
import org.apache.camel.test.junit4.CamelTestSupport;
import org.junit.Test;
public class FirstTest extends CamelTestSupport {
private String inboxDirectory;
private String outboxDirectory;
// Properties einlesen => Verschiedene Schnittstellen flexibel testen,
// z.B. Mock für Test und richtiges MQ für Integrationstest
@Override
protected CamelContext createCamelContext() throws Exception {
CamelContext context = super.createCamelContext();
System.out.println("kai: context.toString: " + context.toString());
PropertiesComponent prop = context.getComponent("properties", PropertiesComponent.class);
prop.setLocation("classpath:firstTest-test.properties");
// System.out.println("kai: PropertiesComponent.getLocations(): " + prop.getLocations().length);
// System.out.println("kai: " + context.getComponent("properties", PropertiesComponent.class).getLocations().length);
return context;
}
// Route, die getestet werden soll:
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
System.out.println("kai route");
// return new FileToFileRoute();
return new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:{{file.inbox}}?noop=true").choice().otherwise().to("file:{{file.outbox}}");
}
};
}
public void setUp() throws Exception {
deleteDirectory("target/inbox");
deleteDirectory("target/outbox");
System.out.println("kai => context is null => NullPointer! " + context.toString());
// inboxDirectory = context.resolvePropertyPlaceholders("{{file.inbox}}");
// outboxDirectory = context.resolvePropertyPlaceholders("{{file.outbox}}");
//
// deleteDirectory(inboxDirectory);
// deleteDirectory(outboxDirectory);
super.setUp();
}
@Test
public void testCopyFile() throws Exception {
template.sendBodyAndHeader("file://target/inbox", "Hello World",
Exchange.FILE_NAME, "hello.txt");
Thread.sleep(1000);
File target = new File("target/outbox/hello.txt");
// Wurde die Datei kopiert?
assertTrue("File not copied", target.exists());
// Besitzt die Datei den korrekten Inhalt der Nachricht?
String content = context.getTypeConverter().convertTo(String.class, target);
assertEquals("Hello World", content);
}
}