Fix JavaFileUnitTest
This commit is contained in:
parent
9bf925cfdf
commit
a7e2e2d6b2
|
@ -1,7 +1,9 @@
|
||||||
package org.baeldung.java.io;
|
package org.baeldung.java.io;
|
||||||
|
|
||||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
import org.apache.commons.io.FileUtils;
|
||||||
import static org.junit.Assert.assertTrue;
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -9,17 +11,29 @@ import java.nio.file.FileSystemException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||||
import org.junit.Test;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
public class JavaFileUnitTest {
|
public class JavaFileUnitTest {
|
||||||
|
|
||||||
// create a file
|
private static final String TEMP_DIR = "src/test/resources/temp" + UUID.randomUUID().toString();
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void setup() throws IOException {
|
||||||
|
Files.createDirectory(Paths.get(TEMP_DIR));
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterClass
|
||||||
|
public static void cleanup() throws IOException {
|
||||||
|
FileUtils.deleteDirectory(new File(TEMP_DIR));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK6_whenCreatingFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK6_whenCreatingFile_thenCorrect() throws IOException {
|
||||||
final File newFile = new File("src/test/resources/newFile_jdk6.txt");
|
final File newFile = new File(TEMP_DIR + "/newFile_jdk6.txt");
|
||||||
final boolean success = newFile.createNewFile();
|
final boolean success = newFile.createNewFile();
|
||||||
|
|
||||||
assertTrue(success);
|
assertTrue(success);
|
||||||
|
@ -27,48 +41,48 @@ public class JavaFileUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK7nio2_whenCreatingFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK7nio2_whenCreatingFile_thenCorrect() throws IOException {
|
||||||
final Path newFilePath = Paths.get("src/test/resources/newFile_jdk7.txt");
|
final Path newFilePath = Paths.get(TEMP_DIR + "/newFile_jdk7.txt");
|
||||||
Files.createFile(newFilePath);
|
Files.createFile(newFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOException {
|
public final void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOException {
|
||||||
FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt"));
|
FileUtils.touch(new File(TEMP_DIR + "/newFile_commonsio.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
|
public final void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
|
||||||
com.google.common.io.Files.touch(new File("src/test/resources/newFile_guava.txt"));
|
com.google.common.io.Files.touch(new File(TEMP_DIR + "/newFile_guava.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
// move a file
|
// move a file
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException {
|
||||||
final File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt");
|
final File fileToMove = new File(TEMP_DIR + "/toMoveFile_jdk6.txt");
|
||||||
fileToMove.createNewFile();// .exists();
|
fileToMove.createNewFile();// .exists();
|
||||||
final File destDir = new File("src/test/resources/");
|
final File destDir = new File(TEMP_DIR + "/");
|
||||||
destDir.mkdir();
|
destDir.mkdir();
|
||||||
|
|
||||||
final boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt"));
|
final boolean isMoved = fileToMove.renameTo(new File(TEMP_DIR + "/movedFile_jdk6.txt"));
|
||||||
if (!isMoved) {
|
if (!isMoved) {
|
||||||
throw new FileSystemException("src/test/resources/movedFile_jdk6.txt");
|
throw new FileSystemException(TEMP_DIR + "/movedFile_jdk6.txt");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException {
|
||||||
final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/" + randomAlphabetic(5) + ".txt"));
|
final Path fileToMovePath = Files.createFile(Paths.get(TEMP_DIR + "/" + randomAlphabetic(5) + ".txt"));
|
||||||
final Path targetPath = Paths.get("src/main/resources/");
|
final Path targetPath = Paths.get(TEMP_DIR + "/");
|
||||||
|
|
||||||
Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
|
Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingGuava_whenMovingFile_thenCorrect() throws IOException {
|
public final void givenUsingGuava_whenMovingFile_thenCorrect() throws IOException {
|
||||||
final File fileToMove = new File("src/test/resources/fileToMove.txt");
|
final File fileToMove = new File(TEMP_DIR + "/fileToMove.txt");
|
||||||
fileToMove.createNewFile();
|
fileToMove.createNewFile();
|
||||||
final File destDir = new File("src/main/resources/");
|
final File destDir = new File(TEMP_DIR + "/temp");
|
||||||
final File targetFile = new File(destDir, fileToMove.getName());
|
final File targetFile = new File(destDir, fileToMove.getName());
|
||||||
com.google.common.io.Files.createParentDirs(targetFile);
|
com.google.common.io.Files.createParentDirs(targetFile);
|
||||||
com.google.common.io.Files.move(fileToMove, targetFile);
|
com.google.common.io.Files.move(fileToMove, targetFile);
|
||||||
|
@ -76,23 +90,24 @@ public class JavaFileUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
|
public final void givenUsingApache_whenMovingFile_thenCorrect() throws IOException {
|
||||||
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
|
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
|
||||||
FileUtils.moveFile(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/test/resources/fileMoved_apache2.txt"));
|
FileUtils.moveFile(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/fileMoved_apache2.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
|
public final void givenUsingApache_whenMovingFileApproach2_thenCorrect() throws IOException {
|
||||||
FileUtils.touch(new File("src/test/resources/fileToMove_apache.txt"));
|
FileUtils.touch(new File(TEMP_DIR + "/fileToMove_apache.txt"));
|
||||||
FileUtils.moveFileToDirectory(FileUtils.getFile("src/test/resources/fileToMove_apache.txt"), FileUtils.getFile("src/main/resources/"), true);
|
Files.createDirectory(Paths.get(TEMP_DIR + "/temp"));
|
||||||
|
FileUtils.moveFileToDirectory(FileUtils.getFile(TEMP_DIR + "/fileToMove_apache.txt"), FileUtils.getFile(TEMP_DIR + "/temp"), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete a file
|
// delete a file
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK6_whenDeletingAFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK6_whenDeletingAFile_thenCorrect() throws IOException {
|
||||||
new File("src/test/resources/fileToDelete_jdk6.txt").createNewFile();
|
new File(TEMP_DIR + "/fileToDelete_jdk6.txt").createNewFile();
|
||||||
|
|
||||||
final File fileToDelete = new File("src/test/resources/fileToDelete_jdk6.txt");
|
final File fileToDelete = new File(TEMP_DIR + "/fileToDelete_jdk6.txt");
|
||||||
final boolean success = fileToDelete.delete();
|
final boolean success = fileToDelete.delete();
|
||||||
|
|
||||||
assertTrue(success);
|
assertTrue(success);
|
||||||
|
@ -100,17 +115,17 @@ public class JavaFileUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingJDK7nio2_whenDeletingAFile_thenCorrect() throws IOException {
|
public final void givenUsingJDK7nio2_whenDeletingAFile_thenCorrect() throws IOException {
|
||||||
Files.createFile(Paths.get("src/test/resources/fileToDelete_jdk7.txt"));
|
Files.createFile(Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt"));
|
||||||
|
|
||||||
final Path fileToDeletePath = Paths.get("src/test/resources/fileToDelete_jdk7.txt");
|
final Path fileToDeletePath = Paths.get(TEMP_DIR + "/fileToDelete_jdk7.txt");
|
||||||
Files.delete(fileToDeletePath);
|
Files.delete(fileToDeletePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenUsingCommonsIo_whenDeletingAFileV1_thenCorrect() throws IOException {
|
public final void givenUsingCommonsIo_whenDeletingAFileV1_thenCorrect() throws IOException {
|
||||||
FileUtils.touch(new File("src/test/resources/fileToDelete_commonsIo.txt"));
|
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete_commonsIo.txt"));
|
||||||
|
|
||||||
final File fileToDelete = FileUtils.getFile("src/test/resources/fileToDelete_commonsIo.txt");
|
final File fileToDelete = FileUtils.getFile(TEMP_DIR + "/fileToDelete_commonsIo.txt");
|
||||||
final boolean success = FileUtils.deleteQuietly(fileToDelete);
|
final boolean success = FileUtils.deleteQuietly(fileToDelete);
|
||||||
|
|
||||||
assertTrue(success);
|
assertTrue(success);
|
||||||
|
@ -118,9 +133,9 @@ public class JavaFileUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUsingCommonsIo_whenDeletingAFileV2_thenCorrect() throws IOException {
|
public void givenUsingCommonsIo_whenDeletingAFileV2_thenCorrect() throws IOException {
|
||||||
FileUtils.touch(new File("src/test/resources/fileToDelete.txt"));
|
FileUtils.touch(new File(TEMP_DIR + "/fileToDelete.txt"));
|
||||||
|
|
||||||
FileUtils.forceDelete(FileUtils.getFile("src/test/resources/fileToDelete.txt"));
|
FileUtils.forceDelete(FileUtils.getFile(TEMP_DIR + "/fileToDelete.txt"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue