2012-10-16 31 views
3

要比較JUnit測試中的多行文本結果,我經常需要從文本表示 轉換爲Java代碼,該代碼使用多行文本初始化字符串。用於Junit測試的換行字符串

E.g.如果測試應該檢查包含XML字符串:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<Customer> 
    <id>100</id> 
    <name>John Doe</name> 
    <orders> 
     <Order> 
      <address>100 main street, smalltown, pa</address> 
      <orderid>1100</orderid> 
     </Order> 
     <Order> 
      <address>5 broadway, ny, ny</address> 
      <orderid>1200</orderid> 
     </Order> 
    </orders> 
</Customer> 

我想用一個工具/發電機採用上面的輸入,並得到以下結果:

String expected =""; 
    expected+="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"; 
expected+="<Customer>\n"; 
expected+=" <id>100</id>\n"; 
expected+=" <name>John Doe</name>\n"; 
expected+=" <orders>\n"; 
expected+="  <Order>\n"; 
expected+="   <address>100 main street, smalltown, pa</address>\n"; 
expected+="   <orderid>1100</orderid>\n"; 
expected+="  </Order>\n"; 
expected+="  <Order>\n"; 
expected+="   <address>5 broadway, ny, ny</address>\n"; 
expected+="   <orderid>1200</orderid>\n"; 
expected+="  </Order>\n"; 
expected+=" </orders>\n"; 
expected+="</Customer>\n"; 

和/或

