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

Commit c2493a8

Browse files
authored
Upgrade tests to JUnit5 (#104)
* upgrade tests except temporary folder * Add temporary dir * Fix code style issues * Fix test failures
1 parent 9f89507 commit c2493a8

Some content is hidden

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

48 files changed

+425
-487
lines changed

pom.xml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,15 @@
161161
<dependencies>
162162
<!-- tests -->
163163
<dependency>
164-
<groupId>junit</groupId>
165-
<artifactId>junit</artifactId>
166-
<version>4.13.1</version>
164+
<groupId>org.junit.jupiter</groupId>
165+
<artifactId>junit-jupiter-api</artifactId>
166+
<version>5.7.0</version>
167+
<scope>test</scope>
168+
</dependency>
169+
<dependency>
170+
<groupId>org.junit.jupiter</groupId>
171+
<artifactId>junit-jupiter-params</artifactId>
172+
<version>5.7.0</version>
167173
<scope>test</scope>
168174
</dependency>
169175
</dependencies>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static String getContext(final String context, final boolean leadingSlash
2626
if(linSlash.isBlank() || linSlash.equals("/")) // handle blank or '/' contexts
2727
return leadingSlash || trailingSlash ? "/" : "";
2828
final String ltSlash = (!(linSlash.charAt(0) == '/') ? '/' : "") + linSlash + (!(linSlash.charAt(linSlash.length()-1) == '/') ? '/' : "");
29-
return ltSlash.substring(leadingSlash ? 0 : 1,ltSlash.length() + (trailingSlash ? 0 : -1));
29+
return ltSlash.substring(leadingSlash ? 0 : 1, ltSlash.length() + (trailingSlash ? 0 : -1));
3030
}
3131

3232
/**
@@ -47,7 +47,7 @@ public static String joinContexts(final boolean leadingSlash, final boolean trai
4747
for(final String context : contexts)
4848
OUT.append(getContext(context, true, false));
4949

50-
return getContext(OUT.toString(),leadingSlash,trailingSlash);
50+
return getContext(OUT.toString(), leadingSlash, trailingSlash);
5151
}
5252

5353
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public abstract class HttpSession {
1515

16-
static final HashMap<String,HttpSession> sessions = new HashMap<>();
16+
static final HashMap<String, HttpSession> sessions = new HashMap<>();
1717

1818
/**
1919
* Creates an empty {@link HttpSession}. Sessions are usually created by {@link HttpSessionHandler#getSession(HttpExchange)}.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
public class HttpSessionHandler {
1616

17-
private final Map<String,HttpSession> sessions = Collections.synchronizedMap(new HashMap<>());
17+
private final Map<String, HttpSession> sessions = Collections.synchronizedMap(new HashMap<>());
1818

1919
private final String cookie;
2020

@@ -60,7 +60,7 @@ private String getSetSession(final Headers headers){
6060
if(headers.containsKey("Set-Cookie"))
6161
for(final String value : headers.get("Set-Cookie"))
6262
if(value.startsWith(cookie + "="))
63-
return value.substring(cookie.length() + 1,value.indexOf(";"));
63+
return value.substring(cookie.length() + 1, value.indexOf(";"));
6464
return null;
6565
}
6666

@@ -79,13 +79,13 @@ public final HttpSession getSession(final HttpExchange exchange){
7979

8080
@SuppressWarnings("SpellCheckingInspection")
8181
final String rcookies = exchange.getRequestHeaders().getFirst("Cookie");
82-
final Map<String,String> cookies = new HashMap<>();
82+
final Map<String, String> cookies = new HashMap<>();
8383

8484
if(rcookies != null && !rcookies.isEmpty()){
8585
final String[] pairs = rcookies.split("; ");
8686
for(final String pair : pairs){
8787
final String[] value = pair.split("=");
88-
cookies.put(value[0],value[1]);
88+
cookies.put(value[0], value[1]);
8989
}
9090
}
9191

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public final Builder setHttpOnly(final boolean httpOnly){
383383
* @author Ktt Development
384384
*/
385385
public final SimpleHttpCookie build(){
386-
return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly);
386+
return new SimpleHttpCookie(name, value, domain, path, sameSite, expires, maxAge, secure, httpOnly);
387387
}
388388

