Skip to content

Commit 5ed84f6

Browse files
committed
Simplified the Test Server startup
Removed the counting latch as Jetty will only start if it's not already running, so the latch was redundant. The stop wasn't being called anyway (which is good - we want the same server to run throughout the test session, vs restarting for each test class). Also ensure the server binds only to localhost, vs to 0.0.0.0. Fixes #1822
1 parent c58112a commit 5ed84f6

File tree

6 files changed

+6
-62
lines changed

6 files changed

+6
-62
lines changed

src/test/java/org/jsoup/integration/ConnectTest.java

-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.jsoup.parser.HtmlTreeBuilder;
1313
import org.jsoup.parser.Parser;
1414
import org.jsoup.parser.XmlTreeBuilder;
15-
import org.junit.jupiter.api.AfterAll;
1615
import org.junit.jupiter.api.BeforeAll;
1716
import org.junit.jupiter.api.Test;
1817

@@ -41,11 +40,6 @@ public static void setUp() {
4140
echoUrl = EchoServlet.Url;
4241
}
4342

44-
@AfterAll
45-
public static void tearDown() {
46-
TestServer.stop();
47-
}
48-
4943
@Test
5044
public void canConnectToLocalServer() throws IOException {
5145
String url = HelloServlet.Url;

src/test/java/org/jsoup/integration/SessionIT.java

-7
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
import org.jsoup.Connection;
44
import org.jsoup.Jsoup;
55
import org.jsoup.UncheckedIOException;
6-
import org.jsoup.integration.servlets.EchoServlet;
76
import org.jsoup.integration.servlets.FileServlet;
87
import org.jsoup.integration.servlets.SlowRider;
98
import org.jsoup.nodes.Document;
10-
import org.junit.jupiter.api.AfterAll;
119
import org.junit.jupiter.api.BeforeAll;
1210
import org.junit.jupiter.api.Test;
1311

@@ -23,11 +21,6 @@ public static void setUp() {
2321
TestServer.start();
2422
}
2523

26-
@AfterAll
27-
public static void tearDown() {
28-
TestServer.stop();
29-
}
30-
3124
@Test
3225
public void multiThread() throws InterruptedException {
3326
int numThreads = 20;

src/test/java/org/jsoup/integration/SessionTest.java

-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.jsoup.nodes.Document;
99
import org.jsoup.parser.Parser;
1010
import org.jsoup.select.Elements;
11-
import org.junit.jupiter.api.AfterAll;
1211
import org.junit.jupiter.api.BeforeAll;
1312
import org.junit.jupiter.api.Test;
1413

@@ -24,11 +23,6 @@ public static void setUp() {
2423
TestServer.start();
2524
}
2625

27-
@AfterAll
28-
public static void tearDown() {
29-
TestServer.stop();
30-
}
31-
3226
private static Elements keyEls(String key, Document doc) {
3327
return doc.select("th:contains(" + key + ") + td");
3428
}

src/test/java/org/jsoup/integration/TestServer.java

+6-23
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
import org.eclipse.jetty.servlet.ServletHandler;
66
import org.jsoup.integration.servlets.BaseServlet;
77

8-
import java.util.concurrent.atomic.AtomicInteger;
8+
import java.net.InetSocketAddress;
99

1010
public class TestServer {
11-
private static final Server jetty = new Server(0);
11+
private static final Server jetty = new Server(new InetSocketAddress("localhost", 0));
1212
private static final ServletHandler handler = new ServletHandler();
13-
private static AtomicInteger latch = new AtomicInteger(0);
1413

1514
static {
1615
jetty.setHandler(handler);
@@ -21,26 +20,10 @@ private TestServer() {
2120

2221
public static void start() {
2322
synchronized (jetty) {
24-
int count = latch.getAndIncrement();
25-
if (count == 0) {
26-
try {
27-
jetty.start();
28-
} catch (Exception e) {
29-
throw new IllegalStateException(e);
30-
}
31-
}
32-
}
33-
}
34-
35-
public static void stop() {
36-
synchronized (jetty) {
37-
int count = latch.getAndDecrement();
38-
if (count == 0) {
39-
try {
40-
jetty.stop();
41-
} catch (Exception e) {
42-
throw new IllegalStateException(e);
43-
}
23+
try {
24+
jetty.start(); // jetty will safely no-op a start on an already running instance
25+
} catch (Exception e) {
26+
throw new IllegalStateException(e);
4427
}
4528
}
4629
}

src/test/java/org/jsoup/nodes/FormElementTest.java

-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.jsoup.integration.servlets.EchoServlet;
88
import org.jsoup.integration.servlets.FileServlet;
99
import org.jsoup.select.Elements;
10-
import org.junit.jupiter.api.AfterAll;
1110
import org.junit.jupiter.api.BeforeAll;
1211
import org.junit.jupiter.api.Test;
1312

@@ -27,11 +26,6 @@ public static void setUp() {
2726
TestServer.start();
2827
}
2928

30-
@AfterAll
31-
public static void tearDown() {
32-
TestServer.stop();
33-
}
34-
3529
@Test public void hasAssociatedControls() {
3630
//"button", "fieldset", "input", "keygen", "object", "output", "select", "textarea"
3731
String html = "<form id=1><button id=1><fieldset id=2 /><input id=3><keygen id=4><object id=5><output id=6>" +

src/test/java/org/jsoup/nodes/PositionTest.java

-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
package org.jsoup.nodes;
22

33
import org.jsoup.Jsoup;
4-
import org.jsoup.integration.TestServer;
5-
import org.jsoup.integration.servlets.EchoServlet;
64
import org.jsoup.integration.servlets.FileServlet;
75
import org.jsoup.parser.Parser;
8-
import org.junit.jupiter.api.AfterAll;
9-
import org.junit.jupiter.api.BeforeAll;
106
import org.junit.jupiter.api.Test;
117

128
import java.io.IOException;
@@ -145,16 +141,6 @@ class PositionTest {
145141
assertEquals("6,1:80-6,17:96", comment.sourceRange().toString());
146142
}
147143

148-
@BeforeAll
149-
static void setUp() {
150-
TestServer.start();
151-
}
152-
153-
@AfterAll
154-
static void tearDown() {
155-
TestServer.stop();
156-
}
157-
158144
@Test void tracksFromFetch() throws IOException {
159145
String url = FileServlet.urlTo("/htmltests/large.html"); // 280 K
160146
Document doc = Jsoup.connect(url).parser(TrackingParser).get();

0 commit comments

Comments
 (0)