Skip to content

Commit

Permalink
Implement hudson.Util.getPastTimeString migration
Browse files Browse the repository at this point in the history
Issue #6
  • Loading branch information
sghill committed Jul 15, 2023
1 parent ace86b3 commit 724eac1
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/main/resources/META-INF/rewrite/hudson-migrations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.jenkins.migrate.hudson.UtilGetPastTimeStringToGetTimeSpanString
displayName: Replace `hudson.Util.getPastTimeString` with `getTimeSpanString`
description: >
`hudson.Util.getPastTimeString` has been [deprecated](https://github.com/jenkinsci/jenkins/pull/4174)
since the [2.204.1 LTS release](https://www.jenkins.io/changelog-stable/#v2.204.1) on 2019-12-18.
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: hudson.Util getPastTimeString(long)
newMethodName: getTimeSpanString
matchOverrides: false
ignoreDefinition: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.openrewrite.jenkins.migrate.hudson;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

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

class UtilGetPastTimeStringToTimeSpanStringTest implements RewriteTest {
@Language("java")
// language=java
private final String hudsonUtil = """
package hudson;
public class Util {
public static String getTimeSpanString(long duration) {
return "anything";
}
@Deprecated
public static String getPastTimeString(long duration) {
return getTimeSpanString(duration);
}
}
""".stripIndent();

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().dependsOn(hudsonUtil));
spec.recipeFromResource("/META-INF/rewrite/hudson-migrations.yml", "org.openrewrite.jenkins.migrate.hudson.UtilGetPastTimeStringToGetTimeSpanString");
}

@Test
void shouldReplaceFullyQualifiedMethodCall() {
// language=java
rewriteRun(java(
"""
package org.example;
class MyConsumer {
String format(long timestamp) {
return hudson.Util.getPastTimeString(timestamp);
}
}
""".stripIndent(),
"""
package org.example;
class MyConsumer {
String format(long timestamp) {
return hudson.Util.getTimeSpanString(timestamp);
}
}
""".stripIndent()
));
}

@Test
void shouldReplaceImportedMethodCall() {
// language=java
rewriteRun(java(
"""
package org.example;
import hudson.Util;
class MyConsumer {
String format(long timestamp) {
return Util.getPastTimeString(timestamp);
}
}
""".stripIndent(),
"""
package org.example;
import hudson.Util;
class MyConsumer {
String format(long timestamp) {
return Util.getTimeSpanString(timestamp);
}
}
""".stripIndent()
));
}

@Test
void shouldReplaceStaticImportedMethodCall() {
// language=java
rewriteRun(java(
"""
package org.example;
import static hudson.Util.getPastTimeString;
class MyConsumer {
String format(long timestamp) {
return getPastTimeString(timestamp);
}
}
""".stripIndent(),
"""
package org.example;
import static hudson.Util.getTimeSpanString;
class MyConsumer {
String format(long timestamp) {
return getTimeSpanString(timestamp);
}
}
""".stripIndent()
));
}
}

0 comments on commit 724eac1

Please sign in to comment.