From 585a88409f6775e879e47240eac4be52061787e9 Mon Sep 17 00:00:00 2001 From: Egima profile Date: Sat, 29 Oct 2016 20:41:01 +0300 Subject: [PATCH] created project for nio2 (#785) * made changes to java reflection * removed redundant method makeSound in Animal abstract class * added project for play-framework article * added project for regex * changed regex project from own model to core-java * added project for routing in play * made changes to regex project * refactored code for REST API with Play project * refactored student store indexing to zero base * added unit tests, removed bad names * added NIO Selector project under core-java module * requested changes made * added project for nio2 --- .../baeldung/java/networking/udp/UDPTest.java | 33 +++ .../baeldung/java/networking/url/UrlTest.java | 104 +++++++ .../java/com/baeldung/java/nio2/FileTest.java | 265 ++++++++++++++++++ .../java/com/baeldung/java/nio2/PathTest.java | 197 +++++++++++++ 4 files changed, 599 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/java/networking/udp/UDPTest.java create mode 100644 core-java/src/test/java/com/baeldung/java/networking/url/UrlTest.java create mode 100644 core-java/src/test/java/com/baeldung/java/nio2/FileTest.java create mode 100644 core-java/src/test/java/com/baeldung/java/nio2/PathTest.java diff --git a/core-java/src/main/java/com/baeldung/java/networking/udp/UDPTest.java b/core-java/src/main/java/com/baeldung/java/networking/udp/UDPTest.java new file mode 100644 index 0000000000..ee93936230 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java/networking/udp/UDPTest.java @@ -0,0 +1,33 @@ +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(); + } +} diff --git a/core-java/src/test/java/com/baeldung/java/networking/url/UrlTest.java b/core-java/src/test/java/com/baeldung/java/networking/url/UrlTest.java new file mode 100644 index 0000000000..14444f0145 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/networking/url/UrlTest.java @@ -0,0 +1,104 @@ +package com.baeldung.java.networking.url; + +import static org.junit.Assert.assertEquals; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.junit.Test; + +public class UrlTest { + + @Test + public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com"); + assertEquals("http", url.getProtocol()); + } + + @Test + public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com"); + assertEquals("baeldung.com", url.getHost()); + } + + @Test + public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException { + URL url = new URL("http://baeldung.com/articles?topic=java&version=8"); + assertEquals("/articles?topic=java&version=8", url.getFile()); + } + + @Test + public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException { + URL url = new URL("http://baeldung.com/guidelines.txt"); + assertEquals("/guidelines.txt", url.getFile()); + } + + @Test + public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com/articles?topic=java&version=8"); + assertEquals("/articles", url.getPath()); + } + + @Test + public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com/articles?topic=java"); + assertEquals("topic=java", url.getQuery()); + } + + @Test + public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com"); + assertEquals(-1, url.getPort()); + assertEquals(80, url.getDefaultPort()); + } + + @Test + public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException { + URL url = new URL("http://baeldung.com:8090"); + assertEquals(8090, url.getPort()); + assertEquals(80, url.getDefaultPort()); + } + + @Test + public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException { + URL baseUrl = new URL("http://baeldung.com"); + URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets"); + assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString()); + } + + @Test + public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException { + URL baseUrl = new URL("http://baeldung.com"); + URL relativeUrl = new URL(baseUrl, "http://baeldung.com/a-guide-to-java-sockets"); + assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString()); + } + + @Test + public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException { + String protocol = "http"; + String host = "baeldung.com"; + String file = "/guidelines.txt"; + URL url = new URL(protocol, host, file); + assertEquals("http://baeldung.com/guidelines.txt", url.toString()); + } + + @Test + public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException { + String protocol = "http"; + String host = "baeldung.com"; + String file = "/articles?topic=java&version=8"; + URL url = new URL(protocol, host, file); + assertEquals("http://baeldung.com/guidelines.txt", url.toString()); + } + + @Test + public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException { + String protocol = "http"; + String host = "baeldung.com"; + int port = 9000; + String file = "/guidelines.txt"; + URL url = new URL(protocol, host, port, file); + assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java b/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java new file mode 100644 index 0000000000..b6fc8d98a4 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/nio2/FileTest.java @@ -0,0 +1,265 @@ +package com.baeldung.java.nio2; + +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; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.Date; + +import org.junit.Test; + +public class FileTest { + private static final String HOME = System.getProperty("user.home"); + + // checking file or dir + @Test + public void givenExistentPath_whenConfirmsFileExists_thenCorrect() { + Path p = Paths.get(HOME); + assertTrue(Files.exists(p)); + } + + @Test + public void givenNonexistentPath_whenConfirmsFileNotExists_thenCorrect() { + Path p = Paths.get(HOME + "/inexistent_file.txt"); + assertTrue(Files.notExists(p)); + } + + @Test + public void givenExistentDirPath_whenConfirmsNotRegularFile_thenCorrect() { + Path p = Paths.get(HOME); + assertFalse(Files.isRegularFile(p)); + } + + @Test + public void givenExistentDirPath_whenConfirmsReadable_thenCorrect() { + Path p = Paths.get(HOME); + assertTrue(Files.isReadable(p)); + } + + @Test + public void givenExistentDirPath_whenConfirmsWritable_thenCorrect() { + Path p = Paths.get(HOME); + assertTrue(Files.isWritable(p)); + } + + @Test + public void givenExistentDirPath_whenConfirmsExecutable_thenCorrect() { + 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); + assertTrue(Files.isSameFile(p1, p2)); + } + + // reading, writing and creating files + // creating file + @Test + public void givenFilePath_whenCreatesNewFile_thenCorrect() throws IOException { + String fileName = "myfile_" + new Date().getTime() + ".txt"; + Path p = Paths.get(HOME + "/" + fileName); + assertFalse(Files.exists(p)); + Files.createFile(p); + assertTrue(Files.exists(p)); + + } + + @Test + public void givenDirPath_whenCreatesNewDir_thenCorrect() throws IOException { + String dirName = "myDir_" + new Date().getTime(); + Path p = Paths.get(HOME + "/" + dirName); + assertFalse(Files.exists(p)); + Files.createDirectory(p); + assertTrue(Files.exists(p)); + assertFalse(Files.isRegularFile(p)); + assertTrue(Files.isDirectory(p)); + + } + + @Test + public void givenDirPath_whenFailsToCreateRecursively_thenCorrect() { + String dirName = "myDir_" + new Date().getTime() + "/subdir"; + Path p = Paths.get(HOME + "/" + dirName); + assertFalse(Files.exists(p)); + try { + Files.createDirectory(p); + } catch (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"); + assertFalse(Files.exists(dir)); + assertFalse(Files.exists(subdir)); + Files.createDirectories(subdir); + assertTrue(Files.exists(dir)); + assertTrue(Files.exists(subdir)); + } + + @Test + public void givenFilePath_whenCreatesTempFile_thenCorrect() throws IOException { + String prefix = "log_"; + String suffix = ".txt"; + Path p = Paths.get(HOME + "/"); + p = Files.createTempFile(p, prefix, suffix); + // like log_8821081429012075286.txt + assertTrue(Files.exists(p)); + + } + + @Test + public void givenFilePath_whenCreatesTempFileWithDefaultsNaming_thenCorrect() throws IOException { + Path p = Paths.get(HOME + "/"); + p = Files.createTempFile(p, null, null); + // like 8600179353689423985.tmp + System.out.println(p); + assertTrue(Files.exists(p)); + } + + @Test + public void givenNoFilePath_whenCreatesTempFileInTempDir_thenCorrect() throws IOException { + Path p = Files.createTempFile(null, null); + // like C:\Users\new\AppData\Local\Temp\6100927974988978748.tmp + assertTrue(Files.exists(p)); + + } + + // delete file + @Test + public void givenPath_whenDeletes_thenCorrect() throws IOException { + Path p = Paths.get(HOME + "/fileToDelete.txt"); + assertFalse(Files.exists(p)); + Files.createFile(p); + assertTrue(Files.exists(p)); + Files.delete(p); + assertFalse(Files.exists(p)); + + } + + @Test + public void givenPath_whenFailsToDeleteNonEmptyDir_thenCorrect() throws IOException { + Path dir = Paths.get(HOME + "/emptyDir" + new Date().getTime()); + Files.createDirectory(dir); + assertTrue(Files.exists(dir)); + Path file = dir.resolve("file.txt"); + Files.createFile(file); + try { + Files.delete(dir); + } catch (IOException e) { + assertTrue(e instanceof DirectoryNotEmptyException); + } + assertTrue(Files.exists(dir)); + + } + + @Test + public void givenInexistentFile_whenDeleteFails_thenCorrect() throws IOException { + Path p = Paths.get(HOME + "/inexistentFile.txt"); + assertFalse(Files.exists(p)); + try { + Files.delete(p); + } catch (IOException e) { + assertTrue(e instanceof NoSuchFileException); + } + + } + + @Test + public void givenInexistentFile_whenDeleteIfExistsWorks_thenCorrect() throws IOException { + Path p = Paths.get(HOME + "/inexistentFile.txt"); + assertFalse(Files.exists(p)); + Files.deleteIfExists(p); + + } + + // 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()); + Files.createDirectory(dir1); + Files.createDirectory(dir2); + Path file1 = dir1.resolve("filetocopy.txt"); + Path file2 = dir2.resolve("filetocopy.txt"); + Files.createFile(file1); + assertTrue(Files.exists(file1)); + assertFalse(Files.exists(file2)); + Files.copy(file1, file2); + assertTrue(Files.exists(file2)); + + } + + @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()); + Files.createDirectory(dir1); + Files.createDirectory(dir2); + Path file1 = dir1.resolve("filetocopy.txt"); + 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) { + assertTrue(e instanceof FileAlreadyExistsException); + } + Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING); + } + + // 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()); + Files.createDirectory(dir1); + Files.createDirectory(dir2); + Path file1 = dir1.resolve("filetocopy.txt"); + Path file2 = dir2.resolve("filetocopy.txt"); + Files.createFile(file1); + assertTrue(Files.exists(file1)); + assertFalse(Files.exists(file2)); + Files.move(file1, file2); + assertTrue(Files.exists(file2)); + assertFalse(Files.exists(file1)); + + } + + @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()); + Files.createDirectory(dir1); + Files.createDirectory(dir2); + Path file1 = dir1.resolve("filetocopy.txt"); + 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) { + assertTrue(e instanceof FileAlreadyExistsException); + } + Files.move(file1, file2, StandardCopyOption.REPLACE_EXISTING); + assertTrue(Files.exists(file2)); + assertFalse(Files.exists(file1)); + } +} diff --git a/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java b/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java new file mode 100644 index 0000000000..c0cb45341e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/nio2/PathTest.java @@ -0,0 +1,197 @@ +package com.baeldung.java.nio2; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Date; + +import org.junit.Test; + +public class PathTest { + + private static final String HOME = System.getProperty("user.home"); + + // creating a path + @Test + public void givenPathString_whenCreatesPathObject_thenCorrect() { + Path p = Paths.get("/articles/baeldung"); + assertEquals("\\articles\\baeldung", p.toString()); + + } + + @Test + public void givenPathParts_whenCreatesPathObject_thenCorrect() { + Path p = Paths.get("/articles", "baeldung"); + assertEquals("\\articles\\baeldung", p.toString()); + + } + + // retrieving path info + @Test + public void givenPath_whenRetrievesFileName_thenCorrect() { + Path p = Paths.get("/articles/baeldung/logs"); + assertEquals("logs", p.getFileName().toString()); + } + + @Test + public void givenPath_whenRetrievesNameByIndex_thenCorrect() { + Path p = Paths.get("/articles/baeldung/logs"); + assertEquals("articles", p.getName(0).toString()); + assertEquals("baeldung", p.getName(1).toString()); + assertEquals("logs", p.getName(2).toString()); + } + + @Test + public void givenPath_whenCountsParts_thenCorrect() { + Path p = Paths.get("/articles/baeldung/logs"); + assertEquals(3, p.getNameCount()); + } + + @Test + public void givenPath_whenCanRetrieveSubsequenceByIndex_thenCorrect() { + Path p = Paths.get("/articles/baeldung/logs"); + assertEquals("articles", p.subpath(0, 1).toString()); + assertEquals("articles\\baeldung", p.subpath(0, 2).toString()); + assertEquals("articles\\baeldung\\logs", p.subpath(0, 3).toString()); + assertEquals("baeldung", p.subpath(1, 2).toString()); + assertEquals("baeldung\\logs", p.subpath(1, 3).toString()); + assertEquals("logs", p.subpath(2, 3).toString()); + } + + @Test + public void givenPath_whenRetrievesParent_thenCorrect() { + Path p1 = Paths.get("/articles/baeldung/logs"); + Path p2 = Paths.get("/articles/baeldung"); + Path p3 = Paths.get("/articles"); + Path p4 = Paths.get("/"); + + assertEquals("\\articles\\baeldung", p1.getParent().toString()); + assertEquals("\\articles", p2.getParent().toString()); + assertEquals("\\", p3.getParent().toString()); + assertEquals(null, p4.getParent()); + } + + @Test + public void givenPath_whenRetrievesRoot_thenCorrect() { + Path p1 = Paths.get("/articles/baeldung/logs"); + Path p2 = Paths.get("c:/articles/baeldung/logs"); + + assertEquals("\\", p1.getRoot().toString()); + assertEquals("c:\\", p2.getRoot().toString()); + } + + // removing redundancies from path + @Test + public void givenPath_whenRemovesRedundancies_thenCorrect1() { + Path p = Paths.get("/home/./baeldung/articles"); + p = p.normalize(); + assertEquals("\\home\\baeldung\\articles", p.toString()); + } + + @Test + public void givenPath_whenRemovesRedundancies_thenCorrect2() { + Path p = Paths.get("/home/baeldung/../articles"); + p = p.normalize(); + assertEquals("\\home\\articles", p.toString()); + } + + // converting a path + @Test + public void givenPath_whenConvertsToBrowseablePath_thenCorrect() { + Path p = Paths.get("/home/baeldung/articles.html"); + URI uri = p.toUri(); + assertEquals("file:///E:/home/baeldung/articles.html", uri.toString()); + } + + @Test + public void givenPath_whenConvertsToAbsolutePath_thenCorrect() { + Path p = Paths.get("/home/baeldung/articles.html"); + assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString()); + } + + @Test + public void givenAbsolutePath_whenRetainsAsAbsolute_thenCorrect() { + Path p = Paths.get("E:\\home\\baeldung\\articles.html"); + assertEquals("E:\\home\\baeldung\\articles.html", p.toAbsolutePath().toString()); + } + + @Test + public void givenExistingPath_whenGetsRealPathToFile_thenCorrect() throws IOException { + Path p = Paths.get(HOME); + assertEquals(HOME, p.toRealPath().toString()); + } + + @Test + public void givenInExistentPath_whenFailsToConvert_thenCorrect() { + Path p = Paths.get("E:\\home\\baeldung\\articles.html"); + try { + p.toRealPath(); + } catch (IOException e) { + assertTrue(true); + } + } + + // joining paths + @Test + public void givenTwoPaths_whenJoinsAndResolves_thenCorrect() throws IOException { + Path p = Paths.get("/baeldung/articles"); + assertEquals("\\baeldung\\articles\\java", p.resolve("java").toString()); + } + + @Test + public void givenAbsolutePath_whenResolutionRetainsIt_thenCorrect() throws IOException { + Path p = Paths.get("/baeldung/articles"); + assertEquals("C:\\baeldung\\articles\\java", p.resolve("C:\\baeldung\\articles\\java").toString()); + } + + @Test + public void givenPathWithRoot_whenResolutionRetainsIt_thenCorrect2() throws IOException { + Path p = Paths.get("/baeldung/articles"); + assertEquals("\\java", p.resolve("/java").toString()); + } + + // creating a path between 2 paths + @Test + public void givenSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException { + Path p1 = Paths.get("articles"); + Path p2 = Paths.get("authors"); + assertEquals("..\\authors", p1.relativize(p2).toString()); + assertEquals("..\\articles", p2.relativize(p1).toString()); + } + + @Test + public void givenNonSiblingPaths_whenCreatesPathToOther_thenCorrect() throws IOException { + Path p1 = Paths.get("/baeldung"); + Path p2 = Paths.get("/baeldung/authors/articles"); + assertEquals("authors\\articles", p1.relativize(p2).toString()); + assertEquals("..\\..", p2.relativize(p1).toString()); + } + + // comparing 2 paths + @Test + public void givenTwoPaths_whenTestsEquality_thenCorrect() throws IOException { + Path p1 = Paths.get("/baeldung/articles"); + Path p2 = Paths.get("/baeldung/articles"); + Path p3 = Paths.get("/baeldung/authors"); + + assertTrue(p1.equals(p2)); + assertFalse(p1.equals(p3)); + } + + @Test + public void givenPath_whenInspectsStart_thenCorrect() { + Path p1 = Paths.get("/baeldung/articles"); + assertTrue(p1.startsWith("/baeldung")); + } + + @Test + public void givenPath_whenInspectsEnd_thenCorrect() { + Path p1 = Paths.get("/baeldung/articles"); + assertTrue(p1.endsWith("articles")); + } +}