Skip to content

Commit d7fe158

Browse files
sullistimtebeek
andauthored
Change dependencies and types for testcontainers v2 (#830)
* support testcontainers 2.x * Adopt test classpath.tsv.gz * No need to provide unchanged groupId * Be more selective with test classpath entries * Avoid duplicate entries * Avoid duplicate entries --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent ba3685d commit d7fe158

File tree

6 files changed

+222
-63
lines changed

6 files changed

+222
-63
lines changed

build.gradle.kts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ recipeDependencies {
4444
testParserClasspath("com.google.truth:truth:1.4.5")
4545
testParserClasspath("org.jboss.byteman:byteman-bmunit:4.0.25")
4646
testParserClasspath("org.powermock:powermock-module-junit4:1.6.5")
47+
48+
testParserClasspath("org.testcontainers:nginx:1.+")
49+
50+
testParserClasspath("org.testcontainers:testcontainers:2.0.1")
51+
testParserClasspath("org.testcontainers:testcontainers-cassandra:2.0.1")
52+
testParserClasspath("org.testcontainers:testcontainers-kafka:2.0.1")
53+
testParserClasspath("org.testcontainers:testcontainers-localstack:2.0.1")
54+
testParserClasspath("org.testcontainers:testcontainers-mysql:2.0.1")
55+
4756
}
4857

4958
val rewriteVersion = rewriteRecipe.rewriteVersion.get()
@@ -88,8 +97,6 @@ dependencies {
8897
testRuntimeOnly("org.easymock:easymock:latest.release")
8998
testRuntimeOnly("org.jboss.arquillian.junit:arquillian-junit-core:latest.release")
9099
testRuntimeOnly("org.mockito.kotlin:mockito-kotlin:5.4.0")
91-
testRuntimeOnly("org.testcontainers:testcontainers:1.+")
92-
testRuntimeOnly("org.testcontainers:nginx:1.+")
93100
testRuntimeOnly("org.testng:testng:latest.release")
94101
}
95102

src/main/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ private Expression getConstructorArgument(J.NewClass newClass, ExecutionContext
8181
maybeAddImport("org.testcontainers.utility.DockerImageName");
8282
return JavaTemplate.builder("DockerImageName.parse(\"" + image + "\")")
8383
.imports("org.testcontainers.utility.DockerImageName")
84-
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers"))
84+
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "testcontainers-1"))
8585
.build()
8686
.apply(getCursor(), newClass.getCoordinates().replace())
8787
.withPrefix(Space.EMPTY);

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

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ name: org.openrewrite.java.testing.testcontainers.TestContainersBestPractices
2020
displayName: Testcontainers best practices
2121
description: Apply best practices to Testcontainers usage.
2222
recipeList:
23-
# Upgrade to latest version of Testcontainers
24-
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
25-
groupId: org.testcontainers
26-
artifactId: "*"
27-
newVersion: 1.21.x
23+
- org.openrewrite.java.testing.testcontainers.Testcontainers2Migration
2824
- org.openrewrite.java.testing.testcontainers.ExplicitContainerImages
2925
- org.openrewrite.java.testing.testcontainers.GetHostMigration
3026
- org.openrewrite.java.ChangeType:
@@ -125,3 +121,75 @@ recipeList:
125121
containerClass: org.testcontainers.containers.PulsarContainer
126122
image: "apachepulsar/pulsar:2.10.0"
127123
parseImage: true
124+
---
125+
type: specs.openrewrite.org/v1beta/recipe
126+
name: org.openrewrite.java.testing.testcontainers.Testcontainers2Migration
127+
displayName: Migrate to testcontainers-java 2.x
128+
description: Change dependencies and types to migrate to testcontainers-java 2.x.
129+
recipeList:
130+
- org.openrewrite.java.dependencies.ChangeDependency:
131+
oldGroupId: org.testcontainers
132+
oldArtifactId: junit-jupiter
133+
newArtifactId: testcontainers-junit-jupiter
134+
newVersion: "2.x"
135+
- org.openrewrite.java.dependencies.ChangeDependency:
136+
oldGroupId: org.testcontainers
137+
oldArtifactId: localstack
138+
newArtifactId: testcontainers-localstack
139+
newVersion: "2.x"
140+
- org.openrewrite.java.dependencies.ChangeDependency:
141+
oldGroupId: org.testcontainers
142+
oldArtifactId: minio
143+
newArtifactId: testcontainers-minio
144+
newVersion: "2.x"
145+
- org.openrewrite.java.dependencies.ChangeDependency:
146+
oldGroupId: org.testcontainers
147+
oldArtifactId: mysql
148+
newArtifactId: testcontainers-mysql
149+
newVersion: "2.x"
150+
- org.openrewrite.java.dependencies.ChangeDependency:
151+
oldGroupId: org.testcontainers
152+
oldArtifactId: postgresql
153+
newArtifactId: testcontainers-postgresql
154+
newVersion: "2.x"
155+
- org.openrewrite.java.dependencies.ChangeDependency:
156+
oldGroupId: org.testcontainers
157+
oldArtifactId: cassandra
158+
newArtifactId: testcontainers-cassandra
159+
newVersion: "2.x"
160+
- org.openrewrite.java.dependencies.ChangeDependency:
161+
oldGroupId: org.testcontainers
162+
oldArtifactId: nginx
163+
newArtifactId: testcontainers-nginx
164+
newVersion: "2.x"
165+
- org.openrewrite.java.dependencies.ChangeDependency:
166+
oldGroupId: org.testcontainers
167+
oldArtifactId: kafka
168+
newArtifactId: testcontainers-kafka
169+
newVersion: "2.x"
170+
- org.openrewrite.java.dependencies.ChangeDependency:
171+
oldGroupId: org.testcontainers
172+
oldArtifactId: ldap
173+
newArtifactId: testcontainers-ldap
174+
newVersion: "2.x"
175+
- org.openrewrite.java.dependencies.ChangeDependency:
176+
oldGroupId: org.testcontainers
177+
oldArtifactId: junit-jupiter
178+
newArtifactId: testcontainers-junit-jupiter
179+
newVersion: "2.x"
180+
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
181+
groupId: org.testcontainers
182+
artifactId: "*"
183+
newVersion: "2.x"
184+
- org.openrewrite.java.ChangeType:
185+
oldFullyQualifiedTypeName: org.testcontainers.containers.CassandraContainer
186+
newFullyQualifiedTypeName: org.testcontainers.cassandra.CassandraContainer
187+
- org.openrewrite.java.ChangeType:
188+
oldFullyQualifiedTypeName: org.testcontainers.containers.MySQLContainer
189+
newFullyQualifiedTypeName: org.testcontainers.mysql.MySQLContainer
190+
- org.openrewrite.java.ChangeType:
191+
oldFullyQualifiedTypeName: org.testcontainers.containers.KafkaContainer
192+
newFullyQualifiedTypeName: org.testcontainers.kafka.KafkaContainer
193+
- org.openrewrite.java.ChangeType:
194+
oldFullyQualifiedTypeName: org.testcontainers.containers.localstack.LocalStackContainer
195+
newFullyQualifiedTypeName: org.testcontainers.localstack.LocalStackContainer

