Author: bdelacretaz Date: Tue Oct 13 11:05:58 2015 New Revision: 1708328 URL: http://svn.apache.org/viewvc?rev=1708328&view=rev Log: SLING-5136 - HApi microdata client, improved tests contributed by Andrei Dulvac, thanks!
Added: sling/trunk/contrib/extensions/hapi/client/src/test/java/util/ sling/trunk/contrib/extensions/hapi/client/src/test/java/util/TestBase.java Modified: sling/trunk/contrib/extensions/hapi/client/src/test/java/FormTest.java sling/trunk/contrib/extensions/hapi/client/src/test/java/GetPostTest.java sling/trunk/contrib/extensions/hapi/client/src/test/java/ItemsTest.java Modified: sling/trunk/contrib/extensions/hapi/client/src/test/java/FormTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/test/java/FormTest.java?rev=1708328&r1=1708327&r2=1708328&view=diff ============================================================================== --- sling/trunk/contrib/extensions/hapi/client/src/test/java/FormTest.java (original) +++ sling/trunk/contrib/extensions/hapi/client/src/test/java/FormTest.java Tue Oct 13 11:05:58 2015 @@ -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.Docu 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 LocalServe 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 LocalServe }); // start server - this.host = this.start(); - this.uri = URIUtils.rewriteURI(new URI("/"), host); + host = TestBase.start(); + uri = URIUtils.rewriteURI(new URI("/"), host); } @Test Modified: sling/trunk/contrib/extensions/hapi/client/src/test/java/GetPostTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/test/java/GetPostTest.java?rev=1708328&r1=1708327&r2=1708328&view=diff ============================================================================== --- sling/trunk/contrib/extensions/hapi/client/src/test/java/GetPostTest.java (original) +++ sling/trunk/contrib/extensions/hapi/client/src/test/java/GetPostTest.java Tue Oct 13 11:05:58 2015 @@ -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.Docu 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 LocalSe 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 LocalSe }); // start server - this.host = this.start(); - this.uri = URIUtils.rewriteURI(new URI("/"), host); + host = TestBase.start(); + uri = URIUtils.rewriteURI(new URI("/"), host); } @Test Modified: sling/trunk/contrib/extensions/hapi/client/src/test/java/ItemsTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/test/java/ItemsTest.java?rev=1708328&r1=1708327&r2=1708328&view=diff ============================================================================== --- sling/trunk/contrib/extensions/hapi/client/src/test/java/ItemsTest.java (original) +++ sling/trunk/contrib/extensions/hapi/client/src/test/java/ItemsTest.java Tue Oct 13 11:05:58 2015 @@ -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.Docu 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 LocalServ 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 LocalServ }); // start server - this.host = this.start(); - this.uri = URIUtils.rewriteURI(new URI("/"), host); + host = TestBase.start(); + uri = URIUtils.rewriteURI(new URI("/"), host); } @Test Added: sling/trunk/contrib/extensions/hapi/client/src/test/java/util/TestBase.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/hapi/client/src/test/java/util/TestBase.java?rev=1708328&view=auto ============================================================================== --- sling/trunk/contrib/extensions/hapi/client/src/test/java/util/TestBase.java (added) +++ sling/trunk/contrib/extensions/hapi/client/src/test/java/util/TestBase.java Tue Oct 13 11:05:58 2015 @@ -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() { + } + } +}