forked from xml4r/libxml-ruby
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtc_namespaces.rb
210 lines (166 loc) · 7.53 KB
/
tc_namespaces.rb
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# encoding: UTF-8
require './test_helper'
require 'test/unit'
class TestNamespaces < Test::Unit::TestCase
def setup
file = File.join(File.dirname(__FILE__), 'model/soap.xml')
@doc = XML::Document.file(file)
end
def teardown
@doc = nil
end
def test_namespace_node
node = @doc.root
ns = node.namespaces.namespace
assert_equal('soap', ns.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', ns.href)
end
def test_namespace_attr
node = @doc.root
attr = node.attributes.get_attribute('encodingStyle')
assert_equal('soap', attr.ns.prefix)
assert_equal('soap', attr.namespaces.namespace.prefix)
end
def test_set_namespace_node
node = XML::Node.new('Envelope')
assert_equal('<Envelope/>', node.to_s)
ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
assert_equal("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
assert_nil(node.namespaces.namespace)
# Now put the node in the soap namespace
node.namespaces.namespace = ns
assert_not_nil(node.namespaces.namespace)
assert_equal("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
end
def test_set_namespace_attribute
# Create node
node = XML::Node.new('Envelope')
assert_equal('<Envelope/>', node.to_s)
# Create attribute
attr = XML::Attr.new(node, "encodingStyle", "http://www.w3.org/2001/12/soap-encoding")
assert_equal('<Envelope encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
node.to_s)
# Create namespace attribute
ns = XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
assert_equal('<Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
node.to_s)
assert_nil(node.namespaces.namespace)
# Now put the node in the soap namespace
node.namespaces.namespace = ns
assert_not_nil(node.namespaces.namespace)
assert_equal('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
node.to_s)
# Now put the attribute in the soap namespace
attr.namespaces.namespace = ns
assert_not_nil(node.namespaces.namespace)
assert_equal('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"/>',
node.to_s)
end
def test_define_namespace
node = XML::Node.new('Envelope')
assert_equal('<Envelope/>', node.to_s)
XML::Namespace.new(node, 'soap', 'http://schemas.xmlsoap.org/soap/envelope/')
assert_equal("<Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
assert_nil(node.namespaces.namespace)
end
def test_define_default_namespace
node = XML::Node.new('Envelope')
assert_equal('<Envelope/>', node.to_s)
XML::Namespace.new(node, nil, 'http://schemas.xmlsoap.org/soap/envelope/')
assert_equal("<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\"/>", node.to_s)
# This seems wrong, but appears to be the way libxml works
assert_nil(node.namespaces.namespace)
end
def test_namespaces
node = @doc.find_first('//ns1:IdAndName',
:ns1 => 'http://domain.somewhere.com')
namespaces = node.namespaces.sort
assert_equal(5, namespaces.length)
namespace = namespaces[0]
assert_instance_of(XML::Namespace, namespace)
assert_equal(nil, namespace.prefix)
assert_equal('http://services.somewhere.com', namespace.href)
namespace = namespaces[1]
assert_instance_of(XML::Namespace, namespace)
assert_equal('ns1', namespace.prefix)
assert_equal('http://domain.somewhere.com', namespace.href)
namespace = namespaces[2]
assert_instance_of(XML::Namespace, namespace)
assert_equal('soap', namespace.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
namespace = namespaces[3]
assert_instance_of(XML::Namespace, namespace)
assert_equal('xsd', namespace.prefix)
assert_equal('http://www.w3.org/2001/XMLSchema', namespace.href)
namespace = namespaces[4]
assert_instance_of(XML::Namespace, namespace)
assert_equal('xsi', namespace.prefix)
assert_equal('http://www.w3.org/2001/XMLSchema-instance', namespace.href)
end
def test_namespaces
node = @doc.find_first('//ns1:IdAndName',
:ns1 => 'http://domain.somewhere.com')
node.namespaces.each do |namespace|
assert_instance_of(XML::Namespace, namespace)
end
end
def test_namespace_definitions
ns_defs = @doc.root.namespaces.definitions
assert_equal(3, ns_defs.size)
namespace = ns_defs[0]
assert_instance_of(XML::Namespace, namespace)
assert_equal('soap', namespace.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
namespace = ns_defs[1]
assert_instance_of(XML::Namespace, namespace)
assert_equal('xsd', namespace.prefix)
assert_equal('http://www.w3.org/2001/XMLSchema', namespace.href)
namespace = ns_defs[2]
assert_instance_of(XML::Namespace, namespace)
assert_equal('xsi', namespace.prefix)
assert_equal('http://www.w3.org/2001/XMLSchema-instance', namespace.href)
node = @doc.root.find_first('//ns:getManufacturerNamesResponse',
:ns => 'http://services.somewhere.com')
ns_defs = node.namespaces.definitions
assert_equal(1, ns_defs.size)
namespace = ns_defs[0]
assert_instance_of(XML::Namespace, namespace)
assert_equal(nil, namespace.prefix)
assert_equal('http://services.somewhere.com', namespace.href)
end
def test_find_by_prefix
namespace = @doc.root.namespaces.find_by_prefix('soap')
assert_instance_of(XML::Namespace, namespace)
assert_equal('soap', namespace.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
end
def test_find_default_ns
namespace = @doc.root.namespaces.find_by_prefix(nil)
assert_nil(namespace)
node = @doc.find_first('//ns1:getManufacturerNamesResponse',
:ns1 => 'http://services.somewhere.com')
namespace = node.namespaces.find_by_prefix(nil)
assert_instance_of(XML::Namespace, namespace)
assert_equal(nil, namespace.prefix)
assert_equal('http://services.somewhere.com', namespace.href)
end
def test_find_ns_by_href
node = @doc.find_first('//ns1:getManufacturerNamesResponse',
:ns1 => 'http://services.somewhere.com')
namespace = node.namespaces.find_by_href('http://schemas.xmlsoap.org/soap/envelope/')
assert_instance_of(XML::Namespace, namespace)
assert_equal('soap', namespace.prefix)
assert_equal('http://schemas.xmlsoap.org/soap/envelope/', namespace.href)
end
def test_default_namespace
doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
ns = doc.root.namespaces.default
assert_equal(ns.href, 'http://schemas.xmlsoap.org/soap/envelope/')
end
def test_default_prefix
doc = XML::Document.string('<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"/>')
doc.root.namespaces.default_prefix = 'soap'
node = doc.root.find_first('/soap:Envelope')
assert_not_nil(node)
end
end