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

Commit f5c2aef

Browse files
committed
server tests
1 parent 77f008b commit f5c2aef

File tree

7 files changed

+122
-103
lines changed

7 files changed

+122
-103
lines changed

src/test/java/simplehttpserver/SimpleHttpServerContextTests.java

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
import com.kttdevelopment.simplehttpserver.*;
44
import com.kttdevelopment.simplehttpserver.handler.RootHandler;
5+
import com.sun.net.httpserver.HttpExchange;
56
import org.junit.Assert;
67
import org.junit.Test;
78

89
import java.io.IOException;
910

10-
public class SimpleHttpServerContextTests {
11+
public final class SimpleHttpServerContextTests {
1112

1213
@Test
13-
public void randContext() throws IOException{
14+
public final void randContext() throws IOException{
1415
final SimpleHttpServer server = SimpleHttpServer.create();
1516

1617
final String initContext = server.getRandomContext();
@@ -21,7 +22,7 @@ public void randContext() throws IOException{
2122
}
2223

2324
@Test
24-
public void randContextHead() throws IOException{
25+
public final void randContextHead() throws IOException{
2526
final SimpleHttpServer server = SimpleHttpServer.create();
2627

2728
final String head = "/head";
@@ -34,23 +35,22 @@ public void randContextHead() throws IOException{
3435
}
3536

3637
@Test
37-
public void removeNullContext() throws IOException{
38+
public final void removeNullContext() throws IOException{
3839
final SimpleHttpServer server = SimpleHttpServer.create();
3940

40-
Exception exception = null;
41-
try{ server.removeContext((String) null);
42-
}catch(final NullPointerException e){ exception = e; }
43-
Assert.assertNotNull("Null string context should throw NPE", exception);
41+
try{
42+
server.removeContext((String) null);
43+
Assert.fail("Null string context should throw NPE");
44+
}catch(final NullPointerException ignored){ }
4445

45-
String context = "";
46-
exception = null;
47-
try{ server.removeContext(context);
48-
}catch(final IllegalArgumentException e){ exception = e; }
49-
Assert.assertNotNull("Server should throw IllegalArgumentException when removing a context that doesn't exist", exception);
46+
try{
47+
server.removeContext("");
48+
Assert.fail("Server should throw IllegalArgumentException when removing a context that doesn't exist");
49+
}catch(final IllegalArgumentException ignored){ }
5050
}
5151

5252
@Test
53-
public void removeContext() throws IOException{
53+
public final void removeContext() throws IOException{
5454
final SimpleHttpServer server = SimpleHttpServer.create();
5555

5656
final String context = "";
@@ -67,7 +67,7 @@ public void removeContext() throws IOException{
6767
}
6868

6969
@Test
70-
public void removeNativeContext() throws IOException{
70+
public final void removeNativeContext() throws IOException{
7171
final SimpleHttpServer server = SimpleHttpServer.create();
7272
String context = "/";
7373

@@ -105,30 +105,27 @@ public void createContext() throws IOException{
105105
}
106106

107107
@Test
108-
public void createRootContext() throws IOException{
109-
final SimpleHttpServer server = SimpleHttpServer.create();
110-
final RootHandler handler = new RootHandler((SimpleHttpHandler) SimpleHttpExchange::close, (SimpleHttpHandler) SimpleHttpExchange::close);
108+
public final void createRootContext() throws IOException{
109+
final SimpleHttpServer server = SimpleHttpServer.create();
110+
final RootHandler handler = new RootHandler(HttpExchange::close,HttpExchange::close);
111111

112112
String context = server.getRandomContext();
113-
Exception exception = null;
114113
try{
115114
server.createContext(context,handler);
116-
}catch(final IllegalArgumentException e){ exception = e; }
117-
Assert.assertNotNull("Server should throw IllegalArgumentException when adding RootHandler to non-root context",exception);
115+
Assert.fail("Server should throw IllegalArgumentException when adding RootHandler to non-root context");
116+
}catch(final IllegalArgumentException ignored){ }
118117

119118
final String[] testRoots = {"/","\\",""};
120119

121-
for(final String testRoot : testRoots){
122-
try{
123-
server.removeContext(server.createContext(testRoot, handler));
124-
}catch(final IllegalArgumentException e){
120+
for(final String testRoot : testRoots)
121+
try{ server.removeContext(server.createContext(testRoot, handler));
122+
}catch(final IllegalArgumentException ignored){
125123
Assert.fail("Server threw IllegalArgumentException for allowed context [" + testRoot + "] for RootHandler");
126124
}
127-
}
128125
}
129126

130127
@Test
131-
public void createSlashedContext() throws IOException{
128+
public final void createSlashedContext() throws IOException{
132129
final SimpleHttpServer server = SimpleHttpServer.create();
133130
final String[] roots = {"/","\\",""};
134131

src/test/java/simplehttpserver/SimpleHttpServerReadTests.java renamed to src/test/java/simplehttpserver/SimpleHttpServerGetTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77
import java.io.IOException;
88

9-
public class SimpleHttpServerReadTests {
9+
public final class SimpleHttpServerGetTests {
1010

1111
@SuppressWarnings("SpellCheckingInspection")
1212
@Test
13-
public void get() throws IOException{
14-
final int port = 10005;
13+
public final void get() throws IOException{
14+
final int port = 8080;
1515

1616
final SimpleHttpServer server = SimpleHttpServer.create();
1717
Assert.assertNotNull("#getHttpServer() should not be null", server.getHttpServer());

src/test/java/simplehttpserver/SimpleHttpServerInitTests.java

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package simplehttpserver.bind;
2+
3+
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
4+
import org.junit.*;
5+
6+
import java.io.IOException;
7+
import java.net.BindException;
8+
9+
public final class SimpleHttpServerBindOccupiedTests {
10+
11+
@Test
12+
public final void testOccupiedPortBind() throws IOException{
13+
final int port = 8080;
14+
final SimpleHttpServer s1 = SimpleHttpServer.create(port);
15+
s1.start();
16+
17+
Exception exception = null;
18+
try{ SimpleHttpServer.create(port);
19+
}catch(final BindException e){ exception = e; }
20+
21+
Assert.assertNotNull("Bind server to occupied port should throw an exception",exception);
22+
23+
s1.stop();
24+
exception = null;
25+
try{ SimpleHttpServer.create(port);
26+
}catch(final BindException e){ exception = e; }
27+
Assert.assertNull("Bind server to now unoccupied port should not throw an exception",exception);
28+
}
29+
30+
}

src/test/java/simplehttpserver/SimpleHttpServerBindTests.java renamed to src/test/java/simplehttpserver/bind/SimpleHttpServerBindRangeTest.java

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
package simplehttpserver;
1+
package simplehttpserver.bind;
22

33
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
4-
import org.junit.*;
4+
import org.junit.Assert;
5+
import org.junit.Test;
56

67
import java.io.IOException;
7-
import java.net.BindException;
88

9-
public class SimpleHttpServerBindTests {
9+
public final class SimpleHttpServerBindRangeTest {
1010

1111
@Test
1212
public void testPortRange() throws IOException{
13-
final int port = 10001;
13+
final int port = 8080;
1414

1515
final SimpleHttpServer server = SimpleHttpServer.create();
1616

1717
Exception exception = null;
1818
try{ server.bind(-1);
1919
}catch(final IllegalArgumentException | IOException e){ exception = e; }
20-
Assert.assertTrue("Bind server to bad port (-1) should throw an exception", exception instanceof IllegalArgumentException);
20+
Assert.assertTrue("Bind server to bad port (-1) should throw an exception", exception instanceof IllegalArgumentException);
2121

2222
exception = null;
2323
try{ server.bind(65536);
2424
}catch(final IllegalArgumentException | IOException e){ exception = e; }
25-
Assert.assertTrue("Bind server to bad port (65536) should throw an exception", exception instanceof IllegalArgumentException);
25+
Assert.assertTrue("Bind server to bad port (65536) should throw an exception", exception instanceof IllegalArgumentException);
2626

2727
exception = null;
2828
try{ server.bind(port);
@@ -33,23 +33,4 @@ public void testPortRange() throws IOException{
3333
Assert.assertEquals("Server bind port should equal address port",port,server.getAddress().getPort());
3434
}
3535

36-
@Test
37-
public void testOccupiedPortBind() throws IOException{
38-
final int port = 10002;
39-
final SimpleHttpServer s1 = SimpleHttpServer.create(port);
40-
s1.start();
41-
42-
Exception exception = null;
43-
try{ SimpleHttpServer.create(port);
44-
}catch(final BindException e){ exception = e; }
45-
s1.stop();
46-
47-
Assert.assertNotNull("Bind server to occupied port should throw an exception",exception);
48-
49-
exception = null;
50-
try{ SimpleHttpServer.create(port);
51-
}catch(final BindException e){ exception = e; }
52-
Assert.assertNull("Bind server to now unoccupied port should not throw an exception",exception);
53-
}
54-
5536
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package simplehttpserver.create;
2+
3+
import com.kttdevelopment.simplehttpserver.SimpleHttpServer;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
9+
public final class SimpleHttpServerCreateTest {
10+
11+
@Test
12+
public final void create() throws IOException{
13+
final int port = 8080;
14+
SimpleHttpServer server = SimpleHttpServer.create();
15+
try{
16+
server.start();
17+
Assert.fail("Start server with no port should throw an exception");
18+
}catch(final IllegalStateException ignored){ }
19+
20+
server.bind(port);
21+
try{
22+
server.stop();
23+
server.stop();
24+
}catch(final IllegalStateException ignored){
25+
Assert.fail("Start server with valid port should not throw an exception");
26+
}
27+
}
28+
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package simplehttpserver.create;
2+
3+
import com.kttdevelopment.simplehttpserver.SimpleHttpsServer;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
7+
import java.io.IOException;
8+
9+
public final class SimpleHttpsServerCreateTest {
10+
11+
@Test
12+
public final void create() throws IOException{
13+
final int port = 8080;
14+
SimpleHttpsServer server = SimpleHttpsServer.create();
15+
try{
16+
server.start();
17+
Assert.fail("Start server with no port should throw an exception");
18+
}catch(final IllegalStateException ignored){ }
19+
20+
server.bind(port);
21+
try{
22+
server.stop();
23+
server.stop();
24+
}catch(final IllegalStateException ignored){
25+
Assert.fail("Start server with valid port should not throw an exception");
26+
}
27+
}
28+
29+
}

0 commit comments

Comments
 (0)