Skip to content

Commit 4f86913

Browse files
committed
Fix flaky JohnzonJavaxtoJakartaTest and use .reduce in var helper
The test's `2.1.\d+` regex matched `<johnzon.version>2.1.0</johnzon.version>` before reaching the jakarta.json-api dependency, so when jakarta.json-api was released as 2.1.3 the expected/actual diverged. Anchor the regex to the jakarta.json-api artifact. Also consolidate the `isReassigned` visitor to use `.reduce(...)` directly, matching the existing idiom in NullCheck and other recipes.
1 parent b58ac91 commit 4f86913

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

src/main/java/org/openrewrite/java/migrate/lang/var/DeclarationCheck.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,20 @@ public static boolean isReassigned(Cursor cursor, J.VariableDeclarations vd) {
254254
if (method == null) {
255255
return false;
256256
}
257-
AtomicBoolean reassigned = new AtomicBoolean(false);
258-
new JavaIsoVisitor<AtomicBoolean>() {
257+
return new JavaIsoVisitor<AtomicBoolean>() {
259258
@Override
260259
public J.Assignment visitAssignment(J.Assignment assignment, AtomicBoolean found) {
261-
if (!found.get() && assignment.getVariable() instanceof J.Identifier) {
262-
JavaType.Variable fieldType = ((J.Identifier) assignment.getVariable()).getFieldType();
263-
if (variableType.equals(fieldType)) {
264-
found.set(true);
265-
}
260+
if (found.get()) {
261+
return assignment;
262+
}
263+
if (assignment.getVariable() instanceof J.Identifier &&
264+
variableType.equals(((J.Identifier) assignment.getVariable()).getFieldType())) {
265+
found.set(true);
266+
return assignment;
266267
}
267268
return super.visitAssignment(assignment, found);
268269
}
269-
}.visit(method, reassigned);
270-
return reassigned.get();
270+
}.reduce(method, new AtomicBoolean(false)).get();
271271
}
272272

273273
public static J.VariableDeclarations transformToVar(J.VariableDeclarations vd) {

src/test/java/org/openrewrite/java/migrate/jakarta/JohnzonJavaxtoJakartaTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ void migrateJohnzonDependencies() {
6868
Matcher version = Pattern.compile("<johnzon.version>([0-9]+\\.[0-9]+\\.[0-9]+)</johnzon.version>")
6969
.matcher(actual);
7070

71-
Matcher jsonApiVersion = Pattern.compile("2.1.\\d+").matcher(actual);
71+
Matcher jsonApiVersion = Pattern.compile("(?s)<artifactId>jakarta\\.json-api</artifactId>\\s*<version>(2\\.1\\.\\d+)</version>")
72+
.matcher(actual);
7273
assertThat(jsonApiVersion.find()).describedAs("Expected jakarta.json-api 2.1.x version in %s", actual).isTrue();
7374

7475
assertThat(version.find()).isTrue();
@@ -94,7 +95,7 @@ void migrateJohnzonDependencies() {
9495
</dependency>
9596
</dependencies>
9697
</project>
97-
""".formatted(version.group(1), jsonApiVersion.group(0));
98+
""".formatted(version.group(1), jsonApiVersion.group(1));
9899
})
99100
),
100101
srcMainJava(

0 commit comments

Comments
 (0)