This is an automated email from the ASF dual-hosted git repository.

sor pushed a commit to branch SUREFIRE-1585
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 3aa28b617ef14af46350f14fea260fbc3450ae2b
Author: Christian Stein <sormu...@gmail.com>
AuthorDate: Wed Oct 24 11:35:23 2018 +0200

    Resolve missing 'org.junit.jupiter:junit-jupiter-engine' artifact
---
 .../plugin/surefire/AbstractSurefireMojo.java      |  4 +-
 .../maven/plugin/surefire/TestClassPath.java       | 60 ++++++++++++++++------
 2 files changed, 47 insertions(+), 17 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
index c976a23..0cf5f62 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/AbstractSurefireMojo.java
@@ -2487,7 +2487,7 @@ public abstract class AbstractSurefireMojo
         }
 
         return new TestClassPath( classpathArtifacts, getClassesDirectory(),
-                getTestClassesDirectory(), getAdditionalClasspathElements(), 
logger );
+                getTestClassesDirectory(), getAdditionalClasspathElements(), 
this );
     }
 
     /**
@@ -2523,7 +2523,7 @@ public abstract class AbstractSurefireMojo
     }
 
 
-    private ArtifactResolutionResult resolveArtifact( Artifact 
filteredArtifact, Artifact providerArtifact )
+    ArtifactResolutionResult resolveArtifact( Artifact filteredArtifact, 
Artifact providerArtifact )
     {
         ArtifactFilter filter = null;
         if ( filteredArtifact != null )
diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java
index b9b39b5..928cc91 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/TestClassPath.java
@@ -20,13 +20,14 @@ package org.apache.maven.plugin.surefire;
  */
 
 import org.apache.maven.artifact.Artifact;
+import org.apache.maven.artifact.DefaultArtifact;
 import org.apache.maven.surefire.booter.Classpath;
-import org.codehaus.plexus.logging.Logger;
 
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import static java.util.Collections.addAll;
@@ -35,22 +36,26 @@ import static 
org.apache.maven.shared.utils.StringUtils.split;
 final class TestClassPath
 {
     private final Iterable<Artifact> artifacts;
+
     private final File classesDirectory;
+
     private final File testClassesDirectory;
+
     private final String[] additionalClasspathElements;
-    private final Logger logger;
+
+    private final AbstractSurefireMojo mojo;
 
     TestClassPath( Iterable<Artifact> artifacts,
                    File classesDirectory,
                    File testClassesDirectory,
                    String[] additionalClasspathElements,
-                   Logger logger )
+                   AbstractSurefireMojo mojo )
     {
         this.artifacts = artifacts;
         this.classesDirectory = classesDirectory;
         this.testClassesDirectory = testClassesDirectory;
         this.additionalClasspathElements = additionalClasspathElements;
-        this.logger = logger;
+        this.mojo = mojo;
     }
 
     void avoidArtifactDuplicates( Set<Artifact> providerArtifacts )
@@ -69,9 +74,9 @@ final class TestClassPath
                         && ( classifier1 == null ? classifier2 == null : 
classifier1.equals( classifier2 ) ) )
                 {
                     it.remove();
-                    if ( logger.isDebugEnabled() )
+                    if ( mojo != null && mojo.getLog().isDebugEnabled() )
                     {
-                        logger.debug( "Removed artifact " + providerArtifact + 
" from provider. "
+                        mojo.getLog().debug( "Removed artifact " + 
providerArtifact + " from provider. "
                                 + "Already appears in test classpath." );
                     }
                 }
@@ -87,15 +92,40 @@ final class TestClassPath
         // TODO Check for actual SurefireProvider -- the following only 
applies for when
         //      
'org.apache.maven.surefire.junitplatform.JUnitPlatformProvider' is active.
 
-        // TODO Add missing 'junit-jupiter-engine' to test runtime
-        // Artifact junitJupiterApi = findArtifact( 
"org.junit.jupiter:junit-jupiter-api", artifacts );
-        // Artifact junitJupiterEngine =
-        //   findArtifact( "org.junit.jupiter:junit-jupiter-engine", 
artifacts, providerArtifacts );
-        // if ( junitJupiterApi != null && junitJupiterEngine == null )
-        // {
-        //     [artifacts | providerArtifacts]
-        //     .add( "org.junit.jupiter:junit-jupiter-engine:" + 
junitJupiterApi.getVersion() );
-        // }
+        if ( mojo == null )
+        {
+            // mojo.getLog().warn( "Can't resolve missing artifacts when mojo 
is not set." );
+            return;
+        }
+
+        // Add missing 'junit-jupiter-engine' to test runtime
+        Map<String, Artifact> artifactMap = mojo.getProjectArtifactMap();
+        Artifact junitJupiterApi = artifactMap.get( 
"org.junit.jupiter:junit-jupiter-api" );
+        Artifact junitJupiterEngine = artifactMap.get( 
"org.junit.jupiter:junit-jupiter-engine" );
+        if ( junitJupiterApi != null && junitJupiterEngine == null )
+        {
+            junitJupiterEngine = new DefaultArtifact(
+                            "org.junit.jupiter",
+                            "junit-jupiter-engine",
+                            junitJupiterApi.getVersionRange(),
+                            "test",
+                            "jar",
+                            "",
+                            junitJupiterApi.getArtifactHandler()
+            );
+            @SuppressWarnings( "unchecked" )
+            Set<Artifact> resolvedArtifacts = mojo.resolveArtifact( null, 
junitJupiterEngine ).getArtifacts();
+            providerArtifacts.addAll( resolvedArtifacts );
+
+            if ( mojo.getLog().isDebugEnabled() )
+            {
+                mojo.getLog().debug( "Resolved missing artifact: " + 
junitJupiterEngine );
+                for ( Artifact resolvedArtifact : resolvedArtifacts )
+                {
+                    mojo.getLog().debug( "  -> " + resolvedArtifact );
+                }
+            }
+        }
 
         // TODO Add missing 'junit-vintage-engine' to test runtime
         // Artifact junit4 = findArtifact( "junit:junit", artifacts );

Reply via email to