Skip to content

Commit

Permalink
Fix relative XPath matching
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Jun 28, 2020
1 parent 9dcdbe7 commit 1b1a6c2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Pom(MavenModel model, Xml.Document document) {
.map(dm -> new DependencyManagement(model.getDependencyManagement(), dm))
.orElse(null);

this.memoizedDependencies = new MemoizedTags<>(document.getRoot(), "project/dependencies/dependency",
this.memoizedDependencies = new MemoizedTags<>(document.getRoot(), "dependencies/dependency",
tag -> new Dependency(
false,
model.getDependencies().stream()
Expand All @@ -89,7 +89,7 @@ public Pom(MavenModel model, Xml.Document document) {
tag
), Dependency::getTag);

this.memoizedProperties = new MemoizedTags<>(document.getRoot(), "project/properties/*",
this.memoizedProperties = new MemoizedTags<>(document.getRoot(), "properties/*",
Property::new, Property::getTag);
}

Expand Down Expand Up @@ -475,7 +475,7 @@ public DependencyManagement(MavenModel.DependencyManagement model, Xml.Tag tag)
this.model = model;
this.tag = tag;

this.memoizedDependencies = new MemoizedTags<>(tag, "dependencyManagement/dependencies/dependency",
this.memoizedDependencies = new MemoizedTags<>(tag, "dependencies/dependency",
dm -> new Dependency(
true,
model.getDependencies().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public boolean matches(Cursor cursor) {
}
}

return expression.startsWith("/") || pathIndex == path.size();
return expression.startsWith("/") || path.size() - pathIndex == 1;
} else if (expression.startsWith("/")) {
Collections.reverse(path);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ class XPathMatcherTest : XmlParser() {

@Test
fun matchRelativeAttribute() {
assertTrue(visitor("artifactId/@scope").visit(x))
assertTrue(visitor("artifactId/@*").visit(x))
assertTrue(visitor("//artifactId/@scope").visit(x))
assertTrue(visitor("dependency/artifactId/@scope").visit(x))
assertTrue(visitor("dependency/artifactId/@*").visit(x))
assertTrue(visitor("//dependency/artifactId/@scope").visit(x))
}

private fun visitor(xPath: String): XmlSourceVisitor<Boolean> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import org.openrewrite.xml.tree.Xml
class FindTagsTest : XmlParser() {
private val x = parse("""
<?xml version="1.0" encoding="UTF-8"?>
<dependencies>
<dependency/>
<dependency/>
</dependency>
<project>
<dependencies>
<dependency/>
<dependency/>
</dependency>
</project>
""".trimIndent())

private val deep = parse("""
Expand All @@ -45,7 +47,7 @@ class FindTagsTest : XmlParser() {

@Test
fun findAbsolute() {
assertThat(FindTags("/dependencies/dependency").visit(x).map { it.name })
assertThat(FindTags("/project/dependencies/dependency").visit(x).map { it.name })
.hasSize(2)
.containsExactly("dependency", "dependency")
}
Expand Down

0 comments on commit 1b1a6c2

Please sign in to comment.