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 16, 2023
1 parent 3a434c8 commit f43aaa6
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
29 changes: 29 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,29 @@
#
# Copyright 2023 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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()
));
}
}

0 comments on commit f43aaa6

Please sign in to comment.