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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
require 'test/unit'
require 'soap/marshal'
require 'soap/rpc/standaloneServer'
require 'soap/rpc/driver'
module SOAP
class TestMap < Test::Unit::TestCase
class CustomHashFactory < SOAP::Mapping::Factory
def initialize(itemname)
@itemname = itemname
end
def obj2soap(soap_class, obj, info, map)
soap_obj = SOAP::SOAPStruct.new(SOAP::Mapping::MapQName)
mark_marshalled_obj(obj, soap_obj)
obj.each do |key, value|
elem = SOAP::SOAPStruct.new
elem.add('key', Mapping._obj2soap(key, map))
elem.add('value', Mapping._obj2soap(value, map))
soap_obj.add(@itemname, elem)
end
soap_obj
end
def soap2obj(obj_class, node, info, map)
false
end
end
Map = SOAP::Mapping::Registry.new
Map.add(Hash, SOAP::SOAPStruct, CustomHashFactory.new('customname'))
Port = 17171
class MapServer < SOAP::RPC::StandaloneServer
def initialize(*arg)
super
add_rpc_method(self, 'echo', 'map')
add_rpc_method(self, 'setmap')
end
def echo(map)
map
end
def setmap
self.mapping_registry = Map
nil
end
end
def setup
@server = MapServer.new(self.class.name, nil, '0.0.0.0', Port)
@server.level = Logger::Severity::ERROR
@t = Thread.new {
@server.start
}
@endpoint = "http://localhost:#{Port}/"
@client = SOAP::RPC::Driver.new(@endpoint)
@client.add_rpc_method('echo', 'map')
@client.add_rpc_method('setmap')
@client.wiredump_dev = STDOUT if $DEBUG
end
def teardown
@server.shutdown if @server
if @t
@t.kill
@t.join
end
@client.reset_stream if @client
end
def test_map
h = {'a' => 1, 'b' => 2}
soap = SOAP::Marshal.marshal(h)
puts soap if $DEBUG
obj = SOAP::Marshal.unmarshal(soap)
assert_equal(h, obj)
#
soap = SOAP::Marshal.marshal(h, Map)
puts soap if $DEBUG
obj = SOAP::Marshal.unmarshal(soap, Map)
assert_equal(h, obj)
end
def test_rpc
h = {'a' => 1, 'b' => 2}
@client.wiredump_dev = str = ''
assert_equal(h, @client.echo(h))
assert_equal(0, str.scan(/customname/).size)
#
@client.setmap
@client.wiredump_dev = str = ''
assert_equal(h, @client.echo(h))
assert_equal(4, str.scan(/customname/).size)
#
@client.mapping_registry = Map
@client.wiredump_dev = str = ''
assert_equal(h, @client.echo(h))
assert_equal(8, str.scan(/customname/).size)
end
end
end
|