diff --git a/pom.xml b/pom.xml index c6a3853..e161bbe 100644 --- a/pom.xml +++ b/pom.xml @@ -158,22 +158,8 @@ - - - jitpack.io - https://jitpack.io - - - - - com.kttdevelopment.core - test-util - main-SNAPSHOT - test - - junit junit diff --git a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java index 2bcfa5f..864ec44 100644 --- a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java +++ b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java @@ -1,19 +1,22 @@ package com.kttdevelopment.simplehttpserver.handlers.file; -import com.kttdevelopment.core.tests.TestUtil; import com.kttdevelopment.simplehttpserver.SimpleHttpServer; import com.kttdevelopment.simplehttpserver.handler.FileHandler; -import org.junit.Assert; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.http.*; +import java.nio.file.Files; import java.util.concurrent.ExecutionException; public final class FileHandlerAddDirTest { + @Rule + public final TemporaryFolder directory = new TemporaryFolder(new File(".")); + @SuppressWarnings("SpellCheckingInspection") @Test public final void addDirectoryTestsNoWalk() throws IOException, InterruptedException{ @@ -24,30 +27,32 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep final String contextWName = "altn"; final String dirNewName = "dirName"; - final String testRealFile = "test.txt", testFileContent = "readme"; - final String noReadDir = "dirnoread"; - final String noReadFile = "innerNoRead.txt"; + final String fileName = "file"; + final String testContent = String.valueOf(System.currentTimeMillis()); - final File noWalk = new File("src/test/resources/directory/nowalk"); + final File dir = directory.getRoot(); + final File subdir = directory.newFolder(); - TestUtil.createTestFile(new File(noWalk,testRealFile),testFileContent); - TestUtil.createTestFile(new File(new File(noWalk,noReadDir),noReadFile),testFileContent); + final File file = new File(directory.getRoot(),fileName); + Files.write(file.toPath(),testContent.getBytes()); + final File walk = new File(subdir,fileName); + Files.write(walk.toPath(),testContent.getBytes()); final String context = ""; - handler.addDirectory(noWalk); // test file & directory read - handler.addDirectory(contextNoName,noWalk); - handler.addDirectory(noWalk,dirNewName); - handler.addDirectory(contextWName,noWalk,dirNewName); + handler.addDirectory(dir); // test file & directory read + handler.addDirectory(contextNoName,dir); + handler.addDirectory(dir,dirNewName); + handler.addDirectory(contextWName,dir,dirNewName); server.createContext(context,handler); server.start(); final String[] validPathsToTest = { // valid reads - noWalk.getName() + '/' + testRealFile, - contextNoName + '/' + noWalk.getName() + '/' + testRealFile, - dirNewName + '/' + testRealFile, - contextWName + '/' + dirNewName + '/' + testRealFile + dir.getName() + '/' + "file", + contextNoName + '/' + dir.getName() + '/' + fileName, + dirNewName + '/' + fileName, + contextWName + '/' + dirNewName + '/' + fileName }; for(final String path : validPathsToTest){ @@ -57,19 +62,21 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep .build(); try{ - final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNotNull("Client did not find data for " + path, response); - Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response); + Assert.assertEquals("Client data did not match server data for " + path,testContent,response); }catch(final ExecutionException ignored){ Assert.fail("Client did not find data for " + path); } } final String[] invalidPathsToTest = { - noWalk.getName() + '/' + noReadDir, - noWalk.getName() + '/' + noReadDir + '/' + noReadFile + dir.getName() + '/' + subdir.getName(), + dir.getName() + '/' + subdir.getName() + '/' + fileName }; for(final String path : invalidPathsToTest){ @@ -80,8 +87,10 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep Exception exception = null; try{ - final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNull("Client found data for blocked path " + path,response); }catch(final ExecutionException e){ diff --git a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java index d6f05f4..fc37f43 100644 --- a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java +++ b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java @@ -1,11 +1,10 @@ package com.kttdevelopment.simplehttpserver.handlers.file; -import com.kttdevelopment.core.tests.TestUtil; import com.kttdevelopment.simplehttpserver.SimpleHttpServer; import com.kttdevelopment.simplehttpserver.handler.FileHandler; import com.kttdevelopment.simplehttpserver.handler.FileHandlerAdapter; -import org.junit.Assert; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; @@ -15,6 +14,10 @@ public final class FileHandlerAddFilesTest { + @Rule + public final TemporaryFolder directory = new TemporaryFolder(new File(".")); + + @Test public final void addFilesTests() throws IOException, ExecutionException, InterruptedException{ final int port = 8080; @@ -35,13 +38,10 @@ public final String getName(final File file){ final FileHandler handler = new FileHandler(adapter); final File[] files = new File[]{ - new File("src/test/resources/files/test.txt"), - new File("src/test/resources/files/test2.txt") + directory.newFile(), + directory.newFile() }; - for(final File file : files) - TestUtil.createTestFile(file); - handler.addFiles(files); handler.addFiles(altContext,files); @@ -52,7 +52,7 @@ public final String getName(final File file){ for(final File file : files){ final String url = "http://localhost:" + port + context; HttpRequest request = HttpRequest.newBuilder() - .uri(URI.create(url + "/" + file.getName().substring(0, file.getName().lastIndexOf('.')))) + .uri(URI.create(url + '/' + file.getName().substring(0, file.getName().lastIndexOf('.')))) .build(); String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) @@ -64,7 +64,7 @@ public final String getName(final File file){ final String altUrl = "http://localhost:" + port + altContext; request = HttpRequest.newBuilder() - .uri(URI.create(altUrl + "/" + file.getName().substring(0,file.getName().lastIndexOf('.')))) + .uri(URI.create(altUrl + '/' + file.getName().substring(0,file.getName().lastIndexOf('.')))) .build(); response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) @@ -74,7 +74,6 @@ public final String getName(final File file){ } server.stop(); - } } diff --git a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java index f674847..26e5e55 100644 --- a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java +++ b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java @@ -1,11 +1,10 @@ package com.kttdevelopment.simplehttpserver.handlers.file; -import com.kttdevelopment.core.tests.TestUtil; import com.kttdevelopment.simplehttpserver.SimpleHttpServer; import com.kttdevelopment.simplehttpserver.handler.ByteLoadingOption; import com.kttdevelopment.simplehttpserver.handler.FileHandler; -import org.junit.Assert; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; @@ -18,6 +17,9 @@ public final class FileHandlerAddTest { + @Rule + public final TemporaryFolder directory = new TemporaryFolder(new File(".")); + @SuppressWarnings("SpellCheckingInspection") @Test public final void addFileTests() throws IOException{ @@ -26,17 +28,19 @@ public final void addFileTests() throws IOException{ final FileHandler handler = new FileHandler(); final String context = ""; - final File dir = new File("src/test/resources/file"); - final Map files = new HashMap<>(); for(final ByteLoadingOption blop : ByteLoadingOption.values()) - files.put(new File(dir, blop.name() + ".txt"),blop); + files.put(directory.newFile(blop.name()),blop); // initial write - final String init = String.valueOf(System.currentTimeMillis()); + final String testContent = String.valueOf(System.currentTimeMillis()); files.forEach((file, loadingOption) -> { - TestUtil.createTestFile(file,init); - handler.addFile(file, loadingOption); + try{ + Files.write(file.toPath(),testContent.getBytes()); + handler.addFile(file, loadingOption); + }catch(final IOException e){ + e.printStackTrace(); + } }); server.createContext(context,handler); @@ -51,7 +55,7 @@ public final void addFileTests() throws IOException{ try{ final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body).get(); - Assert.assertEquals("Client data did not match server data for " + file.getName(),init,response); + Assert.assertEquals("Client data did not match server data for " + file.getName(),testContent,response); }catch(final InterruptedException | ExecutionException ignored){ Assert.fail("Failed to read context of " + file.getName()); } @@ -69,7 +73,7 @@ public final void addFileTests() throws IOException{ final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body).get(); - Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? init : after,response); + Assert.assertEquals("Client data did not match server data for " + file.getName(),loadingOption == ByteLoadingOption.PRELOAD ? testContent : after,response); }catch(final InterruptedException | ExecutionException ignored){ Assert.fail("Failed to read context " + file.getName()); } diff --git a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerNoWalkTest.java b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerNoWalkTest.java index e367920..8beef2a 100644 --- a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerNoWalkTest.java +++ b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerNoWalkTest.java @@ -1,20 +1,23 @@ package com.kttdevelopment.simplehttpserver.handlers.file; -import com.kttdevelopment.core.tests.TestUtil; import com.kttdevelopment.simplehttpserver.SimpleHttpServer; import com.kttdevelopment.simplehttpserver.handler.FileHandler; import com.kttdevelopment.simplehttpserver.handler.FileHandlerAdapter; -import org.junit.Assert; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.http.*; +import java.nio.file.Files; import java.util.concurrent.ExecutionException; public final class FileHandlerNoWalkTest { + @Rule + public final TemporaryFolder directory = new TemporaryFolder(new File(".")); + @SuppressWarnings("SpellCheckingInspection") @Test public final void addDirectoryTestsNoWalkAdapter() throws IOException, InterruptedException{ @@ -36,30 +39,33 @@ public final String getName(final File file){ final String contextWName = "altn"; final String dirNewName = "dirName"; - final String testRealFile = "test", testFileContent = "readme"; - final String noReadDir = "dirnoread"; - final String noReadFile = "innerNoRead"; + final String fileName = "file.txt"; + final String expectedName = "file"; + final String testContent = String.valueOf(System.currentTimeMillis()); - final File noWalk = new File("src/test/resources/directory/nowalk"); + final File dir = directory.getRoot(); + final File subdir = directory.newFolder(); - TestUtil.createTestFile(new File(noWalk, testRealFile + ".txt"), testFileContent); - TestUtil.createTestFile(new File(new File(noWalk,noReadDir),noReadFile + ".txt"),testFileContent); + final File file = new File(directory.getRoot(),fileName); + Files.write(file.toPath(), testContent.getBytes()); + final File walk = new File(subdir,fileName); + Files.write(walk.toPath(),testContent.getBytes()); final String context = ""; - handler.addDirectory(noWalk); // test file & directory read - handler.addDirectory(contextNoName,noWalk); - handler.addDirectory(noWalk,dirNewName); - handler.addDirectory(contextWName,noWalk,dirNewName); + handler.addDirectory(dir); // test file & directory read + handler.addDirectory(contextNoName,dir); + handler.addDirectory(dir,dirNewName); + handler.addDirectory(contextWName,dir,dirNewName); server.createContext(context,handler); server.start(); final String[] validPathsToTest = { // valid reads - noWalk.getName() + '/' + testRealFile, - contextNoName + '/' + noWalk.getName() + '/' + testRealFile, - dirNewName + '/' + testRealFile, - contextWName + '/' + dirNewName + '/' + testRealFile + dir.getName() + '/' + expectedName, + contextNoName + '/' + dir.getName() + '/' + expectedName, + dirNewName + '/' + expectedName, + contextWName + '/' + dirNewName + '/' + expectedName }; for(final String path : validPathsToTest){ @@ -69,19 +75,21 @@ public final String getName(final File file){ .build(); try{ - final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNotNull("Client did not find data for " + path, response); - Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response); + Assert.assertEquals("Client data did not match server data for " + path,testContent,response); }catch(final ExecutionException ignored){ Assert.fail("Client did not find data for " + path); } } final String[] invalidPathsToTest = { - noWalk.getName() + '/' + noReadDir, - noWalk.getName() + '/' + noReadDir + '/' + noReadFile + dir.getName() + '/' + subdir.getName(), + dir.getName() + '/' + subdir.getName() + '/' + fileName }; for(final String path : invalidPathsToTest){ @@ -92,8 +100,10 @@ public final String getName(final File file){ Exception exception = null; try{ - final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNull("Client found data for blocked path " + path,response); }catch(final ExecutionException e){ diff --git a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerWalkTest.java b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerWalkTest.java index b8bceab..0f3e54a 100644 --- a/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerWalkTest.java +++ b/src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerWalkTest.java @@ -1,45 +1,50 @@ package com.kttdevelopment.simplehttpserver.handlers.file; -import com.kttdevelopment.core.tests.TestUtil; import com.kttdevelopment.simplehttpserver.SimpleHttpServer; import com.kttdevelopment.simplehttpserver.handler.FileHandler; -import org.junit.Assert; -import org.junit.Test; +import org.junit.*; +import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; import java.net.URI; import java.net.http.*; +import java.nio.file.Files; import java.util.concurrent.ExecutionException; @SuppressWarnings("SpellCheckingInspection") public final class FileHandlerWalkTest { + @Rule + public final TemporaryFolder directory = new TemporaryFolder(new File(".")); + @Test public final void addDirectoryTestsWalk() throws IOException, InterruptedException{ final int port = 8080; final SimpleHttpServer server = SimpleHttpServer.create(port); final FileHandler handler = new FileHandler(); - final String testRealFile = "test.txt", testFileContent = "readme"; - final String readDir = "dirnoread"; - final String innerFile = "innerNoRead.txt"; + final String fileName = "file"; + final String testContent = String.valueOf(System.currentTimeMillis()); - final File noWalk = new File("src/test/resources/directory/nowalk"); + final File dir = directory.getRoot(); + final File subdir = directory.newFolder(); - TestUtil.createTestFile(new File(noWalk, testRealFile), testFileContent); - TestUtil.createTestFile(new File(new File(noWalk,readDir),innerFile),testFileContent); + final File file = new File(directory.getRoot(),fileName); + Files.write(file.toPath(), testContent.getBytes()); + final File walk = new File(subdir,fileName); + Files.write(walk.toPath(),testContent.getBytes()); final String context = ""; - handler.addDirectory(noWalk,true); + handler.addDirectory(dir,true); server.createContext(context,handler); server.start(); final String[] validPathsToTest = { // valid reads - noWalk.getName() + '/' + testRealFile, - noWalk.getName() + '/' + readDir + '/' + innerFile + dir.getName() + '/' + file.getName(), + dir.getName() + '/' + subdir.getName() + '/' + walk.getName() }; for(final String path : validPathsToTest){ @@ -49,18 +54,20 @@ public final void addDirectoryTestsWalk() throws IOException, InterruptedExcepti .build(); try{ - final String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNotNull("Client did not find data for " + path,response); - Assert.assertEquals("Client data did not match server data for " + path,testFileContent,response); + Assert.assertEquals("Client data did not match server data for " + path,testContent,response); }catch(final ExecutionException ignored){ Assert.fail("Client did not find data for " + path); } } final String[] invalidPathsToTest = { - noWalk.getName() + '/' + readDir + dir.getName() + '/' + subdir.getName() }; for(final String path : invalidPathsToTest){ @@ -71,8 +78,10 @@ public final void addDirectoryTestsWalk() throws IOException, InterruptedExcepti Exception exception = null; try{ - String response = HttpClient.newHttpClient().sendAsync(request, HttpResponse.BodyHandlers.ofString()) - .thenApply(HttpResponse::body).get(); + final String response = HttpClient.newHttpClient() + .sendAsync(request, HttpResponse.BodyHandlers.ofString()) + .thenApply(HttpResponse::body) + .get(); Assert.assertNull("Client found data for blocked path " + path,response); }catch(final ExecutionException e){