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

root/trunk/test/soap/test_custommap.rb

Revision 1963, 2.4 kB (checked in by nahi, 1 year ago)
  • update tests. do not crash at teardown when setup failed. closes #396.
Line 
1 require 'test/unit'
2 require 'soap/marshal'
3 require 'soap/rpc/standaloneServer'
4 require 'soap/rpc/driver'
5
6
7 module SOAP
8
9
10 class TestMap < Test::Unit::TestCase
11
12   class CustomHashFactory < SOAP::Mapping::Factory
13     def initialize(itemname)
14       @itemname = itemname
15     end
16
17     def obj2soap(soap_class, obj, info, map)
18       soap_obj = SOAP::SOAPStruct.new(SOAP::Mapping::MapQName)
19       mark_marshalled_obj(obj, soap_obj)
20       obj.each do |key, value|
21         elem = SOAP::SOAPStruct.new
22         elem.add('key', Mapping._obj2soap(key, map))
23         elem.add('value', Mapping._obj2soap(value, map))
24         soap_obj.add(@itemname, elem)
25       end
26       soap_obj
27     end
28
29     def soap2obj(obj_class, node, info, map)
30       false
31     end
32   end
33
34   Map = SOAP::Mapping::Registry.new
35   Map.add(Hash, SOAP::SOAPStruct, CustomHashFactory.new('customname'))
36
37   Port = 17171
38
39   class MapServer < SOAP::RPC::StandaloneServer
40     def initialize(*arg)
41       super
42       add_rpc_method(self, 'echo', 'map')
43       add_rpc_method(self, 'setmap')
44     end
45
46     def echo(map)
47       map
48     end
49
50     def setmap
51       self.mapping_registry = Map
52       nil
53     end
54   end
55
56   def setup
57     @server = MapServer.new(self.class.name, nil, '0.0.0.0', Port)
58     @server.level = Logger::Severity::ERROR
59     @t = Thread.new {
60       @server.start
61     }
62     @endpoint = "http://localhost:#{Port}/"
63     @client = SOAP::RPC::Driver.new(@endpoint)
64     @client.add_rpc_method('echo', 'map')
65     @client.add_rpc_method('setmap')
66     @client.wiredump_dev = STDOUT if $DEBUG
67   end
68
69   def teardown
70     @server.shutdown if @server
71     if @t
72       @t.kill
73       @t.join
74     end
75     @client.reset_stream if @client
76   end
77
78   def test_map
79     h = {'a' => 1, 'b' => 2}
80     soap = SOAP::Marshal.marshal(h)
81     puts soap if $DEBUG
82     obj = SOAP::Marshal.unmarshal(soap)
83     assert_equal(h, obj)
84     #
85     soap = SOAP::Marshal.marshal(h, Map)
86     puts soap if $DEBUG
87     obj = SOAP::Marshal.unmarshal(soap, Map)
88     assert_equal(h, obj)
89   end
90
91   def test_rpc
92     h = {'a' => 1, 'b' => 2}
93     @client.wiredump_dev = str = ''
94     assert_equal(h, @client.echo(h))
95     assert_equal(0, str.scan(/customname/).size)
96     #
97     @client.setmap
98     @client.wiredump_dev = str = ''
99     assert_equal(h, @client.echo(h))
100     assert_equal(4, str.scan(/customname/).size)
101     #
102     @client.mapping_registry = Map
103     @client.wiredump_dev = str = ''
104     assert_equal(h, @client.echo(h))
105     assert_equal(8, str.scan(/customname/).size)
106   end
107 end
108
109
110 end
Note: See TracBrowser for help on using the browser.