src/test/java/org/openrewrite/java/testing/testcontainers/ExplicitContainerImageTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,26 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.InMemoryExecutionContext;
2021
import org.openrewrite.java.JavaParser;
22+
import org.openrewrite.test.RecipeSpec;
2123
import org.openrewrite.test.RewriteTest;
2224

2325
import static org.openrewrite.java.Assertions.java;
2426

2527
class ExplicitContainerImageTest implements RewriteTest {
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec.parser(JavaParser.fromJavaVersion()
31+
.classpathFromResources(new InMemoryExecutionContext(), "testcontainers-1", "nginx"));
32+
}
33+
2634
@DocumentExample
2735
@Test
2836
void explicitContainerImage() {
2937
rewriteRun(
3038
spec -> spec
31-
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", null))
32-
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
39+
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", null)),
3340
//language=java
3441
java(
3542
"""
@@ -52,21 +59,20 @@ class Foo {
5259
void explicitContainerImageParsed() {
5360
rewriteRun(
5461
spec -> spec
55-
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", true))
56-
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
62+
.recipe(new ExplicitContainerImage("org.testcontainers.containers.NginxContainer", "nginx:1.9.4", true)),
5763
//language=java
5864
java(
5965
"""
6066
import org.testcontainers.containers.NginxContainer;
61-
67+
6268
class Foo {
6369
NginxContainer container = new NginxContainer();
6470
}
6571
""",
6672
"""
6773
import org.testcontainers.containers.NginxContainer;
6874
import org.testcontainers.utility.DockerImageName;
69-
75+
7076
class Foo {
7177
NginxContainer container = new NginxContainer(DockerImageName.parse("nginx:1.9.4"));
7278
}
@@ -78,9 +84,9 @@ class Foo {
7884
@Test
7985
void explicitContainerImages() {
8086
rewriteRun(
81-
spec -> spec
82-
.recipeFromResource("/META-INF/rewrite/testcontainers.yml", "org.openrewrite.java.testing.testcontainers.ExplicitContainerImages")
83-
.parser(JavaParser.fromJavaVersion().classpath("nginx")),
87+
spec -> spec.recipeFromResource(
88+
"/META-INF/rewrite/testcontainers.yml",
89+
"org.openrewrite.java.testing.testcontainers.ExplicitContainerImages"),
8490
//language=java
8591
java(
8692
"""

src/test/java/org/openrewrite/java/testing/testcontainers/TestcontainersBestPracticesTest.java

Lines changed: 124 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
* <p>
44
* Licensed under the Moderne Source Available License (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,8 +15,10 @@
1515
*/
1616
package org.openrewrite.java.testing.testcontainers;
1717

18+
import org.junit.jupiter.api.Nested;
1819
import org.junit.jupiter.api.Test;
1920
import org.openrewrite.DocumentExample;
21+
import org.openrewrite.InMemoryExecutionContext;
2022
import org.openrewrite.java.JavaParser;
2123
import org.openrewrite.test.RecipeSpec;
2224
import org.openrewrite.test.RewriteTest;
@@ -34,7 +36,13 @@ public void defaults(RecipeSpec spec) {
3436
spec
3537
.recipeFromResource("/META-INF/rewrite/testcontainers.yml",
3638
"org.openrewrite.java.testing.testcontainers.TestContainersBestPractices")
37-
.parser(JavaParser.fromJavaVersion().classpath("testcontainers"));
39+
.parser(JavaParser.fromJavaVersion().classpathFromResources(
40+
new InMemoryExecutionContext(),
41+
"testcontainers-2",
42+
"testcontainers-cassandra",
43+
"testcontainers-kafka",
44+
"testcontainers-localstack",
45+
"testcontainers-mysql"));
3846
}
3947

4048
@DocumentExample
@@ -63,53 +71,11 @@ String method(ContainerState container) {
6371
);
6472
}
6573

66-
@Test
67-
void dependencyUpdate() {
68-
rewriteRun(
69-
pomXml(
70-
//language=xml
71-
"""
72-
<project>
73-
<modelVersion>4.0.0</modelVersion>
74-
<groupId>org.openrewrite.example</groupId>
75-
<artifactId>testcontainers</artifactId>
76-
<version>1.0-SNAPSHOT</version>
77-
<dependencies>
78-
<dependency>
79-
<groupId>org.testcontainers</groupId>
80-
<artifactId>testcontainers</artifactId>
81-
<version>1.15.3</version>
82-
</dependency>
83-
</dependencies>
84-
</project>
85-
""",
86-
spec -> spec.after(after -> {
87-
Matcher matcher = Pattern.compile("<version>(1\\.21\\.\\d+)</version>").matcher(after);
88-
assertTrue(matcher.find());
89-
//language=xml
90-
return """
91-
<project>
92-
<modelVersion>4.0.0</modelVersion>
93-
<groupId>org.openrewrite.example</groupId>
94-
<artifactId>testcontainers</artifactId>
95-
<version>1.0-SNAPSHOT</version>
96-
<dependencies>
97-
<dependency>
98-
<groupId>org.testcontainers</groupId>
99-
<artifactId>testcontainers</artifactId>
100-
<version>%s</version>
101-
</dependency>
102-
</dependencies>
103-
</project>
104-
""".formatted(matcher.group(1));
105-
})
106-
)
107-
);
108-
}
109-
11074
@Test
11175
void composeContainer() {
11276
rewriteRun(
77+
spec -> spec.parser(JavaParser.fromJavaVersion()
78+
.classpathFromResources(new InMemoryExecutionContext(), "testcontainers-1")),
11379
java(
11480
"""
11581
import org.testcontainers.containers.DockerComposeContainer;
@@ -132,4 +98,116 @@ void foo(String bar) {
13298
)
13399
);
134100
}
101+
102+
@Nested
103+
class V2Migration {
104+
@Test
105+
void dependencyUpdate() {
106+
rewriteRun(
107+
pomXml(
108+
//language=xml
109+
"""
110+
<project>
111+
<modelVersion>4.0.0</modelVersion>
112+
<groupId>org.openrewrite.example</groupId>
113+
<artifactId>testcontainers</artifactId>
114+
<version>1.0-SNAPSHOT</version>
115+
<dependencies>
116+
<dependency>
117+
<groupId>org.testcontainers</groupId>
118+
<artifactId>testcontainers</artifactId>
119+
<version>1.21.3</version>
120+
</dependency>
121+
<dependency>
122+
<groupId>org.testcontainers</groupId>
123+
<artifactId>mysql</artifactId>
124+
<version>1.21.3</version>
125+
</dependency>
126+
<dependency>
127+
<groupId>org.testcontainers</groupId>
128+
<artifactId>nginx</artifactId>
129+
<version>1.21.3</version>
130+
</dependency>
131+
<dependency>
132+
<groupId>org.testcontainers</groupId>
133+
<artifactId>junit-jupiter</artifactId>
134+
<version>1.21.3</version>
135+
</dependency>
136+
</dependencies>
137+
</project>
138+
""",
139+
spec -> spec.after(after -> {
140+
Matcher matcher = Pattern.compile("<version>(2\\.\\d+\\.\\d+)</version>").matcher(after);
141+
assertTrue(matcher.find());
142+
String afterVersion = matcher.group(1);
143+
//language=xml
144+
return """
145+
<project>
146+
<modelVersion>4.0.0</modelVersion>
147+
<groupId>org.openrewrite.example</groupId>
148+
<artifactId>testcontainers</artifactId>
149+
<version>1.0-SNAPSHOT</version>
150+
<dependencies>
151+
<dependency>
152+
<groupId>org.testcontainers</groupId>
153+
<artifactId>testcontainers</artifactId>
154+
<version>%1$s</version>
155+
</dependency>
156+
<dependency>
157+
<groupId>org.testcontainers</groupId>
158+
<artifactId>testcontainers-mysql</artifactId>
159+
<version>%1$s</version>
160+
</dependency>
161+
<dependency>
162+
<groupId>org.testcontainers</groupId>
163+
<artifactId>testcontainers-nginx</artifactId>
164+
<version>%1$s</version>
165+
</dependency>
166+
<dependency>
167+
<groupId>org.testcontainers</groupId>
168+
<artifactId>testcontainers-junit-jupiter</artifactId>
169+
<version>%1$s</version>
170+
</dependency>
171+
</dependencies>
172+
</project>
173+
""".formatted(afterVersion);
174+
})
175+
)
176+
);
177+
}
178+
179+
@Test
180+
void changeTypes() {
181+
rewriteRun(
182+
java(
183+
"""
184+
import org.testcontainers.containers.CassandraContainer;
185+
import org.testcontainers.containers.KafkaContainer;
186+
import org.testcontainers.containers.localstack.LocalStackContainer;
187+
import org.testcontainers.containers.MySQLContainer;
188+
189+
class A {
190+
private CassandraContainer cassandra = null;
191+
private KafkaContainer kafka = null;
192+
private MySQLContainer mysql = null;
193+
private LocalStackContainer localstack = null;
194+
}
195+
""",
196+
"""
197+
import org.testcontainers.cassandra.CassandraContainer;
198+
import org.testcontainers.kafka.KafkaContainer;
199+
import org.testcontainers.localstack.LocalStackContainer;
200+
import org.testcontainers.mysql.MySQLContainer;
201+
202+
class A {
203+
private CassandraContainer cassandra = null;
204+
private KafkaContainer kafka = null;
205+
private MySQLContainer mysql = null;
206+
private LocalStackContainer localstack = null;
207+
}
208+
"""
209+
)
210+
);
211+
}
212+
}
135213
}
1010 KB
Binary file not shown.

0 commit comments

Comments
 (0)