diff --git a/build.gradle.kts b/build.gradle.kts index a8545e4eb0..c1c3516917 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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") @@ -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") diff --git a/src/main/.gitignore b/src/main/.gitignore index 9ab870da89..86d4c2dd38 100644 --- a/src/main/.gitignore +++ b/src/main/.gitignore @@ -1 +1 @@ -generated/ +generated diff --git a/src/main/java/org/openrewrite/java/migrate/plexus/PlexusFileUtils.java b/src/main/java/org/openrewrite/java/migrate/plexus/PlexusFileUtils.java new file mode 100644 index 0000000000..6aef02bae2 --- /dev/null +++ b/src/main/java/org/openrewrite/java/migrate/plexus/PlexusFileUtils.java @@ -0,0 +1,77 @@ +/* + * 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.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); + } + } + +} diff --git a/src/test/java/org/openrewrite/java/migrate/plexus/PlexusFileUtilsTest.java b/src/test/java/org/openrewrite/java/migrate/plexus/PlexusFileUtilsTest.java new file mode 100644 index 0000000000..42c0328d2f --- /dev/null +++ b/src/test/java/org/openrewrite/java/migrate/plexus/PlexusFileUtilsTest.java @@ -0,0 +1,197 @@ +/* + * 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.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); + } + } + """ + ) + ); + } + } +} \ No newline at end of file