You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/10/29 13:47:37 UTC
svn commit: r1536691 - in
/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration:
HttpTest.java HttpTestBase.java HttpTestNode.java
Author: bdelacretaz
Date: Tue Oct 29 12:47:37 2013
New Revision: 1536691
URL: http://svn.apache.org/r1536691
Log:
SLING-3214 - adapt HttpTestBase and related classes for Junit4-style tests
Added:
sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTest.java
sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestNode.java
Modified:
sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTest.java?rev=1536691&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTest.java (added)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTest.java Tue Oct 29 12:47:37 2013
@@ -0,0 +1,79 @@
+/*
+ * 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.commons.testing.integration;
+
+import java.io.IOException;
+
+import org.apache.commons.httpclient.HttpClient;
+
+/** Helper class for HTTP tests, extends HttpTestBase and adds
+ * a few utilities that we commonly use in our integration tests.
+ *
+ * Meant to be used as a helper class in JUnit4-style tests, as we
+ * gradually move away from JUnit3 style.
+ */
+public class HttpTest extends HttpTestBase {
+ protected String scriptPath;
+ protected String testText;
+ protected String displayUrl;
+
+ public String uploadTestScript(String localFilename,String filenameOnServer) throws IOException {
+ return uploadTestScript(scriptPath, localFilename, filenameOnServer);
+ }
+
+ public static void assertContains(String content, String expected) {
+ if(!content.contains(expected)) {
+ fail("Content does not contain '" + expected + "' (content=" + content + ")");
+ }
+ }
+
+ public static void assertNotContains(String content, String notExpected) {
+ if(content.contains(notExpected)) {
+ fail("Content contains '" + notExpected + "' (content=" + content + ")");
+ }
+ }
+
+ public void setScriptPath(String scriptPath) {
+ this.scriptPath = scriptPath;
+ }
+
+ public String getScriptPath() {
+ return scriptPath;
+ }
+
+ public SlingIntegrationTestClient getTestClient() {
+ return testClient;
+ }
+
+ public HttpClient getHttpClient() {
+ return httpClient;
+ }
+
+ /** Making this public here, changing the base class to public is not convenient
+ * as many derived classes override it as protected.
+ */
+ public void setUp() throws Exception {
+ super.setUp();
+ }
+
+ /** Making this public here, changing the base class to public is not convenient
+ * as many derived classes override it as protected.
+ */
+ public void tearDown() throws Exception {
+ super.tearDown();
+ }
+}
Modified: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java?rev=1536691&r1=1536690&r2=1536691&view=diff
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java (original)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestBase.java Tue Oct 29 12:47:37 2013
@@ -96,26 +96,9 @@ public class HttpTestBase extends TestCa
* stores useful values for testing. Created for JspScriptingTest,
* older test classes do not use it, but it might simplify them.
*/
- protected class TestNode {
- public final String testText;
- public final String nodeUrl;
- public final String resourceType;
- public final String scriptPath;
-
+ protected class TestNode extends HttpTestNode {
public TestNode(String parentPath, Map<String, String> properties) throws IOException {
- if(properties == null) {
- properties = new HashMap<String, String>();
- }
- testText = "This is a test node " + System.currentTimeMillis();
- properties.put("text", testText);
- nodeUrl = testClient.createNode(parentPath + SLING_POST_SERVLET_CREATE_SUFFIX, properties);
- resourceType = properties.get(SLING_RESOURCE_TYPE);
- scriptPath = "/apps/" + (resourceType == null ? "nt/unstructured" : resourceType);
- testClient.mkdirs(WEBDAV_BASE_URL, scriptPath);
- }
-
- public void delete() throws IOException {
- testClient.delete(nodeUrl);
+ super(testClient, parentPath, properties);
}
};
@@ -344,12 +327,12 @@ public class HttpTestBase extends TestCa
}
/** retrieve the contents of given URL and assert its content type (default to HTTP GET method)*/
- protected String getContent(String url, String expectedContentType) throws IOException {
+ public String getContent(String url, String expectedContentType) throws IOException {
return getContent(url, expectedContentType, null);
}
/** retrieve the contents of given URL and assert its content type (default to HTTP GET method)*/
- protected String getContent(String url, String expectedContentType, List<NameValuePair> params) throws IOException {
+ public String getContent(String url, String expectedContentType, List<NameValuePair> params) throws IOException {
return getContent(url, expectedContentType, params, HttpServletResponse.SC_OK);
}
@@ -357,7 +340,7 @@ public class HttpTestBase extends TestCa
* @param expectedContentType use CONTENT_TYPE_DONTCARE if must not be checked
* @throws IOException
* @throws HttpException */
- protected String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
+ public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode) throws IOException {
return getContent(url, expectedContentType, params, expectedStatusCode, HTTP_METHOD_GET);
}
@@ -366,7 +349,7 @@ public class HttpTestBase extends TestCa
* @param httMethod supports just GET and POST methods
* @throws IOException
* @throws HttpException */
- protected String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
+ public String getContent(String url, String expectedContentType, List<NameValuePair> params, int expectedStatusCode, String httpMethod) throws IOException {
HttpMethodBase method = null;
if (HTTP_METHOD_GET.equals(httpMethod)){
@@ -410,7 +393,7 @@ public class HttpTestBase extends TestCa
/** upload rendering test script, and return its URL for future deletion */
- protected String uploadTestScript(String scriptPath, String localFilename,String filenameOnServer) throws IOException {
+ public 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);
Added: sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestNode.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestNode.java?rev=1536691&view=auto
==============================================================================
--- sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestNode.java (added)
+++ sling/trunk/bundles/commons/testing/src/main/java/org/apache/sling/commons/testing/integration/HttpTestNode.java Tue Oct 29 12:47:37 2013
@@ -0,0 +1,48 @@
+/*
+ * 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.commons.testing.integration;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+/** Utility to manage test nodes */
+public class HttpTestNode {
+ public final String testText;
+ public final String nodeUrl;
+ public final String resourceType;
+ public final String scriptPath;
+ private final SlingIntegrationTestClient testClient;
+
+ public HttpTestNode(SlingIntegrationTestClient testClient, String parentPath, Map<String, String> properties) throws IOException {
+ this.testClient = testClient;
+ if(properties == null) {
+ properties = new HashMap<String, String>();
+ }
+ testText = "This is a test node " + System.currentTimeMillis();
+ properties.put("text", testText);
+ nodeUrl = testClient.createNode(parentPath + HttpTestBase.SLING_POST_SERVLET_CREATE_SUFFIX, properties);
+ resourceType = properties.get(HttpTestBase.SLING_RESOURCE_TYPE);
+ scriptPath = "/apps/" + (resourceType == null ? "nt/unstructured" : resourceType);
+ testClient.mkdirs(HttpTestBase.WEBDAV_BASE_URL, scriptPath);
+ }
+
+ public void delete() throws IOException {
+ testClient.delete(nodeUrl);
+ }
+
+}
\ No newline at end of file