Skip to content

Commit 5a4ba98

Browse files
committed
Add few tests
1 parent 6adf1c2 commit 5a4ba98

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeVisitor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ private J migrateMethodCall(MethodCall original, MethodCall updated) {
243243
return original;
244244
}
245245

246-
private MethodCall migrateNonJodaMethod(MethodCall original, MethodCall updated) {
246+
private J.MethodInvocation migrateNonJodaMethod(J.MethodInvocation original, J.MethodInvocation updated) {
247247
if (safeMigration && !acc.getSafeMethodMap().getOrDefault(updated.getMethodType(), false)) {
248248
return original;
249249
}
@@ -252,7 +252,8 @@ private MethodCall migrateNonJodaMethod(MethodCall original, MethodCall updated)
252252
if (updatedReturnType == null) {
253253
return original; // unhandled case
254254
}
255-
return updated.withMethodType(updated.getMethodType().withReturnType(updatedReturnType));
255+
return updated.withMethodType(updated.getMethodType().withReturnType(updatedReturnType))
256+
.withName(updated.getName().withType(updatedReturnType));
256257
}
257258

258259
private boolean hasJodaType(List<Expression> exprs) {

src/test/java/org/openrewrite/java/migrate/joda/JodaTimeRecipeTest.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,105 @@ public void bar(ZonedDateTime dt) {
280280
);
281281
}
282282

283+
@Test
284+
void migrateMethodWithSafeReturnExpression() {
285+
//language=java
286+
rewriteRun(
287+
java(
288+
"""
289+
import org.joda.time.DateTime;
290+
import org.joda.time.Interval;
291+
292+
class A {
293+
public DateTime foo(DateTime dt) {
294+
Interval interval = new Interval(dt, dt.plusDays(1));
295+
return interval.getEnd();
296+
}
297+
298+
private static class Bar {
299+
public void bar(DateTime dt) {
300+
DateTime d = foo(dt);
301+
System.out.println(d.getMillis());
302+
}
303+
}
304+
}
305+
""",
306+
"""
307+
import org.threeten.extra.Interval;
308+
309+
import java.time.ZoneId;
310+
import java.time.ZonedDateTime;
311+
312+
class A {
313+
public ZonedDateTime foo(ZonedDateTime dt) {
314+
Interval interval = Interval.of(dt.toInstant(), dt.plusDays(1).toInstant());
315+
return interval.getEnd().atZone(ZoneId.systemDefault());
316+
}
317+
318+
private static class Bar {
319+
public void bar(ZonedDateTime dt) {
320+
ZonedDateTime d = foo(dt);
321+
System.out.println(d.toInstant().toEpochMilli());
322+
}
323+
}
324+
}
325+
"""
326+
)
327+
);
328+
}
329+
330+
@Test
331+
void migrateMethodWithSafeReturnExpressionAndUnsafeParam() {
332+
//language=java
333+
rewriteRun(
334+
java(
335+
"""
336+
import org.joda.time.DateTime;
337+
import org.joda.time.Interval;
338+
339+
class A {
340+
public DateTime foo(DateTime dt) {
341+
DateTime d = dt.toDateMidnight();
342+
DateTime d2 = DateTime.now();
343+
Interval interval = new Interval(d2, d2.plusDays(1));
344+
return interval.getEnd();
345+
}
346+
347+
private static class Bar {
348+
public void bar() {
349+
DateTime d = foo(new DateTime());
350+
System.out.println(d.getMillis());
351+
}
352+
}
353+
}
354+
""",
355+
"""
356+
import org.joda.time.DateTime;
357+
import org.threeten.extra.Interval;
358+
359+
import java.time.ZoneId;
360+
import java.time.ZonedDateTime;
361+
362+
class A {
363+
public ZonedDateTime foo(DateTime dt) {
364+
DateTime d = dt.toDateMidnight();
365+
ZonedDateTime d2 = ZonedDateTime.now();
366+
Interval interval = Interval.of(d2.toInstant(), d2.plusDays(1).toInstant());
367+
return interval.getEnd().atZone(ZoneId.systemDefault());
368+
}
369+
370+
private static class Bar {
371+
public void bar() {
372+
ZonedDateTime d = foo(new DateTime());
373+
System.out.println(d.toInstant().toEpochMilli());
374+
}
375+
}
376+
}
377+
"""
378+
)
379+
);
380+
}
381+
283382
@Test
284383
void doNotMigrateUnsafeMethodParam() {
285384
//language=java

0 commit comments

Comments
 (0)