Skip to content

Commit

Permalink
Snippet for File Rename
Browse files Browse the repository at this point in the history
File Rename Snippet

Code Snippet to Rename a File

Test class for Code Snippet to Rename a File
  • Loading branch information
rajeshsgr committed Oct 14, 2024
1 parent 19216b0 commit c443228
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/main/java/file/FileRenameSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
* FileRenameSnippet.
*/

public class FileRenameSnippet {

public static void main(String[] args) {
// 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.");

}
}
}
114 changes: 114 additions & 0 deletions src/test/java/file/FileRenameSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.AfterEach;
import org.junit.jupiter.api.BeforeEach;
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 {

private File oldFile;
private File newFile;

@BeforeEach
void setUp() throws IOException {
// Create old file for testing
oldFile = new File("old_file.txt");
newFile = new File("new_file.txt");

// Ensure both files do not exist before each test
if (oldFile.exists()) {
oldFile.delete();
}
if (newFile.exists()) {
newFile.delete();
}

// Create the old file
assertTrue(oldFile.createNewFile(), "Failed to create the old file.");
}

@AfterEach
void tearDown() {
// Clean up the files after each test
if (oldFile.exists()) {
oldFile.delete();
}
if (newFile.exists()) {
newFile.delete();
}
}

@Test
void testFileRenameSuccess() {
// Test renaming when the old file exists and the new file does not exist
assertFalse(newFile.exists(), "New file should not exist before rename.");
assertTrue(oldFile.exists(), "Old file should exist before rename.");

// Attempt to rename the file
boolean renamed = oldFile.renameTo(newFile);

// Assert that the file was renamed successfully
assertTrue(renamed, "File should be renamed successfully.");
assertFalse(oldFile.exists(), "Old file should no longer exist.");
assertTrue(newFile.exists(), "New file should exist after renaming.");
}

@Test
void testOldFileDoesNotExist() {
// Delete the old file before renaming
assertTrue(oldFile.delete(), "Failed to delete the old file.");

// Attempt to rename, which should fail
boolean renamed = oldFile.renameTo(newFile);

// Assert that renaming fails when old file does not exist
assertFalse(renamed, "Renaming should fail because the old file does not exist.");
assertFalse(newFile.exists(), "New file should not exist after failed rename.");
}

@Test
void testNewFileAlreadyExists() throws IOException {
// Create the new file before attempting to rename
assertTrue(newFile.createNewFile(), "Failed to create the new file.");

// Attempt to rename, which should fail
boolean renamed = oldFile.renameTo(newFile);

// Assert that renaming fails because the new file already exists
assertFalse(renamed, "Renaming should fail because the new file already exists.");
assertTrue(oldFile.exists(), "Old file should still exist.");
assertTrue(newFile.exists(), "New file should still exist.");
}
}

0 comments on commit c443228

Please sign in to comment.