smile-xml 1.0.0-jruby → 1.0.2-jruby

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/lib/smile-xml.jar +0 -0
  2. data/src/main/java/smile/xml/AttrJ.java +160 -0
  3. data/src/main/java/smile/xml/AttributesJ.java +212 -0
  4. data/src/main/java/smile/xml/BaseJ.java +60 -0
  5. data/src/main/java/smile/xml/DocumentJ.java +435 -0
  6. data/src/main/java/smile/xml/EncodingJ.java +94 -0
  7. data/src/main/java/smile/xml/ErrorJ.java +162 -0
  8. data/src/main/java/smile/xml/NamespaceJ.java +67 -0
  9. data/src/main/java/smile/xml/NamespacesJ.java +62 -0
  10. data/src/main/java/smile/xml/NodeJ.java +1029 -0
  11. data/src/main/java/smile/xml/NodeSetJ.java +90 -0
  12. data/src/main/java/smile/xml/ParserContextJ.java +44 -0
  13. data/src/main/java/smile/xml/ParserJ.java +196 -0
  14. data/src/main/java/smile/xml/ParserOptionsJ.java +58 -0
  15. data/src/main/java/smile/xml/ReaderJ.java +34 -0
  16. data/src/main/java/smile/xml/SchemaJ.java +66 -0
  17. data/src/main/java/smile/xml/SmileXML.java +65 -0
  18. data/src/main/java/smile/xml/XmlJ.java +58 -0
  19. data/src/main/java/smile/xml/sax/CallbackHandler.java +113 -0
  20. data/src/main/java/smile/xml/sax/SaxParserCallbacksJ.java +71 -0
  21. data/src/main/java/smile/xml/sax/SaxParserJ.java +153 -0
  22. data/src/main/java/smile/xml/util/UtilJ.java +424 -0
  23. data/src/main/java/smile/xml/xpath/CustomNamespaceContext.java +59 -0
  24. data/src/main/java/smile/xml/xpath/XPathContextJ.java +154 -0
  25. data/src/main/java/smile/xml/xpath/XPathExpressionJ.java +62 -0
  26. data/src/main/java/smile/xml/xpath/XPathJ.java +36 -0
  27. data/src/main/java/smile/xml/xpath/XPathObjectJ.java +196 -0
  28. data/src/main/java/smile/xml/xpath/XPointerJ.java +32 -0
  29. data/src/main/ruby/xml/libxml.rb +1 -0
  30. data/src/main/ruby/xml.rb +5 -0
  31. data/src/test/ruby/etc_doc_to_s.rb +21 -0
  32. data/src/test/ruby/ets_doc_file.rb +17 -0
  33. data/src/test/ruby/ets_doc_to_s.rb +23 -0
  34. data/src/test/ruby/ets_gpx.rb +28 -0
  35. data/src/test/ruby/ets_node_gc.rb +23 -0
  36. data/src/test/ruby/ets_test.xml +2 -0
  37. data/src/test/ruby/ets_tsr.rb +11 -0
  38. data/src/test/ruby/model/atom.xml +13 -0
  39. data/src/test/ruby/model/bands.iso-8859-1.xml +5 -0
  40. data/src/test/ruby/model/bands.utf-8.xml +5 -0
  41. data/src/test/ruby/model/bands.xml +5 -0
  42. data/src/test/ruby/model/books.xml +146 -0
  43. data/src/test/ruby/model/merge_bug_data.xml +58 -0
  44. data/src/test/ruby/model/ruby-lang.html +238 -0
  45. data/src/test/ruby/model/rubynet.xml +79 -0
  46. data/src/test/ruby/model/shiporder.rnc +28 -0
  47. data/src/test/ruby/model/shiporder.rng +86 -0
  48. data/src/test/ruby/model/shiporder.xml +23 -0
  49. data/src/test/ruby/model/shiporder.xsd +31 -0
  50. data/src/test/ruby/model/soap.xml +27 -0
  51. data/src/test/ruby/model/xinclude.xml +5 -0
  52. data/src/test/ruby/smile_xml_test.rb +64 -0
  53. data/src/test/ruby/tc_attr.rb +191 -0
  54. data/src/test/ruby/tc_attr_decl.rb +133 -0
  55. data/src/test/ruby/tc_attributes.rb +135 -0
  56. data/src/test/ruby/tc_deprecated_require.rb +13 -0
  57. data/src/test/ruby/tc_document.rb +162 -0
  58. data/src/test/ruby/tc_document_write.rb +212 -0
  59. data/src/test/ruby/tc_dtd.rb +125 -0
  60. data/src/test/ruby/tc_error.rb +150 -0
  61. data/src/test/ruby/tc_html_parser.rb +140 -0
  62. data/src/test/ruby/tc_namespace.rb +62 -0
  63. data/src/test/ruby/tc_namespaces.rb +210 -0
  64. data/src/test/ruby/tc_node.rb +273 -0
  65. data/src/test/ruby/tc_node_cdata.rb +51 -0
  66. data/src/test/ruby/tc_node_comment.rb +33 -0
  67. data/src/test/ruby/tc_node_copy.rb +42 -0
  68. data/src/test/ruby/tc_node_edit.rb +178 -0
  69. data/src/test/ruby/tc_node_text.rb +73 -0
  70. data/src/test/ruby/tc_node_write.rb +108 -0
  71. data/src/test/ruby/tc_node_xlink.rb +29 -0
  72. data/src/test/ruby/tc_parser.rb +371 -0
  73. data/src/test/ruby/tc_parser_context.rb +189 -0
  74. data/src/test/ruby/tc_properties.rb +40 -0
  75. data/src/test/ruby/tc_reader.rb +306 -0
  76. data/src/test/ruby/tc_relaxng.rb +54 -0
  77. data/src/test/ruby/tc_sax_parser.rb +340 -0
  78. data/src/test/ruby/tc_schema.rb +59 -0
  79. data/src/test/ruby/tc_traversal.rb +222 -0
  80. data/src/test/ruby/tc_xinclude.rb +21 -0
  81. data/src/test/ruby/tc_xml.rb +226 -0
  82. data/src/test/ruby/tc_xpath.rb +210 -0
  83. data/src/test/ruby/tc_xpath_context.rb +80 -0
  84. data/src/test/ruby/tc_xpath_expression.rb +38 -0
  85. data/src/test/ruby/tc_xpointer.rb +74 -0
  86. data/src/test/ruby/test_helper.rb +23 -0
  87. data/src/test/ruby/test_suite.rb +41 -0
  88. metadata +91 -5
