incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r612070 - in /incubator/sling/trunk/usling/usling-webapp: ./ src/test/ src/test/java/ src/test/java/org/ src/test/java/org/apache/ src/test/java/org/apache/sling/ src/test/java/org/apache/sling/usling/ src/test/java/org/apache/sling/usling/...
Date Tue, 15 Jan 2008 10:17:44 GMT
Author: bdelacretaz
Date: Tue Jan 15 02:17:40 2008
New Revision: 612070

URL: http://svn.apache.org/viewvc?rev=612070&view=rev
Log:
SLING-149 - merge usling into Sling - usling-webapp integration test framework ready, HttpPingTest
passes

Added:
    incubator/sling/trunk/usling/usling-webapp/src/test/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
  (with props)
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
  (with props)
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
  (with props)
    incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
  (with props)
Modified:
    incubator/sling/trunk/usling/usling-webapp/pom.xml

Modified: incubator/sling/trunk/usling/usling-webapp/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/pom.xml?rev=612070&r1=612069&r2=612070&view=diff
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/pom.xml (original)
+++ incubator/sling/trunk/usling/usling-webapp/pom.xml Tue Jan 15 02:17:40 2008
@@ -54,6 +54,17 @@
       Disable Jetty webapp rescan (override with -D) 
     -->
     <jetty.scan.interval.seconds>0</jetty.scan.interval.seconds>
+    
+    <!-- 
+      Defines which tests are for the "integration-testing" phase
+     -->
+    <integration.test.code.path>**/usling/webapp/integrationtest</integration.test.code.path>
+    
+    <!-- 
+      Set this to true to stop mvn once the integration test Jetty instance is
+      started. Useful to manually test the integration testing webapp.
+     -->
+     <integration.test.wait>false</integration.test.wait>
   </properties>
 
   <build>
@@ -171,9 +182,153 @@
         </configuration>
       </plugin>
 
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-surefire-plugin</artifactId>
+        <configuration>
+          <systemProperties>
+            <!-- 
+              WARNING: make sure these URLs are consistent with the
+              cargo-maven2-plugin settings: AFAIK Maven does not
+              expand variables in the <value> element.
+              And WARNING: these properties are duplicated somewhere in this
+              POM with slightly different values...there must be a better way. 
+            -->
+            <property>
+              <name>usling.http.server.url</name>
+              <value>http://localhost:8080</value>
+            </property>
+            <property>
+              <name>usling.webdav.server.url</name>
+              <value>http://localhost:8080/repository/default</value>
+            </property>
+          </systemProperties>
+        </configuration>
+      </plugin>
+      
     </plugins>
   </build>
 
+  <profiles>
+    <profile>
+      <!--
+        Run the cargo-based integration tests.
+        This profile is active by default, to deactivate it activate another
+        one, for example " mvn -P nullProfile".
+        
+        TODO there must be a way to control this with "mvn.test.skip", but I haven't
+        found it. 
+      -->
+      <id>cargoIntegrationTesting</id>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <executions>
+              <execution>
+                <id>surefire-integration-test</id>
+                <phase>integration-test</phase>
+                <goals>
+                  <goal>test</goal>
+                </goals>
+                <configuration>
+                  <excludes>
+                    <exclude>none</exclude>
+                  </excludes>
+                  <includes>
+                    <include>${integration.test.code.path}/**/*Test.java</include>
+                  </includes>
+                  <systemProperties>
+                    <!-- 
+                      WARNING: make sure these URLs are consistent with the
+                      cargo-maven2-plugin settings: AFAIK Maven does not
+                      expand variables in the <value> element.
+                      And WARNING: these properties are duplicated somewhere in this
+                      POM with slightly different values...there must be a better way. 
+                    -->
+                    <property>
+                      <name>usling.http.server.url</name>
+                      <value>http://localhost:8080/usling-webapp-2.0.0-incubator-SNAPSHOT</value>
+                    </property>
+                    <property>
+                      <name>usling.webdav.server.url</name>
+                      <value>http://localhost:8080/usling-webapp-2.0.0-incubator-SNAPSHOT/repository/default</value>
+                    </property>
+                  </systemProperties>
+                </configuration>
+              </execution>
+            </executions>
+          </plugin>
+          <plugin>
+            <groupId>org.codehaus.cargo</groupId>
+            <artifactId>cargo-maven2-plugin</artifactId>
+            <version>0.3.1</version>
+            <executions>
+              <execution>
+                <id>start-container</id>
+                <phase>pre-integration-test</phase>
+                <goals>
+                  <goal>start</goal>
+                </goals>
+              </execution>
+              <execution>
+                <id>stop-container</id>
+                <phase>post-integration-test</phase>
+                <goals>
+                  <goal>stop</goal>
+                </goals>
+              </execution>
+            </executions>
+            <configuration>
+              <wait>${integration.test.wait}</wait>
+              <container>
+                <containerId>jetty6x</containerId>
+                <type>embedded</type>
+              </container>
+            </configuration>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+    <profile>
+      <!--
+        By default, do not run the integration tests in the test phase, as they have
+        no microsling to talk to.
+        To debug the integration tests faster than with the normal build cycle,
+        deactivate this profile (using -P nullProfile for example) and run a
+        separate instance of microsling that the tests can talk to.
+      -->
+      <id>disableIntegrationTestsInTestPhase</id>
+      <activation>
+        <activeByDefault>true</activeByDefault>
+      </activation>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <configuration>
+              <excludes>
+                <exclude>${integration.test.code.path}/**/*.java</exclude>
+              </excludes>
+            </configuration>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+    <profile>
+      <!-- 
+        Useful to deactivate other profiles from the command-line
+        Is there a better way? mvn doesn't have a "deactivate profile" option. 
+      -->
+      <id>nullUslingProfile</id>
+    </profile>
+  </profiles>
+
   <dependencies>
     <dependency>
       <groupId>org.apache.felix</groupId>
