|
| 1 | +package org.baeldung.java.io; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.nio.file.FileSystemException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.nio.file.Paths; |
| 9 | + |
| 10 | +import org.apache.commons.io.FileUtils; |
| 11 | +import org.junit.Test; |
| 12 | + |
| 13 | +public class JavaFileIntegrationTest { |
| 14 | + |
| 15 | + // create a file |
| 16 | + |
| 17 | + @Test |
| 18 | + public void givenUsingJDK6_whenCreatingFile_thenCorrect() throws IOException { |
| 19 | + final File newFile = new File("src/test/resources/newFile_jdk6.txt"); |
| 20 | + newFile.createNewFile(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void givenUsingJDK7nio2_whenCreatingFile_thenCorrect() throws IOException { |
| 25 | + final Path newFilePath = Paths.get("src/test/resources/newFile_jdk7.txt"); |
| 26 | + Files.createFile(newFilePath); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenUsingApache_whenCreatingFile_thenCorrect() throws IOException { |
| 31 | + FileUtils.touch(new File("src/test/resources/newFile_commonsio.txt")); |
| 32 | + } |
| 33 | + |
| 34 | + // move a file |
| 35 | + |
| 36 | + @Test |
| 37 | + public void givenUsingJDK6_whenMovingFile_thenCorrect() throws IOException { |
| 38 | + final File fileToMove = new File("src/test/resources/toMoveFile_jdk6.txt"); |
| 39 | + fileToMove.exists(); |
| 40 | + final File destDir = new File("src/test/resources/"); |
| 41 | + destDir.mkdir(); |
| 42 | + |
| 43 | + final boolean isMoved = fileToMove.renameTo(new File("src/test/resources/movedFile_jdk6.txt")); |
| 44 | + if (!isMoved) { |
| 45 | + throw new FileSystemException("src/test/resources/movedFile_jdk6.txt"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenUsingJDK7Nio2_whenMovingFile_thenCorrect() throws IOException { |
| 51 | + final Path fileToMovePath = Files.createFile(Paths.get("src/test/resources/fileToMove.txt")); |
| 52 | + final Path dirPath = Paths.get("src/test/resources/"); |
| 53 | + final Path targetPath = Files.createDirectory(dirPath); |
| 54 | + |
| 55 | + Files.move(fileToMovePath, targetPath.resolve(fileToMovePath.getFileName())); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void givenUsingGuava_whenMovingFile_thenCorrect() throws IOException { |
| 60 | + final File fileToMove = new File("src/test/resources/fileToMove.txt"); |
| 61 | + fileToMove.createNewFile(); |
| 62 | + final File destDir = new File("src/test/resources/"); |
| 63 | + final File targetFile = new File(destDir, fileToMove.getName()); |
| 64 | + com.google.common.io.Files.createParentDirs(targetFile); |
| 65 | + com.google.common.io.Files.move(fileToMove, targetFile); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void givenUsingApache_whenMovingFile_thenCorrect() throws IOException { |
| 70 | + FileUtils.moveFile(FileUtils.getFile("src/test/resources/fileToMove.txt"), FileUtils.getFile("src/test/resources/fileMoved.txt")); |
| 71 | + } |
| 72 | + |
| 73 | + // rename a file |
| 74 | + |
| 75 | +} |
0 commit comments