@@ -0,0 +1,191 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class AttrNodeTest < Test::Unit::TestCase
7
+ def setup
8
+ xp = XML::Parser.string(<<-EOS)
9
+ <CityModel
10
+ xmlns="http://www.opengis.net/examples"
11
+ xmlns:city="http://www.opengis.net/examples"
12
+ xmlns:gml="http://www.opengis.net/gml"
13
+ xmlns:xlink="http://www.w3.org/1999/xlink"
14
+ xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
15
+ xsi:schemaLocation="http://www.opengis.net/examples city.xsd">
16
+ <type>City</type>
17
+ <cityMember name="Cambridge"
18
+ xlink:type="simple"
19
+ xlink:title="Trinity Lane"
20
+ xlink:href="http://www.foo.net/cgi-bin/wfs?FeatureID=C10239"
21
+ gml:remoteSchema="city.xsd#xpointer(//complexType[@name='RoadType'])"/>
22
+ </CityModel>
23
+ EOS
24
+
25
+ @doc = xp.parse
26
+ end
27
+
28
+ def teardown
29
+ @doc = nil
30
+ end
31
+
32
+ def city_member
33
+ @doc.find('/city:CityModel/city:cityMember').first
34
+ end
35
+
36
+ def test_doc
37
+ assert_not_nil(@doc)
38
+ assert_equal(XML::Encoding::NONE, @doc.encoding)
39
+ end
40
+
41
+ def test_types
42
+ attribute = city_member.attributes.get_attribute('name')
43
+ assert_instance_of(XML::Attr, attribute)
44
+ assert_equal('attribute', attribute.node_type_name)
45
+ end
46
+
47
+ def test_name
48
+ attribute = city_member.attributes.get_attribute('name')
49
+ assert_equal('name', attribute.name)
50
+ assert_equal(Encoding::ASCII_8BIT, attribute.name.encoding) if defined?(Encoding)
51
+
52
+ attribute = city_member.attributes.get_attribute('href')
53
+ assert_equal('href', attribute.name)
54
+ assert_equal('xlink', attribute.ns.prefix)
55
+ assert_equal('http://www.w3.org/1999/xlink', attribute.ns.href)
56
+
57
+ attribute = city_member.attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
58
+ assert_equal('href', attribute.name)
59
+ assert_equal('xlink', attribute.ns.prefix)
60
+ assert_equal('http://www.w3.org/1999/xlink', attribute.ns.href)
61
+ end
62
+
63
+ def test_value
64
+ attribute = city_member.attributes.get_attribute('name')
65
+ assert_equal('Cambridge', attribute.value)
66
+ assert_equal(Encoding::ASCII_8BIT, attribute.value.encoding) if defined?(Encoding)
67
+
68
+ attribute = city_member.attributes.get_attribute('href')
69
+ assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', attribute.value)
70
+ end
71
+
72
+ def test_set_value
73
+ attribute = city_member.attributes.get_attribute('name')
74
+ attribute.value = 'London'
75
+ assert_equal('London', attribute.value)
76
+ assert_equal(Encoding::ASCII_8BIT, attribute.value.encoding) if defined?(Encoding)
77
+
78
+ attribute = city_member.attributes.get_attribute('href')
79
+ attribute.value = 'http://i.have.changed'
80
+ assert_equal('http://i.have.changed', attribute.value)
81
+ assert_equal(Encoding::ASCII_8BIT, attribute.value.encoding) if defined?(Encoding)
82
+ end
83
+
84
+ def test_set_nil
85
+ attribute = city_member.attributes.get_attribute('name')
86
+ assert_raise(TypeError) do
87
+ attribute.value = nil
88
+ end
89
+ end
90
+
91
+ def test_create
92
+ attributes = city_member.attributes
93
+ assert_equal(5, attributes.length)
94
+
95
+ attr = XML::Attr.new(city_member, 'size', '50,000')
96
+ assert_instance_of(XML::Attr, attr)
97
+
98
+ attributes = city_member.attributes
99
+ assert_equal(6, attributes.length)
100
+
101
+ assert_equal(attributes['size'], '50,000')
102
+ end
103
+
104
+ def test_create_on_node
105
+ attributes = city_member.attributes
106
+ assert_equal(5, attributes.length)
107
+
108
+ attributes['country'] = 'England'
109
+
110
+ attributes = city_member.attributes
111
+ assert_equal(6, attributes.length)
112
+
113
+ assert_equal(attributes['country'], 'England')
114
+ end
115
+
116
+ def test_create_ns
117
+ assert_equal(5, city_member.attributes.length)
118
+
119
+ ns = XML::Namespace.new(city_member, 'my_namepace', 'http://www.mynamespace.com')
120
+ attr = XML::Attr.new(city_member, 'rating', 'rocks', ns)
121
+ assert_instance_of(XML::Attr, attr)
122
+ assert_equal('rating', attr.name)
123
+ assert_equal('rocks', attr.value)
124
+
125
+ attributes = city_member.attributes
126
+ assert_equal(6, attributes.length)
127
+
128
+ assert_equal('rocks', city_member['rating'])
129
+ end
130
+
131
+ def test_remove
132
+ attributes = city_member.attributes
133
+ assert_equal(5, attributes.length)
134
+
135
+ attribute = attributes.get_attribute('name')
136
+ assert_not_nil(attribute.parent)
137
+ assert(attribute.parent?)
138
+
139
+ attribute.remove!
140
+ assert_equal(4, attributes.length)
141
+ assert_nil(attribute.parent)
142
+ assert(!attribute.parent?)
143
+ end
144
+
145
+ def test_first
146
+ attribute = city_member.attributes.first
147
+ assert_instance_of(XML::Attr, attribute)
148
+ puts "TODO #{__FILE__} #{__LINE__}"
149
+ #assert_equal('name', attribute.name)
150
+ puts "TODO #{__FILE__} #{__LINE__}"
151
+ #assert_equal('Cambridge', attribute.value)
152
+
153
+ attribute = attribute.next
154
+ assert_instance_of(XML::Attr, attribute)
155
+ puts "TODO #{__FILE__} #{__LINE__}"
156
+ #assert_equal('type', attribute.name)
157
+ puts "TODO #{__FILE__} #{__LINE__}"
158
+ #assert_equal('simple', attribute.value)
159
+
160
+ attribute = attribute.next
161
+ assert_instance_of(XML::Attr, attribute)
162
+ puts "TODO #{__FILE__} #{__LINE__}"
163
+ #assert_equal('title', attribute.name)
164
+ puts "TODO #{__FILE__} #{__LINE__}"
165
+ #assert_equal('Trinity Lane', attribute.value)
166
+
167
+ attribute = attribute.next
168
+ assert_instance_of(XML::Attr, attribute)
169
+ puts "TODO #{__FILE__} #{__LINE__}"
170
+ #assert_equal('href', attribute.name)
171
+ puts "TODO #{__FILE__} #{__LINE__}"
172
+ #assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', attribute.value)
173
+
174
+ attribute = attribute.next
175
+ assert_instance_of(XML::Attr, attribute)
176
+ puts "TODO #{__FILE__} #{__LINE__}"
177
+ #assert_equal('remoteSchema', attribute.name)
178
+ puts "TODO #{__FILE__} #{__LINE__}"
179
+ #assert_equal("city.xsd#xpointer(//complexType[@name='RoadType'])", attribute.value)
180
+
181
+ attribute = attribute.next
182
+ assert_nil(attribute)
183
+ end
184
+
185
+ def test_no_attributes
186
+ element = @doc.find('/city:CityModel/city:type').first
187
+
188
+ assert_not_nil(element.attributes)
189
+ assert_equal(0, element.attributes.length)
190
+ end
191
+ end
@@ -0,0 +1,133 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class AttrDeclTest < Test::Unit::TestCase
7
+ def setup
8
+ xp = XML::Parser.string(<<-EOS)
9
+ <!DOCTYPE test [
10
+ <!ELEMENT root (property*)>
11
+ <!ELEMENT property EMPTY>
12
+ <!ATTLIST property name NMTOKEN #REQUIRED>
13
+ <!ATTLIST property access (r | w | rw) "rw">
14
+ ]>
15
+ <root>
16
+ <property name="readonly" access="r" />
17
+ <property name="readwrite" />
18
+ </root>
19
+ EOS
20
+ @doc = xp.parse
21
+ end
22
+
23
+ def teardown
24
+ @doc = nil
25
+ end
26
+
27
+ def test_attributes
28
+ # Get a element with an access attribute
29
+ elem = @doc.find_first('/root/property[@name="readonly"]')
30
+ assert_equal(2, elem.attributes.length)
31
+ assert_not_nil(elem['access'])
32
+
33
+ # Get a element node without a access attribute
34
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
35
+ assert_equal(1, elem.attributes.length)
36
+ assert_nil(elem['access'])
37
+ end
38
+
39
+ def test_attr
40
+ # Get a property node without a access attribute
41
+ elem = @doc.find_first('/root/property[@name="readonly"]')
42
+
43
+ # Get the attr_decl
44
+ attr = elem.attributes.get_attribute('access')
45
+ assert_not_nil(attr)
46
+ assert_equal(XML::Node::ATTRIBUTE_NODE, attr.node_type)
47
+ assert_equal('attribute', attr.node_type_name)
48
+
49
+ # Get its value
50
+ assert_equal('r', attr.value)
51
+ end
52
+
53
+ def test_attr_decl
54
+ # Get a property node without a access attribute
55
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
56
+
57
+ # Get the attr_decl
58
+ attr_decl = elem.attributes.get_attribute('access')
59
+ assert_not_nil(attr_decl)
60
+ assert_equal(XML::Node::ATTRIBUTE_DECL, attr_decl.node_type)
61
+ assert_equal('attribute declaration', attr_decl.node_type_name)
62
+
63
+ # Get its value
64
+ assert_equal('rw', attr_decl.value)
65
+ end
66
+
67
+ def test_type
68
+ # Get a property node without a access attribute
69
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
70
+ attr_decl = elem.attributes.get_attribute('access')
71
+
72
+ assert_not_nil(attr_decl)
73
+ assert_equal(XML::Node::ATTRIBUTE_DECL, attr_decl.node_type)
74
+ assert_equal('attribute declaration', attr_decl.node_type_name)
75
+ end
76
+
77
+ def test_name
78
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
79
+ attr_decl = elem.attributes.get_attribute('access')
80
+
81
+ assert_equal('access', attr_decl.name)
82
+ end
83
+
84
+ def test_value
85
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
86
+ attr_decl = elem.attributes.get_attribute('access')
87
+
88
+ assert_equal('rw', attr_decl.value)
89
+ end
90
+
91
+ def test_to_s
92
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
93
+ attr_decl = elem.attributes.get_attribute('access')
94
+
95
+ assert_equal('access = rw', attr_decl.to_s)
96
+ end
97
+
98
+ def test_prev
99
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
100
+ attr_decl = elem.attributes.get_attribute('access')
101
+
102
+ first_decl = attr_decl.prev
103
+ assert_equal(XML::Node::ATTRIBUTE_DECL, first_decl.node_type)
104
+ assert_equal('name', first_decl.name)
105
+ assert_nil(first_decl.value)
106
+
107
+ elem_decl = first_decl.prev
108
+ assert_equal(XML::Node::ELEMENT_DECL, elem_decl.node_type)
109
+ end
110
+
111
+ def test_next
112
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
113
+ attr_decl = elem.attributes.get_attribute('access')
114
+
115
+ next_decl = attr_decl.next
116
+ assert_nil(next_decl)
117
+ end
118
+
119
+ def test_doc
120
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
121
+ attr_decl = elem.attributes.get_attribute('access')
122
+
123
+ assert_same(@doc, attr_decl.doc)
124
+ end
125
+
126
+ def test_parent
127
+ elem = @doc.find_first('/root/property[@name="readwrite"]')
128
+ attr_decl = elem.attributes.get_attribute('access')
129
+
130
+ parent = attr_decl.parent
131
+ assert_instance_of(XML::Dtd, parent)
132
+ end
133
+ end
@@ -0,0 +1,135 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class AttributesTest < Test::Unit::TestCase
7
+ def setup
8
+ xp = XML::Parser.string(<<-EOS)
9
+ <CityModel
10
+ xmlns="http://www.opengis.net/examples"
11
+ xmlns:city="http://www.opengis.net/examples"
12
+ xmlns:gml="http://www.opengis.net/gml"
13
+ xmlns:xlink="http://www.w3.org/1999/xlink"
14
+ xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
15
+ xsi:schemaLocation="http://www.opengis.net/examples city.xsd">
16
+ <cityMember name="Cambridge"
17
+ xlink:type="simple"
18
+ xlink:title="Trinity Lane"
19
+ xlink:href="http://www.foo.net/cgi-bin/wfs?FeatureID=C10239"
20
+ gml:remoteSchema="city.xsd#xpointer(//complexType[@name='RoadType'])"/>
21
+ </CityModel>
22
+ EOS
23
+
24
+ @doc = xp.parse
25
+ end
26
+
27
+ def teardown
28
+ @doc = nil
29
+ end
30
+
31
+ def city_member
32
+ @doc.find('/city:CityModel/city:cityMember').first
33
+ end
34
+
35
+ def test_attributes
36
+ attributes = city_member.attributes
37
+ assert_instance_of(XML::Attributes, attributes)
38
+ assert_equal(5, attributes.length)
39
+ end
40
+
41
+ def test_each
42
+ attributes = city_member.attributes
43
+ length = attributes.inject(0) do |result, attr|
44
+ assert_instance_of(XML::Attr, attr)
45
+ result + 1
46
+ end
47
+ assert_equal(5, length)
48
+ end
49
+
50
+ def test_get_attribute
51
+ attributes = city_member.attributes
52
+
53
+ attr = attributes.get_attribute('name')
54
+ assert_instance_of(XML::Attr, attr)
55
+
56
+ attr = attributes.get_attribute('does_not_exist')
57
+ assert_nil(attr)
58
+
59
+ attr = attributes.get_attribute('name')
60
+ assert_instance_of(XML::Attr, attr)
61
+
62
+ attr = attributes.get_attribute('href')
63
+ assert_instance_of(XML::Attr, attr)
64
+ assert_instance_of(XML::Namespace, attr.ns)
65
+ assert_equal('xlink', attr.ns.prefix)
66
+ assert_equal('http://www.w3.org/1999/xlink', attr.ns.href)
67
+
68
+ attr = attributes.get_attribute_ns('http://www.w3.org/1999/xlink', 'href')
69
+ assert_instance_of(XML::Attr, attr)
70
+
71
+ attr = attributes.get_attribute_ns('http://www.opengis.net/gml', 'remoteSchema')
72
+ assert_instance_of(XML::Attr, attr)
73
+
74
+ attr = attributes.get_attribute_ns('http://i.dont.exist', 'nor do i')
75
+ assert_nil(attr)
76
+ end
77
+
78
+ def test_property
79
+ attr = city_member.property('name')
80
+ assert_instance_of(String, attr)
81
+ assert_equal('Cambridge', attr)
82
+ end
83
+
84
+ def test_get_values
85
+ assert_equal('Cambridge', city_member[:name])
86
+ assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', city_member[:href])
87
+
88
+ attributes = city_member.attributes
89
+ assert_equal('Cambridge', attributes[:name])
90
+ assert_equal('http://www.foo.net/cgi-bin/wfs?FeatureID=C10239', attributes[:href])
91
+ end
92
+
93
+ def test_set_values
94
+ city_member[:name] = 'London'
95
+ assert_equal('London', city_member[:name])
96
+
97
+ city_member[:href] = 'foo'
98
+ assert_equal('foo', city_member[:href])
99
+
100
+ attributes = city_member.attributes
101
+
102
+ attributes[:name] = 'London'
103
+ assert_equal('London', attributes[:name])
104
+
105
+ attributes[:href] = 'foo'
106
+ assert_equal('foo', attributes[:href])
107
+ end
108
+
109
+ def test_str_sym()
110
+ attributes = city_member.attributes
111
+ assert_equal('Cambridge', attributes[:name])
112
+ assert_equal('Cambridge', attributes['name'])
113
+ end
114
+
115
+ def test_remove_first
116
+ attributes = @doc.find_first('/city:CityModel/city:cityMember').attributes
117
+ assert_equal(5, attributes.length)
118
+ attr = attributes.first
119
+ attr.remove!
120
+ assert_equal(4, attributes.length)
121
+ end
122
+
123
+ def test_remove_all
124
+ node = @doc.find_first('/city:CityModel/city:cityMember')
125
+ assert_equal(5, node.attributes.length)
126
+
127
+ attrs = Array.new
128
+ node.attributes.each do |attr|
129
+ attrs << attr
130
+ attr.remove!
131
+ end
132
+ assert_equal(5, attrs.length)
133
+ assert_equal(0, node.attributes.length)
134
+ end
135
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'xml/libxml'
4
+ require 'test/unit'
5
+
6
+ class TestDeprecatedRequire < Test::Unit::TestCase
7
+ def test_basic
8
+ xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
9
+ assert_instance_of(XML::Parser, xp)
10
+ @doc = xp.parse
11
+ assert_instance_of(XML::Document, @doc)
12
+ end
13
+ end
@@ -0,0 +1,162 @@
1
+ # encoding: UTF-8
2
+ require './test_helper'
3
+ require 'test/unit'
4
+
5
+ class TestDocument < Test::Unit::TestCase
6
+ def setup
7
+ xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
8
+ assert_instance_of(XML::Parser, xp)
9
+ @doc = xp.parse
10
+ assert_instance_of(XML::Document, @doc)
11
+ end
12
+
13
+ def teardown
14
+ @doc = nil
15
+ end
16
+
17
+ def test_klass
18
+ assert_instance_of(XML::Document, @doc)
19
+ end
20
+
21
+ def test_context
22
+ context = @doc.context
23
+ assert_instance_of(XML::XPath::Context, context)
24
+ end
25
+
26
+ def test_find
27
+ set = @doc.find('/ruby_array/fixnum')
28
+ assert_instance_of(XML::XPath::Object, set)
29
+ assert_raise(NoMethodError) {
30
+ xpt = set.xpath
31
+ }
32
+ end
33
+
34
+ def test_canonicalize
35
+ doc = XML::Parser.string(<<-EOS).parse
36
+ <?xml-stylesheet href="doc.xsl" type="text/xsl"?>
37
+ <doc>Hello, world!<!-- Comment 1 --></doc>
38
+ EOS
39
+
40
+ expected = "<?xml-stylesheet href=\"doc.xsl\" type=\"text/xsl\"?>\n<doc>Hello, world!</doc>"
41
+ assert_equal(expected, doc.canonicalize )
42
+
43
+ expected = "<?xml-stylesheet href=\"doc.xsl\" type=\"text/xsl\"?>\n<doc>Hello, world!<!-- Comment 1 --></doc>"
44
+ assert_equal(expected, doc.canonicalize(true))
45
+
46
+ expected = "<?xml-stylesheet href=\"doc.xsl\" type=\"text/xsl\"?>\n<doc>Hello, world!</doc>"
47
+ assert_equal(expected, doc.canonicalize(false))
48
+ end
49
+
50
+ def test_compression
51
+ if XML.enabled_zlib?
52
+ 0.upto(9) do |i|
53
+ assert_equal(i, @doc.compression = i)
54
+ assert_equal(i, @doc.compression)
55
+ end
56
+
57
+ 9.downto(0) do |i|
58
+ assert_equal(i, @doc.compression = i)
59
+ assert_equal(i, @doc.compression)
60
+ end
61
+
62
+ 10.upto(20) do |i|
63
+ # assert_equal(9, @doc.compression = i)
64
+ assert_equal(i, @doc.compression = i) # This works around a bug in Ruby 1.8
65
+ assert_equal(9, @doc.compression)
66
+ end
67
+
68
+ -1.downto(-10) do |i|
69
+ # assert_equal(0, @doc.compression = i)
70
+ assert_equal(i, @doc.compression = i) # FIXME This bug should get fixed ASAP
71
+ assert_equal(0, @doc.compression)
72
+ end
73
+ end
74
+ end
75
+
76
+ def test_version
77
+ assert_equal('1.0', @doc.version)
78
+
79
+ doc = XML::Document.new('6.9')
80
+ assert_equal('6.9', doc.version)
81
+ end
82
+
83
+ def test_write_root
84
+ @doc.root = XML::Node.new('rubynet')
85
+ assert_instance_of(XML::Node, @doc.root)
86
+ assert_instance_of(XML::Document, @doc.root.doc)
87
+
88
+ puts "TODO #{__FILE__} #{__LINE__}"
89
+ assert_equal("<rubynet/>",
90
+ remove_header_declaration( @doc.to_s(:indent => false) ) )
91
+
92
+ # assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n",
93
+ # @doc.to_s(:indent => false) )
94
+ end
95
+
96
+ def test_encoding
97
+ doc = XML::Document.new
98
+ assert_equal(XML::Encoding::NONE, doc.encoding)
99
+ assert_equal(Encoding::ASCII_8BIT, doc.rb_encoding) if defined?(Encoding)
100
+
101
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
102
+ doc = XML::Document.file(file)
103
+ assert_equal(XML::Encoding::UTF_8, doc.encoding)
104
+ assert_equal(Encoding::UTF_8, doc.rb_encoding) if defined?(Encoding)
105
+
106
+ doc.encoding = XML::Encoding::ISO_8859_1
107
+ assert_equal(XML::Encoding::ISO_8859_1, doc.encoding)
108
+ assert_equal(Encoding::ISO8859_1, doc.rb_encoding) if defined?(Encoding)
109
+ end
110
+
111
+ def test_doc_node_type
112
+ assert_equal(XML::Node::DOCUMENT_NODE, XML::Document.new.node_type)
113
+ end
114
+
115
+ def test_doc_node_type_name
116
+ assert_equal('document_xml', XML::Document.new.node_type_name)
117
+ end
118
+
119
+ def test_xhtml
120
+ doc = XML::Document.new
121
+ assert(!doc.xhtml?)
122
+ puts "TODO #{__FILE__} #{__LINE__}"
123
+ # XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, doc, true
124
+ # assert(doc.xhtml?)
125
+ end
126
+
127
+ def test_document_root
128
+ doc1 = LibXML::XML::Document.string("<one/>")
129
+ doc2 = LibXML::XML::Document.string("<two/>")
130
+
131
+ error = assert_raise(XML::Error) do
132
+ doc1.root = doc2.root
133
+ end
134
+ assert_equal(" Nodes belong to different documents. You must first import the node by calling XML::Document.import.",
135
+ error.to_s)
136
+
137
+ doc2.root << doc2.import(doc1.root)
138
+ assert_equal('<one/>', doc1.root.to_s)
139
+ assert_equal('<two><one/></two>', doc2.root.to_s(:indent => false))
140
+
141
+ assert(!doc1.root.equal?(doc2.root))
142
+ assert(doc1.root.doc != doc2.root.doc)
143
+ end
144
+
145
+ def test_import_node
146
+ doc1 = XML::Parser.string('<nums><one></one></nums>').parse
147
+ doc2 = XML::Parser.string('<nums><two></two></nums>').parse
148
+
149
+ node = doc1.root.child
150
+
151
+ error = assert_raise(XML::Error) do
152
+ doc2.root << node
153
+ end
154
+ assert_equal(" Nodes belong to different documents. You must first import the node by calling XML::Document.import.",
155
+ error.to_s)
156
+
157
+ doc2.root << doc2.import(node)
158
+
159
+ assert_equal("<nums><two/><one/></nums>",
160
+ doc2.root.to_s(:indent => false))
161
+ end
162
+ end