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

Commit 30cf82d

Browse files
authored
Merge pull request #89 from Ktt-Development/optimize-test-cases@beed1ea
Optimized test cases.
2 parents beed1ea + 9685b3d commit 30cf82d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1344
-1060
lines changed

pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.kttdevelopment</groupId>
88
<artifactId>simplehttpserver</artifactId>
9-
<version>03.05.07</version>
9+
<version>03.05.08</version>
1010
<packaging>jar</packaging>
1111

1212
<url>https://github.com/Ktt-Development/simplehttpserver</url>
@@ -79,8 +79,22 @@
7979
</plugins>
8080
</build>
8181

82+
<repositories>
83+
<repository>
84+
<id>jitpack.io</id>
85+
<url>https://jitpack.io</url>
86+
</repository>
87+
</repositories>
88+
8289
<dependencies>
8390
<!-- tests -->
91+
<dependency>
92+
<groupId>com.kttdevelopment.core</groupId>
93+
<artifactId>test-util</artifactId>
94+
<version>076417be59</version>
95+
<scope>test</scope>
96+
</dependency>
97+
8498
<dependency>
8599
<groupId>junit</groupId>
86100
<artifactId>junit</artifactId>

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpCookie.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @see SimpleHttpExchange
1010
* @see Builder
1111
* @since 02.00.00
12-
* @version 03.05.00
12+
* @version 03.05.08
1313
* @author Ktt Development
1414
*/
1515
public class SimpleHttpCookie {
@@ -61,7 +61,6 @@ private SimpleHttpCookie(final String name, final String value, final String dom
6161
this.httpOnly = httpOnly;
6262
}
6363

64-
@SuppressWarnings("SpellCheckingInspection")
6564
private final SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
6665

6766
/**

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServer.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* @see SimpleHttpsServer
1818
* @see SimpleHttpHandler
1919
* @since 02.00.00
20-
* @version 03.05.02
20+
* @version 03.05.08
2121
* @author Ktt Development
2222
*/
2323
@SuppressWarnings("SpellCheckingInspection")
@@ -270,12 +270,15 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
270270

271271
/**
272272
* Creates an empty context.
273+
* <br>
274+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
273275
*
274276
* @param context the context
275277
* @return the http context associated with the context
276-
* @throws IllegalArgumentException if the context is invalid or taken
278+
* @throws IllegalArgumentException if the context is invalid or taken*
277279
* @throws NullPointerException if the context is null
278280
*
281+
*
279282
* @see HttpContext
280283
* @see #createContext(String, HttpHandler)
281284
* @see #removeContext(String)
@@ -287,11 +290,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
287290

288291
/**
289292
* Creates a context mapped to a specified {@link HttpHandler}.
293+
* <br>
294+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
290295
*
291296
* @param context the context
292297
* @param handler the handler
293298
* @return the http context associated with the context
294-
* @throws IllegalArgumentException if the context is invalid or taken
299+
* @throws IllegalArgumentException if the context is invalid or taken*
295300
* @throws NullPointerException if the context is null
296301
*
297302
* @see HttpContext
@@ -308,11 +313,13 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
308313

309314
/**
310315
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
316+
* <br>
317+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
311318
*
312319
* @param context the context
313320
* @param authenticator authenticator
314321
* @return the http context associated with the context
315-
* @throws IllegalArgumentException if the context is invalid or taken
322+
* @throws IllegalArgumentException if the context is invalid or taken*
316323
* @throws NullPointerException if the context is null
317324
*
318325
* @see HttpContext
@@ -327,12 +334,14 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
327334

328335
/**
329336
* Creates a context mapped to a specific {@link HttpContext} with an {@link Authenticator}.
337+
* <br>
338+
* * {@link HttpServer} often does not throw an exception for duplicate contexts. <a href="https://github.com/Ktt-Development/simplehttpserver/issues/86">#86</a>
330339
*
331340
* @param context the context
332341
* @param handler the handler
333342
* @param authenticator authenticator
334343
* @return the http context associated with the context
335-
* @throws IllegalArgumentException if the context is invalid or taken
344+
* @throws IllegalArgumentException if the context is invalid or taken*
336345
* @throws NullPointerException if the context is null
337346
*
338347
* @see HttpContext

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpServerImpl.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpServer
1515
* @since 02.00.00
16-
* @version 03.05.03
16+
* @version 03.05.08
1717
* @author Ktt Development
1818
*/
1919
@SuppressWarnings("SpellCheckingInspection")
@@ -147,15 +147,18 @@ public synchronized final HttpContext createContext(final String context, final
147147

148148
@Override
149149
public synchronized final HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator){
150-
if(!ContextUtil.getContext(context,true,false).equals("/") && handler instanceof RootHandler)
150+
final String ct = ContextUtil.getContext(context,true,false);
151+
if(!ct.equals("/") && handler instanceof RootHandler)
151152
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
152153

153154
final HttpHandler wrapper = exchange -> {
154155
handle(exchange);
155156
handler.handle(exchange);
156157
};
157158

158-
final HttpContext hc = server.createContext(ContextUtil.getContext(context,true,false),wrapper);
159+
final HttpContext hc = server.createContext(ct);
160+
161+
hc.setHandler(wrapper);
159162
contexts.put(hc,handler);
160163

161164
if(authenticator != null)

src/main/java/com/kttdevelopment/simplehttpserver/SimpleHttpsServerImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @see SimpleHttpsServer
1515
* @since 03.04.00
16-
* @version 03.05.03
16+
* @version 03.05.08
1717
* @author Ktt Development
1818
*/
1919
@SuppressWarnings("SpellCheckingInspection")
@@ -73,7 +73,7 @@ public final HttpsConfigurator getHttpsConfigurator(){
7373
return server.getHttpsConfigurator();
7474
}
7575

76-
// region copySimpleHttpServerImpl
76+
// region copy
7777

7878
@Override
7979
public synchronized final InetSocketAddress bind(final int port) throws IOException{
@@ -159,15 +159,18 @@ public synchronized final HttpContext createContext(final String context, final
159159

160160
@Override
161161
public synchronized final HttpContext createContext(final String context, final HttpHandler handler, final Authenticator authenticator){
162-
if(!ContextUtil.getContext(context,true,false).equals("/") && handler instanceof RootHandler)
162+
final String ct = ContextUtil.getContext(context,true,false);
163+
if(!ct.equals("/") && handler instanceof RootHandler)
163164
throw new IllegalArgumentException("RootHandler can only be used at the root '/' context");
164165

165166
final HttpHandler wrapper = exchange -> {
166167
handle(exchange);
167168
handler.handle(exchange);
168169
};
169170

170-
final HttpContext hc = server.createContext(ContextUtil.getContext(context,true,false),wrapper);
171+
final HttpContext hc = server.createContext(ct);
172+
173+
hc.setHandler(wrapper);
171174
contexts.put(hc,handler);
172175

173176
if(authenticator != null)
@@ -264,7 +267,7 @@ public synchronized final void stop(final int delay){
264267
}
265268
}
266269

267-
// endregion
270+
// endregion copy
268271

269272
@Override
270273
public String toString(){

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @version 03.05.06
2222
* @author Ktt Development
2323
*/
24+
@SuppressWarnings("SpellCheckingInspection")
2425
class DirectoryEntry {
2526

2627
private final File directory;

src/main/java/com/kttdevelopment/simplehttpserver/var/HttpCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @version 01.00.00
88
* @author Ktt Development
99
*/
10-
@SuppressWarnings({"unused", "SpellCheckingInspection"})
10+
@SuppressWarnings({"unused", "SpellCheckingInspection", "RedundantSuppression"})
1111
public abstract class HttpCode {
1212

1313
public static final int HTTP_Continue = 100;

src/main/java/com/kttdevelopment/simplehttpserver/var/RequestMethod.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
* @version 01.00.00
88
* @author Ktt Development
99
*/
10-
@SuppressWarnings("SpellCheckingInspection")
1110
public enum RequestMethod {
1211
GET,
1312
HEAD,

src/test/java/ContextUtilTests.java

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,91 @@
22
import org.junit.Assert;
33
import org.junit.Test;
44

5-
public class ContextUtilTests {
5+
import java.util.Arrays;
6+
7+
public final class ContextUtilTests {
8+
9+
private static final class test{
10+
11+
private final String expected, context;
12+
private final boolean leadingSlash, trailingSlash;
13+
14+
public test(final String expected, final String context, final boolean leadingSlash, final boolean trailingSlash){
15+
this.expected = expected;
16+
this.context = context;
17+
this.leadingSlash = leadingSlash;
18+
this.trailingSlash = trailingSlash;
19+
}
620

7-
@Test
8-
public void testContexts(){
9-
Assert.assertEquals("Failed test on blank (leading slash)" ,"/", ContextUtil.getContext("",true,false));
10-
Assert.assertEquals("Failed test on blank (trailing slash)" ,"/", ContextUtil.getContext("",false,true));
11-
Assert.assertEquals("Failed test on blank (both slashes)" ,"/", ContextUtil.getContext("",true,true));
12-
13-
Assert.assertEquals("Failed test on single (leading slash)" ,"/a" , ContextUtil.getContext("a",true,false));
14-
Assert.assertEquals("Failed test on single (trailing slash)" ,"a/" , ContextUtil.getContext("a",false,true));
15-
Assert.assertEquals("Failed test on single (both slashes)" ,"/a/", ContextUtil.getContext("a",true,true));
16-
17-
Assert.assertEquals("/testLeading" , ContextUtil.getContext("testLeading",true,false));
18-
Assert.assertEquals("testTrailing/" , ContextUtil.getContext("testTrailing",false,true));
19-
Assert.assertEquals("testNone" , ContextUtil.getContext("/testNone/",false,false));
20-
Assert.assertEquals("/testBoth/" , ContextUtil.getContext("testBoth",true,true));
21-
22-
Assert.assertEquals("testBackSlash/" , ContextUtil.getContext("testBackSlash\\",false,true));
23-
Assert.assertEquals("testConsBackSlash/", ContextUtil.getContext("testConsBackSlash\\\\",false,true));
24-
Assert.assertEquals("testConsFwdSlash/" , ContextUtil.getContext("testConsFwdSlash//",false,true));
2521
}
2622

2723
@Test
28-
public void testJoin(){
29-
Assert.assertEquals("Failed test on last blank join" ,"a" ,ContextUtil.joinContexts(false,false,"a",""));
30-
Assert.assertEquals("Failed test on last blank join+","/a/",ContextUtil.joinContexts(true,true,"a",""));
24+
public final void testContexts(){
25+
final test[] tests = {
26+
new test("" , "/" , false , false),
27+
new test("/" , "" , true , false),
28+
new test("/" , "" , false , true ),
29+
new test("/" , "" , true , true ),
30+
new test("a" , "a" , false , false),
31+
new test("/a" , "a" , true , false),
32+
new test("a/" , "a" , false , true ),
33+
new test("/a/" , "a" , true , true ),
34+
new test("testNone" , "/testNone/" , false , false),
35+
new test("/testLeading" , "testLeading" , true , false),
36+
new test("testTrailing/" , "testTrailing" , false , true ),
37+
new test("/testBoth/" , "testBoth" , true , true ),
38+
new test("testNoneBackSlash" , "\\testNoneBackSlash\\" , false , false),
39+
new test("/testBackSlash/" , "\\testBackSlash\\" , true , true ),
40+
new test("/testConsecutiveBackSlash/" , "\\\\testConsecutiveBackSlash\\\\", true , true ),
41+
new test("/testConsecutiveForwardSlash/" , "//testConsecutiveForwardSlash//" , true , true )
42+
};
3143

32-
Assert.assertEquals("Failed test on first blank join" ,"a" ,ContextUtil.joinContexts(false,false,"","a"));
33-
Assert.assertEquals("Failed test on first blank join+","/a/",ContextUtil.joinContexts(true,true,"","a"));
44+
for(final test test : tests)
45+
Assert.assertEquals(String.format("Incorrect context for #(\"%s\", %s, %s)", test.context,test.leadingSlash,test.trailingSlash),test.expected,ContextUtil.getContext(test.context,test.leadingSlash,test.trailingSlash));
46+
}
3447

35-
Assert.assertEquals("Failed test on both blank join" ,"" ,ContextUtil.joinContexts(false,false,"",""));
36-
Assert.assertEquals("Failed test on both blank join+","/",ContextUtil.joinContexts(true,true,"",""));
48+
//
3749

38-
Assert.assertEquals("trailing/slash" ,ContextUtil.joinContexts(false,false,"trailing/","slash/"));
39-
Assert.assertEquals("/trailing/slash+/" ,ContextUtil.joinContexts(true,true,"trailing/","slash+/"));
50+
private static final class testJoin {
4051

41-
Assert.assertEquals("leading/slash" ,ContextUtil.joinContexts(false,false,"/leading","/slash"));
42-
Assert.assertEquals("/leading/slash+/" ,ContextUtil.joinContexts(true,true,"/leading","/slash+"));
52+
private final String expected;
53+
private final String[] contexts;
54+
private final boolean leadingSlash, trailingSlash;
4355

44-
Assert.assertEquals("double/slash" ,ContextUtil.joinContexts(false,false,"/double/","/slash/"));
45-
Assert.assertEquals("/double/slash+/" ,ContextUtil.joinContexts(true,true,"/double/","/slash+/"));
56+
public testJoin(final String expected, final boolean leadingSlash, final boolean trailingSlash, final String... contexts){
57+
this.expected = expected;
58+
this.leadingSlash = leadingSlash;
59+
this.trailingSlash = trailingSlash;
60+
this.contexts = contexts;
61+
}
4662

47-
Assert.assertEquals("no/slash" ,ContextUtil.joinContexts(false,false,"no","slash"));
48-
Assert.assertEquals("/no/slash+/" ,ContextUtil.joinContexts(true,true,"no","slash+"));
63+
}
4964

50-
Assert.assertEquals("cons/slash" ,ContextUtil.joinContexts(false,false,"/cons/","/slash/"));
51-
Assert.assertEquals("/cons/slash+/" ,ContextUtil.joinContexts(true,true,"/cons/","/slash+/"));
65+
@Test
66+
public final void testJoin(){
67+
final testJoin[] tests = {
68+
new testJoin("testBlank" ,false ,false ,"testBlank",""),
69+
new testJoin("/testBlank/" ,true ,true ,"testBlank",""),
70+
new testJoin("testBlank" ,false ,false ,"","testBlank"),
71+
new testJoin("/testBlank/" ,true ,true ,"","testBlank"),
72+
new testJoin("" ,false ,false ,"",""),
73+
new testJoin("/" ,true ,true ,"",""),
74+
new testJoin("trailing/slash" , false , false ,"trailing/","slash/"),
75+
new testJoin("/trailing/slash/" , true , true ,"trailing/","slash/"),
76+
new testJoin("leading/slash" , false , false ,"leading/","slash/"),
77+
new testJoin("/leading/slash/" , true , true ,"leading/","slash/"),
78+
new testJoin("double/slash" , false , false ,"/double/","/slash/"),
79+
new testJoin("/double/slash/" , true , true ,"/double/","/slash/"),
80+
new testJoin("no/slash" , false , false ,"no","slash"),
81+
new testJoin("/no/slash/" , true , true ,"no","slash"),
82+
new testJoin("consecutive/slash" , false , false ,"//consecutive//","//slash//"),
83+
new testJoin("/consecutive/slash/" , true , true ,"//consecutive//","//slash//"),
84+
new testJoin("mixed/slash" , false , false ,"\\mixed\\","//slash//"),
85+
new testJoin("/mixed/slash/" , true , true ,"\\mixed\\","//slash//"),
86+
};
5287

53-
Assert.assertEquals("mix/slash" ,ContextUtil.joinContexts(false,false,"\\mix\\","/slash/"));
54-
Assert.assertEquals("/mix/slash+/" ,ContextUtil.joinContexts(true,true,"\\mix\\","/slash+/"));
88+
for(final testJoin test : tests)
89+
Assert.assertEquals(String.format("Incorrect context for #(%s, %s, %s)", test.leadingSlash, test.trailingSlash, Arrays.toString(test.contexts)), test.expected, ContextUtil.joinContexts(test.leadingSlash, test.trailingSlash, test.contexts));
5590
}
5691

5792
}

0 commit comments

Comments
 (0)