diff --git a/src/main/java/file/FileRenameSnippet.java b/src/main/java/file/FileRenameSnippet.java new file mode 100644 index 00000000..b9d7e28c --- /dev/null +++ b/src/main/java/file/FileRenameSnippet.java @@ -0,0 +1,65 @@ +/* + * MIT License + * + * Copyright (c) 2017-2024 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package file; + +import java.io. File; + +/** + * FileRenameSnippet. + */ + +public class FileRenameSnippet { + + public static void fileRename(String oldFileName, String newFileName) { + // Get the old and new file names from the user + // String oldFileName = "old_file.txt"; + // String newFileName = "new_file.txt"; + + // Create File objects for the old and new files + File oldFile = new File(oldFileName); + File newFile = new File(newFileName); + + // Check if the old file exists + if (!oldFile.exists()) { + System.out .println("The old file " + oldFileName + " does not exist."); + return; + } + + // Check if the new file already exists + if (newFile.exists()) { + System.out.println("The new file " + newFileName + " already exists."); + return; + } + + // Rename the file + boolean renamed = oldFile.renameTo(newFile); + + if (renamed) { + System.out.println("File renamed successfully."); + } else { + System.out.println("Failed to rename the file."); + + } + } +} diff --git a/src/test/java/file/FileRenameSnippetTest.java b/src/test/java/file/FileRenameSnippetTest.java new file mode 100644 index 00000000..7f9bee87 --- /dev/null +++ b/src/test/java/file/FileRenameSnippetTest.java @@ -0,0 +1,115 @@ +/* + * MIT License + * + * Copyright (c) 2017-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package file; + + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.io.IOException; + +import static org.junit.jupiter.api.Assertions.*; + +/* + * Tests for 30 Seconds of Java code library + * + */ +class FileRenameSnippetTest { + + + + + @Test + void testFileRenameSuccess() { + // Create temporary files for testing + File oldFile = new File("old_file_test.txt"); + File newFile = new File("new_file_test.txt"); + + try { + // Create the old file + oldFile.createNewFile(); + + // Rename the file + FileRenameSnippet.fileRename(oldFile.getAbsolutePath(), newFile.getAbsolutePath()); + + // Verify the old file is deleted and the new file is created + assertTrue(newFile.exists()); + assertFalse(oldFile.exists()); + } catch (IOException e) { + fail("Failed to create temporary files: " + e.getMessage()); + } finally { + // Delete the temporary files + oldFile.delete(); + newFile.delete(); + } + } + + @Test + void testFileRenameOldFileDoesNotExist() { + // Create a new file for testing + File newFile = new File("new_file_test.txt"); + + try { + // Try to rename a non-existent old file + FileRenameSnippet.fileRename("non_existent_file.txt", newFile.getAbsolutePath()); + + // Verify the new file is not created + assertFalse(newFile.exists()); + } catch (Exception e) { + // Expecting an exception since the old file doesn't exist + assertTrue(e.getMessage().contains("The old file non_existent_file.txt does not exist.")); + } finally { + // Delete the temporary file + newFile.delete(); + } + } + + @Test + void testFileRenameNewFileAlreadyExists() { + // Create temporary files for testing + File oldFile = new File("old_file_test.txt"); + File newFile = new File("new_file_test.txt"); + + try { + // Create both files + oldFile.createNewFile(); + newFile.createNewFile(); + + // Try to rename the old file to an existing new file + FileRenameSnippet.fileRename(oldFile.getAbsolutePath(), newFile.getAbsolutePath()); + + // Verify the old file is not deleted and the new file is not overwritten + assertTrue(oldFile.exists()); + assertTrue(newFile.exists()); + } catch (IOException e) { + fail("Failed to create temporary files: " + e.getMessage()); + } finally { + // Delete the temporary files + oldFile.delete(); + newFile.delete(); + } + } + } + +