Reputation: 1072
I think I have seen an elegant way to use a file as input for a unit test in apache camel but my google skills are failing me.
What I want is instead of:
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
<snip>...long real life xml that quickly fills up test files.</snip>";
template.sendBody("direct:create", xml);
What I think I have seen is something like
template.sendBody("direct:create", someCamelMetod("/src/data/someXmlFile.xml"));
Anybody knows where/if this is documented?
Edit:
What I ended up doing was just creating a
private void readFile(String fileName) throws ... function
Still interested if anyone knows a nicer way though.
Upvotes: 0
Views: 3192
Reputation: 191
If I understand your question correctly you want to send an xml file as input to the route you want to test. My solution would be to use the adviseWith strategy which is a part of the Camel test support. Read about it here: http://camel.apache.org/testing.html
So, say that the route under test is something like this:
from("jms:myQueue")
.routeId("route-1")
.beanRef(myTransformationBean)
.to("file:outputDirectory");
in your test you can send xml into this route by replacing this from a file polling consumer.
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceRouteFromWith("route-1", "file:myInputDirectory");
}
});
context.start();
Then you can put your input xml file in the myInputDirectory and it will be picket up and used as input to the route.
Upvotes: 3
Reputation: 22279
Not really, you have to do some minor work yourself. You know, reading a text file isn't that simple since you might want to know the encoding. In your first case (inline string), you're always using UTF-16. A file can be whatever, and you have to know it, since it won't tell you what encoding it is. Given you have UTF-8, you can do something like this:
public String streamToString(InputStream str){
Scanner scanner = new Scanner(is, "UTF-8").useDelimiter("\\A");
if (scanner.hasNext())
return scanner.next();
return "";
}
// from classpath using ObjectHelper from Camel.
template.sendBody("direct:create", streamToString(ObjectHelper.loadResourceAsStream("/src/data/someXmlFile.xml")));
Upvotes: 0