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

Upgrade tests to JUnit5 #104

Merged
merged 4 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,15 @@
<dependencies>
<!-- tests -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static String getContext(final String context, final boolean leadingSlash
if(linSlash.isBlank() || linSlash.equals("/")) // handle blank or '/' contexts
return leadingSlash || trailingSlash ? "/" : "";
final String ltSlash = (!(linSlash.charAt(0) == '/') ? '/' : "") + linSlash + (!(linSlash.charAt(linSlash.length()-1) == '/') ? '/' : "");
return ltSlash.substring(leadingSlash ? 0 : 1,ltSlash.length() + (trailingSlash ? 0 : -1));
return ltSlash.substring(leadingSlash ? 0 : 1, ltSlash.length() + (trailingSlash ? 0 : -1));
}

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

return getContext(OUT.toString(),leadingSlash,trailingSlash);
return getContext(OUT.toString(), leadingSlash, trailingSlash);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
public abstract class HttpSession {

static final HashMap<String,HttpSession> sessions = new HashMap<>();
static final HashMap<String, HttpSession> sessions = new HashMap<>();

/**
* Creates an empty {@link HttpSession}. Sessions are usually created by {@link HttpSessionHandler#getSession(HttpExchange)}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
public class HttpSessionHandler {

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

private final String cookie;

Expand Down Expand Up @@ -60,7 +60,7 @@ private String getSetSession(final Headers headers){
if(headers.containsKey("Set-Cookie"))
for(final String value : headers.get("Set-Cookie"))
if(value.startsWith(cookie + "="))
return value.substring(cookie.length() + 1,value.indexOf(";"));
return value.substring(cookie.length() + 1, value.indexOf(";"));
return null;
}

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

@SuppressWarnings("SpellCheckingInspection")
final String rcookies = exchange.getRequestHeaders().getFirst("Cookie");
final Map<String,String> cookies = new HashMap<>();
final Map<String, String> cookies = new HashMap<>();

if(rcookies != null && !rcookies.isEmpty()){
final String[] pairs = rcookies.split("; ");
for(final String pair : pairs){
final String[] value = pair.split("=");
cookies.put(value[0],value[1]);
cookies.put(value[0], value[1]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public final Builder setHttpOnly(final boolean httpOnly){
* @author Ktt Development
*/
public final SimpleHttpCookie build(){
return new SimpleHttpCookie(name,value,domain,path,sameSite,expires,maxAge,secure,httpOnly);
return new SimpleHttpCookie(name, value, domain, path, sameSite, expires, maxAge, secure, httpOnly);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @since 02.00.00
* @author Ktt Development
*/
public abstract Map<String,String> getGetMap();
public abstract Map<String, String> getGetMap();

/**
* Returns if there is a GET request.
Expand Down Expand Up @@ -312,7 +312,7 @@ static SimpleHttpExchange create(final HttpExchange exchange){
* @since 02.00.00
* @author Ktt Development
*/
public abstract Map<String,String> getCookies();
public abstract Map<String, String> getCookies();

/**
* Sets a cookie in the response header.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ final class SimpleHttpExchangeImpl extends SimpleHttpExchange {
private final RequestMethod requestMethod;

private final String rawGet;
private final Map<String,String> getMap;
private final Map<String, String> getMap;
private final boolean hasGet;

private final String rawPost;
@SuppressWarnings("rawtypes")
private final Map postMap;
private final boolean hasPost;

private final Map<String,String> cookies;
private final Map<String, String> cookies;

private final OutputStream outputStream;

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

for(final String pair : pairs){
if(pair.contains("=")){
final String[] kv = pair.split("=");
OUT.put(
URLDecoder.decode(kv[0],StandardCharsets.UTF_8),
kv.length == 2 ? URLDecoder.decode(kv[1],StandardCharsets.UTF_8) : null
URLDecoder.decode(kv[0], StandardCharsets.UTF_8),
kv.length == 2 ? URLDecoder.decode(kv[1], StandardCharsets.UTF_8) : null
);
}
}
Expand Down Expand Up @@ -167,8 +167,8 @@ static SimpleHttpExchange create(final HttpExchange exchange){
}

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

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

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

@Override
public synchronized final void setCookie(final String key, final String value){
setCookie(new SimpleHttpCookie.Builder(key,value).build());
setCookie(new SimpleHttpCookie.Builder(key, value).build());
}

@Override
public synchronized final void setCookie(final SimpleHttpCookie cookie){
final String cstring = cookie.toCookieHeaderString();
getResponseHeaders().add("Set-Cookie",cstring);
getResponseHeaders().add("Set-Cookie", cstring);
}

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

@Override
public synchronized final void send(final int responseCode) throws IOException{
sendResponseHeaders(responseCode,0);
sendResponseHeaders(responseCode, 0);
}

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

@Override
public synchronized final void send(final byte[] response, final int responseCode) throws IOException {
send(response,responseCode,false);
send(response, responseCode, false);
}

@Override
Expand All @@ -363,7 +363,7 @@ public final void send(final byte[] response, final int responseCode, final bool
OUT.flush();
}
}else{
sendResponseHeaders(responseCode,response.length);
sendResponseHeaders(responseCode, response.length);
try(final OutputStream OUT = httpExchange.getResponseBody()){
OUT.write(response);
OUT.flush();
Expand All @@ -383,12 +383,12 @@ public final void send(final String response, final boolean gzip) throws IOExcep

@Override
public synchronized final void send(final String response, final int responseCode) throws IOException{
send(response.getBytes(StandardCharsets.UTF_8),responseCode,false);
send(response.getBytes(StandardCharsets.UTF_8), responseCode, false);
}

@Override
public final void send(final String response, final int responseCode, final boolean gzip) throws IOException{
send(response.getBytes(StandardCharsets.UTF_8),responseCode,gzip);
send(response.getBytes(StandardCharsets.UTF_8), responseCode, gzip);
}

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

@Override
public final void send(final File file, final int responseCode) throws IOException{
send(Files.readAllBytes(file.toPath()),responseCode);
send(Files.readAllBytes(file.toPath()), responseCode);
}

@Override
public final void send(final File file, final int responseCode, final boolean gzip) throws IOException{
send(Files.readAllBytes(file.toPath()),responseCode,gzip);
send(Files.readAllBytes(file.toPath()), responseCode, gzip);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static SimpleHttpServer create(final int port) throws IOException {
* @author Ktt Development
*/
public static SimpleHttpServer create(final int port, final int backlog) throws IOException {
return SimpleHttpServerImpl.createHttpServer(port,backlog);
return SimpleHttpServerImpl.createHttpServer(port, backlog);
}

//
Expand Down Expand Up @@ -414,7 +414,7 @@ public static SimpleHttpServer create(final int port, final int backlog) throws
* @since 02.00.00
* @author Ktt Development
*/
public abstract Map<HttpContext,HttpHandler> getContexts();
public abstract Map<HttpContext, HttpHandler> getContexts();

//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class SimpleHttpServerImpl extends SimpleHttpServer {

private HttpSessionHandler sessionHandler;

private final Map<HttpContext,HttpHandler> contexts = new HashMap<>();
private final Map<HttpContext, HttpHandler> contexts = new HashMap<>();

private boolean running = false;

Expand All @@ -41,12 +41,12 @@ final class SimpleHttpServerImpl extends SimpleHttpServer {
* @author Ktt Development
*/
static SimpleHttpServer createHttpServer(final Integer port, final Integer backlog) throws IOException{
return new SimpleHttpServerImpl(port,backlog);
return new SimpleHttpServerImpl(port, backlog);
}

SimpleHttpServerImpl(final Integer port, final Integer backlog) throws IOException{
if(port != null)
server.bind(new InetSocketAddress(port),backlog != null ? backlog : 0);
server.bind(new InetSocketAddress(port), backlog != null ? backlog : 0);
}

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

@Override
public synchronized final void bind(final InetSocketAddress addr) throws IOException{
server.bind(addr,0);
server.bind(addr, 0);
}

@Override
public synchronized final void bind(final InetSocketAddress addr, final int backlog) throws IOException{
server.bind(addr,backlog);
server.bind(addr, backlog);
}

//
Expand Down Expand Up @@ -130,24 +130,24 @@ public final HttpSession getHttpSession(final SimpleHttpExchange exchange){

@Override
public synchronized final HttpContext createContext(final String context){
return createContext(context,HttpExchange::close,null);
return createContext(context, HttpExchange::close, null);
}

@Override
public synchronized final HttpContext createContext(final String context, final HttpHandler handler){
return createContext(context,handler,null);
return createContext(context, handler, null);
}

//

@Override
public synchronized final HttpContext createContext(final String context, final Authenticator authenticator){
return createContext(context,HttpExchange::close,authenticator);
return createContext(context, HttpExchange::close, authenticator);
}

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

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

hc.setHandler(wrapper);
contexts.put(hc,handler);
contexts.put(hc, handler);

if(authenticator != null)
hc.setAuthenticator(authenticator);
Expand All @@ -173,12 +173,12 @@ public synchronized final HttpContext createContext(final String context, final
@Override
public synchronized final void removeContext(final String context){
try{
server.removeContext(ContextUtil.getContext(context,true,false));
server.removeContext(ContextUtil.getContext(context, true, false));
}catch(final IllegalArgumentException e){
throw e;
}finally{
for(final HttpContext hc : contexts.keySet()){
if(hc.getPath().equalsIgnoreCase(ContextUtil.getContext(context,true,false))){
if(hc.getPath().equalsIgnoreCase(ContextUtil.getContext(context, true, false))){
contexts.remove(hc);
break;
}
Expand All @@ -197,7 +197,7 @@ public synchronized final void removeContext(final HttpContext context){
@Override
public final HttpHandler getContextHandler(final String context){
for(final Map.Entry<HttpContext, HttpHandler> entry : contexts.entrySet())
if(entry.getKey().getPath().equals(ContextUtil.getContext(context,true,false)))
if(entry.getKey().getPath().equals(ContextUtil.getContext(context, true, false)))
return entry.getValue();
return null;
}
Expand All @@ -223,9 +223,9 @@ public synchronized final String getRandomContext(){
public synchronized final String getRandomContext(final String context){
String targetContext;

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

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

return targetContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static SimpleHttpsServer create(final int port) throws IOException {
* @author Ktt Development
*/
public static SimpleHttpsServer create(final int port, final int backlog) throws IOException {
return SimpleHttpsServerImpl.createSimpleHttpsServer(port,backlog);
return SimpleHttpsServerImpl.createSimpleHttpsServer(port, backlog);
}

//
Expand Down
Loading