Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/test/soap/test_mapping.rb

Revision 1921, 3.0 kB (checked in by nahi, 1 year ago)
Line 
1 require 'test/unit'
2 require 'soap/mapping'
3 require 'soap/marshal'
4
5
6 module SOAP
7
8
9 class TestMapping < Test::Unit::TestCase
10
11   class MappablePerson
12     attr_reader :name
13     attr_reader :age
14
15     def initialize(name, age)
16       @name, @age = name, age
17     end
18
19     def self.soap_marshallable
20       true
21     end
22   end
23
24   class UnmappablePerson
25     attr_reader :name
26     attr_reader :age
27
28     def initialize(name, age)
29       @name, @age = name, age
30     end
31
32     def self.soap_marshallable
33       false
34     end
35   end
36
37   def test_mappable
38     xml = <<__XML__
39 <?xml version="1.0" encoding="utf-8" ?>
40 <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
41     xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
42     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
43   <env:Body>
44     <SOAP..TestMapping..MappablePerson env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
45       <name>nahi</name>
46       <age>37</age>
47     </SOAP..TestMapping..MappablePerson>
48   </env:Body>
49 </env:Envelope>
50 __XML__
51     obj = SOAP::Marshal.load(xml)
52     assert_equal(SOAP::TestMapping::MappablePerson, obj.class)
53     #
54     xml = <<__XML__
55 <?xml version="1.0" encoding="utf-8" ?>
56 <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
57     xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
58     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
59   <env:Body>
60     <SOAP..TestMapping..UnmappablePerson env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
61       <name>nahi</name>
62       <age>37</age>
63     </SOAP..TestMapping..UnmappablePerson>
64   </env:Body>
65 </env:Envelope>
66 __XML__
67     obj = SOAP::Marshal.load(xml)
68     assert_equal(SOAP::Mapping::Object, obj.class)
69   end
70
71   def test_nestedexception
72     ele = Thread.new {}
73     obj = [ele]
74     begin
75       SOAP::Marshal.dump(obj)
76     rescue ::SOAP::Mapping::MappingError => e
77       assert(e.backtrace.find { |line| /\[NESTED\]/ =~ line })
78     end
79   end
80
81   def test_date
82     targets = [
83       ["2002-12-31",
84         "2002-12-31Z"],
85       ["2002-12-31+00:00",
86         "2002-12-31Z"],
87       ["2002-12-31-00:00",
88         "2002-12-31Z"],
89       ["-2002-12-31",
90         "-2002-12-31Z"],
91       ["-2002-12-31+00:00",
92         "-2002-12-31Z"],
93       ["-2002-12-31-00:00",
94         "-2002-12-31Z"],
95     ]
96     targets.each do |str, expectec|
97       d = Date.parse(str)
98       assert_equal(d.class, convert(d).class)
99       assert_equal(d, convert(d))
100     end
101   end
102
103   def test_datetime
104     targets = [
105       ["2002-12-31T23:59:59.00",
106         "2002-12-31T23:59:59Z"],
107       ["2002-12-31T23:59:59+00:00",
108         "2002-12-31T23:59:59Z"],
109       ["2002-12-31T23:59:59-00:00",
110         "2002-12-31T23:59:59Z"],
111       ["-2002-12-31T23:59:59.00",
112         "-2002-12-31T23:59:59Z"],
113       ["-2002-12-31T23:59:59+00:00",
114         "-2002-12-31T23:59:59Z"],
115       ["-2002-12-31T23:59:59-00:00",
116         "-2002-12-31T23:59:59Z"],
117     ]
118     targets.each do |str, expectec|
119       d = DateTime.parse(str)
120       assert_equal(d.class, convert(d).class)
121       assert_equal(d, convert(d))
122     end
123   end
124
125   def convert(obj)
126     SOAP::Mapping.soap2obj(SOAP::Mapping.obj2soap(obj))
127   end
128 end
129
130
131 end
Note: See TracBrowser for help on using the browser.