Skip to content

Commit 9cff47c

Browse files
author
Stephane Landelle
committed
Test clean up
1 parent 8eb212b commit 9cff47c

40 files changed

+613
-963
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2010 Ning, Inc.
3+
*
4+
* Ning licenses this file to you under the Apache License, version 2.0
5+
* (the "License"); you may not use this file except in compliance with the
6+
* License. You may obtain a copy of the License at:
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package org.asynchttpclient.async;
17+
18+
import java.io.File;
19+
import java.net.URL;
20+
21+
import org.eclipse.jetty.server.Server;
22+
import org.eclipse.jetty.server.ssl.SslSocketConnector;
23+
import org.eclipse.jetty.util.ssl.SslContextFactory;
24+
import org.slf4j.Logger;
25+
import org.slf4j.LoggerFactory;
26+
import org.testng.annotations.BeforeClass;
27+
28+
public abstract class AbstractBasicHttpsTest extends AbstractBasicTest {
29+
protected static final Logger LOGGER = LoggerFactory.getLogger(AbstractBasicHttpsTest.class);
30+
protected Server server;
31+
protected int port1;
32+
protected int port2;
33+
34+
@BeforeClass(alwaysRun = true)
35+
public void setUpGlobal() throws Exception {
36+
server = new Server();
37+
port1 = findFreePort();
38+
39+
ClassLoader cl = getClass().getClassLoader();
40+
41+
URL keystoreUrl = cl.getResource("ssltest-keystore.jks");
42+
String keyStoreFile = new File(keystoreUrl.toURI()).getAbsolutePath();
43+
LOGGER.info("SSL keystore path: {}", keyStoreFile);
44+
SslContextFactory sslContextFactory = new SslContextFactory(keyStoreFile);
45+
sslContextFactory.setKeyStorePassword("changeit");
46+
47+
String trustStoreFile = new File(cl.getResource("ssltest-cacerts.jks").toURI()).getAbsolutePath();
48+
LOGGER.info("SSL certs path: {}", trustStoreFile);
49+
sslContextFactory.setTrustStore(trustStoreFile);
50+
sslContextFactory.setTrustStorePassword("changeit");
51+
52+
SslSocketConnector connector = new SslSocketConnector(sslContextFactory);
53+
connector.setHost("127.0.0.1");
54+
connector.setPort(port1);
55+
server.addConnector(connector);
56+
57+
server.setHandler(configureHandler());
58+
server.start();
59+
LOGGER.info("Local HTTP server started successfully");
60+
}
61+
}

api/src/test/java/org/asynchttpclient/async/AbstractBasicTest.java

Lines changed: 93 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@
1515
*/
1616
package org.asynchttpclient.async;
1717

18+
import java.io.File;
19+
import java.io.FileOutputStream;
20+
import java.io.IOException;
21+
import java.net.ServerSocket;
22+
import java.nio.charset.Charset;
23+
import java.util.Enumeration;
24+
import java.util.UUID;
25+
26+
import javax.servlet.ServletException;
27+
import javax.servlet.http.Cookie;
28+
import javax.servlet.http.HttpServletRequest;
29+
import javax.servlet.http.HttpServletResponse;
30+
31+
import org.apache.commons.io.FileUtils;
1832
import org.asynchttpclient.AsyncCompletionHandler;
1933
import org.asynchttpclient.AsyncHandler;
2034
import org.asynchttpclient.AsyncHttpClient;
@@ -34,28 +48,56 @@
3448
import org.testng.annotations.AfterClass;
3549
import org.testng.annotations.BeforeClass;
3650

