Skip to content

Commit 492fb03

Browse files
jlouvelThierry Boileau
and
Thierry Boileau
authored
Fixes several issues with unit test cases (#1420)
* Fix unit tests for JUnit 5 (partial) With the upgrade to JUnit 5, several unit test weren't working anymore. Most of the code now compile and tests run successfully. Additional work required to fix failing tests. * Update StatusServiceTestCase.java Fixed broken unit test * Fixed resources (service descriptors were not added in jars), fixed TU * Fixed TU * Fixed TU * Fixed TU * Reformat DirectoryTestCase * Fixed DirectoryTestCase * Format test code * Fixed TUs * Fixed TUs * Fixed TUs * Fixed TUs --------- Co-authored-by: Thierry Boileau <[email protected]>
1 parent 4f33d86 commit 492fb03

File tree

80 files changed

+1304
-1772
lines changed

Some content is hidden

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

80 files changed

+1304
-1772
lines changed

modules/org.restlet.test/pom.xml

+11
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,16 @@
167167
<artifactId>snakeyaml</artifactId>
168168
<version>${lib-snakeyaml-version}</version>
169169
</dependency>
170+
<dependency>
171+
<groupId>org.junit.jupiter</groupId>
172+
<artifactId>junit-jupiter-api</artifactId>
173+
<version>${lib-junit-version}</version>
174+
</dependency>
175+
<dependency>
176+
<groupId>org.junit.jupiter</groupId>
177+
<artifactId>junit-jupiter-params</artifactId>
178+
<version>${lib-junit-version}</version>
179+
<scope>compile</scope>
180+
</dependency>
170181
</dependencies>
171182
</project>

modules/org.restlet.test/src/main/java/org/restlet/test/CallTestCase.java

+10
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,13 @@ public void testBaseRef() {
106106
final Reference resourceRef = getReference(resourceRefURI);
107107
request.setResourceRef(resourceRefURI);
108108
assertEquals(resourceRef, request.getResourceRef());
109+
109110
String uri = "http://restlet.org/path";
110111
Reference reference = getReference(uri);
111112
request.getResourceRef().setBaseRef(uri);
112113
assertEquals(uri, request.getResourceRef().getBaseRef().toString());
113114
assertEquals(reference, request.getResourceRef().getBaseRef());
115+
114116
uri = "http://restlet.org/path/to";
115117
reference = getReference(uri);
116118
request.getResourceRef().setBaseRef(uri);
@@ -139,6 +141,7 @@ public void testClientAgent() {
139141
String name = "Restlet";
140142
client.setAgent(name);
141143
assertEquals(name, client.getAgent());
144+
142145
name = "Restlet Client";
143146
client.setAgent(name);
144147
assertEquals(name, client.getAgent());
@@ -156,6 +159,7 @@ public void testClientForwardedAddresses() {
156159
secondAddress });
157160
client.getForwardedAddresses().addAll(addresses);
158161
assertEquals(addresses, client.getForwardedAddresses());
162+
159163
client.getForwardedAddresses().clear();
160164
client.getForwardedAddresses().addAll(addresses);
161165
assertEquals(addresses, client.getForwardedAddresses());
@@ -169,6 +173,7 @@ public void testMethod() {
169173
final Request request = getRequest();
170174
request.setMethod(Method.GET);
171175
assertEquals(Method.GET, request.getMethod());
176+
172177
request.setMethod(Method.POST);
173178
assertEquals(Method.POST, request.getMethod());
174179
}
@@ -184,6 +189,7 @@ public void testRedirectionRef() {
184189
Reference reference = getReference(uri);
185190
response.setLocationRef(uri);
186191
assertEquals(reference, response.getLocationRef());
192+
187193
uri = "http://restlet.org/something";
188194
reference = getReference(uri);
189195
response.setLocationRef(reference);
@@ -200,6 +206,7 @@ public void testReferrerRef() {
200206
Reference reference = getReference(uri);
201207
request.setReferrerRef(uri);
202208
assertEquals(reference, request.getReferrerRef());
209+
203210
uri = "http://restlet.org/something";
204211
reference = getReference(uri);
205212
request.setReferrerRef(reference);
@@ -232,6 +239,7 @@ public void testServerAddress() {
232239
String address = "127.0.0.1";
233240
response.getServerInfo().setAddress(address);
234241
assertEquals(address, response.getServerInfo().getAddress());
242+
235243
address = "192.168.99.10";
236244
response.getServerInfo().setAddress(address);
237245
assertEquals(address, response.getServerInfo().getAddress());
@@ -247,6 +255,7 @@ public void testServerAgent() {
247255
String name = "Restlet";
248256
response.getServerInfo().setAgent(name);
249257
assertEquals(name, response.getServerInfo().getAgent());
258+
250259
name = "Restlet Server";
251260
response.getServerInfo().setAgent(name);
252261
assertEquals(name, response.getServerInfo().getAgent());
@@ -261,6 +270,7 @@ public void testStatus() {
261270
final Response response = getResponse(request);
262271
response.setStatus(Status.SUCCESS_OK);
263272
assertEquals(Status.SUCCESS_OK, response.getStatus());
273+
264274
response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
265275
assertEquals(Status.CLIENT_ERROR_BAD_REQUEST, response.getStatus());
266276
}

modules/org.restlet.test/src/main/java/org/restlet/test/HeaderTestCase.java

+84-122
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
import java.util.Collections;
3131

32+
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.BeforeEach;
3235
import org.junit.jupiter.api.Test;
3336
import org.restlet.Client;
3437
import org.restlet.Component;
@@ -40,129 +43,88 @@
4043
import org.restlet.data.Method;
4144
import org.restlet.data.Protocol;
4245
import org.restlet.data.Status;
43-
import org.restlet.engine.header.HeaderConstants;
44-
import org.restlet.representation.StringRepresentation;
45-
import org.restlet.util.Series;
4646

4747
public class HeaderTestCase extends RestletTestCase {
4848

49-
/**
50-
* Restlet that returns as a new Representation the list of values of
51-
* "testHeader" header.
52-
*/
53-
public static class TestHeaderRestlet extends Restlet {
54-
@Override
55-
public void handle(Request request, Response response) {
56-
StringBuilder stb = new StringBuilder();
57-
Series<Header> headers = getHttpHeaders(request);
58-
59-
for (Header header : headers) {
60-
if (TEST_HEADER.equalsIgnoreCase(header.getName())) {
61-
stb.append(header.getValue());
62-
stb.append('\n');
63-
}
64-
}
65-
66-
response.setEntity(new StringRepresentation(stb,
67-
MediaType.TEXT_PLAIN));
68-
}
69-
}
70-
71-
/**
72-
* Name of a test header field
73-
*/
74-
private static final String TEST_HEADER = "testHeader";
75-
76-
/**
77-
* Returns the list of HTTP headers of a request as a Form.
78-
*
79-
* @param request
80-
* The request.
81-
* @return The list of headers as a Form object.
82-
*/
83-
private static Series<Header> getHttpHeaders(Request request) {
84-
@SuppressWarnings("unchecked")
85-
Series<Header> headers = (Series<Header>) request.getAttributes().get(
86-
HeaderConstants.ATTRIBUTE_HEADERS);
87-
88-
if (headers == null) {
89-
headers = new Series<>(Header.class);
90-
request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS,
91-
headers);
92-
}
93-
94-
return headers;
95-
}
96-
97-
private Client client;
98-
99-
private Component component;
100-
101-
/**
102-
* Handle a new request built according to the parameters and return the
103-
* response object.
104-
*
105-
* @param additionalHeaders
106-
* The list of header used to build the request.
107-
* @return The response of the request.
108-
*/
109-
private Response getWithParams(Header... additionalHeaders) {
110-
Request request = new Request(Method.GET, "http://localhost:"
111-
+ TEST_PORT);
112-
Series<Header> headers = getHttpHeaders(request);
113-
114-
Collections.addAll(headers, additionalHeaders);
115-
116-
return client.handle(request);
117-
}
118-
119-
@Override
120-
public void setUp() throws Exception {
121-
super.setUp();
122-
this.client = new Client(Protocol.HTTP);
123-
124-
if (this.component == null) {
125-
this.component = new Component();
126-
this.component.getServers().add(Protocol.HTTP, TEST_PORT);
127-
this.component.getDefaultHost().attachDefault(
128-
new TestHeaderRestlet());
129-
}
130-
131-
if (!this.component.isStarted()) {
132-
this.component.start();
133-
}
134-
}
135-
136-
@Override
137-
public void tearDown() throws Exception {
138-
this.client.stop();
139-
this.component.stop();
140-
this.component = null;
141-
super.tearDown();
142-
}
143-
144-
/** test with no test header */
145-
@Test
146-
public void test0() throws Exception {
147-
Response response = getWithParams();
148-
assertEquals(Status.SUCCESS_OK, response.getStatus());
149-
assertNull(response.getEntity().getText());
150-
}
151-
152-
/** test with one test header */
153-
@Test
154-
public void test1() throws Exception {
155-
Response response = getWithParams(new Header(TEST_HEADER, "a"));
156-
assertEquals(Status.SUCCESS_OK, response.getStatus());
157-
assertEquals("a\n", response.getEntity().getText());
158-
}
159-
160-
/** test with two test headers */
161-
@Test
162-
public void test2() throws Exception {
163-
Response response = getWithParams(new Header(TEST_HEADER, "a"),
164-
new Header(TEST_HEADER, "b"));
165-
assertEquals(Status.SUCCESS_OK, response.getStatus());
166-
assertEquals("a\nb\n", response.getEntity().getText());
167-
}
49+
/**
50+
* Restlet that returns as a new Representation the list of values of
51+
* "testHeader" header.
52+
*/
53+
public static class TestHeaderRestlet extends Restlet {
54+
@Override
55+
public void handle(Request request, Response response) {
56+
StringBuilder stb = new StringBuilder();
57+
58+
for (Header header : request.getHeaders()) {
59+
if (TEST_HEADER.equalsIgnoreCase(header.getName())) {
60+
stb.append(header.getValue());
61+
stb.append('\n');
62+
}
63+
}
64+
65+
response.setEntity(stb.toString(), MediaType.TEXT_PLAIN);
66+
}
67+
}
68+
69+
/**
70+
* Name of a test header field
71+
*/
72+
private static final String TEST_HEADER = "testHeader";
73+
74+
private Client client;
75+
76+
private Component component;
77+
78+
/**
79+
* Handle a new request built according to the parameters and return the
80+
* response object.
81+
*
82+
* @param additionalHeaders The list of header used to build the request.
83+
* @return The response of the request.
84+
*/
85+
private Response getWithParams(Header... additionalHeaders) {
86+
Request request = new Request(Method.GET, "http://localhost:" + TEST_PORT);
87+
Collections.addAll(request.getHeaders(), additionalHeaders);
88+
return client.handle(request);
89+
}
90+
91+
@BeforeEach
92+
void setUpEach() throws Exception {
93+
this.client = new Client(Protocol.HTTP);
94+
component = new Component();
95+
component.getServers().add(Protocol.HTTP, TEST_PORT);
96+
component.getDefaultHost().attachDefault(new TestHeaderRestlet());
97+
component.start();
98+
}
99+
100+
@AfterEach
101+
void tearDownEach() throws Exception {
102+
this.client.stop();
103+
this.component.stop();
104+
this.component = null;
105+
}
106+
107+
/** Test with no test header */
108+
@Test
109+
public void test0() throws Exception {
110+
Response response = getWithParams();
111+
assertEquals(Status.SUCCESS_OK, response.getStatus());
112+
assertNull(response.getEntity().getText());
113+
}
114+
115+
/** Test with one test header */
116+
@Test
117+
public void test1() throws Exception {
118+
Response response = getWithParams(new Header(TEST_HEADER, "a"));
119+
assertEquals(Status.SUCCESS_OK, response.getStatus());
120+
assertEquals("a\n", response.getEntity().getText());
121+
}
122+
123+
/** Test with two test headers */
124+
@Test
125+
public void test2() throws Exception {
126+
Response response = getWithParams(new Header(TEST_HEADER, "a"), new Header(TEST_HEADER, "b"));
127+
assertEquals(Status.SUCCESS_OK, response.getStatus());
128+
assertEquals("a\nb\n", response.getEntity().getText());
129+
}
168130
}

modules/org.restlet.test/src/main/java/org/restlet/test/MockFilter.java

+15-15
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@
3737
* several threads at the same time and therefore must be thread-safe. You
3838
* should be especially careful when storing state in member variables.
3939
*
40-
* @author Lars Heuer (heuer[at]semagia.com) <a
41-
* href="http://www.semagia.com/">Semagia</a>
40+
* @author Lars Heuer (heuer[at]semagia.com)
41+
* <a href="http://www.semagia.com/">Semagia</a>
4242
*/
4343
public class MockFilter extends Filter {
44-
public MockFilter(Context context) {
45-
super(context);
46-
}
44+
public MockFilter(Context context) {
45+
super(context);
46+
}
4747

48-
@Override
49-
protected int beforeHandle(Request request, Response response) {
50-
if (!super.isStarted()) {
51-
throw new IllegalStateException("Filter is not started");
52-
}
53-
if (!super.hasNext()) {
54-
throw new IllegalStateException("Target is not set");
55-
}
48+
@Override
49+
protected int beforeHandle(Request request, Response response) {
50+
if (!super.isStarted()) {
51+
throw new IllegalStateException("Filter is not started");
52+
}
53+
if (!super.hasNext()) {
54+
throw new IllegalStateException("Target is not set");
55+
}
5656

57-
return CONTINUE;
58-
}
57+
return CONTINUE;
58+
}
5959

6060
}

0 commit comments

Comments
 (0)