@@ -203,6 +358,44 @@
       <version>1.0.0-v20070606</version>
       <scope>provided</scope>
     </dependency>
+    
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.3</version>
+      <scope>test</scope>
+    </dependency>
+    
+    <dependency>
+      <groupId>commons-httpclient</groupId>
+      <artifactId>commons-httpclient</artifactId>
+      <version>3.1</version>
+      <scope>test</scope>
+      <exclusions>
+        <!-- 
+          Same problem as JCR-683: commons-httpclient depends on commons-logging. Since 
+          this webapp uses log4j (see the slf4j-log4j12 dependency below), we need to
+          override this dependency with jcl104-over-sflf4j, found below
+        -->
+        <exclusion>
+          <groupId>commons-logging</groupId>
+          <artifactId>commons-logging</artifactId>
+        </exclusion>
+      </exclusions>    
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+      <version>1.3.0</version>
+      <scope>test</scope>
+     </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>jcl104-over-slf4j</artifactId>
+      <version>1.3.0</version>
+      <scope>test</scope>
+    </dependency>
+    
 
     <!-- The servlet container will have to provide these API classes -->
     <dependency>

Added: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java?rev=612070&view=auto
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
(added)
+++ incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
Tue Jan 15 02:17:40 2008
@@ -0,0 +1,35 @@
+/*
+ * 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.usling.webapp.integrationtest;
+
+
+/** Ping the usling server to verify that our integration test
+ *  setup is ok.
+ */
+public class HttpPingTest extends UslingHttpTestBase {
+    public void testWebServerRoot() throws Exception
+    {
+        // The root URL returns a 404, Sling doesn't resolve the root node??
+        assertHttpStatus(HTTP_BASE_URL + "/", 404);
+    }
+    
+    public void test404() throws Exception
+    {
+        assertHttpStatus(HTTP_BASE_URL + "/someNonExistentUrl", 404);
+    }
+    
+}
\ No newline at end of file

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/HttpPingTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java?rev=612070&view=auto
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
(added)
+++ incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
Tue Jan 15 02:17:40 2008
@@ -0,0 +1,180 @@
+/*
+ * 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.usling.webapp.integrationtest;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.httpclient.Credentials;
+import org.apache.commons.httpclient.Header;
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.HttpException;
+import org.apache.commons.httpclient.NameValuePair;
+import org.apache.commons.httpclient.UsernamePasswordCredentials;
+import org.apache.commons.httpclient.auth.AuthScope;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.sling.usling.webapp.integrationtest.helpers.UslingIntegrationTestClient;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.ScriptableObject;
+
+/** Base class for HTTP-based usling integration tests */
+public class UslingHttpTestBase extends TestCase {
+    public static final String HTTP_BASE_URL = System.getProperty("usling.http.server.url");
+    public static final String WEBDAV_BASE_URL = System.getProperty("usling.webdav.server.url");
+    
+    /** base path for test files */
+    public static final String TEST_PATH = "/usling-integration-tests";
+    
+    public static final String CONTENT_TYPE_HTML = "text/html";
+    public static final String CONTENT_TYPE_XML = "text/xml";
+    public static final String CONTENT_TYPE_PLAIN = "text/plain";
+    public static final String CONTENT_TYPE_JSON = "application/json";
+    public static final String CONTENT_TYPE_JS = "application/x-javascript";
+    public static final String CONTENT_TYPE_CSS = "text/css";
+    
+    protected UslingIntegrationTestClient testClient;
+    protected HttpClient httpClient;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        
+        // assume http and webdav are on the same host + port
+        URL url = null;
+        try {
+            url = new URL(HTTP_BASE_URL);
+        } catch(MalformedURLException mfe) {
+            // MalformedURLException doesn't tell us the URL by default
+            throw new IOException("MalformedURLException: " + HTTP_BASE_URL);
+        }
+        
+        // setup HTTP client, with authentication (using default Jackrabbit credentials)
+        httpClient = new HttpClient();
+        httpClient.getParams().setAuthenticationPreemptive(true);
+        Credentials defaultcreds = new UsernamePasswordCredentials("admin", "admin");
+        httpClient.getState().setCredentials(new AuthScope(url.getHost(), url.getPort(),
AuthScope.ANY_REALM), defaultcreds);
+
+        testClient = new UslingIntegrationTestClient(httpClient);
+    }
+
+    /** Verify that given URL returns expectedStatusCode 
+     * @throws IOException */
+    protected void assertHttpStatus(String urlString, int expectedStatusCode, String assertMessage)
throws IOException {
+        final int status = httpClient.executeMethod(new GetMethod(urlString));
+        if(assertMessage == null) {
+            assertEquals(expectedStatusCode, status);
+        } else {
+            assertEquals(assertMessage, expectedStatusCode, status);
+        }
+    }
+    
+    /** Verify that given URL returns expectedStatusCode 
+     * @throws IOException */
+    protected void assertHttpStatus(String urlString, int expectedStatusCode) throws IOException
{
+        assertHttpStatus(urlString, expectedStatusCode, null);
+    }
+    
+    /** Execute a POST request and check status */
+    protected void assertPostStatus(String url, int expectedStatusCode, List<NameValuePair>
postParams, String assertMessage)
+    throws IOException {
+        final PostMethod post = new PostMethod(url);
+        post.setFollowRedirects(false);
+        
+        if(postParams!=null) {
+            final NameValuePair [] nvp = {};
+            post.setRequestBody(postParams.toArray(nvp));
+        }
+        
+        final int status = httpClient.executeMethod(post);
+        if(assertMessage == null) {
+            assertEquals(expectedStatusCode, status);
+        } else {
+            assertEquals(assertMessage, expectedStatusCode, status);
+        }
+    }
+    
+    /** retrieve the contents of given URL and assert its content type
+     * @throws IOException
+     * @throws HttpException */
+    protected String getContent(String url, String expectedContentType) throws IOException
{
+        final GetMethod get = new GetMethod(url);
+        final int status = httpClient.executeMethod(get);
+        assertEquals("Expected status 200 for " + url,200,status);
+        final Header h = get.getResponseHeader("Content-Type");
+        if(expectedContentType == null) {
+            if(h!=null) {
+                fail("Expected null Content-Type, got " + h.getValue());
+            }
+        } else {
+            assertTrue(
+                "Expected Content-Type that starts with '" + expectedContentType 
+                + "' for " + url + ", got '" + h.getValue() + "'",
+                h.getValue().startsWith(expectedContentType)
+            );
+        }
+        return get.getResponseBodyAsString();
+    }
+
+    /** upload rendering test script, and return its URL for future deletion */
+    protected String uploadTestScript(String scriptPath, String localFilename,String filenameOnServer)
throws IOException {
+        final String url = WEBDAV_BASE_URL + scriptPath + "/" + filenameOnServer;
+        final String testFile = "/integration-test/" + localFilename;
+        final InputStream data = getClass().getResourceAsStream(testFile);
+        try {
+            testClient.upload(url, data);
+        } finally {
+            if(data!=null) {
+                data.close();
+            }
+        }
+        return url;
+    }
+
+    /** Evaluate given code using given jsonData as the "data" object */ 
+    protected void assertJavascript(String expectedOutput, String jsonData, String code)
throws IOException {
+        // build the code, something like
+        //  data = <jsonData> ;
+        //  <code>
+        final String jsCode = "data=" + jsonData + ";\n" + code;
+        final Context rhinoContext = Context.enter();
+        final ScriptableObject scope = rhinoContext.initStandardObjects();
+
+        // execute the script, out script variable maps to sw 
+        final StringWriter sw = new StringWriter();
+        final PrintWriter pw = new PrintWriter(sw, true);
+        ScriptableObject.putProperty(scope, "out", Context.javaToJS(pw, scope));
+        final int lineNumber = 1;
+        final Object securityDomain = null;
+        rhinoContext.evaluateString(scope, jsCode, getClass().getSimpleName(), 
+                lineNumber, securityDomain);
+        
+        // check script output
+        pw.flush();
+        final String result = sw.toString().trim();
+        if(!result.equals(expectedOutput)) {
+            fail("Expected '" + expectedOutput + "' but got '" + result + "' for script='"
+ jsCode + "'");
+        }
+    }
+}
\ No newline at end of file

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/UslingHttpTestBase.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java?rev=612070&view=auto
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
(added)
+++ incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
Tue Jan 15 02:17:40 2008
@@ -0,0 +1,34 @@
+/*
+ * 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.usling.webapp.integrationtest.helpers;
+
+import org.apache.commons.httpclient.HttpMethodBase;
+
+/** Allows any HTTP method for HtttpClient */
+public class HttpAnyMethod extends HttpMethodBase {
+    private final String methodName;
+    
+    public HttpAnyMethod(String methodName, String uri) {
+        super(uri);
+        this.methodName = methodName;
+    }
+
+    @Override
+    public String getName() {
+        return methodName;
+    }
+}

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/HttpAnyMethod.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Added: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java?rev=612070&view=auto
==============================================================================
--- incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
(added)
+++ incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
Tue Jan 15 02:17:40 2008
@@ -0,0 +1,123 @@
+/*
+ * 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.usling.webapp.integrationtest.helpers;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Map;
+
+import org.apache.commons.httpclient.HttpClient;
+import org.apache.commons.httpclient.methods.DeleteMethod;
+import org.apache.commons.httpclient.methods.GetMethod;
+import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
+import org.apache.commons.httpclient.methods.PostMethod;
+import org.apache.commons.httpclient.methods.PutMethod;
+
+/** Client functions to interact with microsling in integration tests */ 
+public class UslingIntegrationTestClient {
+    private final HttpClient httpClient;
+    
+    public UslingIntegrationTestClient(HttpClient client) {
+        this.httpClient = client;
+    }
+    
+    /** Upload a file to the microsling repository 
+     *  @return the HTTP status code
+     */
+    public int upload(String toUrl, InputStream is) throws IOException {
+        final PutMethod put = new PutMethod(toUrl);
+        put.setRequestEntity(new InputStreamRequestEntity(is));
+        return httpClient.executeMethod(put);
+    }
+    
+    /** Delete a file from the microsling repository 
+     *  @return the HTTP status code
+     */
+    public int delete(String url) throws IOException {
+        final DeleteMethod delete = new DeleteMethod(url);
+        return httpClient.executeMethod(delete);
+    }
+    
+    /** Create the given directory via WebDAV, if needed, under given URL */
+    public void mkdir(String url) throws IOException {
+        int status = 0;
+        status = httpClient.executeMethod(new GetMethod(url));
+        if(status != 200) {
+            status = httpClient.executeMethod(new HttpAnyMethod("MKCOL",url));
+            if(status!=201) {
+                throw new IOException("mkdir(" + url + ") failed, status code=" + status);
+            }
+        }
+    }
+    
+    /** Create the given directory via WebDAV, including parent directories */ 
+    public void mkdirs(String baseUrl,String path) throws IOException {
+        final String [] paths = path.split("/");
+        if(baseUrl.endsWith("/")) {
+            baseUrl = baseUrl.substring(0,baseUrl.length() - 1);
+        }
+        
+        String currentPath = baseUrl;
+        for(String pathElement : paths) {
+            if(pathElement.length() == 0) {
+                continue;
+            }
+            currentPath += "/" + pathElement;
+            mkdir(currentPath);
+        }
+        
+        final String url = baseUrl + path;
+        final int status = httpClient.executeMethod(new GetMethod(url));
+        if(status!=200) {
+            throw new IOException("Expected status 200, got " + status + " for URL=" + url);
+        }
+    }
+
+    /** Call the other createNode method with headers==null */
+    public String createNode(String url, Map<String,String> nodeProperties) throws
IOException {
+        return createNode(url, nodeProperties, null);
+    }
+    
+    /** Create a node under given path, using a POST to microsling
+     *  @param url under which node is created 
+     *  @return the URL that microsling provides to display the node 
+     */
+    public String createNode(String url, Map<String,String> nodeProperties, Map<String,String>
requestHeaders) 
+    throws IOException {
+        final PostMethod post = new PostMethod(url);
+        post.setFollowRedirects(false);
+        
+        if(nodeProperties != null) {
+            for(Map.Entry<String,String> e : nodeProperties.entrySet()) {
+                post.addParameter(e.getKey(),e.getValue());
+            }
+        }
+        
+        if(requestHeaders != null) {
+            for(Map.Entry<String,String> e : requestHeaders.entrySet()) {
+                post.addRequestHeader(e.getKey(), e.getValue());
+            }
+        }
+        
+        final int status = httpClient.executeMethod(post);
+        if(status!=302) {
+            throw new IOException("Expected status code 302 for POST, got " + status + ",
URL=" + url);
+        }
+        final String location = post.getResponseHeader("Location").getValue();
+        return location;
+    }
+}

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/usling/usling-webapp/src/test/java/org/apache/sling/usling/webapp/integrationtest/helpers/UslingIntegrationTestClient.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message