diff --git a/s3auth-hosts/pom.xml b/s3auth-hosts/pom.xml index ba7512d5..bc778d3e 100644 --- a/s3auth-hosts/pom.xml +++ b/s3auth-hosts/pom.xml @@ -185,6 +185,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. org.hamcrest hamcrest-core 1.3 + test diff --git a/s3auth-hosts/src/main/java/com/s3auth/hosts/DirectoryListing.java b/s3auth-hosts/src/main/java/com/s3auth/hosts/DirectoryListing.java index 2ae6c8a6..a6b82834 100644 --- a/s3auth-hosts/src/main/java/com/s3auth/hosts/DirectoryListing.java +++ b/s3auth-hosts/src/main/java/com/s3auth/hosts/DirectoryListing.java @@ -132,7 +132,7 @@ public int status() { @Override public long writeTo(final OutputStream stream) throws IOException { stream.write(this.content); - return (long) this.content.length; + return this.content.length; } @Override diff --git a/s3auth-relay/pom.xml b/s3auth-relay/pom.xml index 28d1fcb8..33cfdd51 100644 --- a/s3auth-relay/pom.xml +++ b/s3auth-relay/pom.xml @@ -146,11 +146,6 @@ OF THE POSSIBILITY OF SUCH DAMAGE. runtime - - com.google.code.findbugs - annotations - 3.0.1u2 - com.rexsl rexsl-test @@ -162,6 +157,16 @@ OF THE POSSIBILITY OF SUCH DAMAGE. commons-net 3.10.0 + + com.google.guava + guava + + + org.hamcrest + hamcrest-core + 1.3 + test + @@ -244,6 +249,7 @@ OF THE POSSIBILITY OF SUCH DAMAGE. xml:/src/it/settings.xml xml:/src/main/production/pom.xml + duplicatefinder:.* diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/FtpFacade.java b/s3auth-relay/src/main/java/com/s3auth/relay/FtpFacade.java index be6838f4..6c544dde 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/FtpFacade.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/FtpFacade.java @@ -76,23 +76,17 @@ final class FtpFacade implements Closeable { /** * Executor service, with socket openers. */ - private final transient ScheduledExecutorService frontend = - Executors.newScheduledThreadPool(2, new VerboseThreads("FTP-front")); + private final transient ScheduledExecutorService frontend; /** * Executor service, with consuming threads. */ - private final transient ScheduledExecutorService backend = - Executors.newScheduledThreadPool( - FtpFacade.THREADS, - new VerboseThreads("FTP-back") - ); + private final transient ScheduledExecutorService backend; /** * Blocking queue of ready-to-be-processed sockets. */ - private final transient BlockingQueue sockets = - new SynchronousQueue<>(); + private final transient BlockingQueue sockets; /** * Server socket. @@ -105,12 +99,21 @@ final class FtpFacade implements Closeable { * @param port Port number * @throws IOException If can't initialize */ + @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") FtpFacade(@NotNull final Hosts hosts, final int port) throws IOException { + this.frontend = Executors.newScheduledThreadPool( + 2, new VerboseThreads("FTP-front") + ); + this.backend = Executors.newScheduledThreadPool( + FtpFacade.THREADS, + new VerboseThreads("FTP-back") + ); + this.sockets = new SynchronousQueue<>(); this.server = new ServerSocket(port); final FtpThread thread = new FtpThread(this.sockets, hosts); final Runnable runnable = new VerboseRunnable( - new FtpFacade.FTPThreadRunnable(thread), true, true + new FtpThreadRunnable(thread), true, true ); for (int idx = 0; idx < FtpFacade.THREADS; ++idx) { this.backend.scheduleWithFixedDelay( @@ -125,9 +128,7 @@ final class FtpFacade implements Closeable { */ public void listen() { this.frontend.scheduleWithFixedDelay( - new VerboseRunnable( - () -> FtpFacade.this.process(FtpFacade.this.server) - ), + new VerboseRunnable(() -> this.process(this.server)), 0L, 1L, TimeUnit.NANOSECONDS ); } @@ -206,8 +207,10 @@ private void shutdown(final ExecutorService service) /** * Dispatcher of FTPThread. + * + * @since 0.0.1 */ - private static final class FTPThreadRunnable implements Runnable { + private static final class FtpThreadRunnable implements Runnable { /** * The thread to run. */ @@ -217,7 +220,7 @@ private static final class FTPThreadRunnable implements Runnable { * Constructor. * @param thrd The FTPThread */ - FTPThreadRunnable(final FtpThread thrd) { + FtpThreadRunnable(final FtpThread thrd) { this.thread = thrd; } diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/FtpResponse.java b/s3auth-relay/src/main/java/com/s3auth/relay/FtpResponse.java index 1b80852e..05bbf468 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/FtpResponse.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/FtpResponse.java @@ -80,7 +80,7 @@ public FtpResponse withText(@NotNull final String text) { * @return The response's canonical String representation ($code $text) */ public String asString() { - return ""; + return this.toString(); } @Override @@ -92,6 +92,7 @@ public String toString() { * Send it to the socket. * @param socket The socket to write to * @return How many bytes were actually sent + * @checkstyle NonStaticMethodCheck (10 lines) */ @Loggable( value = Loggable.DEBUG, limit = Integer.MAX_VALUE, diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/HttpFacade.java b/s3auth-relay/src/main/java/com/s3auth/relay/HttpFacade.java index d3d69cd1..6a0b7330 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/HttpFacade.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/HttpFacade.java @@ -79,23 +79,17 @@ final class HttpFacade implements Closeable { /** * Executor service, with socket openers. */ - private final transient ScheduledExecutorService frontend = - Executors.newScheduledThreadPool(2, new VerboseThreads("front")); + private final transient ScheduledExecutorService frontend; /** * Executor service, with consuming threads. */ - private final transient ScheduledExecutorService backend = - Executors.newScheduledThreadPool( - HttpFacade.THREADS, - new VerboseThreads("back") - ); + private final transient ScheduledExecutorService backend; /** * Blocking queue of ready-to-be-processed sockets. */ - private final transient BlockingQueue sockets = - new SynchronousQueue<>(); + private final transient BlockingQueue sockets; /** * Server socket. @@ -114,9 +108,16 @@ final class HttpFacade implements Closeable { * @param sslport SSL port number. * @throws IOException If can't initialize */ + @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") HttpFacade(@NotNull final Hosts hosts, final int port, final int sslport) throws IOException { + this.frontend = Executors.newScheduledThreadPool(2, new VerboseThreads("front")); this.server = new ServerSocket(port); + this.backend = Executors.newScheduledThreadPool( + HttpFacade.THREADS, + new VerboseThreads("back") + ); + this.sockets = new SynchronousQueue<>(); this.secured = SSLServerSocketFactory.getDefault() .createServerSocket(sslport); final Runnable runnable = new VerboseRunnable( @@ -139,13 +140,13 @@ final class HttpFacade implements Closeable { public void listen() { this.frontend.scheduleWithFixedDelay( new VerboseRunnable( - () -> HttpFacade.this.process(HttpFacade.this.server) + () -> this.process(this.server) ), 0L, 1L, TimeUnit.NANOSECONDS ); this.frontend.scheduleWithFixedDelay( new VerboseRunnable( - () -> HttpFacade.this.process(HttpFacade.this.secured) + () -> this.process(this.secured) ), 0L, 1L, TimeUnit.NANOSECONDS ); @@ -232,12 +233,15 @@ private void shutdown(final ExecutorService service) /** * Dispatcher of HttpThread. + * + * @since 0.0.1 */ private static final class HttpThreadRunnable implements Runnable { /** * The thread to run. */ private final transient HttpThread thread; + /** * Constructor. * @param thrd The HttpThread @@ -245,6 +249,7 @@ private static final class HttpThreadRunnable implements Runnable { HttpThreadRunnable(final HttpThread thrd) { this.thread = thrd; } + @Override public void run() { try { diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/HttpRequest.java b/s3auth-relay/src/main/java/com/s3auth/relay/HttpRequest.java index d364ef5c..f2feb85c 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/HttpRequest.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/HttpRequest.java @@ -40,6 +40,7 @@ import java.net.Socket; import java.net.URI; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -52,7 +53,6 @@ import lombok.EqualsAndHashCode; import lombok.ToString; import org.apache.commons.collections4.map.CaseInsensitiveMap; -import org.apache.commons.io.Charsets; import org.apache.commons.lang3.StringUtils; /** @@ -141,9 +141,10 @@ final class HttpRequest { * @see HTTP * @checkstyle ExecutableStatementCountCheck (100 lines) */ + @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors") HttpRequest(@NotNull final Socket socket) throws IOException { final BufferedReader reader = new BufferedReader( - new InputStreamReader(socket.getInputStream(), Charsets.UTF_8) + new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8) ); final String top = reader.readLine(); if (top == null) { @@ -159,7 +160,7 @@ final class HttpRequest { String.format("invalid first line: '%s'", top) ); } - this.parms = Collections.unmodifiableMap(this.parseParameters(top)); + this.parms = Collections.unmodifiableMap(HttpRequest.parseParameters(top)); final String method = matcher.group(1); if (!"GET".equals(method) && !"HEAD".equals(method)) { throw new HttpException( @@ -187,7 +188,7 @@ final class HttpRequest { } headers.add(line); } - this.hdrs = Collections.unmodifiableMap(this.parseHeaders(headers)); + this.hdrs = Collections.unmodifiableMap(HttpRequest.parseHeaders(headers)); } /** @@ -260,7 +261,7 @@ public Range range() throws HttpException { * @return Map of headers * @throws HttpException If some socket problem */ - private Map> parseHeaders( + private static Map> parseHeaders( final Iterable lines) throws HttpException { final Map> map = new CaseInsensitiveMap<>(); @@ -287,7 +288,7 @@ private Map> parseHeaders( * @param request Request string * @return Map of headers */ - private Map> parseParameters( + private static Map> parseParameters( final CharSequence request) { final Map> map = new HashMap<>(0); diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/HttpResponse.java b/s3auth-relay/src/main/java/com/s3auth/relay/HttpResponse.java index d53118b5..5ad00ee9 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/HttpResponse.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/HttpResponse.java @@ -37,6 +37,7 @@ import java.io.Writer; import java.net.HttpURLConnection; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.LinkedList; import java.util.concurrent.ConcurrentHashMap; @@ -44,7 +45,6 @@ import javax.validation.constraints.NotNull; import lombok.EqualsAndHashCode; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.io.Charsets; /** * HTTP response, writable to IO socket. @@ -157,7 +157,7 @@ public HttpResponse withBody(@NotNull final String text) { ) public long send(@NotNull final Socket socket) throws IOException { final OutputStream stream = socket.getOutputStream(); - final Writer writer = new OutputStreamWriter(stream, Charsets.UTF_8); + final Writer writer = new OutputStreamWriter(stream, StandardCharsets.UTF_8); try { writer.write( String.format( diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/LocalHost.java b/s3auth-relay/src/main/java/com/s3auth/relay/LocalHost.java index b6637528..8928d7b8 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/LocalHost.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/LocalHost.java @@ -38,7 +38,6 @@ import com.s3auth.hosts.Resource; import com.s3auth.hosts.Stats; import com.s3auth.hosts.Version; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import java.io.IOException; import java.net.HttpURLConnection; import java.net.URI; @@ -75,6 +74,7 @@ final class LocalHost implements Host { * @param name The name of host, provided in "Host" HTTP header * @return TRUE if this is a localhost */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") public static boolean isIt(@NotNull final String name) { return "relay.s3auth.com".equals(name); } @@ -136,7 +136,6 @@ public Stats stats() { * @return The exception to throw */ @SuppressWarnings("PMD.DoNotCallSystemExit") - @SuppressFBWarnings("DM_EXIT") private IOException halt(final String uri) { if (uri.equals(LocalHost.SHUTDOWN)) { Logger.warn(this, "fetch(%s): shutting down..", uri); @@ -155,6 +154,8 @@ private IOException halt(final String uri) { /** * Dummy host stats. + * + * @since 0.0.1 */ private static final class DummyStats implements Stats { @Override diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/Main.java b/s3auth-relay/src/main/java/com/s3auth/relay/Main.java index 0e92dc0c..3279eabf 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/Main.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/Main.java @@ -29,7 +29,6 @@ */ package com.s3auth.relay; -import com.amazonaws.SDKGlobalConfiguration; import com.jcabi.aspects.Immutable; import com.jcabi.aspects.Loggable; import com.jcabi.log.Logger; @@ -65,6 +64,7 @@ private Main() { * @todo #213:30min Create a FtpFacade in order to provide a FTP gateway. * Also unignore test 'connectDisconnect' in FtpFacadeTest. */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") public static void main(final String[] args) throws Exception { final OptionParser parser = new OptionParser("p:s:d"); final OptionSet options = parser.parse(args); @@ -83,10 +83,6 @@ public static void main(final String[] args) throws Exception { final HttpFacade facade = new HttpFacade(new DynamoHosts(), port, secured); facade.listen(); - System.setProperty( - SDKGlobalConfiguration.ENABLE_S3_SIGV4_SYSTEM_PROPERTY, - "true" - ); Logger.warn(Main.class, "started at http://localhost:%d...", port); if (options.has("d")) { while (true) { diff --git a/s3auth-relay/src/main/java/com/s3auth/relay/SecuredHost.java b/s3auth-relay/src/main/java/com/s3auth/relay/SecuredHost.java index 5dc35dcb..77e92ac7 100644 --- a/s3auth-relay/src/main/java/com/s3auth/relay/SecuredHost.java +++ b/s3auth-relay/src/main/java/com/s3auth/relay/SecuredHost.java @@ -36,9 +36,9 @@ import com.s3auth.hosts.Stats; import com.s3auth.hosts.Version; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.validation.constraints.NotNull; @@ -46,7 +46,6 @@ import lombok.EqualsAndHashCode; import lombok.ToString; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.lang3.CharEncoding; /** * Single HTTP processing thread. @@ -169,14 +168,10 @@ private Resource secured(final URI uri, final Range range, ); } final String[] parts; - try { - parts = new String( - Base64.decodeBase64(matcher.group(1)), - CharEncoding.UTF_8 - ).split(":", 2); - } catch (final UnsupportedEncodingException ex) { - throw new IllegalStateException(ex); - } + parts = new String( + Base64.decodeBase64(matcher.group(1)), + StandardCharsets.UTF_8 + ).split(":", 2); if (parts.length != 2) { throw new HttpException( HttpURLConnection.HTTP_BAD_REQUEST, diff --git a/s3auth-relay/src/mock/java/com/s3auth/relay/HttpRequestMocker.java b/s3auth-relay/src/mock/java/com/s3auth/relay/HttpRequestMocker.java index 91d5e128..22765dec 100644 --- a/s3auth-relay/src/mock/java/com/s3auth/relay/HttpRequestMocker.java +++ b/s3auth-relay/src/mock/java/com/s3auth/relay/HttpRequestMocker.java @@ -30,16 +30,19 @@ package com.s3auth.relay; import java.net.Socket; +import java.nio.charset.StandardCharsets; import org.apache.commons.io.IOUtils; import org.mockito.Mockito; /** * Mocker of {@link HttpRequest}. + * + * @since 0.0.1 */ public final class HttpRequestMocker { /** - * It's a unility class at the moment. + * It's a utility class at the moment. */ private HttpRequestMocker() { // intentionally empty @@ -51,9 +54,10 @@ private HttpRequestMocker() { * @return Requests * @throws Exception If there is some problem inside */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") public static HttpRequest toRequest(final String text) throws Exception { final Socket socket = Mockito.mock(Socket.class); - Mockito.doReturn(IOUtils.toInputStream(text)) + Mockito.doReturn(IOUtils.toInputStream(text, StandardCharsets.UTF_8)) .when(socket).getInputStream(); return new HttpRequest(socket); } diff --git a/s3auth-relay/src/mock/java/com/s3auth/relay/HttpResponseMocker.java b/s3auth-relay/src/mock/java/com/s3auth/relay/HttpResponseMocker.java index e9e146a6..2dc6e5cb 100644 --- a/s3auth-relay/src/mock/java/com/s3auth/relay/HttpResponseMocker.java +++ b/s3auth-relay/src/mock/java/com/s3auth/relay/HttpResponseMocker.java @@ -35,11 +35,13 @@ /** * Mocker of {@link HttpResponse}. + * + * @since 0.0.1 */ public final class HttpResponseMocker { /** - * It's a unility class at the moment. + * It's a utility class at the moment. */ private HttpResponseMocker() { // intentionally empty @@ -51,6 +53,7 @@ private HttpResponseMocker() { * @return Text form * @throws Exception If there is some problem inside */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") public static String toString(final HttpResponse resp) throws Exception { final Socket socket = Mockito.mock(Socket.class); final ByteArrayOutputStream stream = new ByteArrayOutputStream(); diff --git a/s3auth-relay/src/mock/java/com/s3auth/relay/PortMocker.java b/s3auth-relay/src/mock/java/com/s3auth/relay/PortMocker.java index d386e3b5..d38ebe86 100644 --- a/s3auth-relay/src/mock/java/com/s3auth/relay/PortMocker.java +++ b/s3auth-relay/src/mock/java/com/s3auth/relay/PortMocker.java @@ -33,11 +33,13 @@ /** * Mocker of HTTP port. + * + * @since 0.0.1 */ public final class PortMocker { /** - * It's a unility class at the moment. + * It's a utility class at the moment. */ private PortMocker() { // intentionally empty @@ -47,8 +49,9 @@ private PortMocker() { * Find and return the first available port. * @return The port number */ + @SuppressWarnings("PMD.ProhibitPublicStaticMethods") public static int reserve() { - int port; + final int port; try { final ServerSocket socket = new ServerSocket(0); try { diff --git a/s3auth-relay/src/test/aspect/.gitkeep b/s3auth-relay/src/test/aspect/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/FtpFacadeTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/FtpFacadeTest.java index 3c9f28f2..fd4ab2bb 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/FtpFacadeTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/FtpFacadeTest.java @@ -49,6 +49,8 @@ /** * Test case for {@link FtpFacade}. + * + * @since 0.0.1 */ final class FtpFacadeTest { @@ -58,7 +60,7 @@ final class FtpFacadeTest { */ @Test @Disabled - public void connectDisconnect() throws IOException { + void connectDisconnect() throws IOException { final FtpFacade facade = FtpFacadeTest.mockFacade(); final FTPClient ftp = new FTPClient(); try { @@ -85,20 +87,19 @@ public void connectDisconnect() throws IOException { private static FtpFacade mockFacade() throws IOException { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(new Date(5000L)) - .when(answer).lastModified(); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(new Date(5000L)) + .when(answer).lastModified(); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); return new FtpFacade(hosts, PortMocker.reserve()); diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/FtpResponseTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/FtpResponseTest.java index d2a716c3..876d3bf4 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/FtpResponseTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/FtpResponseTest.java @@ -37,6 +37,8 @@ /** * Test case for {@link com.s3auth.relay.FtpResponse}. + * + * @since 0.0.1 */ final class FtpResponseTest { @@ -45,13 +47,12 @@ final class FtpResponseTest { */ @Test @Disabled - public void constructsCorrectly() { + void constructsCorrectly() { MatcherAssert.assertThat( new FtpResponse() .withCode(FTPReply.COMMAND_OK) .withText("hi!") - .asString() - , + .asString(), Matchers.equalTo("200 hi!") ); } diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/HttpExceptionTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/HttpExceptionTest.java index 76a82ddc..47d54cf0 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/HttpExceptionTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/HttpExceptionTest.java @@ -37,6 +37,8 @@ /** * Test case for {@link HttpException}. + * + * @since 0.0.1 */ final class HttpExceptionTest { diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/HttpFacadeTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/HttpFacadeTest.java index 448d17c3..701dfa9b 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/HttpFacadeTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/HttpFacadeTest.java @@ -45,6 +45,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URI; +import java.nio.charset.StandardCharsets; import java.util.Date; import java.util.concurrent.TimeUnit; import java.util.zip.GZIPInputStream; @@ -52,7 +53,6 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.RandomStringUtils; import org.apache.http.client.utils.DateUtils; @@ -65,6 +65,7 @@ /** * Test case for {@link HttpFacade}. + * * @since 0.0.1 */ @SuppressWarnings({ @@ -82,16 +83,15 @@ final class HttpFacadeTest { void handlesParallelHttpRequests() throws Exception { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - TimeUnit.SECONDS.sleep(1L); - throw new IllegalStateException("hello, world!"); - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + TimeUnit.SECONDS.sleep(1L); + throw new IllegalStateException("hello, world!"); + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -116,20 +116,19 @@ void handlesParallelHttpRequests() throws Exception { void handlesIfModifiedSinceHeader() throws Exception { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(new Date(5000L)) - .when(answer).lastModified(); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(new Date(5000L)) + .when(answer).lastModified(); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -165,7 +164,7 @@ void handlesIfModifiedSinceHeader() throws Exception { ) .header( HttpHeaders.IF_MODIFIED_SINCE, - DateUtils.formatDate(new Date(10000L)) + DateUtils.formatDate(new Date(10_000L)) ).uri().back().fetch().as(RestResponse.class) .assertStatus(HttpURLConnection.HTTP_NOT_MODIFIED); } finally { @@ -182,20 +181,19 @@ void respondsWithLastModifiedHeader() throws Exception { final Date date = new Date(); final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(date) - .when(answer).lastModified(); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(date) + .when(answer).lastModified(); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -233,19 +231,18 @@ void respondsWithLastModifiedHeader() throws Exception { void respondsWithAgeHeader() throws Exception { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - Thread.sleep(1100L); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + Thread.sleep(1100L); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -284,22 +281,21 @@ void canParseVersionQuery() throws Exception { final String version = "1234"; final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - MatcherAssert.assertThat( - ((Version) inv.getArguments()[2]).version(), - Matchers.is(version) - ); - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + MatcherAssert.assertThat( + ((Version) inv.getArguments()[2]).version(), + Matchers.is(version) + ); + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -332,22 +328,21 @@ void canParseVersionQuery() throws Exception { void getsLatestVersion() throws Exception { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - MatcherAssert.assertThat( - (Version) inv.getArguments()[2], - Matchers.is(Version.LATEST) - ); - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + MatcherAssert.assertThat( + (Version) inv.getArguments()[2], + Matchers.is(Version.LATEST) + ); + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -380,22 +375,21 @@ void getsLatestVersion() throws Exception { void getsVersionListing() throws Exception { final Host host = Mockito.mock(Host.class); Mockito.doAnswer( - (Answer) inv -> { - MatcherAssert.assertThat( - (Version) inv.getArguments()[2], - Matchers.is(Version.LIST) - ); - final Resource answer = Mockito.mock(Resource.class); - Mockito.doReturn(HttpURLConnection.HTTP_OK) - .when(answer).status(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + MatcherAssert.assertThat( + (Version) inv.getArguments()[2], + Matchers.is(Version.LIST) + ); + final Resource answer = Mockito.mock(Resource.class); + Mockito.doReturn(HttpURLConnection.HTTP_OK) + .when(answer).status(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -429,23 +423,22 @@ void getsCompressedContent() throws Exception { final Host host = Mockito.mock(Host.class); final String body = "compressed"; Mockito.doAnswer( - (Answer) inv -> { - final Resource answer = Mockito.spy( - new ResourceMocker().init() - .withContent(body) - .withHeaders("gzip") - .mock() - ); - Mockito.doReturn("text/plain") - .when(answer).contentType(); - return answer; - } - ).when(host) - .fetch( - Mockito.any(URI.class), - Mockito.any(Range.class), - Mockito.any(Version.class) - ); + (Answer) inv -> { + final Resource answer = Mockito.spy( + new ResourceMocker().init() + .withContent(body) + .withHeaders("gzip") + .mock() + ); + Mockito.doReturn("text/plain") + .when(answer).contentType(); + return answer; + } + ).when(host).fetch( + Mockito.any(URI.class), + Mockito.any(Range.class), + Mockito.any(Version.class) + ); final Hosts hosts = Mockito.mock(Hosts.class); Mockito.doReturn(host).when(hosts).find(Mockito.anyString()); final int port = PortMocker.reserve(); @@ -472,7 +465,7 @@ void getsCompressedContent() throws Exception { new GZIPInputStream( new ByteArrayInputStream(resp.binary()) ), - Charsets.UTF_8 + StandardCharsets.UTF_8 ), Matchers.is(body) ); @@ -492,7 +485,7 @@ void getsCompressedContent() throws Exception { */ @Test @Disabled - public void getsContentOverSSL() throws Exception { + void getsContentOverSsl() throws Exception { MatcherAssert.assertThat( System.getProperty("javax.net.ssl.keyStore"), Matchers.notNullValue() diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/HttpRequestTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/HttpRequestTest.java index a8b42818..432ec7bc 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/HttpRequestTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/HttpRequestTest.java @@ -32,10 +32,12 @@ import com.s3auth.hosts.Range; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** * Test case for {@link HttpRequest}. + * * @since 0.0.1 */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") @@ -224,12 +226,14 @@ void supportsHeadMethod() throws Exception { /** * HttpRequest handles invalid URI correctly. - * @throws Exception If something goes wrong */ - @Test(expected = HttpException.class) - public void handlesInvalidUriCorrectly() throws Exception { - HttpRequestMocker.toRequest( - "HEAD /%7B%7B%20item[' HTTP/1.1\nHost:local\nAccept:text/plain\n" + @Test + void handlesInvalidUriCorrectly() { + Assertions.assertThrows( + HttpException.class, + () -> HttpRequestMocker.toRequest( + "HEAD /%7B%7B%20item[' HTTP/1.1\nHost:local\nAccept:text/plain\n" + ) ); } diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/HttpResponseTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/HttpResponseTest.java index 35ac6bcf..e2f61183 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/HttpResponseTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/HttpResponseTest.java @@ -38,6 +38,7 @@ import java.net.HttpURLConnection; import java.net.ServerSocket; import java.net.Socket; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -46,7 +47,6 @@ import java.util.concurrent.TimeUnit; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; -import org.apache.commons.io.Charsets; import org.apache.commons.io.IOUtils; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -85,7 +85,7 @@ void sendsDataToSocket() throws Exception { */ @Test @SuppressWarnings("PMD.DoNotUseThreads") - public void sendsDataFromSlowResource() throws Exception { + void sendsDataFromSlowResource() throws Exception { final String content = "\u0433 some text"; // @checkstyle AnonInnerLength (50 lines) final Resource res = new Resource() { @@ -97,32 +97,38 @@ public long writeTo(final OutputStream stream) { throw new IllegalStateException(ex); } final PrintWriter writer = new PrintWriter( - new OutputStreamWriter(stream, Charsets.UTF_8) + new OutputStreamWriter(stream, StandardCharsets.UTF_8) ); writer.print(content); writer.flush(); - return (long) content.getBytes().length; + return content.getBytes().length; } + @Override public int status() { return HttpURLConnection.HTTP_OK; } + @Override public Collection headers() { return Collections.emptyList(); } + @Override public String etag() { return ""; } + @Override public Date lastModified() { return new Date(); } + @Override public String contentType() { return "text/plain"; } + @Override // @checkstyle MethodBodyComments (2 lines) public void close() { @@ -139,7 +145,7 @@ public void close() { final Socket reading = server.accept(); received.append( IOUtils.toString( - reading.getInputStream(), Charsets.UTF_8 + reading.getInputStream(), StandardCharsets.UTF_8 ) ); reading.close(); diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/LocalHostTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/LocalHostTest.java index 2a097a7d..72364ebb 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/LocalHostTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/LocalHostTest.java @@ -41,6 +41,8 @@ /** * Test case for {@link LocalHost}. + * + * @since 0.0.1 */ final class LocalHostTest { diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/MainTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/MainTest.java index 2b9e7154..2f82d20b 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/MainTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/MainTest.java @@ -36,10 +36,13 @@ import java.util.concurrent.TimeUnit; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; /** * Test case for {@link Main}. + * + * @since 0.0.1 */ final class MainTest { @@ -50,9 +53,9 @@ final class MainTest { * in runtime. We should find a way to mock it properly. */ @Test - @org.junit.Ignore + @Disabled @SuppressWarnings("PMD.DoNotUseThreads") - public void startsAndListensOnPort() throws Exception { + void startsAndListensOnPort() throws Exception { final int port = PortMocker.reserve(); final CountDownLatch done = new CountDownLatch(1); final Thread thread = new Thread( diff --git a/s3auth-relay/src/test/java/com/s3auth/relay/SecuredHostTest.java b/s3auth-relay/src/test/java/com/s3auth/relay/SecuredHostTest.java index e4a35717..8573d329 100644 --- a/s3auth-relay/src/test/java/com/s3auth/relay/SecuredHostTest.java +++ b/s3auth-relay/src/test/java/com/s3auth/relay/SecuredHostTest.java @@ -37,9 +37,9 @@ import com.s3auth.hosts.Version; import java.net.HttpURLConnection; import java.net.URI; +import java.nio.charset.StandardCharsets; import javax.ws.rs.core.HttpHeaders; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.lang3.CharEncoding; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Assert; @@ -48,6 +48,8 @@ /** * Test case for {@link SecuredHost}. + * + * @since 0.0.1 */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", @@ -134,28 +136,34 @@ void reportsToStringWhenAuthorizationFails() throws Exception { public String toString() { return "hello, world!"; } + @Override public boolean isHidden(final URI uri) { return true; } + @Override public boolean authorized(final String user, final String pwd) { return false; } + @Override public Resource fetch(final URI uri, final Range range, final Version version) { throw new UnsupportedOperationException(); } + @Override public void close() { throw new UnsupportedOperationException(); } + @Override public String syslog() { throw new UnsupportedOperationException(); } + @Override public Stats stats() { throw new UnsupportedOperationException(); @@ -201,7 +209,7 @@ void recognizesCredentialsWithSpecialCharacters() throws Exception { "GET / HTTP/1.1\nAuthorization: Basic %s\n\n", Base64.encodeBase64String( String.format("%s:%s", user, password) - .getBytes(CharEncoding.UTF_8) + .getBytes(StandardCharsets.UTF_8) ) ) )