@@ -39,7 +39,7 @@ public void addFileTests() throws IOException{
3939 Files .write (file .toPath (), init .getBytes ());
4040 else
4141 Assert .fail ("Failed to create new file for testing: " + file .getPath ());
42- }catch (Exception e ){
42+ }catch (final Exception ignored ){
4343 Assert .fail ("Failed to create new file for testing: " + file .getPath ());
4444 }
4545
@@ -63,7 +63,7 @@ public void addFileTests() throws IOException{
6363 .thenApply (HttpResponse ::body ).get ();
6464
6565 Assert .assertEquals ("Client data did not match server data for " + file .getName (),init ,response );
66- }catch (InterruptedException | ExecutionException e ){
66+ }catch (final InterruptedException | ExecutionException ignored ){
6767 Assert .fail ("Failed to read context of " + file .getName ());
6868 }
6969
@@ -72,7 +72,7 @@ public void addFileTests() throws IOException{
7272 final String after = String .valueOf (System .currentTimeMillis ());
7373 try {
7474 Files .write (file .toPath (), after .getBytes ());
75- }catch (Exception e ){
75+ }catch (final Exception ignored ){
7676 Assert .fail ("Failed to second write file " + file .getPath ());
7777 }
7878
@@ -81,7 +81,7 @@ public void addFileTests() throws IOException{
8181 .thenApply (HttpResponse ::body ).get ();
8282
8383 Assert .assertEquals ("Client data did not match server data for " + file .getName (),loadingOption == ByteLoadingOption .PRELOAD ? init : after ,response );
84- }catch (InterruptedException | ExecutionException e ){
84+ }catch (final InterruptedException | ExecutionException ignored ){
8585 Assert .fail ("Failed to read context " + file .getName ());
8686 }
8787 });
@@ -194,7 +194,7 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
194194
195195 Assert .assertNotNull ("Client did not find data for " + path ,response );
196196 Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
197- }catch (ExecutionException e ){
197+ }catch (final ExecutionException ignored ){
198198 Assert .fail ("Client did not find data for " + path );
199199 }
200200 }
@@ -216,7 +216,93 @@ public void addDirectoryTestsNoWalk() throws IOException, InterruptedException{
216216 .thenApply (HttpResponse ::body ).get ();
217217
218218 Assert .assertNull ("Client found data for blocked path " + path ,response );
219- }catch (ExecutionException e ){
219+ }catch (final ExecutionException e ){
220+ exception = e ;
221+ }
222+ Assert .assertNotNull ("Client found data for blocked path" ,exception );
223+ }
224+
225+ server .stop ();
226+ }
227+
228+ @ Test
229+ public void addDirectoryTestsNoWalkAdapter () throws IOException , InterruptedException {
230+ final int port = 25022 ;
231+ final SimpleHttpServer server = SimpleHttpServer .create (port );
232+ final FileHandlerAdapter adapter = new FileHandlerAdapter () {
233+ @ Override
234+ public byte [] getBytes (final File file , final byte [] bytes ){
235+ return bytes ;
236+ }
237+
238+ @ Override
239+ public String getName (final File file ){
240+ return file .getName ().substring (0 ,file .getName ().lastIndexOf ('.' ));
241+ }
242+ };
243+ final FileHandler handler = new FileHandler (adapter );
244+ final String contextNoName = "alt" ;
245+ final String contextWName = "altn" ;
246+ final String dirNewName = "dirName" ;
247+
248+ final String testRealFile = "test" , testFileContent = "readme" ;
249+ final String noReadDir = "dirnoread" ;
250+ final String noReadFile = "innerNoRead" ;
251+
252+ final File noWalk = new File ("src/test/resources/directory/nowalk" );
253+
254+ final String context = "" ;
255+
256+ handler .addDirectory (noWalk ); // test file & directory read
257+ handler .addDirectory (contextNoName ,noWalk );
258+ handler .addDirectory (noWalk ,dirNewName );
259+ handler .addDirectory (contextWName ,noWalk ,dirNewName );
260+
261+ server .createContext (context ,handler );
262+ server .start ();
263+
264+ final String [] validPathsToTest = { // valid reads
265+ noWalk .getName () + '/' + testRealFile ,
266+ contextNoName + '/' + noWalk .getName () + '/' + testRealFile ,
267+ dirNewName + '/' + testRealFile ,
268+ contextWName + '/' + dirNewName + '/' + testRealFile
269+ };
270+
271+ for (final String path : validPathsToTest ){
272+ String url = "http://localhost:" + port + '/' + path ;
273+ HttpRequest request = HttpRequest .newBuilder ()
274+ .uri (URI .create (url ))
275+ .build ();
276+
277+ try {
278+ String response = HttpClient .newHttpClient ().sendAsync (request , HttpResponse .BodyHandlers .ofString ())
279+ .thenApply (HttpResponse ::body ).get ();
280+
281+ Assert .assertNotNull ("Client did not find data for " + path ,response );
282+ Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
283+ }catch (final ExecutionException ignored ){
284+ Assert .fail ("Client did not find data for " + path );
285+ }
286+ }
287+
288+ final String [] invalidPathsToTest = {
289+ noWalk .getName () + '/' + noReadDir ,
290+ noWalk .getName () + '/' + noReadDir + '/' + noReadFile
291+ };
292+
293+ for (final String path : invalidPathsToTest ){
294+ String url = "http://localhost:" + port + '/' + path ;
295+ HttpRequest request = HttpRequest .newBuilder ()
296+ .uri (URI .create (url ))
297+ .build ();
298+
299+ Exception exception = null ;
300+ try {
301+ String response = HttpClient .newHttpClient ().sendAsync (request , HttpResponse .BodyHandlers .ofString ())
302+ .thenApply (HttpResponse ::body ).get ();
303+
304+ Assert .assertNull ("Client found data for blocked path " + path ,response );
305+ }catch (final ExecutionException e ){
220306 exception = e ;
221307 }
222308 Assert .assertNotNull ("Client found data for blocked path" ,exception );
@@ -261,7 +347,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
261347
262348 Assert .assertNotNull ("Client did not find data for " + path ,response );
263349 Assert .assertEquals ("Client data did not match server data for " + path ,testFileContent ,response );
264- }catch (ExecutionException e ){
350+ }catch (final ExecutionException ignored ){
265351 Assert .fail ("Client did not find data for " + path );
266352 }
267353 }
@@ -282,7 +368,7 @@ public void addDirectoryTestsWalk() throws IOException, InterruptedException{
282368 .thenApply (HttpResponse ::body ).get ();
283369
284370 Assert .assertNull ("Client found data for blocked path " + path ,response );
285- }catch (ExecutionException e ){
371+ }catch (final ExecutionException e ){
286372 exception = e ;
287373 }
288374 Assert .assertNotNull ("Client found data for blocked path" ,exception );
0 commit comments