Skip to content

Commit 1fd73d3

Browse files
authored
Only upgrade maven-surefire-plugin on older Maven versions (#517)
* Only upgrade surefire plugin on older Maven versions * Add marker to pom.xml * Add missing document example annotation
1 parent 07525f9 commit 1fd73d3

File tree

2 files changed

+101
-91
lines changed

2 files changed

+101
-91
lines changed

src/main/resources/META-INF/rewrite/junit5.yml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,7 @@ recipeList:
6363
pluginArtifactId: maven-surefire-plugin
6464
groupId: org.apache.maven.surefire
6565
artifactId: surefire-junit*
66-
- org.openrewrite.maven.AddPlugin:
67-
groupId: org.apache.maven.plugins
68-
artifactId: maven-surefire-plugin
69-
version: 3.2.5
70-
- org.openrewrite.maven.AddPluginDependency:
71-
pluginGroupId: org.apache.maven.plugins
72-
pluginArtifactId: maven-surefire-plugin
73-
groupId: org.junit.platform
74-
artifactId: junit-platform-surefire-provider
75-
version: 1.1.0
66+
- org.openrewrite.java.testing.junit5.UpgradeSurefirePlugin
7667
- org.openrewrite.java.testing.junit5.UseHamcrestAssertThat
7768
- org.openrewrite.java.testing.junit5.MigrateAssumptions
7869
- org.openrewrite.java.testing.junit5.UseMockitoExtension
@@ -317,3 +308,24 @@ recipeList:
317308
newGroupId: org.xmlunit
318309
newArtifactId: xmlunit-legacy
319310
newVersion: 2.x
311+
---
312+
type: specs.openrewrite.org/v1beta/recipe
313+
name: org.openrewrite.java.testing.junit5.UpgradeSurefirePlugin
314+
displayName: Upgrade Surefire Plugin
315+
description: Upgrades the Maven Surefire Plugin to the latest version if still using an older Maven version.
316+
preconditions:
317+
- org.openrewrite.java.search.HasBuildToolVersion:
318+
type: Maven
319+
version: 0.0.1-3.5.4
320+
recipeList:
321+
- org.openrewrite.maven.AddPlugin:
322+
groupId: org.apache.maven.plugins
323+
artifactId: maven-surefire-plugin
324+
version: 3.2.5
325+
- org.openrewrite.maven.AddPluginDependency:
326+
pluginGroupId: org.apache.maven.plugins
327+
pluginArtifactId: maven-surefire-plugin
328+
groupId: org.junit.platform
329+
artifactId: junit-platform-surefire-provider
330+
version: 1.1.0
331+

src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java

Lines changed: 79 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616
package org.openrewrite.java.testing.junit5;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
1920
import org.openrewrite.InMemoryExecutionContext;
2021
import org.openrewrite.Issue;
22+
import org.openrewrite.Tree;
2123
import org.openrewrite.config.Environment;
2224
import org.openrewrite.java.JavaParser;
25+
import org.openrewrite.marker.BuildTool;
2326
import org.openrewrite.test.RecipeSpec;
2427
import org.openrewrite.test.RewriteTest;
2528

26-
import java.util.List;
27-
import java.util.regex.MatchResult;
2829
import java.util.regex.Pattern;
2930

3031
import static org.assertj.core.api.Assertions.assertThat;
@@ -74,6 +75,7 @@ void method() {
7475
);
7576
}
7677

