5

JUnit テストで複数行テキストの結果を比較するには、テキスト表現から、複数行テキストで文字列を初期化する Java コードに移行する必要があることがよくあります。

たとえば、テストで以下を含む 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 コードの文字列リテラルから再作成する必要があります。

4

4 に答える 4

8

別のソースから複数行のテキストをコピーしていて、Eclipse を使用していると仮定すると、テキストを複数行の文字列リテラルに自動的に変換できます。

私のバージョンでは、Windows -> Preferences -> Java -> Editor -> Typing -> Escape text when paste into a Stringliteral の下で有効にします

次に、入力すると

String expected = "

次のようなテキストをコピーします

blah
blah
blah
blah
blah

次に文字列を貼り付けると、Eclipse は以下を作成します。

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

個人的には、Java に Perl の HERE ドキュメントに相当する複数行があればいいと思います。

于 2012-10-16T15:16:13.877 に答える
1

XML を別のファイルに保存し、Apache Commons を使用して String に読み込んでみませんか?

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

FileUtils については、こちらを参照してください - https://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

于 2012-10-16T15:22:02.653 に答える
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
于 2012-10-17T07:31:24.657 に答える
0

私には、これは失敗のように見えます。基本的に、OS 固有の改行を含むきれいに印刷された XML に対してテストする必要があります。

すべてが期待どおりであることを確認するために、結果の XML と期待される XML を横断する単純な Visitor パターンを簡単に記述できませんか? このようにして、予期された XML と生成された XML が無効になる問題も回避できます。

また、名前空間の宣言が順番に変更されたり、属性が順番に変更されたりすると想像してみてください。XML は意味的には同等ですが、構文的には異なります。これはどのように扱われるべきですか?失敗したテストですか?私の見解では、それはパスです。

于 2012-10-16T15:40:35.333 に答える