Skip to content

Commit 7db7f05

Browse files
committed
GROOVY-11667: GPathResult: overload getAttribute / setAttribute
1 parent 9299d69 commit 7db7f05

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

subprojects/groovy-xml/src/main/java/groovy/xml/slurpersupport/GPathResult.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,25 @@ public GPathResult(final GPathResult parent, final String name, final String nam
8888
*/
8989
@Override
9090
public void setMetaClass(final MetaClass metaClass) {
91-
final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {
91+
super.setMetaClass(new DelegatingMetaClass(metaClass) {
9292
@Override
9393
public Object getAttribute(final Object object, final String attribute) {
9494
return GPathResult.this.getProperty("@" + attribute);
9595
}
96+
@Override
97+
public Object getAttribute(final Class sender, final Object object, final String attribute, final boolean isSuper) {
98+
return this.getAttribute(object, attribute);
99+
}
96100

97101
@Override
98102
public void setAttribute(final Object object, final String attribute, final Object newValue) {
99103
GPathResult.this.setProperty("@" + attribute, newValue);
100104
}
101-
};
102-
super.setMetaClass(newMetaClass);
105+
@Override
106+
public void setAttribute(final Class sender, final Object object, final String attribute, final Object newValue, final boolean isSuper, final boolean isInner) {
107+
this.setAttribute(object, attribute, newValue);
108+
}
109+
});
103110
}
104111

105112
/**

subprojects/groovy-xml/src/test/groovy/groovy/xml/XmlSlurperTest.groovy

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
*/
1919
package groovy.xml
2020

21-
import groovy.test.GroovyTestCase
21+
import groovy.xml.slurpersupport.GPathResult
22+
import org.junit.jupiter.api.Test
2223

2324
import static groovy.xml.XmlUtil.serialize
25+
import static org.junit.jupiter.api.Assertions.assertEquals
2426

25-
class XmlSlurperTest extends GroovyTestCase {
27+
final class XmlSlurperTest {
2628

27-
def getRoot = { xml -> new XmlSlurper().parseText(xml) }
29+
final Closure<GPathResult> getRoot = { String xml -> new XmlSlurper().parseText(xml) }
2830

31+
@Test
2932
void testWsdl() {
3033
def wsdl = '''
3134
<definitions name="AgencyManagementService"
@@ -54,6 +57,7 @@ class XmlSlurperTest extends GroovyTestCase {
5457
xml.message.findAll { true }.each { assert it.name() == "message"}
5558
}
5659

60+
@Test
5761
void testElement() {
5862
// can't update value directly with XmlSlurper, use replaceNode instead
5963
// GpathSyntaxTestSupport.checkUpdateElementValue(getRoot)
@@ -65,39 +69,68 @@ class XmlSlurperTest extends GroovyTestCase {
6569
GpathSyntaxTestSupport.checkCDataText(getRoot)
6670
}
6771

72+
@Test
6873
void testAttribute() {
6974
GpathSyntaxTestSupport.checkAttribute(getRoot)
7075
GpathSyntaxTestSupport.checkAttributes(getRoot)
7176
GpathSyntaxTestSupport.checkAttributeTruth(getRoot)
7277
}
7378

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
74102
void testNavigation() {
75103
GpathSyntaxTestSupport.checkChildren(getRoot)
76104
GpathSyntaxTestSupport.checkParent(getRoot)
77105
GpathSyntaxTestSupport.checkNestedSizeExpressions(getRoot)
78106
}
79107

108+
@Test
80109
void testTraversal() {
81110
TraversalTestSupport.checkDepthFirst(getRoot)
82111
TraversalTestSupport.checkBreadthFirst(getRoot)
83112
}
84113

114+
@Test
85115
void testIndices() {
86116
GpathSyntaxTestSupport.checkNegativeIndices(getRoot)
87117
GpathSyntaxTestSupport.checkRangeIndex(getRoot)
88118
}
89119

120+
@Test
90121
void testReplacementsAndAdditions() {
91122
GpathSyntaxTestSupport.checkReplaceNode(getRoot)
92123
GpathSyntaxTestSupport.checkReplaceMultipleNodes(getRoot)
93124
GpathSyntaxTestSupport.checkPlus(getRoot)
94125
}
95126

127+
@Test
96128
void testMixedMarkup() {
97129
MixedMarkupTestSupport.checkMixedMarkup(getRoot)
98130
MixedMarkupTestSupport.checkMixedMarkupText(getRoot)
99131
}
100132

133+
@Test
101134
void testReplace() {
102135
def input = "<doc><sec>Hello<p>World</p></sec></doc>"
103136
def replaceSlurper = new XmlSlurper().parseText(input)
@@ -109,6 +142,7 @@ class XmlSlurperTest extends GroovyTestCase {
109142
assert output == "<doc><t>Hello<p>World</p></t></doc>"
110143
}
111144

145+
@Test
112146
void testNamespacedName() {
113147
def wsdl = '''
114148
<definitions name="AgencyManagementService"
@@ -130,6 +164,7 @@ class XmlSlurperTest extends GroovyTestCase {
130164
}
131165

132166
// GROOVY-4637
167+
@Test
133168
void testNamespacedAttributes() {
134169
def xml = """
135170
<RootElement xmlns="http://www.ivan.com/ns1" xmlns:two="http://www.ivan.com/ns2">
@@ -144,6 +179,7 @@ class XmlSlurperTest extends GroovyTestCase {
144179
}
145180

146181
// GROOVY-6255
182+
@Test
147183
void testXmlNamespacedAttributes() {
148184
def xml = '''
149185
<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 {
157193
}
158194

159195
// GROOVY-6356
196+
@Test
160197
void testSetAndRemoveAttributesWithNamespace() {
161198
def xmlSource = '''<bob:root
162199
xmlns:bob="stuff"
@@ -179,6 +216,7 @@ class XmlSlurperTest extends GroovyTestCase {
179216
}
180217

181218
// GROOVY-6356
219+
@Test
182220
void testSetAndRemoveAttributesNamespaceUnaware() {
183221
def xmlSource = '''<bob:root
184222
xmlns:bob="stuff"
@@ -198,11 +236,13 @@ class XmlSlurperTest extends GroovyTestCase {
198236
}
199237

200238
// GROOVY-5931
239+
@Test
201240
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+
'''
206246

207247
def root = new XmlSlurper().parseText(xml)
208248

@@ -215,21 +255,22 @@ class XmlSlurperTest extends GroovyTestCase {
215255
}
216256

217257
// GROOVY-7781
258+
@Test
218259
void testNamespacedAttributesAccessedWithDifferentPrefix() {
219260
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>
223264
'''
224265

225266
def root = new XmlSlurper(false, true).parseText(xml).declareNamespace(t: 'blah')
226267
assert root.'t:child'.text() == 'c'
227268
assert root.'t:child'.@'t:id' == '1'
228269
}
229270

271+
@Test
230272
void testParsePath() {
231-
232-
def file = File.createTempFile('test','xml')
273+
File file = File.createTempFile('test','xml')
233274
file.deleteOnExit()
234275
file.text = '''
235276
<definitions name="AgencyManagementService"
@@ -257,6 +298,5 @@ class XmlSlurperTest extends GroovyTestCase {
257298
assert xml.message.part.lookupNamespace("") == "http://schemas.xmlsoap.org/wsdl/"
258299
assert xml.message.part.lookupNamespace("undefinedPrefix") == null
259300
xml.message.findAll { true }.each { assert it.name() == "message"}
260-
261301
}
262302
}

0 commit comments

Comments
 (0)