This commit is contained in:
DOHA 2016-10-30 12:18:40 +02:00
parent 7918324702
commit 1110c53d81
2 changed files with 46 additions and 80 deletions

View File

@ -1,33 +0,0 @@
package com.baeldung.java.networking.udp;
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class UDPTest {
EchoClient client = null;
@Before
public void setup() throws IOException {
new EchoServer().start();
client = new EchoClient();
}
@Test
public void whenCanSendAndReceivePacket_thenCorrect1() {
String echo = client.sendEcho("hello server");
assertEquals("hello server", echo);
echo = client.sendEcho("server is working");
assertFalse(echo.equals("hello server"));
}
@After
public void tearDown() {
client.sendEcho("end");
client.close();
}
}

View File

@ -4,7 +4,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.file.CopyOption;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
@ -16,50 +15,50 @@ import java.util.Date;
import org.junit.Test;
public class FileTest {
public class FileUnitTest {
private static final String HOME = System.getProperty("user.home");
// checking file or dir
@Test
public void givenExistentPath_whenConfirmsFileExists_thenCorrect() {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertTrue(Files.exists(p));
}
@Test
public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() {
Path p = Paths.get(HOME + "/inexistent_file.txt");
final Path p = Paths.get(HOME + "/inexistent_file.txt");
assertTrue(Files.notExists(p));
}
@Test
public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertFalse(Files.isRegularFile(p));
}
@Test
public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertTrue(Files.isReadable(p));
}
@Test
public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertTrue(Files.isWritable(p));
}
@Test
public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() {
Path p = Paths.get(HOME);
final Path p = Paths.get(HOME);
assertTrue(Files.isExecutable(p));
}
@Test
public void givenSameFilePaths_whenConfirmsIsSame_thenCorrect() throws IOException {
Path p1 = Paths.get(HOME);
Path p2 = Paths.get(HOME);
final Path p1 = Paths.get(HOME);
final Path p2 = Paths.get(HOME);
assertTrue(Files.isSameFile(p1, p2));
}
@ -67,8 +66,8 @@ public class FileTest {
// creating file
@Test
public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException {
String fileName = "myfile_" + new Date().getTime() + ".txt";
Path p = Paths.get(HOME + "/" + fileName);
final String fileName = "myfile_" + new Date().getTime() + ".txt";
final Path p = Paths.get(HOME + "/" + fileName);
assertFalse(Files.exists(p));
Files.createFile(p);
assertTrue(Files.exists(p));
@ -77,8 +76,8 @@ public class FileTest {
@Test
public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException {
String dirName = "myDir_" + new Date().getTime();
Path p = Paths.get(HOME + "/" + dirName);
final String dirName = "myDir_" + new Date().getTime();
final Path p = Paths.get(HOME + "/" + dirName);
assertFalse(Files.exists(p));
Files.createDirectory(p);
assertTrue(Files.exists(p));
@ -89,20 +88,20 @@ public class FileTest {
@Test
public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() {
String dirName = "myDir_" + new Date().getTime() + "/subdir";
Path p = Paths.get(HOME + "/" + dirName);
final String dirName = "myDir_" + new Date().getTime() + "/subdir";
final Path p = Paths.get(HOME + "/" + dirName);
assertFalse(Files.exists(p));
try {
Files.createDirectory(p);
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(e instanceof NoSuchFileException);
}
}
@Test
public void givenDirPath_whenCreatesRecursively_thenCorrect() throws IOException {
Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime());
Path subdir = dir.resolve("subdir");
final Path dir = Paths.get(HOME + "/myDir_" + new Date().getTime());
final Path subdir = dir.resolve("subdir");
assertFalse(Files.exists(dir));
assertFalse(Files.exists(subdir));
Files.createDirectories(subdir);
@ -112,8 +111,8 @@ public class FileTest {
@Test
public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException {
String prefix = "log_";
String suffix = ".txt";
final String prefix = "log_";
final String suffix = ".txt";
Path p = Paths.get(HOME + "/");
p = Files.createTempFile(p, prefix, suffix);
// like log_8821081429012075286.txt
@ -132,7 +131,7 @@ public class FileTest {
@Test
public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException {
Path p = Files.createTempFile(null, null);
final Path p = Files.createTempFile(null, null);
// like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp
assertTrue(Files.exists(p));
@ -141,7 +140,7 @@ public class FileTest {
// delete file
@Test
public void givenPath_whenDeletes_thenCorrect() throws IOException {
Path p = Paths.get(HOME + "/fileToDelete.txt");
final Path p = Paths.get(HOME + "/fileToDelete.txt");
assertFalse(Files.exists(p));
Files.createFile(p);
assertTrue(Files.exists(p));
@ -152,14 +151,14 @@ public class FileTest {
@Test
public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException {
Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime());
final Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime());
Files.createDirectory(dir);
assertTrue(Files.exists(dir));
Path file = dir.resolve("file.txt");
final Path file = dir.resolve("file.txt");
Files.createFile(file);
try {
Files.delete(dir);
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(e instanceof DirectoryNotEmptyException);
}
assertTrue(Files.exists(dir));
@ -168,11 +167,11 @@ public class FileTest {
@Test
public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException {
Path p = Paths.get(HOME + "/inexistentFile.txt");
final Path p = Paths.get(HOME + "/inexistentFile.txt");
assertFalse(Files.exists(p));
try {
Files.delete(p);
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(e instanceof NoSuchFileException);
}
@ -180,7 +179,7 @@ public class FileTest {
@Test
public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException {
Path p = Paths.get(HOME + "/inexistentFile.txt");
final Path p = Paths.get(HOME + "/inexistentFile.txt");
assertFalse(Files.exists(p));
Files.deleteIfExists(p);
@ -189,12 +188,12 @@ public class FileTest {
// copy file
@Test
public void givenFilePath_whenCopiesToNewLocation_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
Files.createDirectory(dir1);
Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt");
Path file2 = dir2.resolve("filetocopy.txt");
final Path file1 = dir1.resolve("filetocopy.txt");
final Path file2 = dir2.resolve("filetocopy.txt");
Files.createFile(file1);
assertTrue(Files.exists(file1));
assertFalse(Files.exists(file2));
@ -205,19 +204,19 @@ public class FileTest {
@Test
public void givenPath_whenCopyFailsDueToExistingFile_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
Files.createDirectory(dir1);
Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt");
Path file2 = dir2.resolve("filetocopy.txt");
final Path file1 = dir1.resolve("filetocopy.txt");
final Path file2 = dir2.resolve("filetocopy.txt");
Files.createFile(file1);
Files.createFile(file2);
assertTrue(Files.exists(file1));
assertTrue(Files.exists(file2));
try {
Files.copy(file1, file2);
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(e instanceof FileAlreadyExistsException);
}
Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING);
@ -226,12 +225,12 @@ public class FileTest {
// moving files
@Test
public void givenFilePath_whenMovesToNewLocation_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
Files.createDirectory(dir1);
Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt");
Path file2 = dir2.resolve("filetocopy.txt");
final Path file1 = dir1.resolve("filetocopy.txt");
final Path file2 = dir2.resolve("filetocopy.txt");
Files.createFile(file1);
assertTrue(Files.exists(file1));
assertFalse(Files.exists(file2));
@ -243,19 +242,19 @@ public class FileTest {
@Test
public void givenFilePath_whenMoveFailsDueToExistingFile_thenCorrect() throws IOException {
Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
final Path dir1 = Paths.get(HOME + "/firstdir_" + new Date().getTime());
final Path dir2 = Paths.get(HOME + "/otherdir_" + new Date().getTime());
Files.createDirectory(dir1);
Files.createDirectory(dir2);
Path file1 = dir1.resolve("filetocopy.txt");
Path file2 = dir2.resolve("filetocopy.txt");
final Path file1 = dir1.resolve("filetocopy.txt");
final Path file2 = dir2.resolve("filetocopy.txt");
Files.createFile(file1);
Files.createFile(file2);
assertTrue(Files.exists(file1));
assertTrue(Files.exists(file2));
try {
Files.move(file1, file2);
} catch (IOException e) {
} catch (final IOException e) {
assertTrue(e instanceof FileAlreadyExistsException);
}
Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING);