Skip to content

Commit

Permalink
[SUREFIRE-2270] Use JUnit5 in surefire-shadefire
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Sep 24, 2024
1 parent 7a98850 commit d7f4dbb
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1154,7 +1154,9 @@ protected List<ProviderInfo> createProviders(TestClassPath testClasspath) throws
new TestNgProviderInfo(getTestNgArtifact()),
new JUnitCoreProviderInfo(getJunitArtifact(), junitDepArtifact),
new JUnit4ProviderInfo(getJunitArtifact(), junitDepArtifact),
new JUnit3ProviderInfo());
new JUnit3ProviderInfo(),
new JUnitPlatformProviderShadefireInfo(
getJUnitPlatformRunnerArtifact(), getJUnit5Artifact(), testClasspath));
}

SurefireProperties setupProperties() {
Expand Down Expand Up @@ -3034,7 +3036,7 @@ public Set<Artifact> getProviderClasspath() throws MojoExecutionException {
}
}

final class JUnitPlatformProviderInfo implements ProviderInfo {
class JUnitPlatformProviderInfo implements ProviderInfo {
private static final String PROVIDER_DEP_GID = "org.junit.platform";
private static final String PROVIDER_DEP_AID = "junit-platform-launcher";

Expand All @@ -3057,6 +3059,10 @@ public String getProviderName() {
return "org.apache.maven.surefire.junitplatform.JUnitPlatformProvider";
}

protected String getProviderArtifactName() {
return "surefire-junit-platform";
}

@Override
public boolean isApplicable() {
return junitPlatformRunnerArtifact == null && junitPlatformArtifact != null;
Expand All @@ -3082,7 +3088,7 @@ public Set<Artifact> getProviderClasspath() throws MojoExecutionException {
Map<String, Artifact> providerArtifacts = surefireDependencyResolver.getProviderClasspathAsMap(
session.getRepositorySession(),
project.getRemotePluginRepositories(),
"surefire-junit-platform",
getProviderArtifactName(),
surefireVersion);
Map<String, Artifact> testDeps = testClasspath.getTestDependencies();

Expand Down Expand Up @@ -3162,7 +3168,7 @@ private void narrowDependencies(
providerArtifacts.keySet().removeAll(testDependencies.keySet());
}

private void alignProviderVersions(Map<String, Artifact> providerArtifacts) throws MojoExecutionException {
protected void alignProviderVersions(Map<String, Artifact> providerArtifacts) throws MojoExecutionException {
String version = junitPlatformArtifact.getBaseVersion();
for (Artifact launcherArtifact : resolve(PROVIDER_DEP_GID, PROVIDER_DEP_AID, version, null, "jar")) {
String key = launcherArtifact.getGroupId() + ":" + launcherArtifact.getArtifactId();
Expand Down Expand Up @@ -3197,6 +3203,41 @@ private boolean hasDependencyPlatformEngine(Map<String, Artifact> dependencies)
}
}

final class JUnitPlatformProviderShadefireInfo extends JUnitPlatformProviderInfo {

JUnitPlatformProviderShadefireInfo(
Artifact junitPlatformRunnerArtifact,
Artifact junitPlatformArtifact,
@Nonnull TestClassPath testClasspath) {
super(junitPlatformRunnerArtifact, junitPlatformArtifact, testClasspath);
}

@Override
public boolean isApplicable() {
// newer discover this provider automatically
return false;
}

@Override
@Nonnull
public String getProviderName() {
return "org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider";
}

@Override
protected String getProviderArtifactName() {
return "surefire-shadefire";
}

@Override
protected void alignProviderVersions(Map<String, Artifact> providerArtifacts) throws MojoExecutionException {
// shadefire is used as booter we can not provide additional dependencies,
// so we need add a launcher here
providerArtifacts.put("org.junit.platform:junit-platform-launcher", null);
super.alignProviderVersions(providerArtifacts);
}
}

final class JUnitCoreProviderInfo implements ProviderInfo {
private final Artifact junitArtifact;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,31 @@
*/
package org.apache.maven.surefire.its.jiras;

import org.apache.maven.shared.verifier.VerificationException;
import org.apache.maven.surefire.its.fixture.SurefireJUnit4IntegrationTestCase;
import org.junit.Test;

import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.StringContains.containsString;

/**
* Test for shadefire usage
*
* @author Slawomir Jaranowski
*/
public class Surefire2006ShadefireTransformersIT extends SurefireJUnit4IntegrationTestCase {
@Test
public void shadefireShouldBeUsed() {
public void shadefireShouldBeUsed() throws VerificationException {
unpack("surefire-2006-shadefire-transformers")
.debugLogging()
.executeTest()
.assertTestSuiteResults(1, 0, 0, 0)
.verifyTextInLog(
"[INFO] Using configured provider org.apache.maven.shadefire.surefire.junit.JUnit3Provider");
.assertTestSuiteResults(2, 0, 0, 0)
.assertThatLogLine(
containsString(
"[INFO] Using configured provider org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider"),
is(1))
.assertThatLogLine(containsString("[INFO] Running PojoTest"), is(0))
.assertThatLogLine(containsString("[INFO] Running JUnit4Test"), is(1))
.assertThatLogLine(containsString("[INFO] Running JUnit5Test"), is(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@
<maven.compiler.target>${java.specification.version}</maven.compiler.target>
</properties>

<!-- NOTE: This has no dependencies on JUnit or TestNG, just nothing -->
<!-- shadefire will support JUnit5 an JUnit4 by vintage engine -->
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.junit.Test;

public class JUnit4Test {

@Test
public void success() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import org.junit.jupiter.api.Test;

public class JUnit5Test {

@Test
public void success() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* under the License.
*/

/**
* This test will be not executed ...
*/
public class PojoTest
{
public void testSuccess()
Expand Down
5 changes: 2 additions & 3 deletions surefire-shadefire/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit3</artifactId>
<artifactId>surefire-junit-platform</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>
Expand Down Expand Up @@ -81,8 +81,7 @@
<include>org.apache.maven.surefire:surefire-extensions-spi</include>
<include>org.apache.maven.surefire:surefire-booter</include>
<include>org.apache.maven.surefire:common-java5</include>
<include>org.apache.maven.surefire:common-junit3</include>
<include>org.apache.maven.surefire:surefire-junit3</include>
<include>org.apache.maven.surefire:surefire-junit-platform</include>
</includes>
<excludes>
<!-- with rebuild old shaded artifact is used -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
# specific language governing permissions and limitations
# under the License.
#
org.apache.maven.shadefire.surefire.junit.JUnit3Provider
org.apache.maven.shadefire.surefire.junitplatform.JUnitPlatformProvider

0 comments on commit d7f4dbb

Please sign in to comment.