3838import org .openrewrite .xml .tree .Content ;
3939import org .openrewrite .xml .tree .Xml ;
4040
41- import java .util .ArrayList ;
4241import java .util .List ;
4342import java .util .Optional ;
4443
44+ import static java .util .Collections .emptyList ;
45+
4546public class AddMockitoJavaAgentToMavenSurefirePlugin extends Recipe {
4647
4748 private static final XPathMatcher MAVEN_SUREFIRE_PLUGIN_MATCHER = new XPathMatcher (
@@ -75,7 +76,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
7576 private final String CONFIGURATION_TAG_TEMPLATE = "<configuration><!--suppress MavenModelInspection --><argLine>%s</argLine></configuration>" ;
7677
7778 private String getArgLineJavaAgentArgument () {
78- String mockitoCoreVersion = getResolutionResult ().getDependencies ().getOrDefault (Scope .Test , new ArrayList <> ()).stream ()
79+ String mockitoCoreVersion = getResolutionResult ().getDependencies ().getOrDefault (Scope .Test , emptyList ()).stream ()
7980 .filter (dependency -> dependency .getGroupId ().equals ("org.mockito" ) && dependency .getArtifactId ()
8081 .equals ("mockito-core" )).findFirst ().map (ResolvedDependency ::getVersion ).get ();
8182
@@ -127,14 +128,14 @@ public Xml.Document visitDocument(Xml.Document document, ExecutionContext ctx) {
127128 @ Override
128129 public Xml .Tag visitTag (Xml .Tag tag , ExecutionContext ctx ) {
129130 Xml .Tag t = super .visitTag (tag , ctx );
130- String argLineJavaAgentParam = getArgLineJavaAgentArgument ();
131- Xml .Tag configurationTag = buildConfigurationTag (argLineJavaAgentParam , false );
132131
133132 //noinspection unchecked
134133 final List <Content > tagContents = (List <Content >) t .getContent ();
135134 if (MAVEN_SUREFIRE_PLUGIN_MATCHER .matches (getCursor ()) && !t .getChild ("configuration" ).isPresent ()) {
136- return autoFormat (t .withContent (ListUtils .concat (tagContents , configurationTag )), ctx );
135+ return autoFormat (t .withContent (ListUtils .concat (tagContents ,
136+ buildConfigurationTag (getArgLineJavaAgentArgument (), false ))), ctx );
137137 } else if (MAVEN_SUREFIRE_PLUGIN_CONFIGURATION_MATCHER .matches (getCursor ())) {
138+ String argLineJavaAgentParam = getArgLineJavaAgentArgument ();
138139 List <Xml .Tag > argLineTagChildren = t .getChildren ("argLine" );
139140 if (argLineTagChildren .size () == 1 ) {
140141 Xml .Tag argLineTag = argLineTagChildren .get (0 );
@@ -148,7 +149,7 @@ public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) {
148149 }
149150 } else if (argLineTagChildren .isEmpty ()) {
150151 return autoFormat (t .withContent (
151- ListUtils .concatAll (tagContents , configurationTag .getContent ())), ctx );
152+ ListUtils .concatAll (tagContents , buildConfigurationTag ( argLineJavaAgentParam , false ) .getContent ())), ctx );
152153 }
153154 }
154155 return t ;
0 commit comments