Skip to content

Commit 276316d

Browse files
authored
Upgrade jakarta.annotation-api to 3.0.x in Jakarta EE 11 recipe (#1042)
* Upgrade jakarta.annotation-api to 3.0.x in Jakarta EE 11 recipe The UpdateJakartaPlatform11 recipe was missing the version upgrade for jakarta.annotation:jakarta.annotation-api to 3.0.x, which is the version specified by the Jakarta EE 11 platform BOM. Jakarta EE 10 already upgrades to 2.1.x via MigrationToJakarta10Apis. Also adds tests for JavaxAnnotationMigrationToJakartaAnnotation covering both the explicit dependency migration and the no-explicit-dependency scenarios. * Use .actual() on assertion chain in annotation migration test * Use .actual() on remaining assertion chain in annotation migration test
1 parent 2c285d5 commit 276316d

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

src/main/resources/META-INF/rewrite/jakarta-ee-11.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ recipeList:
3939
groupId: jakarta.platform
4040
artifactId: "*"
4141
newVersion: 11.0.x
42+
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
43+
groupId: jakarta.annotation
44+
artifactId: jakarta.annotation-api
45+
newVersion: 3.0.x
4246
- org.openrewrite.java.dependencies.UpgradeDependencyVersion:
4347
groupId: jakarta.servlet.jsp
4448
artifactId: jakarta.servlet.jsp-api
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license
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.migrate.jakarta;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.java.JavaParser;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.openrewrite.java.Assertions.mavenProject;
26+
import static org.openrewrite.java.Assertions.srcMainJava;
27+
import static org.openrewrite.java.Assertions.java;
28+
import static org.openrewrite.maven.Assertions.pomXml;
29+
30+
class JavaxAnnotationMigrationToJakartaAnnotationTest implements RewriteTest {
31+
32+
@Override
33+
public void defaults(RecipeSpec spec) {
34+
spec.recipeFromResource(
35+
"/META-INF/rewrite/jakarta-ee-9.yml",
36+
"org.openrewrite.java.migrate.jakarta.JavaxAnnotationMigrationToJakartaAnnotation");
37+
}
38+
39+
@DocumentExample
40+
@Test
41+
void migratesExplicitDependency() {
42+
rewriteRun(
43+
spec -> spec.parser(JavaParser.fromJavaVersion()
44+
.dependsOn("package javax.annotation; public @interface Nonnull {}")),
45+
mavenProject("my-project",
46+
srcMainJava(
47+
//language=java
48+
java(
49+
"""
50+
import javax.annotation.Nonnull;
51+
52+
class A {
53+
@Nonnull
54+
String name;
55+
}
56+
""",
57+
"""
58+
import jakarta.annotation.Nonnull;
59+
60+
class A {
61+
@Nonnull
62+
String name;
63+
}
64+
"""
65+
)
66+
),
67+
//language=xml
68+
pomXml(
69+
"""
70+
<project>
71+
<modelVersion>4.0.0</modelVersion>
72+
<groupId>com.example</groupId>
73+
<artifactId>demo</artifactId>
74+
<version>0.0.1-SNAPSHOT</version>
75+
<dependencies>
76+
<dependency>
77+
<groupId>javax.annotation</groupId>
78+
<artifactId>javax.annotation-api</artifactId>
79+
<version>1.3.2</version>
80+
</dependency>
81+
</dependencies>
82+
</project>
83+
""",
84+
spec -> spec.after(pom -> assertThat(pom)
85+
.contains("<groupId>jakarta.annotation</groupId>")
86+
.contains("<artifactId>jakarta.annotation-api</artifactId>")
87+
.containsPattern("<version>2\\.0\\.\\d+</version>")
88+
.actual())
89+
)
90+
)
91+
);
92+
}
93+
94+
@Test
95+
void addsDependencyWhenNoExplicitAnnotationDependency() {
96+
rewriteRun(
97+
spec -> spec.parser(JavaParser.fromJavaVersion()
98+
.dependsOn("package javax.annotation; public @interface Nonnull {}")),
99+
mavenProject("my-project",
100+
srcMainJava(
101+
//language=java
102+
java(
103+
"""
104+
import javax.annotation.Nonnull;
105+
106+
class A {
107+
@Nonnull
108+
String name;
109+
}
110+
""",
111+
"""
112+
import jakarta.annotation.Nonnull;
113+
114+
class A {
115+
@Nonnull
116+
String name;
117+
}
118+
"""
119+
)
120+
),
121+
//language=xml
122+
pomXml(
123+
"""
124+
<project>
125+
<modelVersion>4.0.0</modelVersion>
126+
<groupId>com.example</groupId>
127+
<artifactId>demo</artifactId>
128+
<version>0.0.1-SNAPSHOT</version>
129+
</project>
130+
""",
131+
spec -> spec.after(pom -> assertThat(pom)
132+
.containsPattern(
133+
"<groupId>jakarta\\.annotation</groupId>\\s*" +
134+
"<artifactId>jakarta\\.annotation-api</artifactId>\\s*" +
135+
"<version>2\\.0\\.\\d+</version>")
136+
.actual())
137+
)
138+
)
139+
);
140+
}
141+
}

0 commit comments

Comments
 (0)