Skip to content

Commit ebd5381

Browse files
committed
Merge branch 'master' of github.com:openrewrite/rewrite-testing-frameworks
2 parents 700fbf4 + 9b2b6e2 commit ebd5381

File tree

5 files changed

+200
-1
lines changed

5 files changed

+200
-1
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ val assertJVersion = "3.18.1"
6161

6262
dependencies {
6363
implementation("org.openrewrite:rewrite-java:latest.integration")
64+
implementation("org.openrewrite:rewrite-maven:latest.integration")
6465

6566
testImplementation("org.jetbrains.kotlin:kotlin-reflect")
6667
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.junit5;
17+
18+
import org.openrewrite.AutoConfigure;
19+
import org.openrewrite.CompositeRefactorVisitor;
20+
import org.openrewrite.Validated;
21+
import org.openrewrite.java.JavaRefactorVisitor;
22+
import org.openrewrite.java.tree.J;
23+
import org.openrewrite.maven.AddDependency;
24+
import org.openrewrite.maven.MavenRefactorVisitor;
25+
import org.openrewrite.maven.tree.Maven;
26+
27+
import static org.openrewrite.Validated.required;
28+
29+
@AutoConfigure
30+
public class MaybeAddJUnit5Dependencies extends CompositeRefactorVisitor {
31+
private String version = "5.x";
32+
33+
private boolean junitReferencesExist = false;
34+
35+
public MaybeAddJUnit5Dependencies() {
36+
addVisitor(new FindJUnit());
37+
addVisitor(new AddDependencies());
38+
}
39+
40+
public void setVersion(String version) {
41+
this.version = version;
42+
}
43+
44+
@Override
45+
public Validated validate() {
46+
return required("version", version);
47+
}
48+
49+
private class FindJUnit extends JavaRefactorVisitor {
50+
@Override
51+
public J visitCompilationUnit(J.CompilationUnit cu) {
52+
junitReferencesExist = cu.hasType("junit.framework.Test") ||
53+
cu.hasType("org.junit.jupiter.api.Test");
54+
return cu;
55+
}
56+
}
57+
58+
private class AddDependencies extends MavenRefactorVisitor {
59+
@Override
60+
public Maven visitMaven(Maven maven) {
61+
if (junitReferencesExist) {
62+
AddDependency addJunitApi = new AddDependency();
63+
addJunitApi.setGroupId("org.junit.jupiter");
64+
addJunitApi.setArtifactId("junit-jupiter-api");
65+
addJunitApi.setVersion(version);
66+
addJunitApi.setScope("test");
67+
andThen(addJunitApi);
68+
69+
AddDependency addJunitJupiterEngine = new AddDependency();
70+
addJunitJupiterEngine.setGroupId("org.junit.jupiter");
71+
addJunitJupiterEngine.setArtifactId("junit-jupiter-engine");
72+
addJunitJupiterEngine.setVersion(version);
73+
addJunitJupiterEngine.setScope("test");
74+
andThen(addJunitJupiterEngine);
75+
}
76+
77+
return super.visitMaven(maven);
78+
}
79+
}
80+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NonNullApi
17+
package org.openrewrite.java.testing.junit5;
18+
19+
import org.openrewrite.internal.lang.NonNullApi;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,21 @@ visitors:
4646
- org.openrewrite.java.OrderImports:
4747
removeUnused: true
4848
---
49+
type: specs.openrewrite.org/v1beta/visitor
50+
name: org.openrewrite.java.testing.junit5.RemoveJUnit4Dependency
51+
visitors:
52+
- org.openrewrite.maven.RemoveDependency
53+
groupId: junit
54+
artifactId: junit
55+
---
56+
type: specs.openrewrite.org/v1beta/visitor
57+
name: org.openrewrite.java.testing.junit5.ExcludeJUnitVintageEngine
58+
visitors:
59+
- org.openrewrite.maven.ExcludeDependency
60+
groupId: org.junit.vintage
61+
artifactId: junit-vintage-engine
62+
---
4963
type: specs.openrewrite.org/v1beta/recipe
5064
name: org.openrewrite.java.testing.JUnit5Migration
5165
include:
5266
- 'org.openrewrite.java.testing.junit5.*'
53-
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2020 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.junit5
17+
18+
import org.assertj.core.api.Assertions.assertThat
19+
import org.junit.jupiter.api.Test
20+
import org.openrewrite.Refactor
21+
import org.openrewrite.RefactorVisitorTest
22+
import org.openrewrite.java.JavaParser
23+
import org.openrewrite.maven.MavenParser
24+
25+
class MaybeAddJUnit5DependenciesTest : RefactorVisitorTest {
26+
@Test
27+
fun addDependenciesWhenJUnitTestsExist() {
28+
val java = JavaParser.fromJavaVersion()
29+
.classpath(JavaParser.dependenciesFromClasspath("junit-jupiter-api", "apiguardian-api"))
30+
.build().parse("""
31+
import org.junit.jupiter.api.Test;
32+
class MyTest {
33+
@Test
34+
void test() {
35+
}
36+
}
37+
""".trimIndent())
38+
39+
val maven = MavenParser.builder().build().parse("""
40+
<project>
41+
<groupId>com.mycompany.app</groupId>
42+
<artifactId>my-app</artifactId>
43+
<version>1</version>
44+
</project>
45+
""".trimIndent())
46+
47+
val changes = Refactor()
48+
.visit(MaybeAddJUnit5Dependencies())
49+
.fix(maven + java)
50+
51+
assertThat(changes).hasSize(1)
52+
assertThat(changes.first()?.fixed?.printTrimmed()).isEqualTo("""
53+
<project>
54+
<groupId>com.mycompany.app</groupId>
55+
<artifactId>my-app</artifactId>
56+
<version>1</version>
57+
<dependencies>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-api</artifactId>
61+
<version>5.7.0</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>org.junit.jupiter</groupId>
66+
<artifactId>junit-jupiter-api</artifactId>
67+
<version>5.7.0</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.junit.jupiter</groupId>
72+
<artifactId>junit-jupiter-engine</artifactId>
73+
<version>5.7.0</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>org.junit.jupiter</groupId>
78+
<artifactId>junit-jupiter-engine</artifactId>
79+
<version>5.7.0</version>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
</project>
84+
""".trimIndent())
85+
}
86+
}

0 commit comments

Comments
 (0)