Fix intellij warnings in CustomRequestLogTest.

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-04-15 19:04:30 +10:00
parent 382aed8374
commit 61ead137b2
1 changed files with 35 additions and 27 deletions

View File

@ -14,7 +14,6 @@
package org.eclipse.jetty.test;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
@ -53,6 +52,7 @@ import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.BlockingArrayQueue;
import org.eclipse.jetty.util.DateCache;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.security.Constraint;
import org.eclipse.jetty.util.security.Credential;
import org.junit.jupiter.api.AfterEach;
@ -66,22 +66,24 @@ import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.lessThanOrEqualTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
public class CustomRequestLogTest
{
CustomRequestLog _log;
Server _server;
LocalConnector _connector;
BlockingQueue<String> _entries = new BlockingArrayQueue<>();
BlockingQueue<Long> requestTimes = new BlockingArrayQueue<>();
ServerConnector _serverConnector;
URI _serverURI;
private final BlockingQueue<String> _entries = new BlockingArrayQueue<>();
private final BlockingQueue<Long> requestTimes = new BlockingArrayQueue<>();
private CustomRequestLog _log;
private Server _server;
private LocalConnector _connector;
private ServerConnector _serverConnector;
private URI _serverURI;
private static final long DELAY = 2000;
@BeforeEach
public void before() throws Exception
public void before()
{
_server = new Server();
_connector = new LocalConnector(_server);
@ -111,6 +113,7 @@ public class CustomRequestLogTest
_serverURI = new URI(String.format("http://%s:%d/", host, localPort));
}
@SuppressWarnings("SameParameterValue")
private static SecurityHandler getSecurityHandler(String username, String password, String realm)
{
HashLoginService loginService = new HashLoginService();
@ -213,16 +216,16 @@ public class CustomRequestLogTest
"%{server}a|%{server}p|" +
"%{client}a|%{client}p");
Enumeration e = NetworkInterface.getNetworkInterfaces();
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements())
{
NetworkInterface n = (NetworkInterface)e.nextElement();
NetworkInterface n = e.nextElement();
if (n.isLoopback())
{
Enumeration ee = n.getInetAddresses();
Enumeration<InetAddress> ee = n.getInetAddresses();
while (ee.hasMoreElements())
{
InetAddress i = (InetAddress)ee.nextElement();
InetAddress i = ee.nextElement();
try (Socket client = newSocket(i.getHostAddress(), _serverURI.getPort()))
{
OutputStream os = client.getOutputStream();
@ -233,7 +236,7 @@ public class CustomRequestLogTest
os.write(request.getBytes(StandardCharsets.ISO_8859_1));
os.flush();
String[] log = _entries.poll(5, TimeUnit.SECONDS).split("\\|");
String[] log = Objects.requireNonNull(_entries.poll(5, TimeUnit.SECONDS)).split("\\|");
assertThat(log.length, is(8));
String localAddr = log[0];
@ -444,7 +447,7 @@ public class CustomRequestLogTest
_connector.getResponse("GET / HTTP/1.0\n\n");
String log = _entries.poll(5, TimeUnit.SECONDS);
long requestTime = requestTimes.poll(5, TimeUnit.SECONDS);
long requestTime = getTimeRequestReceived();
DateCache dateCache = new DateCache(CustomRequestLog.DEFAULT_DATE_FORMAT, Locale.getDefault(), "GMT");
assertThat(log, is("RequestTime: [" + dateCache.format(requestTime) + "]"));
}
@ -458,7 +461,8 @@ public class CustomRequestLogTest
_connector.getResponse("GET / HTTP/1.0\n\n");
String log = _entries.poll(5, TimeUnit.SECONDS);
long requestTime = requestTimes.poll(5, TimeUnit.SECONDS);
assertNotNull(log);
long requestTime = getTimeRequestReceived();
DateCache dateCache1 = new DateCache("EEE MMM dd HH:mm:ss zzz yyyy", Locale.getDefault(), "GMT");
DateCache dateCache2 = new DateCache("EEE MMM dd HH:mm:ss zzz yyyy", Locale.getDefault(), "EST");
@ -477,7 +481,8 @@ public class CustomRequestLogTest
_connector.getResponse("GET /delay HTTP/1.0\n\n");
String log = _entries.poll(5, TimeUnit.SECONDS);
long lowerBound = requestTimes.poll(5, TimeUnit.SECONDS);
assertNotNull(log);
long lowerBound = getTimeRequestReceived();
long upperBound = System.currentTimeMillis();
long measuredDuration = Long.parseLong(log);
@ -495,7 +500,8 @@ public class CustomRequestLogTest
_connector.getResponse("GET /delay HTTP/1.0\n\n");
String log = _entries.poll(5, TimeUnit.SECONDS);
long lowerBound = requestTimes.poll(5, TimeUnit.SECONDS);
assertNotNull(log);
long lowerBound = getTimeRequestReceived();
long upperBound = System.currentTimeMillis();
long measuredDuration = Long.parseLong(log);
@ -513,7 +519,8 @@ public class CustomRequestLogTest
_connector.getResponse("GET /delay HTTP/1.0\n\n");
String log = _entries.poll(5, TimeUnit.SECONDS);
long lowerBound = requestTimes.poll(5, TimeUnit.SECONDS);
assertNotNull(log);
long lowerBound = getTimeRequestReceived();
long upperBound = System.currentTimeMillis();
long measuredDuration = Long.parseLong(log);
@ -591,11 +598,6 @@ public class CustomRequestLogTest
fail(log);
}
protected Socket newSocket() throws Exception
{
return newSocket(_serverURI.getHost(), _serverURI.getPort());
}
protected Socket newSocket(String host, int port) throws Exception
{
Socket socket = new Socket(host, port);
@ -620,10 +622,17 @@ public class CustomRequestLogTest
}
}
private long getTimeRequestReceived() throws InterruptedException
{
Long requestTime = requestTimes.poll(5, TimeUnit.SECONDS);
assertNotNull(requestTime);
return requestTime;
}
private class TestServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
Request baseRequest = Objects.requireNonNull(Request.getBaseRequest(request));
@ -668,8 +677,7 @@ public class CustomRequestLogTest
if (request.getContentLength() > 0)
{
InputStream in = request.getInputStream();
while (in.read() > 0);
IO.readBytes(request.getInputStream());
}
}
}