xmlunit の調査 (その2)
XMLUnit の XMLAssert は、うまく使えなかったので、
XMLUnit の Diff を使う事にした。
テスト用の2つの xml ファイルと、テストケースを示す。
$ cat hello1.xml
<struct>
<int> 3 </int>
<boolean>false</boolean>
</struct>
$ cat hello2.xml
<struct>
<boolean>false</boolean>
<int>3</int>
</struct>
$ cat SampleTest.rb
# $KCODE = 'utf8'
require 'pp'
require 'java'
require 'test/unit'
require 'xmlunit-1.2.jar'
import 'org.custommonkey.xmlunit.XMLUnit'
import 'org.custommonkey.xmlunit.Diff'
import 'org.custommonkey.xmlunit.DetailedDiff'
import 'org.xml.sax.InputSource'
class Utils
def self.compare(file1, file2)
source1 = InputSource.new(file1)
source2 = InputSource.new(file2)
Diff.new(source1, source2)
end
end
class SampleTest < Test::Unit::TestCase
def setup
# pp "----- setUp"
end
def teardown
# pp "----- tearDown"
end
def test_Diff
myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
myDiff = Diff.new(myControlXML, myTestXML);
assert_equal(true, myDiff.similar(), "pieces of XML are similar " + myDiff.to_s)
assert_equal(false, myDiff.identical(), "but are they identical? " + myDiff.to_s)
end
def test_DetailedDiff
myControlXML = "<struct><int>3</int><boolean>false</boolean></struct>";
myTestXML = "<struct><boolean>false</boolean><int>3</int></struct>";
myDiff = DetailedDiff.new(Diff.new(myControlXML, myTestXML));
# myDiff.getAllDifferences.each do |d|
# pp d
# end
end
def test_Compare
c = Utils::compare('hello1.xml', 'hello2.xml')
assert_equal(false, c.similar()) # node の順番を無視して比較
assert_equal(false, c.identical())
end
def test_Compare
# 空白文字の差を無視する
XMLUnit::setIgnoreWhitespace(true)
c = Utils::compare('hello1.xml', 'hello2.xml')
assert_equal(true, c.similar()) # node の順番を無視して比較
assert_equal(false, c.identical())
end
end
実行結果はこんなふうになる。
$ jruby SampleTest.rb
Loaded suite SampleTest
Started
...
Finished in 0.15 seconds.
3 tests, 4 assertions, 0 failures, 0 errors
xml を出力する jruby で書くツールの unit テストには、この方法を使ってみようと思う。
# ファイル経由で比較するなら、 java, ruby, jruby を問わずにテストできる。
最近のコメント