diff --git a/src/main/resources/META-INF/rewrite/hudson-migrations.yml b/src/main/resources/META-INF/rewrite/hudson-migrations.yml new file mode 100644 index 0000000..0f535f4 --- /dev/null +++ b/src/main/resources/META-INF/rewrite/hudson-migrations.yml @@ -0,0 +1,29 @@ +# +# Copyright 2023 the original author or authors. +#
+# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +#
+# https://www.apache.org/licenses/LICENSE-2.0 +#
+# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +--- +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 diff --git a/src/test/java/org/openrewrite/jenkins/migrate/hudson/UtilGetPastTimeStringToTimeSpanStringTest.java b/src/test/java/org/openrewrite/jenkins/migrate/hudson/UtilGetPastTimeStringToTimeSpanStringTest.java new file mode 100644 index 0000000..578ef4b --- /dev/null +++ b/src/test/java/org/openrewrite/jenkins/migrate/hudson/UtilGetPastTimeStringToTimeSpanStringTest.java @@ -0,0 +1,132 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * https://www.apache.org/licenses/LICENSE-2.0 + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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() + )); + } +}