diff --git a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java index 9b96d5da4e..939e422547 100644 --- a/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java +++ b/src/main/java/org/openrewrite/java/migrate/AddMockitoJavaAgentToMavenSurefirePlugin.java @@ -70,7 +70,8 @@ public class AddMockitoJavaAgentToMavenSurefirePlugin extends Recipe { @Override public TreeVisitor getVisitor() { return Preconditions.check(new DependencyInsight("org.mockito", "mockito-core", "test", null, false), new MavenIsoVisitor() { - private final String CONFIGURATION_TAG_TEMPLATE = "%s"; + // The newline before the comment keeps auto-format from attaching it to a preceding sibling as a trailing comment + private final String CONFIGURATION_TAG_TEMPLATE = "\n%s"; private String getArgLineJavaAgentArgument() { String mockitoCoreVersion = getResolutionResult().getDependencies().getOrDefault(Scope.Test, emptyList()).stream() diff --git a/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java b/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java index 42cd88f511..2c1813eb69 100644 --- a/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java +++ b/src/main/java/org/openrewrite/java/migrate/JpaCacheProperties.java @@ -20,6 +20,7 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.internal.ListUtils; +import org.openrewrite.xml.RemoveContentVisitor; import org.openrewrite.xml.XPathMatcher; import org.openrewrite.xml.XmlVisitor; import org.openrewrite.xml.tree.Content; @@ -31,7 +32,6 @@ import java.util.Optional; import static org.openrewrite.xml.AddOrUpdateChild.addOrUpdateChild; -import static org.openrewrite.xml.FilterTagChildrenVisitor.filterTagChildren; @EqualsAndHashCode(callSuper = false) @Value @@ -185,7 +185,7 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { if (sdh.openJPACacheProperty != null) { String attrValue = getAttributeValue("value", sdh.openJPACacheProperty); if ("true".equalsIgnoreCase(attrValue) || "false".equalsIgnoreCase(attrValue)) { - sdh.propertiesElement = filterTagChildren(sdh.propertiesElement, child -> child != sdh.openJPACacheProperty); + sdh.propertiesElement = removeContent(sdh.propertiesElement, sdh.openJPACacheProperty, ctx); t = addOrUpdateChild(t, sdh.propertiesElement, getCursor().getParentOrThrow()); } } @@ -193,12 +193,20 @@ public Xml visitTag(Xml.Tag tag, ExecutionContext ctx) { // if both shared-cache-mode and javax cache property are set, delete the // javax cache property if (sdh.sharedCacheModeElement != null && sdh.sharedCacheModeProperty != null) { - sdh.propertiesElement = filterTagChildren(sdh.propertiesElement, child -> child != sdh.sharedCacheModeProperty); + sdh.propertiesElement = removeContent(sdh.propertiesElement, sdh.sharedCacheModeProperty, ctx); t = addOrUpdateChild(t, sdh.propertiesElement, getCursor().getParentOrThrow()); } return t; } + /** + * Unlike {@code filterTagChildren}, this hands the removed element's prefix to a comment that trailed it, + * so the comment keeps its own line rather than collapsing onto the preceding sibling. + */ + private static Xml.Tag removeContent(Xml.Tag parent, Xml.Tag child, ExecutionContext ctx) { + return (Xml.Tag) new RemoveContentVisitor(child, false, false).visitNonNull(parent, ctx); + } + private SharedDataHolder extractData(Xml.Tag puNode) { SharedDataHolder sdh = new SharedDataHolder(); diff --git a/src/test/java/org/openrewrite/java/migrate/JpaCachePropertiesTest.java b/src/test/java/org/openrewrite/java/migrate/JpaCachePropertiesTest.java index 28f944a3f0..94baafd4aa 100644 --- a/src/test/java/org/openrewrite/java/migrate/JpaCachePropertiesTest.java +++ b/src/test/java/org/openrewrite/java/migrate/JpaCachePropertiesTest.java @@ -607,8 +607,7 @@ void notset_set_set1() { - - + @@ -644,8 +643,7 @@ void notset_set_set2() { - - + @@ -681,8 +679,7 @@ void notset_set_set3() { - - + @@ -783,8 +780,7 @@ void notset_set_notset3() { NONE - - + @@ -909,14 +905,12 @@ void notset_notset_set3() { - - + ENABLE_SELECTIVE NONE - - + @@ -947,14 +941,12 @@ void notset_notset_set4() { - - + DISABLE_SELECTIVE NONE - - + @@ -1139,8 +1131,7 @@ void openjpa_cache3_flagged() { - - + NONE NONE