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/06 15:10:11 UTC

[sling-whiteboard] 13/16: Prepared testing infrastructure for multiple client implementations

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

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit f7693f15424d2242626e5d9833f720e86ce6a675
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Wed Jun 5 17:39:07 2019 +0200

    Prepared testing infrastructure for multiple client implementations
---
 url-connection-agent/pom.xml                       | 26 +++++++++++
 .../java/org/apache/sling/uca/impl/AgentIT.java    | 32 ++++++++++++--
 .../test/java/org/apache/sling/uca/impl/Main.java  | 51 ++++++++++++++++++++--
 3 files changed, 101 insertions(+), 8 deletions(-)

diff --git a/url-connection-agent/pom.xml b/url-connection-agent/pom.xml
index 1e96414..f6b36e8 100644
--- a/url-connection-agent/pom.xml
+++ b/url-connection-agent/pom.xml
@@ -32,6 +32,7 @@
     <build>
         <plugins>
             <plugin>
+                <!-- package the agent as an all-in-one jar -->
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <executions>
@@ -58,6 +59,25 @@
                 </executions>
             </plugin>
             <plugin>
+                <!-- save certain jars in a well-known location for easy usage in tests -->
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-dependency-plugin</artifactId>
+                <executions>
+                    <execution>
+                        <id>copy-it-dependencies</id>
+                        <phase>pre-integration-test</phase>
+                        <goals>
+                            <goal>copy-dependencies</goal>
+                        </goals>
+                    </execution>
+                </executions>
+                <configuration>
+                    <outputDirectory>${project.build.directory}/it-dependencies</outputDirectory>
+                    <stripVersion>true</stripVersion>
+                </configuration>
+            </plugin>
+            <plugin>
+                <!-- Run certain ITs in the integration-test phase, once the agent jar is packaged -->
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-failsafe-plugin</artifactId>
                 <executions>
@@ -95,5 +115,11 @@
             <version>5.4.2</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>commons-httpclient</groupId>
+            <artifactId>commons-httpclient</artifactId>
+            <version>3.1</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
\ No newline at end of file
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/AgentIT.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/AgentIT.java
index 8cda1ae..58e713e 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/AgentIT.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/AgentIT.java
@@ -20,6 +20,7 @@ import static java.time.Duration.ofSeconds;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTimeout;
 
+import java.io.File;
 import java.io.IOException;
 import java.lang.ProcessBuilder.Redirect;
 import java.net.SocketTimeoutException;
@@ -27,6 +28,8 @@ import java.net.URL;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import org.junit.jupiter.api.Test;
@@ -88,9 +91,8 @@ public class AgentIT {
     private RecordedThrowable runTest(String urlSpec) throws IOException, InterruptedException {
 
         Process process = runForkedCommandWithAgent(new URL(urlSpec), 3, 3);
-        int exitCode = process.waitFor();
+        boolean done = process.waitFor(10, TimeUnit.SECONDS);
         
-        LOG.info("Exited with code {}", exitCode);
         LOG.info("Dump of stdout: ");
         Files
             .lines(STDOUT)
@@ -101,6 +103,13 @@ public class AgentIT {
             .lines(STDERR)
             .forEach(LOG::info);
 
+        if ( !done ) {
+            process.destroy();
+            throw new IllegalStateException("Terminated process since it did not exit in a reasonable amount of time.");
+        }
+        int exitCode = process.exitValue();
+        LOG.info("Exited with code {}", exitCode);
+        
         if ( exitCode != 0 ) {
             return Files.lines(STDERR)
                 .filter( l -> l.startsWith("Exception in thread \"main\""))
@@ -118,6 +127,8 @@ public class AgentIT {
             .filter( p -> p.getFileName().toString().endsWith("-jar-with-dependencies.jar"))
             .findFirst()
             .orElseThrow( () -> new IllegalStateException("Did not find the agent jar. Did you run mvn package first?"));
+        
+        String classPath = buildClassPath();
 
         String javaHome = System.getProperty("java.home");
         Path javaExe = Paths.get(javaHome, "bin", "java");
@@ -126,9 +137,10 @@ public class AgentIT {
             "-showversion",
             "-javaagent:" + jar +"=" + TimeUnit.SECONDS.toMillis(connectTimeoutSeconds) +"," + TimeUnit.SECONDS.toMillis(readTimeoutSeconds),
             "-cp",
-            Paths.get("target", "classes").toString(),
+            classPath,
             "org.apache.sling.uca.impl.Main",
-            url.toString()
+            url.toString(),
+            "JavaNet"
         );
         
         pb.redirectInput(Redirect.INHERIT);
@@ -138,6 +150,18 @@ public class AgentIT {
         return pb.start();
     }
     
+    private String buildClassPath() throws IOException {
+        
+        List<String> elements = new ArrayList<>();
+        elements.add(Paths.get("target", "test-classes").toString());
+        
+        Files.list(Paths.get("target", "it-dependencies"))
+            .filter( p -> p.getFileName().toString().startsWith("commons-"))
+            .forEach( p -> elements.add(p.toString()));
+        
+        return String.join(File.pathSeparator, elements);
+    }
+
     private RecordedThrowable newRecordedThrowable(String string) {
      
         string = string.replace("Exception in thread \"main\"", "");
diff --git a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/Main.java b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/Main.java
index e6bc48d..baf7feb 100644
--- a/url-connection-agent/src/test/java/org/apache/sling/uca/impl/Main.java
+++ b/url-connection-agent/src/test/java/org/apache/sling/uca/impl/Main.java
@@ -24,14 +24,33 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLConnection;
 
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.HttpMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+
 public class Main {
 
     public static void main(String[] args) throws MalformedURLException, IOException {
         
-        if ( args.length != 1 )
-            throw new IllegalArgumentException("Usage: java -jar ... <URL>");
+        if ( args.length != 2 )
+            throw new IllegalArgumentException("Usage: java -cp ... " + Main.class.getName() + " <URL> JavaNet|HC3|HC4");
+
+        switch ( args[1] ) {
+            case "JavaNet":
+                runUsingJavaNet(args[0]);
+                break;
+            case "HC3":
+                runUsingHttpClient3(args[0]);
+                break;
+            default:
+                throw new IllegalArgumentException("Usage: java -cp ... " + Main.class.getName() + " <URL> JavaNet|HC3|HC4");
+        }
+    }
 
-        URLConnection con = new URL(args[0]).openConnection();
+    private static void runUsingJavaNet(String targetUrl) throws MalformedURLException, IOException {
+        URLConnection con = new URL(targetUrl).openConnection();
         System.out.println("Connection type is " + con);
         
         try (InputStream in = con.getInputStream();
@@ -41,7 +60,31 @@ public class Main {
             while ( (line = br.readLine()) != null )
                 System.out.println("[WEB] " + line);
         }
-
     }
 
+
+    private static void runUsingHttpClient3(String targetUrl) throws HttpException, IOException {
+        HttpClient client = new HttpClient();
+        HttpMethod get = new GetMethod(targetUrl);
+        
+        client.executeMethod(get);
+        
+        System.out.println("[WEB] " + get.getStatusLine());
+        
+        for ( Header header : get.getResponseHeaders() )
+            System.out.print("[WEB] " + header.toExternalForm());
+        
+        
+        try (InputStream in = get.getResponseBodyAsStream()) {
+            if (in != null) {
+                try (InputStreamReader isr = new InputStreamReader(in); 
+                        BufferedReader br = new BufferedReader(isr)) {
+                    String line;
+                    while ((line = br.readLine()) != null)
+                        System.out.println("[WEB] " + line);
+
+                }
+            }
+        }
+    }
 }