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 2017/11/07 09:36:47 UTC
[sling-org-apache-sling-hapi] 02/24: SLING-5136 - HApi microdata
client, improved tests contributed by Andrei Dulvac, thanks!
This is an automated email from the ASF dual-hosted git repository.
rombert pushed a commit to annotated tag org.apache.sling.hapi.client-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hapi.git
commit 6d11d9f8624afefd7a26784fb9b8d11c8cd54acc
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Tue Oct 13 11:05:58 2015 +0000
SLING-5136 - HApi microdata client, improved tests contributed by Andrei Dulvac, thanks!
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/hapi/client@1708328 13f79535-47bb-0310-9956-ffa450edef68
---
src/test/java/FormTest.java | 23 +++++++-------
src/test/java/GetPostTest.java | 26 +++++++++-------
src/test/java/ItemsTest.java | 23 +++++++-------
src/test/java/util/TestBase.java | 67 ++++++++++++++++++++++++++++++++++++++++
4 files changed, 104 insertions(+), 35 deletions(-)
diff --git a/src/test/java/FormTest.java b/src/test/java/FormTest.java
index 3c9424d..c179b9b 100644
--- a/src/test/java/FormTest.java
+++ b/src/test/java/FormTest.java
@@ -21,7 +21,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.StringEntity;
-import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
@@ -30,9 +29,9 @@ import org.apache.sling.hapi.client.Document;
import org.apache.sling.hapi.client.Items;
import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import util.TestBase;
import java.io.IOException;
import java.net.URI;
@@ -43,7 +42,7 @@ import java.util.List;
import static org.hamcrest.core.IsEqual.equalTo;
-public class FormTest extends LocalServerTestBase {
+public class FormTest extends TestBase {
public static final String GET_URL = "/test1";
public static final String POST_URL = "/testpost1";
public static final String OK_RESPONSE = "TEST_OK";
@@ -51,18 +50,18 @@ public class FormTest extends LocalServerTestBase {
public static String html;
- private HttpHost host;
- private URI uri;
+ private static HttpHost host;
+ private static URI uri;
@BeforeClass
- public static void setUpClass() throws IOException {
+ public static void setUp() throws Exception {
FormTest.html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_forms.html"), "UTF-8");
+ setupServer();
}
- @Before
- public void setup() throws Exception {
- super.setUp();
- this.serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
+ public static void setupServer() throws Exception {
+ TestBase.setUp();
+ serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext)
throws HttpException, IOException {
@@ -84,8 +83,8 @@ public class FormTest extends LocalServerTestBase {
});
// start server
- this.host = this.start();
- this.uri = URIUtils.rewriteURI(new URI("/"), host);
+ host = TestBase.start();
+ uri = URIUtils.rewriteURI(new URI("/"), host);
}
@Test
diff --git a/src/test/java/GetPostTest.java b/src/test/java/GetPostTest.java
index e45b8b1..23589b6 100644
--- a/src/test/java/GetPostTest.java
+++ b/src/test/java/GetPostTest.java
@@ -21,7 +21,6 @@
import org.apache.http.*;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.StringEntity;
-import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.sling.hapi.client.ClientException;
@@ -29,8 +28,9 @@ import org.apache.sling.hapi.client.Document;
import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient;
import org.hamcrest.core.StringContains;
import org.junit.Assert;
-import org.junit.Before;
+import org.junit.BeforeClass;
import org.junit.Test;
+import util.TestBase;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
@@ -39,7 +39,7 @@ import java.net.URISyntaxException;
import static org.hamcrest.core.StringContains.containsString;
-public class GetPostTest extends LocalServerTestBase {
+public class GetPostTest extends TestBase {
public static final String GET_URL = "/test";
public static final String GET_AUTH_URL = "/testauth";
public static final String OK_RESPONSE = "TEST_OK";
@@ -50,13 +50,17 @@ public class GetPostTest extends LocalServerTestBase {
private static final String REDIRECT_URL = "/test_redirect";
- private HttpHost host;
- private URI uri;
+ private static HttpHost host;
+ private static URI uri;
- @Before
- public void setup() throws Exception {
- super.setUp();
- this.serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
+ @BeforeClass
+ public static void setUp() throws Exception {
+ setupServer();
+ }
+
+ public static void setupServer() throws Exception {
+ TestBase.setUp();
+ serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext)
throws HttpException, IOException {
@@ -83,8 +87,8 @@ public class GetPostTest extends LocalServerTestBase {
});
// start server
- this.host = this.start();
- this.uri = URIUtils.rewriteURI(new URI("/"), host);
+ host = TestBase.start();
+ uri = URIUtils.rewriteURI(new URI("/"), host);
}
@Test
diff --git a/src/test/java/ItemsTest.java b/src/test/java/ItemsTest.java
index 9e0b962..7c09f45 100644
--- a/src/test/java/ItemsTest.java
+++ b/src/test/java/ItemsTest.java
@@ -21,7 +21,6 @@ import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.entity.StringEntity;
-import org.apache.http.localserver.LocalServerTestBase;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.sling.hapi.client.ClientException;
@@ -29,9 +28,9 @@ import org.apache.sling.hapi.client.Document;
import org.apache.sling.hapi.client.Items;
import org.apache.sling.hapi.client.microdata.MicrodataHtmlClient;
import org.junit.Assert;
-import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
+import util.TestBase;
import java.io.IOException;
import java.net.URI;
@@ -40,7 +39,7 @@ import java.net.URISyntaxException;
import static org.hamcrest.core.IsEqual.equalTo;
-public class ItemsTest extends LocalServerTestBase {
+public class ItemsTest extends TestBase {
public static final String GET_URL = "/test";
public static final String GET_LINKS_URL = "/testlinks";
public static final String OK_RESPONSE = "TEST_OK";
@@ -49,19 +48,19 @@ public class ItemsTest extends LocalServerTestBase {
public static String html;
public static String htmlLinks;
- private HttpHost host;
- private URI uri;
+ private static HttpHost host;
+ private static URI uri;
@BeforeClass
- public static void setUpClass() throws IOException {
+ public static void setUp() throws Exception {
ItemsTest.html = IOUtils.toString(ItemsTest.class.getResourceAsStream("items.html"), "UTF-8");
ItemsTest.htmlLinks = IOUtils.toString(ItemsTest.class.getResourceAsStream("items_links.html"), "UTF-8");
+ setupServer();
}
- @Before
- public void setup() throws Exception {
- super.setUp();
- this.serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
+ public static void setupServer() throws Exception {
+ TestBase.setUp();
+ serverBootstrap.registerHandler(GET_URL, new HttpRequestHandler() {
@Override
public void handle(HttpRequest httpRequest, HttpResponse httpResponse, HttpContext httpContext)
throws HttpException, IOException {
@@ -78,8 +77,8 @@ public class ItemsTest extends LocalServerTestBase {
});
// start server
- this.host = this.start();
- this.uri = URIUtils.rewriteURI(new URI("/"), host);
+ host = TestBase.start();
+ uri = URIUtils.rewriteURI(new URI("/"), host);
}
@Test
diff --git a/src/test/java/util/TestBase.java b/src/test/java/util/TestBase.java
new file mode 100644
index 0000000..d1dbc25
--- /dev/null
+++ b/src/test/java/util/TestBase.java
@@ -0,0 +1,67 @@
+/*******************************************************************************
+ * 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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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 util;
+
+import org.apache.http.HttpHost;
+import org.apache.http.config.SocketConfig;
+import org.apache.http.impl.bootstrap.HttpServer;
+import org.apache.http.impl.bootstrap.ServerBootstrap;
+import org.apache.http.localserver.SSLTestContexts;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+
+import java.util.concurrent.TimeUnit;
+
+public abstract class TestBase {
+ public static final String ORIGIN = "TEST/1.1";
+ protected static TestBase.ProtocolScheme scheme = TestBase.ProtocolScheme.http;
+ protected static ServerBootstrap serverBootstrap;
+ protected static HttpServer server;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(5000).build();
+ serverBootstrap = ServerBootstrap.bootstrap().setSocketConfig(socketConfig).setServerInfo("TEST/1.1");
+ if(scheme.equals(TestBase.ProtocolScheme.https)) {
+ serverBootstrap.setSslContext(SSLTestContexts.createServerSSLContext());
+ }
+ }
+
+ @AfterClass
+ public static void shutDown() throws Exception {
+ if(server != null) {
+ server.shutdown(-1, TimeUnit.SECONDS);
+ }
+
+ }
+
+ public static HttpHost start() throws Exception {
+ server = serverBootstrap.create();
+ server.start();
+ return new HttpHost("localhost", server.getLocalPort(), scheme.name());
+ }
+
+ public static enum ProtocolScheme {
+ http,
+ https;
+
+ private ProtocolScheme() {
+ }
+ }
+}
--
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.