Merge branch 'master' into pr/850-yasser-generics
This commit is contained in:
commit
1f0065299f
@ -1,64 +1,72 @@
|
|||||||
package com.baeldung.java.nio2;
|
package com.baeldung.java.nio2;
|
||||||
|
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.junit.AfterClass;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.DirectoryNotEmptyException;
|
|
||||||
import java.nio.file.FileAlreadyExistsException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.NoSuchFileException;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class FileTest {
|
public class FileTest {
|
||||||
private static final String HOME = System.getProperty("user.home");
|
private static final String TEMP_DIR = String.format("%s/temp%s", System.getProperty("user.home"), 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));
|
||||||
|
}
|
||||||
|
|
||||||
// checking file or dir
|
// checking file or dir
|
||||||
@Test
|
@Test
|
||||||
public void givenExistentPath_whenConfirmsFileExists_thenCorrect() {
|
public void givenExistentPath_whenConfirmsFileExists_thenCorrect() {
|
||||||
Path p = Paths.get(HOME);
|
Path p = Paths.get(TEMP_DIR);
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() {
|
public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() {
|
||||||
Path p = Paths.get(HOME + "/inexistent_file.txt");
|
Path p = Paths.get(TEMP_DIR + "/inexistent_file.txt");
|
||||||
assertTrue(Files.notExists(p));
|
assertTrue(Files.notExists(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDirPath_whenConfirmsNotRegularFile_thenCorrect() {
|
public void givenDirPath_whenConfirmsNotRegularFile_thenCorrect() {
|
||||||
Path p = Paths.get(HOME);
|
Path p = Paths.get(TEMP_DIR);
|
||||||
assertFalse(Files.isRegularFile(p));
|
assertFalse(Files.isRegularFile(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() {
|
public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() {
|
||||||
Path p = Paths.get(HOME);
|
Path p = Paths.get(TEMP_DIR);
|
||||||
assertTrue(Files.isReadable(p));
|
assertTrue(Files.isReadable(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() {
|
public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() {
|
||||||
Path p = Paths.get(HOME);
|
Path p = Paths.get(System.getProperty("user.home"));
|
||||||
assertTrue(Files.isWritable(p));
|
assertTrue(Files.isWritable(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() {
|
public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() {
|
||||||
Path p = Paths.get(HOME);
|
Path p = Paths.get(System.getProperty("user.home"));
|
||||||
assertTrue(Files.isExecutable(p));
|
assertTrue(Files.isExecutable(p));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException {
|
public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException {
|
||||||
Path p1 = Paths.get(HOME);
|
Path p1 = Paths.get(TEMP_DIR);
|
||||||
Path p2 = Paths.get(HOME);
|
Path p2 = Paths.get(TEMP_DIR);
|
||||||
assertTrue(Files.isSameFile(p1, p2));
|
assertTrue(Files.isSameFile(p1, p2));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +75,7 @@ public class FileTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
|
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
|
||||||
String fileName = "myfile_" + UUID.randomUUID().toString() + ".txt";
|
String fileName = "myfile_" + UUID.randomUUID().toString() + ".txt";
|
||||||
Path p = Paths.get(HOME + "/" + fileName);
|
Path p = Paths.get(TEMP_DIR + "/" + fileName);
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.createFile(p);
|
Files.createFile(p);
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
@ -77,7 +85,7 @@ public class FileTest {
|
|||||||
@Test
|
@Test
|
||||||
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
|
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
|
||||||
String dirName = "myDir_" + UUID.randomUUID().toString();
|
String dirName = "myDir_" + UUID.randomUUID().toString();
|
||||||
Path p = Paths.get(HOME + "/" + dirName);
|
Path p = Paths.get(TEMP_DIR + "/" + dirName);
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.createDirectory(p);
|
Files.createDirectory(p);
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
@ -89,7 +97,7 @@ public class FileTest {
|
|||||||
@Test(expected = NoSuchFileException.class)
|
@Test(expected = NoSuchFileException.class)
|
||||||
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
|
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() throws IOException {
|
||||||
String dirName = "myDir_" + UUID.randomUUID().toString() + "/subdir";
|
String dirName = "myDir_" + UUID.randomUUID().toString() + "/subdir";
|
||||||
Path p = Paths.get(HOME + "/" + dirName);
|
Path p = Paths.get(TEMP_DIR + "/" + dirName);
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.createDirectory(p);
|
Files.createDirectory(p);
|
||||||
|
|
||||||
@ -97,7 +105,7 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
|
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
|
||||||
Path dir = Paths.get(HOME + "/myDir_" + UUID.randomUUID().toString());
|
Path dir = Paths.get(TEMP_DIR + "/myDir_" + UUID.randomUUID().toString());
|
||||||
Path subdir = dir.resolve("subdir");
|
Path subdir = dir.resolve("subdir");
|
||||||
assertFalse(Files.exists(dir));
|
assertFalse(Files.exists(dir));
|
||||||
assertFalse(Files.exists(subdir));
|
assertFalse(Files.exists(subdir));
|
||||||
@ -110,7 +118,7 @@ public class FileTest {
|
|||||||
public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
|
public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
|
||||||
String prefix = "log_";
|
String prefix = "log_";
|
||||||
String suffix = ".txt";
|
String suffix = ".txt";
|
||||||
Path p = Paths.get(HOME + "/");
|
Path p = Paths.get(TEMP_DIR + "/");
|
||||||
p = Files.createTempFile(p, prefix, suffix);
|
p = Files.createTempFile(p, prefix, suffix);
|
||||||
// like log_8821081429012075286.txt
|
// like log_8821081429012075286.txt
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
@ -119,7 +127,7 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenPath_whenCreatesTempFileWithDefaults_thenCorrect() throws IOException {
|
public void givenPath_whenCreatesTempFileWithDefaults_thenCorrect() throws IOException {
|
||||||
Path p = Paths.get(HOME + "/");
|
Path p = Paths.get(TEMP_DIR + "/");
|
||||||
p = Files.createTempFile(p, null, null);
|
p = Files.createTempFile(p, null, null);
|
||||||
// like 8600179353689423985.tmp
|
// like 8600179353689423985.tmp
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
@ -136,7 +144,7 @@ public class FileTest {
|
|||||||
// delete file
|
// delete file
|
||||||
@Test
|
@Test
|
||||||
public void givenPath_whenDeletes_thenCorrect() throws IOException {
|
public void givenPath_whenDeletes_thenCorrect() throws IOException {
|
||||||
Path p = Paths.get(HOME + "/fileToDelete.txt");
|
Path p = Paths.get(TEMP_DIR + "/fileToDelete.txt");
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.createFile(p);
|
Files.createFile(p);
|
||||||
assertTrue(Files.exists(p));
|
assertTrue(Files.exists(p));
|
||||||
@ -147,7 +155,7 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test(expected = DirectoryNotEmptyException.class)
|
@Test(expected = DirectoryNotEmptyException.class)
|
||||||
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
|
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
|
||||||
Path dir = Paths.get(HOME + "/emptyDir" + UUID.randomUUID().toString());
|
Path dir = Paths.get(TEMP_DIR + "/emptyDir" + UUID.randomUUID().toString());
|
||||||
Files.createDirectory(dir);
|
Files.createDirectory(dir);
|
||||||
assertTrue(Files.exists(dir));
|
assertTrue(Files.exists(dir));
|
||||||
Path file = dir.resolve("file.txt");
|
Path file = dir.resolve("file.txt");
|
||||||
@ -160,7 +168,7 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test(expected = NoSuchFileException.class)
|
@Test(expected = NoSuchFileException.class)
|
||||||
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
|
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
|
||||||
Path p = Paths.get(HOME + "/inexistentFile.txt");
|
Path p = Paths.get(TEMP_DIR + "/inexistentFile.txt");
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.delete(p);
|
Files.delete(p);
|
||||||
|
|
||||||
@ -168,7 +176,7 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException {
|
public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException {
|
||||||
Path p = Paths.get(HOME + "/inexistentFile.txt");
|
Path p = Paths.get(TEMP_DIR + "/inexistentFile.txt");
|
||||||
assertFalse(Files.exists(p));
|
assertFalse(Files.exists(p));
|
||||||
Files.deleteIfExists(p);
|
Files.deleteIfExists(p);
|
||||||
|
|
||||||
@ -177,8 +185,8 @@ public class FileTest {
|
|||||||
// copy file
|
// copy file
|
||||||
@Test
|
@Test
|
||||||
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
|
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
|
||||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
Path dir1 = Paths.get(TEMP_DIR + "/firstdir_" + UUID.randomUUID().toString());
|
||||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
Path dir2 = Paths.get(TEMP_DIR + "/otherdir_" + UUID.randomUUID().toString());
|
||||||
Files.createDirectory(dir1);
|
Files.createDirectory(dir1);
|
||||||
Files.createDirectory(dir2);
|
Files.createDirectory(dir2);
|
||||||
Path file1 = dir1.resolve("filetocopy.txt");
|
Path file1 = dir1.resolve("filetocopy.txt");
|
||||||
@ -193,8 +201,8 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test(expected = FileAlreadyExistsException.class)
|
@Test(expected = FileAlreadyExistsException.class)
|
||||||
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
|
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
|
||||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
Path dir1 = Paths.get(TEMP_DIR + "/firstdir_" + UUID.randomUUID().toString());
|
||||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
Path dir2 = Paths.get(TEMP_DIR + "/otherdir_" + UUID.randomUUID().toString());
|
||||||
Files.createDirectory(dir1);
|
Files.createDirectory(dir1);
|
||||||
Files.createDirectory(dir2);
|
Files.createDirectory(dir2);
|
||||||
Path file1 = dir1.resolve("filetocopy.txt");
|
Path file1 = dir1.resolve("filetocopy.txt");
|
||||||
@ -210,8 +218,8 @@ public class FileTest {
|
|||||||
// moving files
|
// moving files
|
||||||
@Test
|
@Test
|
||||||
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
|
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
|
||||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
Path dir1 = Paths.get(TEMP_DIR + "/firstdir_" + UUID.randomUUID().toString());
|
||||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
Path dir2 = Paths.get(TEMP_DIR + "/otherdir_" + UUID.randomUUID().toString());
|
||||||
Files.createDirectory(dir1);
|
Files.createDirectory(dir1);
|
||||||
Files.createDirectory(dir2);
|
Files.createDirectory(dir2);
|
||||||
Path file1 = dir1.resolve("filetocopy.txt");
|
Path file1 = dir1.resolve("filetocopy.txt");
|
||||||
@ -227,8 +235,8 @@ public class FileTest {
|
|||||||
|
|
||||||
@Test(expected = FileAlreadyExistsException.class)
|
@Test(expected = FileAlreadyExistsException.class)
|
||||||
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
|
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
|
||||||
Path dir1 = Paths.get(HOME + "/firstdir_" + UUID.randomUUID().toString());
|
Path dir1 = Paths.get(TEMP_DIR + "/firstdir_" + UUID.randomUUID().toString());
|
||||||
Path dir2 = Paths.get(HOME + "/otherdir_" + UUID.randomUUID().toString());
|
Path dir2 = Paths.get(TEMP_DIR + "/otherdir_" + UUID.randomUUID().toString());
|
||||||
Files.createDirectory(dir1);
|
Files.createDirectory(dir1);
|
||||||
Files.createDirectory(dir2);
|
Files.createDirectory(dir2);
|
||||||
Path file1 = dir1.resolve("filetocopy.txt");
|
Path file1 = dir1.resolve("filetocopy.txt");
|
||||||
|
@ -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"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,15 @@
|
|||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung.ejb</groupId>
|
<groupId>com.baeldung.ejb</groupId>
|
||||||
<artifactId>ejb</artifactId>
|
<artifactId>ejb</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<artifactId>ejb-client</artifactId>
|
<artifactId>ejb-client</artifactId>
|
||||||
<name>EJB3 Client Maven</name>
|
<name>EJB3 Client Maven</name>
|
||||||
<description>EJB3 Client Maven</description>
|
<description>EJB3 Client Maven</description>
|
||||||
<properties>
|
|
||||||
<junit.version>4.12</junit.version>
|
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.wildfly</groupId>
|
<groupId>org.wildfly</groupId>
|
||||||
@ -49,6 +44,4 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
|
||||||
</project>
|
</project>
|
@ -5,4 +5,4 @@ remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOANONYMO
|
|||||||
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false
|
remote.connection.default.connect.options.org.xnio.Options.SASL_POLICY_NOPLAINTEXT=false
|
||||||
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER}
|
remote.connection.default.connect.options.org.xnio.Options.SASL_DISALLOWED_MECHANISMS=${host.auth:JBOSS-LOCAL-USER}
|
||||||
remote.connection.default.username=testUser
|
remote.connection.default.username=testUser
|
||||||
remote.connection.default.password=admin1234!
|
remote.connection.default.password=admin1234!
|
||||||
|
@ -12,11 +12,12 @@
|
|||||||
|
|
||||||
<!-- <name>ejb-remote</name> -->
|
<!-- <name>ejb-remote</name> -->
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jboss.spec.javax.ejb</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>jboss-ejb-api_3.2_spec</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
<scope>provided</scope>
|
<version>7.0</version>
|
||||||
</dependency>
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -1,13 +1,19 @@
|
|||||||
package com.baeldung.ejb.tutorial;
|
package com.baeldung.ejb.tutorial;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import javax.ejb.SessionContext;
|
||||||
import javax.ejb.Stateless;
|
import javax.ejb.Stateless;
|
||||||
|
|
||||||
@Stateless(name = "HelloWorld")
|
@Stateless(name = "HelloWorld")
|
||||||
public class HelloWorldBean implements HelloWorld {
|
public class HelloWorldBean implements HelloWorld {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SessionContext context;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getHelloWorld() {
|
public String getHelloWorld() {
|
||||||
return "Welcome to EJB Tutorial!";
|
return "Welcome to EJB Tutorial!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -36,11 +36,10 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jboss.spec</groupId>
|
<groupId>javax</groupId>
|
||||||
<artifactId>jboss-javaee-7.0</artifactId>
|
<artifactId>javaee-api</artifactId>
|
||||||
<version>1.0.1.Final</version>
|
<version>7.0</version>
|
||||||
<type>pom</type>
|
<scope>provided</scope>
|
||||||
<scope>import</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
- [Wiring in Spring: @Autowired, @Resource and @Inject](http://www.baeldung.com/spring-annotations-resource-inject-autowire)
|
||||||
|
- [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory)
|
||||||
|
@ -4,14 +4,11 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>dependency-injection</artifactId>
|
<artifactId>spring-core</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<name>dependency-injection</name>
|
<name>spring-core</name>
|
||||||
<description>Accompanying the demonstration of the use of the annotations related to injection mechanisms, namely
|
|
||||||
Resource, Inject, and Autowired
|
|
||||||
</description>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user