Apache camel 使用文件作为单元测试的输入
我想我已经看到了在ApacheCamel中使用文件作为单元测试输入的优雅方式,但我的google技能让我失望 我想要的不是:Apache camel 使用文件作为单元测试的输入,apache-camel,Apache Camel,我想我已经看到了在ApacheCamel中使用文件作为单元测试输入的优雅方式,但我的google技能让我失望 我想要的不是: 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",
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);
有人知道在哪里/是否有记录吗
编辑:
- 交叉邮寄至
- 我最后做的只是创造一个 私有void readFile(字符串文件名)引发。。。作用
如果有人知道更好的方法,你仍然很感兴趣。不太感兴趣,你必须自己做一些小工作。你知道,读取文本文件并不是那么简单,因为你可能想知道编码。在第一种情况下(内联字符串),您总是使用UTF-16。一个文件可以是任何东西,你必须知道它,因为它不会告诉你它是什么编码。如果您有UTF-8,您可以这样做:
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")));
from("jms:myQueue")
.routeId("route-1")
.beanRef(myTransformationBean)
.to("file:outputDirectory");
不太好,你得自己做些小活儿。你知道,读取文本文件并不是那么简单,因为你可能想知道编码。在第一种情况下(内联字符串),您总是使用UTF-16。一个文件可以是任何东西,你必须知道它,因为它不会告诉你它是什么编码。如果您有UTF-8,您可以这样做:
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")));
from("jms:myQueue")
.routeId("route-1")
.beanRef(myTransformationBean)
.to("file:outputDirectory");
如果我正确理解了您的问题,那么您希望向要测试的路由发送一个xml文件作为输入。我的解决方案是使用adviseWith策略,它是骆驼测试支持的一部分。请在此处阅读: 那么,假设测试中的路线是这样的:
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")));
from("jms:myQueue")
.routeId("route-1")
.beanRef(myTransformationBean)
.to("file:outputDirectory");
在您的测试中,您可以通过替换来自文件轮询消费者的xml来将xml发送到此路由
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceRouteFromWith("route-1", "file:myInputDirectory");
}
});
context.start();
然后,您可以将输入xml文件放在myInputDirectory中,它将被拾取并用作路由的输入 如果我正确理解了您的问题,您希望将xml文件作为输入发送到要测试的路由。我的解决方案是使用adviseWith策略,它是骆驼测试支持的一部分。请在此处阅读: 那么,假设测试中的路线是这样的:
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")));
from("jms:myQueue")
.routeId("route-1")
.beanRef(myTransformationBean)
.to("file:outputDirectory");
在您的测试中,您可以通过替换来自文件轮询消费者的xml来将xml发送到此路由
context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
@Override
public void configure() throws Exception {
replaceRouteFromWith("route-1", "file:myInputDirectory");
}
});
context.start();
然后,您可以将输入xml文件放在myInputDirectory中,它将被拾取并用作路由的输入