Skip to content

Commit 92e10e3

Browse files
UpdateSdkMan recipe should not allow a downgrade of Java version (#771)
* Adding test for UpdateSdkMan showing where it incorrectly downgrades the version if high enough already * Guarding against the new version being lower than the current version for `UpdateSdkMan` * Tweaking logic a bit to allow for specifying just the major version and a different distribution and still allowing it to switch distributions if it can find a higher version number than the current one. * Adding additional test cases to show it is still working for specific scenarios of providng a more generic version number that still scopes the current version number
1 parent e15dadc commit 92e10e3

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/main/java/org/openrewrite/java/migrate/UpdateSdkMan.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,17 @@ public Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
102102
.filter(candidate -> releaseComparator.isValid(newBasis, candidate))
103103
.max(releaseComparator)
104104
.orElse(null);
105-
if (idealCandidate != null) {
105+
if (idealCandidate != null && !isRequestedDowngrade(matcher.group(1) + dist, idealCandidate, dist)) {
106106
return plainText.withText(matcher.replaceFirst("java=" + idealCandidate));
107107
}
108108
}
109109
return sourceFile;
110110
}
111111

112+
private boolean isRequestedDowngrade(String currentVersionWithNewDist, String requestedVersionWithDist, String dist) {
113+
return new LatestRelease(dist).compare(null, currentVersionWithNewDist, requestedVersionWithDist) > 0;
114+
}
115+
112116
private List<String> readSdkmanJavaCandidates() {
113117
try (InputStream resourceAsStream = UpdateSdkMan.class.getResourceAsStream("/sdkman-java.csv");
114118
InputStreamReader inputStreamReader = new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8);

src/test/java/org/openrewrite/java/migrate/UpdateSdkManTest.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,62 @@ void zuluNonCrac() {
166166
)
167167
);
168168
}
169+
170+
@Test
171+
void upgradeIfNewVersionIsStillSameVersionBasisAndSameDistribution() {
172+
rewriteRun(
173+
spec -> spec.recipe(new UpdateSdkMan("21", null)),
174+
text(
175+
"""
176+
java=21.0.6-zulu
177+
""",
178+
"""
179+
java=21.0.7-zulu
180+
""",
181+
spec -> spec.path(".sdkmanrc")
182+
)
183+
);
184+
}
185+
186+
@Test
187+
void upgradeIfNewVersionIsStillSameVersionBasisAndDifferentDistribution() {
188+
rewriteRun(
189+
spec -> spec.recipe(new UpdateSdkMan("23", "amzn")),
190+
text(
191+
"""
192+
java=23.0.1-open
193+
""",
194+
"""
195+
java=23.0.2-amzn
196+
""",
197+
spec -> spec.path(".sdkmanrc")
198+
)
199+
);
200+
}
201+
202+
@Test
203+
void doNotDowngradeVersionIfAlreadyHighEnoughSameDistribution() {
204+
rewriteRun(
205+
spec -> spec.recipe(new UpdateSdkMan("17", null)),
206+
text(
207+
"""
208+
java=24-librca
209+
""",
210+
spec -> spec.path(".sdkmanrc")
211+
)
212+
);
213+
}
214+
215+
@Test
216+
void doNotDowngradeVersionIfAlreadyHighEnoughDifferentDistribution() {
217+
rewriteRun(
218+
spec -> spec.recipe(new UpdateSdkMan("23", "zulu")),
219+
text(
220+
"""
221+
java=24-amzn
222+
""",
223+
spec -> spec.path(".sdkmanrc")
224+
)
225+
);
226+
}
169227
}

0 commit comments

Comments
 (0)