78+
@DocumentExample
7779
@Test
7880
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/145")
7981
void assertThatReceiver() {
@@ -170,22 +172,6 @@ void dontExcludeJunit4DependencyfromTestcontainers() {
170172
<scope>test</scope>
171173
</dependency>
172174
</dependencies>
173-
<build>
174-
<plugins>
175-
<plugin>
176-
<groupId>org.apache.maven.plugins</groupId>
177-
<artifactId>maven-surefire-plugin</artifactId>
178-
<version>3.2.5</version>
179-
<dependencies>
180-
<dependency>
181-
<groupId>org.junit.platform</groupId>
182-
<artifactId>junit-platform-surefire-provider</artifactId>
183-
<version>1.1.0</version>
184-
</dependency>
185-
</dependencies>
186-
</plugin>
187-
</plugins>
188-
</build>
189175
</project>
190176
""";
191177
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
@@ -229,22 +215,6 @@ void dontExcludeJunit4DependencyfromSpringBootTestcontainers() {
229215
<scope>test</scope>
230216
</dependency>
231217
</dependencies>
232-
<build>
233-
<plugins>
234-
<plugin>
235-
<groupId>org.apache.maven.plugins</groupId>
236-
<artifactId>maven-surefire-plugin</artifactId>
237-
<version>3.2.5</version>
238-
<dependencies>
239-
<dependency>
240-
<groupId>org.junit.platform</groupId>
241-
<artifactId>junit-platform-surefire-provider</artifactId>
242-
<version>1.1.0</version>
243-
</dependency>
244-
</dependencies>
245-
</plugin>
246-
</plugins>
247-
</build>
248218
</project>
249219
""";
250220
// Output identical, but we want to make sure we don't exclude junit4 from testcontainers
@@ -343,7 +313,7 @@ public void test() {
343313
import org.junit.jupiter.api.AfterEach;
344314
import org.junit.jupiter.api.BeforeEach;
345315
import org.junit.jupiter.api.Test;
346-
316+
347317
public class A extends AbstractTest {
348318
@BeforeEach
349319
public void before() {
@@ -368,54 +338,82 @@ void noJunitDependencyIfApiAlreadyPresent() {
368338
spec -> spec.beforeRecipe(withToolingApi()),
369339
//language=groovy
370340
buildGradle(
371-
"""
372-
plugins {
373-
id 'java-library'
374-
}
375-
repositories {
376-
mavenCentral()
377-
}
378-
dependencies {
379-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
380-
}
381-
tasks.withType(Test).configureEach {
382-
useJUnitPlatform()
383-
}
384-
"""),
341+
"""
342+
plugins {
343+
id 'java-library'
344+
}
345+
repositories {
346+
mavenCentral()
347+
}
348+
dependencies {
349+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.2'
350+
}
351+
tasks.withType(Test).configureEach {
352+
useJUnitPlatform()
353+
}
354+
"""),
385355
//language=xml
386356
pomXml(
387-
"""
388-
<project>
389-
<modelVersion>4.0.0</modelVersion>
390-
<groupId>dev.ted</groupId>
391-
<artifactId>testcontainer-migrate</artifactId>
392-
<version>0.0.1</version>
393-
<dependencies>
394-
<dependency>
395-
<groupId>org.junit.jupiter</groupId>
396-
<artifactId>junit-jupiter-api</artifactId>
397-
<version>5.7.2</version>
398-
<scope>test</scope>
399-
</dependency>
400-
</dependencies>
401-
<build>
402-
<plugins>
403-
<plugin>
404-
<groupId>org.apache.maven.plugins</groupId>
405-
<artifactId>maven-surefire-plugin</artifactId>
406-
<version>3.2.5</version>
407-
<dependencies>
408-
<dependency>
409-
<groupId>org.junit.platform</groupId>
410-
<artifactId>junit-platform-surefire-provider</artifactId>
411-
<version>1.1.0</version>
412-
</dependency>
413-
</dependencies>
414-
</plugin>
415-
</plugins>
416-
</build>
417-
</project>
418-
""")
357+
"""
358+
<project>
359+
<modelVersion>4.0.0</modelVersion>
360+
<groupId>dev.ted</groupId>
361+
<artifactId>testcontainer-migrate</artifactId>
362+
<version>0.0.1</version>
363+
<dependencies>
364+
<dependency>
365+
<groupId>org.junit.jupiter</groupId>
366+
<artifactId>junit-jupiter-api</artifactId>
367+
<version>5.7.2</version>
368+
<scope>test</scope>
369+
</dependency>
370+
</dependencies>
371+
</project>
372+
""")
373+
);
374+
}
375+
376+
@Test
377+
void bumpSurefireOnOlderMavenVersions() {
378+
rewriteRun(
379+
spec -> spec.recipeFromResource("/META-INF/rewrite/junit5.yml", "org.openrewrite.java.testing.junit5.UpgradeSurefirePlugin"),
380+
pomXml(
381+
//language=xml
382+
"""
383+
<project>
384+
<modelVersion>4.0.0</modelVersion>
385+
<groupId>dev.ted</groupId>
386+
<artifactId>testcontainer-migrate</artifactId>
387+
<version>0.0.1</version>
388+
</project>
389+
""",
390+
//language=xml
391+
"""
392+
<project>
393+
<modelVersion>4.0.0</modelVersion>
394+
<groupId>dev.ted</groupId>
395+
<artifactId>testcontainer-migrate</artifactId>
396+
<version>0.0.1</version>
397+
<build>
398+
<plugins>
399+
<plugin>
400+
<groupId>org.apache.maven.plugins</groupId>
401+
<artifactId>maven-surefire-plugin</artifactId>
402+
<version>3.2.5</version>
403+
<dependencies>
404+
<dependency>
405+
<groupId>org.junit.platform</groupId>
406+
<artifactId>junit-platform-surefire-provider</artifactId>
407+
<version>1.1.0</version>
408+
</dependency>
409+
</dependencies>
410+
</plugin>
411+
</plugins>
412+
</build>
413+
</project>
414+
""",
415+
spec -> spec.markers(new BuildTool(Tree.randomId(), BuildTool.Type.Maven, "3.5.4"))
416+
)
419417
);
420418
}
421419
}

0 commit comments

Comments
 (0)