2012-11-03 69 views
0

我想我已經看到了一個優雅的方式來使用文件作爲apache單元測試的輸入駱駝,但我的谷歌技能讓我失望。使用一個文件作爲輸入到單元測試

我要的是不是:

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); 

我覺得我看到的是像

template.sendBody("direct:create", someCamelMetod("/src/data/someXmlFile.xml")); 

任何人都知道在哪裏/如果這是記錄?

編輯:

如果有人知道更好的方式,仍然感興趣。

回答

0

不是真的,你必須自己做一些小的工作。你知道,閱讀一個文本文件並不那麼簡單,因爲你可能想知道編碼。在你的第一種情況下(內聯字符串),你總是使用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"))); 
3

如果我理解你的問題正確,您要發送一個XML文件作爲輸入你要測試的路線。我的解決方案是使用作爲駱駝測試支持一部分的adviseWith策略。閱讀在這裏:http://camel.apache.org/testing.html

所以,說下測試的路線是這樣的:在您的測試

from("jms:myQueue") 
    .routeId("route-1") 
    .beanRef(myTransformationBean) 
    .to("file:outputDirectory"); 

您可以通過從文件輪詢消費者更換該發送XML到這條路線。

context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() { 
    @Override 
    public void configure() throws Exception { 
     replaceRouteFromWith("route-1", "file:myInputDirectory"); 
    } 
}); 
context.start(); 

然後你就可以把你的輸入XML文件中的myInputDirectory這將是糾察並用作輸入路徑。

相關問題