37-
import javax.servlet.ServletException;
38-
import javax.servlet.http.HttpServletRequest;
39-
import javax.servlet.http.HttpServletResponse;
40-
import java.io.IOException;
41-
import java.net.ServerSocket;
42-
import java.util.Enumeration;
43-
4451
public abstract class AbstractBasicTest {
4552
protected final Logger log = LoggerFactory.getLogger(AbstractBasicTest.class);
4653
protected Server server;
4754
protected int port1;
4855
protected int port2;
4956

5057
public final static int TIMEOUT = 30;
58+
public static final File TMP = new File(System.getProperty("java.io.tmpdir"), "ahc-tests-" + UUID.randomUUID().toString().substring(0, 8));
59+
public static final byte[] PATTERN_BYTES = "FooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQix".getBytes(Charset.forName("UTF-16"));
60+
public static final File LARGE_IMAGE_FILE;
61+
public static byte[] LARGE_IMAGE_BYTES;
62+
public static final File SIMPLE_TEXT_FILE;
63+
64+
static {
65+
try {
66+
TMP.mkdirs();
67+
TMP.deleteOnExit();
68+
LARGE_IMAGE_FILE = new File(AbstractBasicTest.class.getClassLoader().getResource("300k.png").toURI());
69+
LARGE_IMAGE_BYTES = FileUtils.readFileToByteArray(LARGE_IMAGE_FILE);
70+
SIMPLE_TEXT_FILE = new File(AbstractBasicTest.class.getClassLoader().getResource("SimpleTextFile.txt").toURI());
71+
} catch (Exception e) {
72+
throw new ExceptionInInitializerError(e);
73+
}
74+
}
75+
76+
public static File createTempFile(byte[] pattern, int repeat) throws IOException {
77+
File tmpFile = File.createTempFile("tmpfile-", ".data", TMP);
78+
tmpFile.deleteOnExit();
79+
FileOutputStream out = null;
80+
try {
81+
out = new FileOutputStream(tmpFile);
82+
for (int i = 0; i < repeat; i++) {
83+
out.write(pattern);
84+
}
85+
86+
long expectedFileSize = PATTERN_BYTES.length * repeat;
87+
Assert.assertEquals(expectedFileSize, tmpFile.length(), "Invalid file length");
88+
89+
return tmpFile;
90+
} finally {
91+
if (out != null) {
92+
out.close();
93+
}
94+
}
95+
}
5196

5297
public static class EchoHandler extends AbstractHandler {
5398

54-
/* @Override */
55-
public void handle(String pathInContext,
56-
Request request,
57-
HttpServletRequest httpRequest,
58-
HttpServletResponse httpResponse) throws IOException, ServletException {
99+
@Override
100+
public void handle(String pathInContext, Request request, HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException, ServletException {
59101

60102
if (httpRequest.getHeader("X-HEAD") != null) {
61103
httpResponse.setContentLength(1);
@@ -68,8 +110,9 @@ public void handle(String pathInContext,
68110
}
69111

70112
if (request.getMethod().equalsIgnoreCase("OPTIONS")) {
71-
httpResponse.addHeader("Allow","GET,HEAD,POST,OPTIONS,TRACE");
72-
};
113+
httpResponse.addHeader("Allow", "GET,HEAD,POST,OPTIONS,TRACE");
114+
}
115+
;
73116

74117
Enumeration<?> e = httpRequest.getHeaderNames();
75118
String param;
@@ -110,9 +153,9 @@ public void handle(String pathInContext,
110153

111154
httpResponse.addHeader("X-KEEP-ALIVE", httpRequest.getRemoteAddr() + ":" + httpRequest.getRemotePort());
112155

113-
javax.servlet.http.Cookie[] cs = httpRequest.getCookies();
156+
Cookie[] cs = httpRequest.getCookies();
114157
if (cs != null) {
115-
for (javax.servlet.http.Cookie c : cs) {
158+
for (Cookie c : cs) {
116159
httpResponse.addCookie(c);
117160
}
118161
}
@@ -142,9 +185,35 @@ public void handle(String pathInContext,
142185
}
143186
}
144187

188+
@BeforeClass(alwaysRun = true)
189+
public void setUpGlobal() throws Exception {
190+
server = new Server();
191+
192+
port1 = findFreePort();
193+
port2 = findFreePort();
194+
195+
Connector listener = new SelectChannelConnector();
196+
197+
listener.setHost("127.0.0.1");
198+
listener.setPort(port1);
199+
200+
server.addConnector(listener);
201+
202+
listener = new SelectChannelConnector();
203+
listener.setHost("127.0.0.1");
204+
listener.setPort(port2);
205+
206+
server.addConnector(listener);
207+
208+
server.setHandler(configureHandler());
209+
server.start();
210+
log.info("Local HTTP server started successfully");
211+
}
212+
145213
@AfterClass(alwaysRun = true)
146214
public void tearDownGlobal() throws Exception {
147-
server.stop();
215+
if (server != null)
216+
server.stop();
148217
}
149218

150219
protected int findFreePort() throws IOException {
@@ -154,8 +223,7 @@ protected int findFreePort() throws IOException {
154223
socket = new ServerSocket(0);
155224

156225
return socket.getLocalPort();
157-
}
158-
finally {
226+
} finally {
159227
if (socket != null) {
160228
socket.close();
161229
}
@@ -174,31 +242,6 @@ public AbstractHandler configureHandler() throws Exception {
174242
return new EchoHandler();
175243
}
176244

177-
@BeforeClass(alwaysRun = true)
178-
public void setUpGlobal() throws Exception {
179-
server = new Server();
180-
181-
port1 = findFreePort();
182-
port2 = findFreePort();
183-
184-
Connector listener = new SelectChannelConnector();
185-
186-
listener.setHost("127.0.0.1");
187-
listener.setPort(port1);
188-
189-
server.addConnector(listener);
190-
191-
listener = new SelectChannelConnector();
192-
listener.setHost("127.0.0.1");
193-
listener.setPort(port2);
194-
195-
server.addConnector(listener);
196-
197-
server.setHandler(configureHandler());
198-
server.start();
199-
log.info("Local HTTP server started successfully");
200-
}
201-
202245
public static class AsyncCompletionHandlerAdapter extends AsyncCompletionHandler<Response> {
203246
public Runnable runnable;
204247

@@ -207,7 +250,7 @@ public Response onCompleted(Response response) throws Exception {
207250
return response;
208251
}
209252

210-
/* @Override */
253+
@Override
211254
public void onThrowable(Throwable t) {
212255
t.printStackTrace();
213256
Assert.fail("Unexpected exception: " + t.getMessage(), t);
@@ -217,35 +260,32 @@ public void onThrowable(Throwable t) {
217260

218261
public static class AsyncHandlerAdapter implements AsyncHandler<String> {
219262

220-
221-
/* @Override */
263+
@Override
222264
public void onThrowable(Throwable t) {
223265
t.printStackTrace();
224266
Assert.fail("Unexpected exception", t);
225267
}
226268

227-
/* @Override */
269+
@Override
228270
public STATE onBodyPartReceived(final HttpResponseBodyPart content) throws Exception {
229271
return STATE.CONTINUE;
230272
}
231273

232-
/* @Override */
274+
@Override
233275
public STATE onStatusReceived(final HttpResponseStatus responseStatus) throws Exception {
234276
return STATE.CONTINUE;
235277
}
236278

237-
/* @Override */
279+
@Override
238280
public STATE onHeadersReceived(final HttpResponseHeaders headers) throws Exception {
239281
return STATE.CONTINUE;
240282
}
241283

242-
/* @Override */
284+
@Override
243285
public String onCompleted() throws Exception {
244286
return "";
245287
}
246-
247288
}
248289

249290
public abstract AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config);
250-
251291
}

api/src/test/java/org/asynchttpclient/async/AsyncProvidersBasicTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.asynchttpclient.StringPart;
6363

6464
public abstract class AsyncProvidersBasicTest extends AbstractBasicTest {
65+
6566
private static final String UTF_8 = "text/html;charset=UTF-8";
6667

6768
@Test(groups = { "standalone", "default_provider", "async" })

api/src/test/java/org/asynchttpclient/async/AsyncStreamHandlerTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
import java.util.concurrent.atomic.AtomicBoolean;
3939

4040
public abstract class AsyncStreamHandlerTest extends AbstractBasicTest {
41-
private final static String RESPONSE = "param_1_";
42-
private final static String UTF8 = "text/html;charset=utf-8";
41+
42+
private static final String RESPONSE = "param_1_";
43+
private static final String UTF8 = "text/html;charset=utf-8";
4344

4445
@Test(groups = { "standalone", "default_provider" })
4546
public void asyncStreamGETTest() throws Throwable {

api/src/test/java/org/asynchttpclient/async/AuthTimeoutTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151

5252
public abstract class AuthTimeoutTest extends AbstractBasicTest {
5353

54-
private final static String user = "user";
54+
private static final String USER = "user";
5555

56-
private final static String admin = "admin";
56+
private static final String ADMIN = "admin";
5757

5858
protected AsyncHttpClient client;
5959

@@ -76,16 +76,16 @@ public void setUpServer(String auth) throws Exception {
7676

7777
Constraint constraint = new Constraint();
7878
constraint.setName(auth);
79-
constraint.setRoles(new String[] { user, admin });
79+
constraint.setRoles(new String[] { USER, ADMIN });
8080
constraint.setAuthenticate(true);
8181

8282
ConstraintMapping mapping = new ConstraintMapping();
8383
mapping.setConstraint(constraint);
8484
mapping.setPathSpec("/*");
8585

8686
Set<String> knownRoles = new HashSet<String>();
87-
knownRoles.add(user);
88-
knownRoles.add(admin);
87+
knownRoles.add(USER);
88+
knownRoles.add(ADMIN);
8989

9090
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
9191

@@ -258,7 +258,7 @@ protected Future<Response> execute(boolean preemptive) throws IOException {
258258
}
259259

260260
private Realm realm(boolean preemptive) {
261-
return (new Realm.RealmBuilder()).setPrincipal(user).setPassword(admin).setUsePreemptiveAuth(preemptive).build();
261+
return (new Realm.RealmBuilder()).setPrincipal(USER).setPassword(ADMIN).setUsePreemptiveAuth(preemptive).build();
262262
}
263263

264264
@Override

0 commit comments

Comments
 (0)