-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plexus FileUtils refaster template recipes (#272)
* 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
Showing
4 changed files
with
278 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
generated/ | ||
generated |
77 changes: 77 additions & 0 deletions
77
src/main/java/org/openrewrite/java/migrate/plexus/PlexusFileUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
197 changes: 197 additions & 0 deletions
197
src/test/java/org/openrewrite/java/migrate/plexus/PlexusFileUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} | ||
} |