// Create test file 
    java.io.PrintWriter srcWriter = new java.io.PrintWriter(new java.io.FileOutputStream(testFile)); 
    srcWriter.println("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); 
    srcWriter.println("<Customer>\n"); 
    srcWriter.println(" <id>100</id>\n"); 
    srcWriter.println(" <name>John Doe</name>\n"); 
    srcWriter.println(" <orders>\n"); 
    srcWriter.println("  <Order>\n"); 
    srcWriter.println("   <address>100 main street, smalltown, pa</address>\n"); 
    srcWriter.println("   <orderid>1100</orderid>\n"); 
    srcWriter.println("  </Order>\n"); 
    srcWriter.println("  <Order>\n"); 
    srcWriter.println("   <address>5 broadway, ny, ny</address>\n"); 
    srcWriter.println("   <orderid>1200</orderid>\n"); 
    srcWriter.println("  </Order>\n"); 
    srcWriter.println(" </orders>\n"); 
    srcWriter.println("</Customer>\n"); 
    srcWriter.close(); 
    // PrintWriter never throws Exceptions, one must check the error state manually 
    // 
    if (srcWriter.checkError()) 
    { 
     throw new IOException("can not write " + testFile); 
    } 

什麼是開發工具/ eclipse實用程序或插件來實現這一目標?

  • 需要從文件中多輸入文本(在IDE或命令行)
  • 逃逸引號和反斜線
  • 轉換成Java代碼初始化字符串文字和/或將執行文件建立在Java代碼中,而不需要額外的文件資源
  • 輸出結果,以新的文件和/或控制檯或直接進入編輯器在編譯時使用

輸出文件(如果有的話)不應隨編譯結果一起發貨。在文件模式下,應該從java代碼中的字符串文字重新創建輸入文件的等效項。

+0

那你有以上不工作的兩種解決方案?目前還不清楚問題是什麼,你能否澄清? –

+0

我正在尋找一種將多行文本轉換爲代碼的工具,以便我不必一次又一次地手動執行此操作。 –

+1

如果您有一個字符串對象作爲輸入,您只需將其寫入文件(不需要轉義)。而且,如果您閱讀帶有引號或其他特殊字符的文件,則不需要「轉義」。只有在Java代碼中輸入字符串文字時,才需要使用轉義字符作爲String xyz =「sfdsf」,而不是在使用String實例時。 –

回答

8

假設您從其他源複製多行文本,並且您正在使用Eclipse,它可以自動將文本轉換爲多行String文字。

在我的版本,使其在Windows - >首選項 - >爪哇 - >編輯 - >鍵入 - >粘貼到文本

然後,如果你輸入一個字符串時逃生文本

String expected = " 

和複製一些文字像

blah 
blah 
blah 
blah 
blah 

,然後再粘貼的字符串,Eclipse將創建:

String expected = "blah\n" + 
"blah\n" + 
"blah\n" + 
"blah\n" + 
"blah" 

就我個人而言,如果Java有一個與Perl的HERE文檔相當的多行代碼,那就好了。

+0

優秀 - 我必須明天接受並接受,因爲我的upvote配額今天已經用完了 –

+0

+1真的很高興知道。謝謝。 –

+0

節省很多挫折。謝謝! – Gowtham

1

爲什麼不把XML存儲在單獨的文件中並使用Apache Commons將它讀入字符串?

String fileName = "path/to/your/file"; 
String fileText = FileUtils.readFileToString(new File(fileName)); 

的文件實用程序見本 - https://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

+0

我不想在編譯時使用外部文件。在運行時,他們是o.k. –

+0

我不確定我在你的問題中看到了區別,但是如果是這種情況,我建議你考慮編輯問題以刪除引用'從文件中取多行輸入文本'的項目符號。 –

+0

順便說一句 - 根據你的編輯,我仍然建議這是一個可能的解決方案。這些文件本身可以位於源代碼樹中,該位置不需要在項目外部或文件系統中的其他位置。這簡單歸結爲你希望如何構建你的項目;我已經看到使用'資源'包來存儲文件之類的單元測試等的約定...... –

0

對我來說,這看起來像一個失敗。基本上,你想測試一個漂亮的打印XML與OS特定的換行符。

難道你不能簡單地寫一些簡單的Visitor模式穿越結果和預期的XML來驗證一切是否如預期的那樣?通過這種方式,您還可以避免無效預期和生成XML的問題。

另外,想象一下,如果命名空間聲明按順序變化或者屬性按順序變化:XML在語義上等同,但在語法上不同。應該如何處理?這是一個失敗的測試?在我看來,這是一個通行證。

+0

xml只是一個例子。任何多行字符串都可以作爲輸入。 –

+0

我認爲你可能仍然遇到換行問題。 –

0

對於非Eclipse用戶下面的腳本解決方案可能會有所幫助:

#!/bin/bash 
# 
# Copyright (C) 2012 BITPlan GmbH (http://www.bitplan.com) 
# 
# Author: Wolfgang Fahl 
# 
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#  http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 


# 
# wrap code for use in JUnit Test cases 
# 
wrapJUnit() { 
    file=$1 
    mode=$2 
    echo "wrapping $file with mode $mode" 
    cat $file | gawk -v mode="$mode" ' 

# check mode and add header if mode is file 
BEGIN { 
    if (mode=="file") { 
     print " // Create test file" 
     print " java.io.PrintWriter srcWriter = new java.io.PrintWriter(new java.io.FileOutputStream(testFile));" 
    } 
} 
# work on each line 
{ 
    line=$0 
    # replace a single \ with \\ 
    gsub("\\","\\\\",line) 
    # replace a quote with \ quote 
    gsub("\"","\\\"",line) 
    # output the modified line 
    if (mode=="file") { 
     print " srcWriter.println(\"" line "\\n\");" 
    } else { 
     if (more++) { 
      print " expected+=\"" line "\\n\";" 
     } else { 
      print " expected =\"" line "\\n\";" 
     } 
    } 
} 

END { 
    if (mode=="file") { 
     print " srcWriter.close();" 
     print " // PrintWriter never throws Exceptions, one must check the error state manually" 
     print " //" 
     print " if (srcWriter.checkError())" 
     print " {" 
     print "  throw new IOException(\"can not write \" + testFile);" 
     print " } " 
    } 
} 

' > $1.wrap 
cat $1.wrap 
} 

# 
# 
# show usage 
# 
usage() { 
    echo "usage: wrapcode inputfile [simple]" 
    echo " will take the text and prepare it for JUnit usage as a string"; 
    exit 1 
} 

# 
# check number of command line parameters 
# 
if [ $# -lt 1 ] 
then 
    usage 
fi 

file=$1 

if [ $# -lt 2 ] 
then 
    wrapJUnit $file file 
else 
    wrapJUnit $file string 
fi