summaryrefslogtreecommitdiff
path: root/jni/ruby/test/xmlrpc/test_parser.rb
blob: 5591645cd458d9e8b105059cf0875d6f2f6e4cfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
require 'test/unit'
require 'xmlrpc/datetime'
require "xmlrpc/parser"
require 'yaml'

module TestXMLRPC
module GenericParserTest
  def datafile(base)
    File.join(File.dirname(__FILE__), "data", base)
  end

  def load_data(name)
    [File.read(datafile(name) + ".xml"), YAML.load(File.read(datafile(name) + ".expected"))]
  end

  def setup
    @xml1, @expected1 = load_data('xml1')
    @xml2, @expected2 = load_data('bug_covert')
    @xml3, @expected3 = load_data('bug_bool')
    @xml4, @expected4 = load_data('value')

    @cdata_xml, @cdata_expected = load_data('bug_cdata')

    @datetime_xml = File.read(datafile('datetime_iso8601.xml'))
    @datetime_expected = XMLRPC::DateTime.new(2004, 11, 5, 1, 15, 23)

    @fault_doc = File.read(datafile('fault.xml'))
  end

  # test parseMethodResponse --------------------------------------------------

  def test_parseMethodResponse1
    assert_equal(@expected1, @p.parseMethodResponse(@xml1))
  end

  def test_parseMethodResponse2
    assert_equal(@expected2, @p.parseMethodResponse(@xml2))
  end

  def test_parseMethodResponse3
    assert_equal(@expected3, @p.parseMethodResponse(@xml3))
  end

  def test_cdata
    assert_equal(@cdata_expected, @p.parseMethodResponse(@cdata_xml))
  end

  def test_dateTime
    assert_equal(@datetime_expected, @p.parseMethodResponse(@datetime_xml)[1])
  end

  # test parseMethodCall ------------------------------------------------------

  def test_parseMethodCall
    assert_equal(@expected4, @p.parseMethodCall(@xml4))
  end

  # test fault ----------------------------------------------------------------

  def test_fault
    flag, fault = @p.parseMethodResponse(@fault_doc)
     assert_equal(flag, false)
     unless fault.is_a? XMLRPC::FaultException
       assert(false, "must be an instance of class XMLRPC::FaultException")
     end
     assert_equal(fault.faultCode, 4)
     assert_equal(fault.faultString, "an error message")
  end

  def test_fault_message
    fault = XMLRPC::FaultException.new(1234, 'an error message')
    assert_equal('an error message', fault.to_s)
    assert_equal('#<XMLRPC::FaultException: an error message>', fault.inspect)
  end
end

# create test class for each installed parser
XMLRPC::XMLParser.each_installed_parser do |parser|
  klass = parser.class
  name = klass.to_s.split("::").last

  eval %{
    class Test_#{name} < Test::Unit::TestCase
      include GenericParserTest

      def setup
        super
        @p = #{klass}.new
      end
    end
  }
end
end