| 1 |
require 'test/unit' |
|---|
| 2 |
require 'wsdl/parser' |
|---|
| 3 |
require 'soap/mapping/wsdlencodedregistry' |
|---|
| 4 |
require 'soap/marshal' |
|---|
| 5 |
require 'wsdl/soap/wsdl2ruby' |
|---|
| 6 |
require File.join(File.dirname(File.expand_path(__FILE__)), '..', '..', 'testutil.rb') |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
class WSDLMarshaller |
|---|
| 10 |
def initialize(wsdlfile) |
|---|
| 11 |
wsdl = WSDL::Parser.new.parse(File.open(wsdlfile) { |f| f.read }) |
|---|
| 12 |
types = wsdl.collect_complextypes |
|---|
| 13 |
@opt = { |
|---|
| 14 |
:decode_typemap => types, |
|---|
| 15 |
:generate_explicit_type => false, |
|---|
| 16 |
:pretty => true |
|---|
| 17 |
} |
|---|
| 18 |
@mapping_registry = ::SOAP::Mapping::WSDLEncodedRegistry.new(types) |
|---|
| 19 |
end |
|---|
| 20 |
|
|---|
| 21 |
def dump(obj, io = nil) |
|---|
| 22 |
ele = ::SOAP::Mapping.obj2soap(obj, @mapping_registry) |
|---|
| 23 |
ele.elename = XSD::QName.new(nil, ele.type.name.to_s) |
|---|
| 24 |
::SOAP::Processor.marshal(::SOAP::SOAPEnvelope.new(nil, ::SOAP::SOAPBody.new(ele)), @opt, io) |
|---|
| 25 |
end |
|---|
| 26 |
|
|---|
| 27 |
def load(io) |
|---|
| 28 |
header, body = ::SOAP::Processor.unmarshal(io, @opt) |
|---|
| 29 |
::SOAP::Mapping.soap2obj(body.root_node, @mapping_registry) |
|---|
| 30 |
end |
|---|
| 31 |
end |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
require File.join(File.dirname(__FILE__), 'person_org') |
|---|
| 35 |
|
|---|
| 36 |
class Person |
|---|
| 37 |
def ==(rhs) |
|---|
| 38 |
@familyname == rhs.familyname and @givenname == rhs.givenname and |
|---|
| 39 |
@var1 == rhs.var1 and @var2 == rhs.var2 and @var3 == rhs.var3 |
|---|
| 40 |
end |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
class TestWSDLMarshal < Test::Unit::TestCase |
|---|
| 45 |
DIR = File.dirname(File.expand_path(__FILE__)) |
|---|
| 46 |
|
|---|
| 47 |
def test_marshal |
|---|
| 48 |
marshaller = WSDLMarshaller.new(pathname('person.wsdl')) |
|---|
| 49 |
obj = Person.new("NAKAMURA", "Hiroshi", 1, 1.0, "1") |
|---|
| 50 |
str = marshaller.dump(obj) |
|---|
| 51 |
puts str if $DEBUG |
|---|
| 52 |
obj2 = marshaller.load(str) |
|---|
| 53 |
assert_equal(obj, obj2) |
|---|
| 54 |
assert_equal(str, marshaller.dump(obj2)) |
|---|
| 55 |
end |
|---|
| 56 |
|
|---|
| 57 |
def test_classdef |
|---|
| 58 |
gen = WSDL::SOAP::WSDL2Ruby.new |
|---|
| 59 |
gen.location = pathname("person.wsdl") |
|---|
| 60 |
gen.basedir = DIR |
|---|
| 61 |
gen.logger.level = Logger::FATAL |
|---|
| 62 |
gen.opt['classdef'] = nil |
|---|
| 63 |
gen.opt['force'] = true |
|---|
| 64 |
gen.run |
|---|
| 65 |
compare("person_org.rb", "Person.rb") |
|---|
| 66 |
File.unlink(pathname('Person.rb')) unless $DEBUG |
|---|
| 67 |
end |
|---|
| 68 |
|
|---|
| 69 |
def compare(expected, actual) |
|---|
| 70 |
TestUtil.filecompare(pathname(expected), pathname(actual)) |
|---|
| 71 |
end |
|---|
| 72 |
|
|---|
| 73 |
def pathname(filename) |
|---|
| 74 |
File.join(DIR, filename) |
|---|
| 75 |
end |
|---|
| 76 |
end |
|---|