You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2019/06/05 12:33:15 UTC

[sling-whiteboard] 06/08: Improved test support classes naming and documentation

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/url-connection-agent-testing
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 126660db83c510e9dbb3d2be92aeb034381c7239
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 14:20:44 2019 +0200

    Improved test support classes naming and documentation
---
 .../org/apache/sling/uca/impl/IntegrationTest.java |  5 ++--
 .../sling/uca/impl/MisbehavingServerControl.java   | 31 ++++++++++++++++++++++
 ...erRule.java => MisbehavingServerExtension.java} | 23 ++++++++--------
 .../org/apache/sling/uca/impl/ServerControl.java   |  6 -----
 4 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
index aa02dd1..9dec971 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/IntegrationTest.java
@@ -29,13 +29,12 @@ import java.net.URL;
 import java.net.URLConnection;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.sling.uca.impl.ServerRule.MisbehavingServer;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-@ExtendWith(ServerRule.class)
+@ExtendWith(MisbehavingServerExtension.class)
 public class IntegrationTest {
     
     private static final Logger LOG = LoggerFactory.getLogger(IntegrationTest.class);
@@ -66,7 +65,7 @@ public class IntegrationTest {
      * @throws IOException various I/O problems
      */
     @Test
-    public void readTimeout(@MisbehavingServer ServerControl server) throws IOException {
+    public void readTimeout(MisbehavingServerControl server) throws IOException {
         
         SocketTimeoutException exception = assertThrows(SocketTimeoutException.class, 
             () -> assertTimeout(ofSeconds(10),  () -> runTest("http://localhost:" + server.getLocalPort()))
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java
new file mode 100644
index 0000000..9b0bf93
--- /dev/null
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerControl.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.uca.impl;
+
+/**
+ * Allows control of a local server
+ *
+ */
+public interface MisbehavingServerControl {
+
+    /**
+     * Returns the port on which the local server is bound
+     * 
+     * @return the port
+     */
+    int getLocalPort();
+}
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
similarity index 85%
rename from url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
rename to url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
index 80d4fc4..702b82f 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerRule.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/MisbehavingServerExtension.java
@@ -17,10 +17,6 @@
 package org.apache.sling.uca.impl;
 
 import java.io.IOException;
-import java.lang.annotation.ElementType;
-import java.lang.annotation.Retention;
-import java.lang.annotation.RetentionPolicy;
-import java.lang.annotation.Target;
 import java.util.concurrent.TimeUnit;
 
 import javax.servlet.ServletException;
@@ -41,11 +37,14 @@ import org.junit.jupiter.api.extension.ParameterResolver;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolver, ServerControl {
-    
-    @Retention(RetentionPolicy.RUNTIME)
-    @Target(ElementType.PARAMETER)
-    public @interface MisbehavingServer { }
+/**
+ * Provides an Jetty-based local server that can be configured to timeout
+ * 
+ * <p>After extending a JUnit Jupiter test with this extension, any parameter of type {@link MisbehavingServerControl}
+ * will be resolved.</p>
+ *
+ */
+class MisbehavingServerExtension implements BeforeAllCallback, AfterAllCallback, ParameterResolver, MisbehavingServerControl {
     
     private final Logger logger = LoggerFactory.getLogger(getClass());
     
@@ -58,16 +57,16 @@ class ServerRule implements BeforeAllCallback, AfterAllCallback, ParameterResolv
     @Override
     public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
             throws ParameterResolutionException {
-        return parameterContext.isAnnotated(MisbehavingServer.class);
+        return parameterContext.getParameter().getType() == MisbehavingServerControl.class;
     }
     
     @Override
     public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
             throws ParameterResolutionException {
-        if ( parameterContext.getParameter().getType() == ServerControl.class )
+        if ( parameterContext.getParameter().getType() == MisbehavingServerControl.class )
             return this;
         
-        throw new ParameterResolutionException("Unable to get a Server instance for " + parameterContext);
+        throw new ParameterResolutionException("Unable to get a " + MisbehavingServerControl.class.getSimpleName() + " instance for " + parameterContext);
     }
     
     @Override
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
deleted file mode 100644
index e73d682..0000000
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/ServerControl.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package org.apache.sling.uca.impl;
-
-public interface ServerControl {
-
-    int getLocalPort();
-}