|
20 | 20 | import org.apache.maven.plugin.logging.Log; |
21 | 21 | import org.apache.maven.project.MavenProject; |
22 | 22 | import org.codehaus.plexus.util.xml.Xpp3Dom; |
| 23 | +import org.eclipse.aether.artifact.DefaultArtifact; |
| 24 | +import org.eclipse.aether.resolution.ArtifactRequest; |
| 25 | +import org.eclipse.aether.resolution.ArtifactResolutionException; |
| 26 | +import org.eclipse.aether.resolution.ArtifactResult; |
23 | 27 | import org.pitest.classinfo.ClassName; |
24 | 28 | import org.pitest.classpath.DirectoryClassPathRoot; |
25 | 29 | import org.pitest.functional.FCollection; |
|
40 | 44 | import java.util.Enumeration; |
41 | 45 | import java.util.HashSet; |
42 | 46 | import java.util.List; |
| 47 | +import java.util.Optional; |
43 | 48 | import java.util.Properties; |
44 | 49 | import java.util.Set; |
45 | 50 | import java.util.function.Function; |
@@ -77,18 +82,84 @@ public ReportOptions convert() { |
77 | 82 |
|
78 | 83 | classPath.addAll(this.mojo.getAdditionalClasspathElements()); |
79 | 84 |
|
| 85 | + autoAddJUnitPlatform(classPath); |
| 86 | + removeExcludedDependencies(classPath); |
| 87 | + |
| 88 | + ReportOptions option = parseReportOptions(classPath); |
| 89 | + return updateFromSurefire(option); |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * The junit 5 plugin needs junit-platform-launcher to run, but this will not be on the classpath |
| 95 | + * of the project. We want to use the same version that surefire (and therefore the SUT) uses, not |
| 96 | + * the one the plugin was built against. |
| 97 | + * <p> |
| 98 | + * It is not declared as a normal dependency, instead surefire picks the version to use based on |
| 99 | + * other junit jars on the classpath. We're forced to do something similar here. |
| 100 | + * |
| 101 | + * @param classPath classpath to modify |
| 102 | + */ |
| 103 | + private void autoAddJUnitPlatform(List<String> classPath) { |
| 104 | + List<Artifact> junitDependencies = this.mojo.getProject().getArtifacts().stream() |
| 105 | + .filter(a -> a.getGroupId().equals("org.junit.platform")) |
| 106 | + .collect(Collectors.toList()); |
| 107 | + |
| 108 | + // If the launcher has been manually added to the dependencies, there is nothing to do |
| 109 | + if (junitDependencies.stream().anyMatch(a -> a.getArtifactId().equals("junit-platform-launcher"))) { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + Optional<Artifact> maybeJUnitPlatform = findJUnitArtifact(junitDependencies); |
| 114 | + if (!maybeJUnitPlatform.isPresent()) { |
| 115 | + this.log.debug("JUnit 5 not on classpath"); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + // Look for platform engine or platform commons on classpath |
| 120 | + Artifact toMatch = maybeJUnitPlatform.get(); |
| 121 | + |
| 122 | + // Assume that platform launcher has been released with same version number as engine and commons |
| 123 | + DefaultArtifact platformLauncher = new DefaultArtifact(toMatch.getGroupId(), "junit-platform-launcher", "jar", |
| 124 | + toMatch.getVersion()); |
| 125 | + |
| 126 | + try { |
| 127 | + ArtifactRequest r = new ArtifactRequest(); |
| 128 | + r.setArtifact(platformLauncher); |
| 129 | + |
| 130 | + r.setRepositories(this.mojo.getProject().getRemotePluginRepositories()); |
| 131 | + ArtifactResult resolved = this.mojo.repositorySystem().resolveArtifact(mojo.session().getRepositorySession(), r); |
| 132 | + |
| 133 | + this.log.info("Auto adding " + resolved + " to classpath."); |
| 134 | + classPath.add(resolved.getArtifact().getFile().getAbsolutePath()); |
| 135 | + } catch (ArtifactResolutionException e) { |
| 136 | + this.log.error("Could not resolve " + platformLauncher); |
| 137 | + throw new RuntimeException(e); |
| 138 | + } |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + private static Optional<Artifact> findJUnitArtifact(List<Artifact> junitDependencies) { |
| 143 | + Optional<Artifact> maybeEngine = junitDependencies.stream() |
| 144 | + .filter(a -> a.getArtifactId().equals("junit-platform-engine")) |
| 145 | + .findAny(); |
| 146 | + if (maybeEngine.isPresent()) { |
| 147 | + return maybeEngine; |
| 148 | + } |
| 149 | + |
| 150 | + return junitDependencies.stream() |
| 151 | + .filter(a -> a.getArtifactId().equals("junit-platform-commons")) |
| 152 | + .findAny(); |
| 153 | + } |
| 154 | + |
| 155 | + private void removeExcludedDependencies(List<String> classPath) { |
80 | 156 | for (Object artifact : this.mojo.getProject().getArtifacts()) { |
81 | 157 | final Artifact dependency = (Artifact) artifact; |
82 | | - |
83 | 158 | if (this.mojo.getClasspathDependencyExcludes().contains( |
84 | 159 | dependency.getGroupId() + ":" + dependency.getArtifactId())) { |
85 | 160 | classPath.remove(dependency.getFile().getPath()); |
86 | 161 | } |
87 | 162 | } |
88 | | - |
89 | | - ReportOptions option = parseReportOptions(classPath); |
90 | | - return updateFromSurefire(option); |
91 | | - |
92 | 163 | } |
93 | 164 |
|
94 | 165 | private ReportOptions parseReportOptions(final List<String> classPath) { |
@@ -270,6 +341,11 @@ private void addOwnDependenciesToClassPath(final List<String> classPath) { |
270 | 341 | + dependency.getArtifactId() + " to SUT classpath"); |
271 | 342 | classPath.add(dependency.getFile().getAbsolutePath()); |
272 | 343 | } |
| 344 | + |
| 345 | + // If the user as explicitly added junit platform classes to the pitest classpath, trust that they |
| 346 | + // know what they're doing and add them in |
| 347 | + this.mojo.getPluginArtifactMap().values().stream().filter(a -> a.getGroupId().equals("org.junit.platform")) |
| 348 | + .forEach(dependency -> classPath.add(dependency.getFile().getAbsolutePath())); |
273 | 349 | } |
274 | 350 |
|
275 | 351 | private Collection<Predicate<String>> globStringsToPredicates( |
|
0 commit comments