18
18
*/
19
19
package groovy.xml
20
20
21
- import groovy.test.GroovyTestCase
21
+ import groovy.xml.slurpersupport.GPathResult
22
+ import org.junit.jupiter.api.Test
22
23
23
24
import static groovy.xml.XmlUtil.serialize
25
+ import static org.junit.jupiter.api.Assertions.assertEquals
24
26
25
- class XmlSlurperTest extends GroovyTestCase {
27
+ final class XmlSlurperTest {
26
28
27
- def getRoot = { xml -> new XmlSlurper (). parseText(xml) }
29
+ final Closure< GPathResult > getRoot = { String xml -> new XmlSlurper (). parseText(xml) }
28
30
31
+ @Test
29
32
void testWsdl () {
30
33
def wsdl = '''
31
34
<definitions name="AgencyManagementService"
@@ -54,6 +57,7 @@ class XmlSlurperTest extends GroovyTestCase {
54
57
xml. message. findAll { true }. each { assert it. name() == " message" }
55
58
}
56
59
60
+ @Test
57
61
void testElement() {
58
62
// can't update value directly with XmlSlurper, use replaceNode instead
59
63
// GpathSyntaxTestSupport.checkUpdateElementValue(getRoot)
@@ -65,39 +69,68 @@ class XmlSlurperTest extends GroovyTestCase {
65
69
GpathSyntaxTestSupport . checkCDataText(getRoot)
66
70
}
67
71
72
+ @Test
68
73
void testAttribute() {
69
74
GpathSyntaxTestSupport . checkAttribute(getRoot)
70
75
GpathSyntaxTestSupport . checkAttributes(getRoot)
71
76
GpathSyntaxTestSupport . checkAttributeTruth(getRoot)
72
77
}
73
78
79
+ // GROOVY-11667
80
+ @Test
81
+ void testAttribute2() {
82
+ def xml = '''
83
+ <person>
84
+ <name>John Doe</name>
85
+ </person>
86
+ '''
87
+ def path = getRoot(xml)
88
+ def text = path. @id. text() ?: null
89
+
90
+ assert text == null
91
+ assert new Inner (path). @id == null
92
+ }
93
+
94
+ static class Inner {
95
+ protected id
96
+ Inner (GPathResult gpath) {
97
+ this . @id = gpath. @id. text() ?: null
98
+ }
99
+ }
100
+
101
+ @Test
74
102
void testNavigation() {
75
103
GpathSyntaxTestSupport . checkChildren(getRoot)
76
104
GpathSyntaxTestSupport . checkParent(getRoot)
77
105
GpathSyntaxTestSupport . checkNestedSizeExpressions(getRoot)
78
106
}
79
107
108
+ @Test
80
109
void testTraversal() {
81
110
TraversalTestSupport . checkDepthFirst(getRoot)
82
111
TraversalTestSupport . checkBreadthFirst(getRoot)
83
112
}
84
113
114
+ @Test
85
115
void testIndices() {
86
116
GpathSyntaxTestSupport . checkNegativeIndices(getRoot)
87
117
GpathSyntaxTestSupport . checkRangeIndex(getRoot)
88
118
}
89
119
120
+ @Test
90
121
void testReplacementsAndAdditions() {
91
122
GpathSyntaxTestSupport . checkReplaceNode(getRoot)
92
123
GpathSyntaxTestSupport . checkReplaceMultipleNodes(getRoot)
93
124
GpathSyntaxTestSupport . checkPlus(getRoot)
94
125
}
95
126
127
+ @Test
96
128
void testMixedMarkup() {
97
129
MixedMarkupTestSupport . checkMixedMarkup(getRoot)
98
130
MixedMarkupTestSupport . checkMixedMarkupText(getRoot)
99
131
}
100
132
133
+ @Test
101
134
void testReplace() {
102
135
def input = " <doc><sec>Hello<p>World</p></sec></doc>"
103
136
def replaceSlurper = new XmlSlurper (). parseText(input)
@@ -109,6 +142,7 @@ class XmlSlurperTest extends GroovyTestCase {
109
142
assert output == " <doc><t>Hello<p>World</p></t></doc>"
110
143
}
111
144
145
+ @Test
112
146
void testNamespacedName() {
113
147
def wsdl = '''
114
148
<definitions name="AgencyManagementService"
@@ -130,6 +164,7 @@ class XmlSlurperTest extends GroovyTestCase {
130
164
}
131
165
132
166
// GROOVY-4637
167
+ @Test
133
168
void testNamespacedAttributes() {
134
169
def xml = """
135
170
<RootElement xmlns="http://www.ivan.com/ns1" xmlns:two="http://www.ivan.com/ns2">
@@ -144,6 +179,7 @@ class XmlSlurperTest extends GroovyTestCase {
144
179
}
145
180
146
181
// GROOVY-6255
182
+ @Test
147
183
void testXmlNamespacedAttributes() {
148
184
def xml = '''
149
185
<appendix version="5.0" xmlns="http://docbook.org/ns/docbook" xmlns:xml="http://www.w3.org/XML/1998/namespace">
@@ -157,6 +193,7 @@ class XmlSlurperTest extends GroovyTestCase {
157
193
}
158
194
159
195
// GROOVY-6356
196
+ @Test
160
197
void testSetAndRemoveAttributesWithNamespace() {
161
198
def xmlSource = ''' <bob:root
162
199
xmlns:bob="stuff"
@@ -179,6 +216,7 @@ class XmlSlurperTest extends GroovyTestCase {
179
216
}
180
217
181
218
// GROOVY-6356
219
+ @Test
182
220
void testSetAndRemoveAttributesNamespaceUnaware() {
183
221
def xmlSource = ''' <bob:root
184
222
xmlns:bob="stuff"
@@ -198,11 +236,13 @@ class XmlSlurperTest extends GroovyTestCase {
198
236
}
199
237
200
238
// GROOVY-5931
239
+ @Test
201
240
void testIterableGPathResult() {
202
- def xml = """
203
- <RootElement>
204
- <ChildElement ItemId="FirstItemId">Child element data</ChildElement>
205
- </RootElement>"""
241
+ def xml = '''
242
+ <RootElement>
243
+ <ChildElement ItemId="FirstItemId">Child element data</ChildElement>
244
+ </RootElement>
245
+ '''
206
246
207
247
def root = new XmlSlurper (). parseText(xml)
208
248
@@ -215,21 +255,22 @@ class XmlSlurperTest extends GroovyTestCase {
215
255
}
216
256
217
257
// GROOVY-7781
258
+ @Test
218
259
void testNamespacedAttributesAccessedWithDifferentPrefix() {
219
260
def xml = '''
220
- <x:root xmlns:x="blah">
221
- <x:child x:id="1">c</x:child>
222
- </x:root>
261
+ <x:root xmlns:x="blah">
262
+ <x:child x:id="1">c</x:child>
263
+ </x:root>
223
264
'''
224
265
225
266
def root = new XmlSlurper (false , true ). parseText(xml). declareNamespace(t : ' blah' )
226
267
assert root. ' t:child' . text() == ' c'
227
268
assert root. ' t:child' . @' t:id' == ' 1'
228
269
}
229
270
271
+ @Test
230
272
void testParsePath() {
231
-
232
- def file = File . createTempFile(' test' ,' xml' )
273
+ File file = File . createTempFile(' test' ,' xml' )
233
274
file. deleteOnExit()
234
275
file. text = '''
235
276
<definitions name="AgencyManagementService"
@@ -257,6 +298,5 @@ class XmlSlurperTest extends GroovyTestCase {
257
298
assert xml. message. part. lookupNamespace(" " ) == " http://schemas.xmlsoap.org/wsdl/"
258
299
assert xml. message. part. lookupNamespace(" undefinedPrefix" ) == null
259
300
xml. message. findAll { true }. each { assert it. name() == " message" }
260
-
261
301
}
262
302
}
0 commit comments