incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1084217 - in /sling/trunk/testing: junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/ junit/remote/src/main/java/org/apache/sling/junit/remote/ide/ junit/...
Date Tue, 22 Mar 2011 15:27:13 GMT
Author: bdelacretaz
Date: Tue Mar 22 15:27:12 2011
New Revision: 1084217

URL: http://svn.apache.org/viewvc?rev=1084217&view=rev
Log:
SLING-2029 - RequestCustomizer allows for setting credentials (or other options) in RemoteTestHttpClient

Added:
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
  (with props)
Modified:
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java
    sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
    sling/trunk/testing/samples/integration-tests/pom.xml
    sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
    sling/trunk/testing/tools/pom.xml
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/Request.java
    sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestExecutor.java

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java
(original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/exported/ExampleRemoteTest.java
Tue Mar 22 15:27:12 2011
@@ -18,8 +18,11 @@ package org.apache.sling.junit.remote.ex
 
 import static org.junit.Assert.fail;
 import org.apache.sling.junit.remote.ide.SlingRemoteExecutionRule;
+import org.apache.sling.testing.tools.http.Request;
 import org.junit.Rule;
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** Test that can be run remotely on a Sling instance from an IDE, by
  *  setting the {@link SlingRemoteExecutionRule.SLING_REMOTE_TEST_URL}
@@ -27,9 +30,18 @@ import org.junit.Test;
  *  the Sling JUnit servlet (like http://localhost:8080/system/sling/junit)
  */
 public class ExampleRemoteTest {
-    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    /** Execute this test remotely and customize the request (could be
+     *  used to set credentials for example)
+     */
     @Rule
-    public SlingRemoteExecutionRule execRule = new SlingRemoteExecutionRule();
+    public SlingRemoteExecutionRule execRule = new SlingRemoteExecutionRule() {
+        @Override
+        public void customizeRequest(Request r) {
+            log.info("Customizing request {}", r);
+        }
+    };
     
     @Test
     public void testAlwaysPasses() {

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java
(original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/httpclient/RemoteTestHttpClient.java
Tue Mar 22 15:27:12 2011
@@ -23,6 +23,7 @@ import org.apache.http.client.ClientProt
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.sling.testing.tools.http.Request;
 import org.apache.sling.testing.tools.http.RequestBuilder;
+import org.apache.sling.testing.tools.http.RequestCustomizer;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -34,6 +35,7 @@ public class RemoteTestHttpClient {
     private final String junitServletUrl;
     private StringBuilder subpath;
     private boolean consumeContent;
+    private RequestCustomizer requestCustomizer;
     private static final String SLASH = "/";
     private static final String DOT = ".";
     
@@ -45,6 +47,10 @@ public class RemoteTestHttpClient {
         this.consumeContent = consumeContent;
     }
     
+    public void setRequestCustomizer(RequestCustomizer c) {
+        requestCustomizer = c;
+    }
+    
     public RequestExecutor runTests(String testClassesSelector, String testMethodSelector,
String extension) 
     throws ClientProtocolException, IOException {
         final RequestBuilder builder = new RequestBuilder(junitServletUrl);
@@ -82,7 +88,9 @@ public class RemoteTestHttpClient {
         
         log.info("Executing test remotely, path={} JUnit servlet URL={}", 
                 subpath, junitServletUrl);
-        final Request r = builder.buildPostRequest(subpath.toString());
+        final Request r = builder
+        .buildPostRequest(subpath.toString())
+        .withCustomizer(requestCustomizer);
         executor.execute(r).assertStatus(200);
 
         return executor;

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java
(original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/ide/SlingRemoteExecutionRule.java
Tue Mar 22 15:27:12 2011
@@ -20,6 +20,8 @@ import java.io.ObjectInputStream;
 
 import org.apache.http.HttpEntity;
 import org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient;
+import org.apache.sling.testing.tools.http.Request;
+import org.apache.sling.testing.tools.http.RequestCustomizer;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.junit.rules.MethodRule;
 import org.junit.runners.model.FrameworkMethod;
@@ -32,7 +34,7 @@ import org.slf4j.LoggerFactory;
  *  Used to execute tests from an IDE and have then run
  *  on a remote Sling instance. 
  */
-public class SlingRemoteExecutionRule implements MethodRule {
+public class SlingRemoteExecutionRule implements MethodRule, RequestCustomizer {
    private static final Logger log = 
        LoggerFactory.getLogger(SlingRemoteExecutionRule.class);
 
@@ -76,6 +78,7 @@ public class SlingRemoteExecutionRule im
        final String methodName = method.getMethod().getName();
        
        final RemoteTestHttpClient testHttpClient = new RemoteTestHttpClient(remoteUrl, false);
+       testHttpClient.setRequestCustomizer(this);
        final RequestExecutor executor = testHttpClient.runTests(
                testClassesSelector, methodName, "serialized"
        );
@@ -98,4 +101,10 @@ public class SlingRemoteExecutionRule im
            }
        }
    }
+   
+   /** @inheritDoc */
+   public void customizeRequest(Request r) {
+       // Do nothing by default, tests that use this rule can
+       // customize this method
+   }
 }
\ No newline at end of file

Modified: sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
(original)
+++ sling/trunk/testing/junit/remote/src/main/java/org/apache/sling/junit/remote/testrunner/SlingRemoteTestRunner.java
Tue Mar 22 15:27:12 2011
@@ -23,6 +23,7 @@ import org.apache.sling.commons.json.JSO
 import org.apache.sling.commons.json.JSONObject;
 import org.apache.sling.commons.json.JSONTokener;
 import org.apache.sling.junit.remote.httpclient.RemoteTestHttpClient;
+import org.apache.sling.testing.tools.http.RequestCustomizer;
 import org.apache.sling.testing.tools.http.RequestExecutor;
 import org.junit.internal.AssumptionViolatedException;
 import org.junit.internal.runners.model.EachTestNotifier;
@@ -73,6 +74,13 @@ public class SlingRemoteTestRunner exten
         }
         
         testHttpClient = new RemoteTestHttpClient(testParameters.getJunitServletUrl(), true);
+
+        // Let the parameters class customize the request if desired 
+        if(testParameters instanceof RequestCustomizer) {
+            testHttpClient.setRequestCustomizer((RequestCustomizer)testParameters);
+        }
+        
+        // Run tests remotely and get response
         final RequestExecutor executor = testHttpClient.runTests(
                 testParameters.getTestClassesSelector(),
                 testParameters.getTestMethodSelector(),

Modified: sling/trunk/testing/samples/integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/pom.xml?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/pom.xml (original)
+++ sling/trunk/testing/samples/integration-tests/pom.xml Tue Mar 22 15:27:12 2011
@@ -222,6 +222,7 @@
                         <sling.additional.bundle.3>org.apache.sling.testing.samples.sampletests</sling.additional.bundle.3>
                         <sling.additional.bundle.4>org.apache.sling.testing.samples.failingtests</sling.additional.bundle.4>
                         <sling.additional.bundle.5>org.apache.sling.junit.remote</sling.additional.bundle.5>
+                        <sling.additional.bundle.6>org.apache.sling.testing.tools</sling.additional.bundle.6>
                     </systemPropertyVariables>
                 </configuration>
             </plugin>
@@ -260,6 +261,12 @@
             <version>0.1.1-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.testing.tools</artifactId>
+            <version>0.1.1-SNAPSHOT</version>
+            <scope>provided</scope>
+        </dependency>
         
         <!-- actual dependencies -->
         <dependency>

Modified: sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
(original)
+++ sling/trunk/testing/samples/integration-tests/src/test/java/org/apache/sling/testing/samples/integrationtests/serverside/ServerSideSampleTest.java
Tue Mar 22 15:27:12 2011
@@ -16,21 +16,32 @@
  */
 package org.apache.sling.testing.samples.integrationtests.serverside;
 
+import static org.junit.Assert.fail;
 import static org.junit.Assert.assertEquals;
 import org.apache.sling.junit.remote.testrunner.SlingRemoteTestParameters;
 import org.apache.sling.junit.remote.testrunner.SlingRemoteTestRunner;
 import org.apache.sling.junit.remote.testrunner.SlingTestsCountChecker;
+import org.apache.sling.testing.tools.http.Request;
+import org.apache.sling.testing.tools.http.RequestCustomizer;
 import org.junit.runner.RunWith;
 
-/** Run some server-side tests */
+/** Run some server-side tests, and verify that the RequestCustomizer
+ *  interface is used by the test runner */
 @RunWith(SlingRemoteTestRunner.class)
 public class ServerSideSampleTest extends ServerSideTestsBase 
-implements SlingRemoteTestParameters, SlingTestsCountChecker {
+implements SlingRemoteTestParameters, SlingTestsCountChecker, RequestCustomizer {
     
     public static final String TEST_SELECTOR = "org.apache.sling.testing.samples.sampletests";
     public static final int TESTS_AT_THIS_PATH = 5;
+    private int customizeCalled;
     
     public void checkNumberOfTests(int numberOfTestsExecuted) {
+        // This assumes this method is called after customizeRequest, which
+        // should be the case as the request must be executed to find out
+        // how many tests are present
+        if(customizeCalled == 0) {
+            fail("customizeRequest not called?");
+        }
         assertEquals(TESTS_AT_THIS_PATH, numberOfTestsExecuted);
     }
 
@@ -49,4 +60,8 @@ implements SlingRemoteTestParameters, Sl
     public String getTestMethodSelector() {
         return null;
     }
+
+    public void customizeRequest(Request r) {
+        customizeCalled++;
+    }
 }
\ No newline at end of file

Modified: sling/trunk/testing/tools/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/pom.xml?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/tools/pom.xml (original)
+++ sling/trunk/testing/tools/pom.xml Tue Mar 22 15:27:12 2011
@@ -28,7 +28,7 @@
 
     <artifactId>org.apache.sling.testing.tools</artifactId>
     <version>0.1.1-SNAPSHOT</version>
-    <packaging>jar</packaging>
+    <packaging>bundle</packaging>
 
     <name>Apache Sling Test Tools</name>
     <description>
@@ -40,6 +40,28 @@
         <developerConnection> scm:svn:https://svn.apache.org/repos/asf/sling/trunk/testing/tools</developerConnection>
         <url>http://svn.apache.org/viewvc/sling/trunk/testing/tools</url>
     </scm>
+    
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Export-Package>
+                            org.apache.sling.testing.tools.*
+                        </Export-Package>
+                        <Import-Package>
+                            org.apache.commons.exec.*; resolution:=optional,
+                            org.apache.http.*; resolution:=optional,
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
 
     <dependencies>
         <dependency>

Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/Request.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/Request.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/Request.java
(original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/Request.java
Tue Mar 22 15:27:12 2011
@@ -31,6 +31,7 @@ public class Request {
     private String username;
     private String password;
     private boolean redirects = true;
+    private RequestCustomizer customizer;
     
     Request(HttpUriRequest r) {
         request = r;
@@ -40,6 +41,10 @@ public class Request {
         return request;
     }
     
+    public String toString() {
+        return getClass().getSimpleName() + ": " + request.getURI();
+    }
+    
     public Request withHeader(String name, String value) {
         request.addHeader(name, value);
         return this;
@@ -75,6 +80,18 @@ public class Request {
         return this;
     }
     
+    public Request withCustomizer(RequestCustomizer c) {
+        customizer = c;
+        return this;
+    }
+    
+    // Execute our {@link RequestCustomizer} if we have one */
+    void customizeIfNeeded() {
+        if(customizer != null) {
+            customizer.customizeRequest(this);
+        }
+    }
+    
     public String getUsername() {
         return username;
     }
@@ -86,4 +103,8 @@ public class Request {
     public boolean getRedirects() {
         return redirects;
     }
+    
+    public RequestCustomizer getCustomizer() {
+        return customizer;
+    }
 }

Added: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java?rev=1084217&view=auto
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
(added)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
Tue Mar 22 15:27:12 2011
@@ -0,0 +1,22 @@
+/*
+ * 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.testing.tools.http;
+
+/** Customize a Request before it is executed */
+public interface RequestCustomizer {
+    void customizeRequest(Request r);
+}

Propchange: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestCustomizer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestExecutor.java
URL: http://svn.apache.org/viewvc/sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestExecutor.java?rev=1084217&r1=1084216&r2=1084217&view=diff
==============================================================================
--- sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestExecutor.java
(original)
+++ sling/trunk/testing/tools/src/main/java/org/apache/sling/testing/tools/http/RequestExecutor.java
Tue Mar 22 15:27:12 2011
@@ -104,6 +104,7 @@ public class RequestExecutor {
     
     public RequestExecutor execute(Request r) throws ClientProtocolException, IOException
{
         clear();
+        r.customizeIfNeeded();
         request = r.getRequest();
         
         // Optionally setup for basic authentication



Mime
View raw message