Skip to content
This repository was archived by the owner on Jul 31, 2022. It is now read-only.

Commit 4e27c77

Browse files
committed
Add #110; required tests
1 parent e238878 commit 4e27c77

4 files changed

Lines changed: 53 additions & 14 deletions

File tree

‎src/main/java/com/kttdevelopment/simplehttpserver/handler/FileHandler.java‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @see SimpleHttpHandler
2727
* @see com.sun.net.httpserver.HttpHandler
2828
* @since 02.00.00
29-
* @version 4.0.0
29+
* @version 4.1.0
3030
* @author Ktt Development
3131
*/
3232
public class FileHandler implements SimpleHttpHandler {
@@ -791,6 +791,30 @@ public final void addDirectory(final String context, final File directory, final
791791
}catch(final UncheckedIOException ignored){}
792792
}
793793

794+
/**
795+
* Removes a file from the file handler.
796+
*
797+
* @param context the context of the file to remove
798+
*
799+
* @since 4.1.0
800+
* @author Ktt Development
801+
*/
802+
public final void removeFile(final String context){
803+
files.remove(ContextUtil.joinContexts(true, false, context));
804+
}
805+
806+
/**
807+
* Removes a directory from the file handler.
808+
*
809+
* @param context the context of the directory to remove
810+
*
811+
* @since 4.1.0
812+
* @author Ktt Development
813+
*/
814+
public final void removeDirectory(final String context){
815+
directories.remove(ContextUtil.joinContexts(true, false, context));
816+
}
817+
794818
//
795819

796820
private String getName(final File file){

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddDirTest.java renamed to src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerDirTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
import java.util.UUID;
1515
import java.util.concurrent.ExecutionException;
1616

17-
public final class FileHandlerAddDirTest {
17+
public final class FileHandlerDirTest {
1818

1919
@TempDir
2020
public final File dir = new File(UUID.randomUUID().toString());
2121

2222
@SuppressWarnings("SpellCheckingInspection")
2323
@Test
24-
public final void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
24+
public final void addAndRemoveTest() throws IOException, InterruptedException{
2525
final int port = 8080;
2626
final SimpleHttpServer server = SimpleHttpServer.create(port);
2727
final FileHandler handler = new FileHandler();
@@ -41,6 +41,7 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep
4141
Files.write(walk.toPath(), testContent.getBytes());
4242

4343
final String context = "";
44+
final String empty = handler.toString();
4445

4546
handler.addDirectory(dir); // test file & directory read
4647
handler.addDirectory(contextNoName, dir);
@@ -102,6 +103,12 @@ public final void addDirectoryTestsNoWalk() throws IOException, InterruptedExcep
102103
}
103104

104105
server.stop();
106+
107+
handler.removeDirectory(dir.getName()); // test file & directory read
108+
handler.removeDirectory(contextNoName + '/' + dir.getName());
109+
handler.removeDirectory(dirNewName);
110+
handler.removeDirectory(contextWName + '/' + dirNewName);
111+
Assertions.assertEquals(empty, handler.toString());
105112
}
106113

107114
}

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddTest.java renamed to src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerFileTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
import java.util.*;
1616
import java.util.concurrent.ExecutionException;
1717

18-
public final class FileHandlerAddTest {
18+
public final class FileHandlerFileTest {
1919

2020
@TempDir
2121
public final File dir = new File(UUID.randomUUID().toString());
2222

2323
@SuppressWarnings("SpellCheckingInspection")
2424
@Test
25-
public final void addFileTests() throws IOException{
25+
public final void addAndRemoveTest() throws IOException{
2626
final int port = 8080;
2727
final SimpleHttpServer server = SimpleHttpServer.create(port);
2828
final FileHandler handler = new FileHandler();
@@ -32,16 +32,12 @@ public final void addFileTests() throws IOException{
3232
Arrays.stream(ByteLoadingOption.values())
3333
.filter(o -> o != ByteLoadingOption.CACHELOAD)
3434
.forEach(blop -> files.put(new File(dir, blop.name()), blop));
35-
35+
final String empty = handler.toString();
3636
// initial write
3737
final String testContent = String.valueOf(System.currentTimeMillis());
3838
files.forEach((file, loadingOption) -> {
39-
try{
40-
Files.write(file.toPath(), testContent.getBytes());
41-
handler.addFile(file, loadingOption);
42-
}catch(final IOException e){
43-
e.printStackTrace();
44-
}
39+
Assertions.assertDoesNotThrow(() -> Files.write(file.toPath(), testContent.getBytes()));
40+
handler.addFile(file, loadingOption);
4541
});
4642

4743
server.createContext(context, handler);
@@ -79,6 +75,9 @@ public final void addFileTests() throws IOException{
7975
});
8076

8177
server.stop();
78+
79+
files.forEach((file, loadingOption) -> handler.removeFile(file.getName()));
80+
Assertions.assertEquals(empty, handler.toString());
8281
}
8382

8483
}

src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerAddFilesTest.java renamed to src/test/java/com/kttdevelopment/simplehttpserver/handlers/file/FileHandlerFilesTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
import java.util.UUID;
1515
import java.util.concurrent.ExecutionException;
1616

17-
public final class FileHandlerAddFilesTest {
17+
public final class FileHandlerFilesTest {
1818

1919
@TempDir
2020
public final File dir = new File(UUID.randomUUID().toString());
2121

2222
@Test
23-
public final void addFilesTests() throws IOException, ExecutionException, InterruptedException{
23+
public final void addAndRemoveTest() throws IOException, ExecutionException, InterruptedException{
2424
final int port = 8080;
2525
final SimpleHttpServer server = SimpleHttpServer.create(port);
2626
final String override = "override";
@@ -44,6 +44,8 @@ public final String getName(final File file){
4444
new File(dir, UUID.randomUUID().toString() + ".txt")
4545
};
4646

47+
final String empty = handler.toString();
48+
4749
for(final File file : files)
4850
Assertions.assertTrue(file.createNewFile());
4951

@@ -79,6 +81,13 @@ public final String getName(final File file){
7981
}
8082

8183
server.stop();
84+
85+
for(final File file : files){
86+
handler.removeFile(adapter.getName(file));
87+
handler.removeFile(altContext + '/' + adapter.getName(file));
88+
}
89+
90+
Assertions.assertEquals(empty, handler.toString());
8291
}
8392

8493
}

0 commit comments

Comments
 (0)