389389
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
220220
* @since 02.00.00
221221
* @author Ktt Development
222222
*/
223-
public abstract Map<String,String> getGetMap();
223+
public abstract Map<String, String> getGetMap();
224224

225225
/**
226226
* Returns if there is a GET request.
@@ -312,7 +312,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
312312
* @since 02.00.00
313313
* @author Ktt Development
314314
*/
315-
public abstract Map<String,String> getCookies();
315+
public abstract Map<String, String> getCookies();
316316

317317
/**
318318
* Sets a cookie in the response header.

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

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,29 @@ final class SimpleHttpExchangeImpl extends SimpleHttpExchange {
3838
private final RequestMethod requestMethod;
3939

4040
private final String rawGet;
41-
private final Map<String,String> getMap;
41+
private final Map<String, String> getMap;
4242
private final boolean hasGet;
4343

4444
private final String rawPost;
4545
@SuppressWarnings("rawtypes")
4646
private final Map postMap;
4747
private final boolean hasPost;
4848

49-
private final Map<String,String> cookies;
49+
private final Map<String, String> cookies;
5050

5151
private final OutputStream outputStream;
5252

5353
@SuppressWarnings("FieldCanBeLocal")
54-
private final Function<String,Map<String,String>> parseWwwFormEnc = s -> {
55-
final LinkedHashMap<String,String> OUT = new LinkedHashMap<>();
54+
private final Function<String, Map<String, String>> parseWwwFormEnc = s -> {
55+
final LinkedHashMap<String, String> OUT = new LinkedHashMap<>();
5656
final String[] pairs = s.split("&");
5757

5858
for(final String pair : pairs){
5959
if(pair.contains("=")){
6060
final String[] kv = pair.split("=");
6161
OUT.put(
62-
URLDecoder.decode(kv[0],StandardCharsets.UTF_8),
63-
kv.length == 2 ? URLDecoder.decode(kv[1],StandardCharsets.UTF_8) : null
62+
URLDecoder.decode(kv[0], StandardCharsets.UTF_8),
63+
kv.length == 2 ? URLDecoder.decode(kv[1], StandardCharsets.UTF_8) : null
6464
);
6565
}
6666
}
@@ -167,8 +167,8 @@ static SimpleHttpExchange create(final HttpExchange exchange){
167167
}
168168

169169
final Map row = new HashMap();
170-
row.put("headers",postHeaders);
171-
row.put("value",pair.substring(pair.indexOf("\r\n\r\n")+4,pair.lastIndexOf("\r\n")));
170+
row.put("headers", postHeaders);
171+
row.put("value", pair.substring(pair.indexOf("\r\n\r\n")+4, pair.lastIndexOf("\r\n")));
172172

173173
postMap.put(
174174
((HashMap<String, String>) postHeaders.get("Content-Disposition").get("parameters")).get("name"),
@@ -189,7 +189,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
189189
final String[] cookedCookie = rawCookie.split("; "); // pair
190190
for(final String pair : cookedCookie){
191191
String[] value = pair.split("=");
192-
cookies.put(value[0],value[1]);
192+
cookies.put(value[0], value[1]);
193193
}
194194
}
195195

@@ -307,13 +307,13 @@ public final Map<String, String> getCookies(){
307307

308308
@Override
309309
public synchronized final void setCookie(final String key, final String value){
310-
setCookie(new SimpleHttpCookie.Builder(key,value).build());
310+
setCookie(new SimpleHttpCookie.Builder(key, value).build());
311311
}
312312

313313
@Override
314314
public synchronized final void setCookie(final SimpleHttpCookie cookie){
315315
final String cstring = cookie.toCookieHeaderString();
316-
getResponseHeaders().add("Set-Cookie",cstring);
316+
getResponseHeaders().add("Set-Cookie", cstring);
317317
}
318318

319319
//
@@ -332,7 +332,7 @@ public synchronized final void sendResponseHeaders(final int code, final long le
332332

333333
@Override
334334
public synchronized final void send(final int responseCode) throws IOException{
335-
sendResponseHeaders(responseCode,0);
335+
sendResponseHeaders(responseCode, 0);
336336
}
337337

338338
@Override
@@ -347,7 +347,7 @@ public final void send(final byte[] response, final boolean gzip) throws IOExcep
347347

348348
@Override
349349
public synchronized final void send(final byte[] response, final int responseCode) throws IOException {
350-
send(response,responseCode,false);
350+
send(response, responseCode, false);
351351
}
352352

353353
@Override
@@ -363,7 +363,7 @@ public final void send(final byte[] response, final int responseCode, final bool
363363
OUT.flush();
364364
}
365365
}else{
366-
sendResponseHeaders(responseCode,response.length);
366+
sendResponseHeaders(responseCode, response.length);
367367
try(final OutputStream OUT = httpExchange.getResponseBody()){
368368
OUT.write(response);
369369
OUT.flush();
@@ -383,12 +383,12 @@ public final void send(final String response, final boolean gzip) throws IOExcep
383383

384384
@Override
385385
public synchronized final void send(final String response, final int responseCode) throws IOException{
386-
send(response.getBytes(StandardCharsets.UTF_8),responseCode,false);
386+
send(response.getBytes(StandardCharsets.UTF_8), responseCode, false);
387387
}
388388

389389
@Override
390390
public final void send(final String response, final int responseCode, final boolean gzip) throws IOException{
391-
send(response.getBytes(StandardCharsets.UTF_8),responseCode,gzip);
391+
send(response.getBytes(StandardCharsets.UTF_8), responseCode, gzip);
392392
}
393393

394394
@Override
@@ -403,12 +403,12 @@ public final void send(final File file, final boolean gzip) throws IOException{
403403

404404
@Override
405405
public final void send(final File file, final int responseCode) throws IOException{
406-
send(Files.readAllBytes(file.toPath()),responseCode);
406+
send(Files.readAllBytes(file.toPath()), responseCode);
407407
}
408408

409409
@Override
410410
public final void send(final File file, final int responseCode, final boolean gzip) throws IOException{
411-
send(Files.readAllBytes(file.toPath()),responseCode,gzip);
411+
send(Files.readAllBytes(file.toPath()), responseCode, gzip);
412412
}
413413

414414
//

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static SimpleHttpServer create(final int port) throws IOException {
8282
* @author Ktt Development
8383
*/
8484
public static SimpleHttpServer create(final int port, final int backlog) throws IOException {
85-
return SimpleHttpServerImpl.createHttpServer(port,backlog);
85+
return SimpleHttpServerImpl.createHttpServer(port, backlog);
8686
}
8787

