Skip to content

Commit

Permalink
Plexus FileUtils refaster template recipes (#272)
Browse files Browse the repository at this point in the history
* Plexus FileUtils refaster template recipes

* Use correct variable name in test

* Add method that should not be migrated

* Add a minimal fully qualified test to aid debugging

* Ignore generated as symlink too

* Fix minimal test

* Split out different ways of how imports should be handled

* Add recipe for `FileUtils.fileExists(String)`

* Unqualify before templates to fix recipes for now

* Convert `FileUtils.getFile`
  • Loading branch information
timtebeek authored Aug 15, 2023
1 parent 1e7e95a commit 2a8527b
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 2 deletions.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ dependencies {
implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion")
implementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

implementation("commons-io:commons-io:2.+")
implementation("org.codehaus.plexus:plexus-utils:3.+")

runtimeOnly("org.openrewrite:rewrite-java-8")
runtimeOnly("org.openrewrite:rewrite-java-11")
runtimeOnly("org.openrewrite:rewrite-java-17")
Expand All @@ -47,7 +50,6 @@ dependencies {

testImplementation("commons-codec:commons-codec:1.+")
testImplementation("org.apache.commons:commons-lang3:3.+")
testImplementation("org.codehaus.plexus:plexus-utils:3.+")
testImplementation("org.apache.maven.shared:maven-shared-utils:3.+")

testRuntimeOnly("com.fasterxml.jackson.datatype:jackson-datatype-jsr353")
Expand Down
2 changes: 1 addition & 1 deletion src/main/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
generated/
generated
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.java.migrate.plexus;

import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;

class PlexusFileUtils {

// https://github.com/codehaus-plexus/plexus-utils/blob/master/src/main/java/org/codehaus/plexus/util/StringUtils.java

static class DeleteDirectoryFile {
@BeforeTemplate
void before(File dir) throws IOException {
FileUtils.deleteDirectory(dir);
}

@AfterTemplate
void after(File dir) throws IOException {
org.apache.commons.io.FileUtils.deleteDirectory(dir);
}
}

static class DeleteDirectoryString {
@BeforeTemplate
void before(String dir) throws IOException {
FileUtils.deleteDirectory(dir);
}

@AfterTemplate
void after(String dir) throws IOException {
org.apache.commons.io.FileUtils.deleteDirectory(new File(dir));
}
}

static class FileExistsString {
@BeforeTemplate
boolean before(String fileName) throws IOException {
return FileUtils.fileExists(fileName);
}

@AfterTemplate
boolean after(String fileName) throws IOException {
return new File(fileName).exists();
}
}

static class GetFile {
@BeforeTemplate
File before(String fileName) throws IOException {
return FileUtils.getFile(fileName);
}

@AfterTemplate
File after(String fileName) throws IOException {
return new File(fileName);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* 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.java.migrate.plexus;

import org.junit.jupiter.api.Nested;
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 PlexusFileUtilsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion().classpath("commons-io", "plexus-utils"))
.recipe(new PlexusFileUtilsRecipes());
}

@Nested
class DeleteDirectory {

@Test
void deleteDirectoryFullyQualified() {
rewriteRun(
//language=java
java(
"""
import java.io.File;
class Test {
void test() throws Exception {
org.codehaus.plexus.util.FileUtils.deleteDirectory("test");
File file = new File("test");
org.codehaus.plexus.util.FileUtils.deleteDirectory(file);
}
}
""",
"""
import org.apache.commons.io.FileUtils;
import java.io.File;
class Test {
void test() throws Exception {
FileUtils.deleteDirectory(new File("test"));
File file = new File("test");
FileUtils.deleteDirectory(file);
}
}
"""
)
);
}

@Test
void deleteDirectorySimpleImport() {
rewriteRun(
//language=java
java(
"""
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
class Test {
void test() throws Exception {
FileUtils.deleteDirectory("test");
}
}
""",
"""
import org.apache.commons.io.FileUtils;
import java.io.File;
class Test {
void test() throws Exception {
FileUtils.deleteDirectory(new File("test"));
}
}
"""
)
);
}

@Test
void deleteDirectoryRetainedImport() {
rewriteRun(
//language=java
java(
"""
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
class Test {
void test() throws Exception {
FileUtils.deleteDirectory("test");
FileUtils.dirname("/foo/bar");
}
}
""",
"""
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
class Test {
void test() throws Exception {
org.apache.commons.io.FileUtils.deleteDirectory(new File("test"));
FileUtils.dirname("/foo/bar");
}
}
"""
)
);
}
}

@Nested
class FileExists {
@Test
void fileExists() {
rewriteRun(
//language=java
java(
"""
import org.codehaus.plexus.util.FileUtils;
class Test {
boolean test(String fileName) throws Exception {
return FileUtils.fileExists(fileName);
}
}
""",
"""
import java.io.File;
class Test {
boolean test(String fileName) throws Exception {
return new File(fileName).exists();
}
}
"""
)
);
}
}

@Nested
class GetFile {
@Test
void getFile() {
rewriteRun(
//language=java
java(
"""
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
class Test {
File test(String fileName) throws Exception {
return FileUtils.getFile(fileName);
}
}
""",
"""
import java.io.File;
class Test {
File test(String fileName) throws Exception {
return new File(fileName);
}
}
"""
)
);
}
}
}

0 comments on commit 2a8527b

Please sign in to comment.