Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hacktober fest raje pr1 #226

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/main/java/file/FileRenameSnippet.java
Original file line number Diff line number Diff line change
@@ -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.");

}
}
}
115 changes: 115 additions & 0 deletions src/test/java/file/FileRenameSnippetTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
}


Loading