The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

megachucky (17) [Avatar] Offline
#1
Section 6.1.6: Using the PropertiesComponent => NullPointer at CamelContext
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 smilie
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);
}


}
davsclaus (413) [Avatar] Offline
#2
Re: Section 6.1.6: Using the PropertiesComponent => NullPointer at CamelContext
Can you post the stacktrace
megachucky (17) [Avatar] Offline
#3
Re: Section 6.1.6: Using the PropertiesComponent => NullPointer at CamelContext
Sure, here it is.

Line 57 of FileToFileTest.java is this line:

inboxDirectory = context.resolvePropertyPlaceholders("{{file.inbox}}");

I also tried out this line:
System.out.println("kai => context is null => NullPointer! " + context.toString());

=> Same Nullpointer, thus the object "context" is null. I dont understand why it is smilie


-------------------------------------------------------------------------------
Test set: cameltest.FileToFileTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.193 sec <<< FAILURE!
testCopyFile(cameltest.FileToFileTest) Time elapsed: 0.071 sec <<< ERROR!
java.lang.NullPointerException
at cameltest.FileToFileTest.setUp(FileToFileTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:59)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:115)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:102)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
davsclaus (413) [Avatar] Offline
#4
Re: Section 6.1.6: Using the PropertiesComponent =&gt; NullPointer at CamelContext
You have to be sure to invoke super.setUp() before you access the CamelContext. Because it's the super which creates the camel context.
megachucky (17) [Avatar] Offline
#5
Re: Section 6.1.6: Using the PropertiesComponent =&gt; NullPointer at CamelContext
Thank you Claus.

Sometimes there is such an easy solution to a problem... smilie

Best regards,
Kai