8888
//
@@ -414,7 +414,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
414414
* @since 02.00.00
415415
* @author Ktt Development
416416
*/
417-
public abstract Map<HttpContext,HttpHandler> getContexts();
417+
public abstract Map<HttpContext, HttpHandler> getContexts();
418418

419419
//
420420

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class SimpleHttpServerImpl extends SimpleHttpServer {
2323

2424
private HttpSessionHandler sessionHandler;
2525

26-
private final Map<HttpContext,HttpHandler> contexts = new HashMap<>();
26+
private final Map<HttpContext, HttpHandler> contexts = new HashMap<>();
2727

2828
private boolean running = false;
2929

@@ -41,12 +41,12 @@ final class SimpleHttpServerImpl extends SimpleHttpServer {
4141
* @author Ktt Development
4242
*/
4343
static SimpleHttpServer createHttpServer(final Integer port, final Integer backlog) throws IOException{
44-
return new SimpleHttpServerImpl(port,backlog);
44+
return new SimpleHttpServerImpl(port, backlog);
4545
}
4646

4747
SimpleHttpServerImpl(final Integer port, final Integer backlog) throws IOException{
4848
if(port != null)
49-
server.bind(new InetSocketAddress(port),backlog != null ? backlog : 0);
49+
server.bind(new InetSocketAddress(port), backlog != null ? backlog : 0);
5050
}
5151

5252
private void handle(final HttpExchange exchange){
@@ -79,12 +79,12 @@ public synchronized final InetSocketAddress bind(final int port, final int backl
7979

8080
@Override
8181
public synchronized final void bind(final InetSocketAddress addr) throws IOException{
82-
server.bind(addr,0);
82+
server.bind(addr, 0);
8383
}
8484

8585
@Override
8686
public synchronized final void bind(final InetSocketAddress addr, final int backlog) throws IOException{
87-
server.bind(addr,backlog);
87+
server.bind(addr, backlog);
8888
}
8989

9090
//
@@ -130,24 +130,24 @@ public final HttpSession getHttpSession(final SimpleHttpExchange exchange){
130130

131131
@Override
132132
public synchronized final HttpContext createContext(final String context){
133-
return createContext(context,HttpExchange::close,null);
133+
return createContext(context, HttpExchange::close, null);
134134
}
135135

136136
@Override
137137
public synchronized final HttpContext createContext(final String context, final HttpHandler handler){
138-
return createContext(context,handler,null);
138+
return createContext(context, handler, null);
139139
}
140140

141141
//
142142

143143
@Override
144144
public synchronized final HttpContext createContext(final String context, final Authenticator authenticator){
145-
return createContext(context,HttpExchange::close,authenticator);
145+
return createContext(context, HttpExchange::close, authenticator);
146146
}
147147

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

@@ -159,7 +159,7 @@ public synchronized final HttpContext createContext(final String context, final
159159
final HttpContext hc = server.createContext(ct);
160160

161161
hc.setHandler(wrapper);
162-
contexts.put(hc,handler);
162+
contexts.put(hc, handler);
163163

164164
if(authenticator != null)
165165
hc.setAuthenticator(authenticator);
@@ -173,12 +173,12 @@ public synchronized final HttpContext createContext(final String context, final
173173
@Override
174174
public synchronized final void removeContext(final String context){
175175
try{
176-
server.removeContext(ContextUtil.getContext(context,true,false));
176+
server.removeContext(ContextUtil.getContext(context, true, false));
177177
}catch(final IllegalArgumentException e){
178178
throw e;
179179
}finally{
180180
for(final HttpContext hc : contexts.keySet()){
181-
if(hc.getPath().equalsIgnoreCase(ContextUtil.getContext(context,true,false))){
181+
if(hc.getPath().equalsIgnoreCase(ContextUtil.getContext(context, true, false))){
182182
contexts.remove(hc);
183183
break;
184184
}
@@ -197,7 +197,7 @@ public synchronized final void removeContext(final HttpContext context){
197197
@Override
198198
public final HttpHandler getContextHandler(final String context){
199199
for(final Map.Entry<HttpContext, HttpHandler> entry : contexts.entrySet())
200-
if(entry.getKey().getPath().equals(ContextUtil.getContext(context,true,false)))
200+
if(entry.getKey().getPath().equals(ContextUtil.getContext(context, true, false)))
201201
return entry.getValue();
202202
return null;
203203
}
@@ -223,9 +223,9 @@ public synchronized final String getRandomContext(){
223223
public synchronized final String getRandomContext(final String context){
224224
String targetContext;
225225

226-
final String head = context.isEmpty() ? "" : ContextUtil.getContext(context,true,false);
226+
final String head = context.isEmpty() ? "" : ContextUtil.getContext(context, true, false);
227227

228-
do targetContext = head + ContextUtil.getContext(UUID.randomUUID().toString(),true,false);
228+
do targetContext = head + ContextUtil.getContext(UUID.randomUUID().toString(), true, false);
229229
while(getContextHandler(targetContext) != null);
230230

231231
return targetContext;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static SimpleHttpsServer create(final int port) throws IOException {
7979
* @author Ktt Development
8080
*/
8181
public static SimpleHttpsServer create(final int port, final int backlog) throws IOException {
82-
return SimpleHttpsServerImpl.createSimpleHttpsServer(port,backlog);
82+
return SimpleHttpsServerImpl.createSimpleHttpsServer(port, backlog);
8383
}
8484

8585
//

0 commit comments

Comments
 (0)