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

Commit c62bea0

Browse files
authored
Add remove methods to FileHandler (#111)
* version * Add #110; required tests * Add additional remove methods - context, file - file - context, directory - directory
1 parent a0a4364 commit c62bea0

File tree

5 files changed

+116
-15
lines changed

5 files changed

+116
-15
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<groupId>com.kttdevelopment</groupId>
1212
<artifactId>simplehttpserver</artifactId>
13-
<version>4.0.0</version>
13+
<version>4.1.0</version>
1414

1515
<name>simplehttpserver</name>
1616
<description>📕 SimpleHttpServer :: Simplified implementation of the sun http server :: Simplified handlers to execute complex operations</description>

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

Lines changed: 87 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,92 @@ public final void addDirectory(final String context, final File directory, final
791791
}catch(final UncheckedIOException ignored){}
792792
}
793793

794+
//
795+
796+
/**
797+
* Removes a file from the handler at a specified context.
798+
*
799+
* @param full_context context and file name
800+
*
801+
* @see #removeFile(String, File)
802+
* @see #removeFile(File)
803+
* @since 4.1.0
804+
* @author Ktt Development
805+
*/
806+
public final void removeFile(final String full_context){
807+
files.remove(ContextUtil.getContext(full_context, true, false));
808+
}
809+
810+
/**
811+
* Removes a file from the handler.
812+
*
813+
* @param file file to remove
814+
*
815+
* @see #removeFile(String)
816+
* @see #removeFile(String, File)
817+
*/
818+
public final void removeFile(final File file){
819+
removeFile(adapter.getName(file));
820+
}
821+
822+
/**
823+
* Removes a file from the handler at a specified context.
824+
*
825+
* @param context context
826+
* @param file file
827+
*
828+
* @see #removeFile(String)
829+
* @see #removeFile(File)
830+
* @since 4.1.0
831+
* @author Ktt Development
832+
*/
833+
public final void removeFile(final String context, final File file){
834+
removeFile(ContextUtil.joinContexts(true, false, context, adapter.getName(file)));
835+
}
836+
837+
/**
838+
* Removes a directory from the handler at a specified context.
839+
*
840+
* @param full_context context and directory name
841+
*
842+
* @see #removeDirectory(File)
843+
* @see #removeDirectory(String, File)
844+
* @since 4.1.0
845+
* @author Ktt Development
846+
*/
847+
public final void removeDirectory(final String full_context){
848+
directories.remove(ContextUtil.getContext(full_context, true, false));
849+
}
850+
851+
/**
852+
* Removes a directory from the handler.
853+
*
854+
* @param directory directory to remove
855+
*
856+
* @see #removeDirectory(String)
857+
* @see #removeDirectory(String, File)
858+
* @since 4.1.0
859+
* @author Ktt Development
860+
*/
861+
public final void removeDirectory(final File directory){
862+
removeDirectory(getName(directory));
863+
}
864+
865+
/**
866+
* Removes a directory from the handler at a specified context.
867+
*
868+
* @param context context
869+
* @param directory directory
870+
*
871+
* @see #removeDirectory(String)
872+
* @see #removeDirectory(File)
873+
* @since 4.1.0
874+
* @author Ktt Development
875+
*/
876+
public final void removeDirectory(final String context, final File directory){
877+
removeDirectory(ContextUtil.joinContexts(true, false, context, getName(directory)));
878+
}
879+
794880
//
795881

796882
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); // test file & directory read
108+
handler.removeDirectory(contextNoName, dir);
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));
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(file);
87+
handler.removeFile(altContext, file);
88+
}
89+
90+
Assertions.assertEquals(empty, handler.toString());
8291
}
8392

8493
}

0 commit comments

Comments
 (0)