Skip to content
Permalink
Browse files
CXF-8744: Get rid of httpunit (#973)
  • Loading branch information
reta committed Aug 1, 2022
1 parent 63a2c2b commit d3d370bf1c53a0bd25395a2d615df4f1a74e8dc3
Show file tree
Hide file tree
Showing 15 changed files with 954 additions and 793 deletions.
@@ -232,30 +232,6 @@
<scope>test</scope>
<version>${cxf.spring.version}</version>
</dependency>
<dependency>
<groupId>httpunit</groupId>
<artifactId>httpunit</artifactId>
<version>1.7</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xmlParserAPIs</artifactId>
</exclusion>
<exclusion>
<groupId>jtidy</groupId>
<artifactId>jtidy</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
@@ -0,0 +1,75 @@
/**
* 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.cxf.systest.servlet;

import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

import static org.junit.Assert.fail;

public abstract class AbstractJettyServer extends AbstractBusTestServerBase {
private org.eclipse.jetty.server.Server server;
private final String descriptor;
private final String resourcePath;
private final String contextPath;
private final int port;

protected AbstractJettyServer(final String descriptor, final String resourcePath,
final String contextPath, int portNumber) {
this.descriptor = descriptor;
this.resourcePath = resourcePath;
this.contextPath = contextPath;
this.port = portNumber;
}

protected void run() {
server = new Server(port);

try {
final WebAppContext context = new WebAppContext();
context.setContextPath(contextPath);
context.setBaseResource(Resource.newClassPathResource(resourcePath));
context.setDescriptor(Resource.newClassPathResource(descriptor).getURI().toString());

HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[] {context, new DefaultHandler()});
server.setHandler(handlers);
server.start();
} catch (final Exception ex) {
ex.printStackTrace();
fail(ex.getMessage());
}
}

public void tearDown() throws Exception {
super.tearDown();

if (server != null) {
server.stop();
server.destroy();
server = null;
}
}
}
@@ -21,60 +21,105 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Collection;
import java.util.HashSet;

import org.xml.sax.SAXException;

import com.meterware.httpunit.HttpException;
import com.meterware.httpunit.HttpNotFoundException;
import com.meterware.httpunit.HttpUnitOptions;
import com.meterware.httpunit.WebRequest;
import com.meterware.servletunit.ServletRunner;
import com.meterware.servletunit.ServletUnitClient;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.apache.cxf.test.AbstractCXFTest;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.junit.After;
import org.junit.Before;
import org.apache.cxf.test.TestUtilities;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.apache.html.dom.HTMLAnchorElementImpl;
import org.apache.html.dom.HTMLDocumentImpl;
import org.apache.http.HttpHeaders;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.cyberneko.html.parsers.DOMParser;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public abstract class AbstractServletTest extends AbstractCXFTest {
public abstract class AbstractServletTest extends AbstractBusClientServerTestBase {
public static final String CONTEXT = "/mycontext";
public static final String CONTEXT_URL = "http://localhost/mycontext";
protected ServletRunner sr;

@Before
public void setUp() throws Exception {
InputStream configurationStream = getResourceAsStream(getConfiguration());
sr = new ServletRunner(configurationStream, CONTEXT);
try {
sr.newClient().getResponse(CONTEXT_URL + "/services");
} catch (HttpNotFoundException e) {
// ignore, we just want to boot up the servlet
}
protected TestUtilities testUtilities;

protected AbstractServletTest() {
testUtilities = new TestUtilities(getClass());
testUtilities.addDefaultNamespaces();
}

HttpUnitOptions.setExceptionsThrownOnErrorStatus(true);
configurationStream.close();
/**
* Add a namespace that will be used for XPath expressions.
*
* @param ns Namespace name.
* @param uri The namespace uri.
*/
public void addNamespace(String ns, String uri) {
testUtilities.addNamespace(ns, uri);
}
@After
public void tearDown() throws Exception {
if (sr != null) {
sr.shutDown();
}

/**
* Assert that the following XPath query selects one or more nodes.
*
* @param xpath
* @throws Exception
*/
public NodeList assertValid(String xpath, Node node) throws Exception {
return testUtilities.assertValid(xpath, node);
}

/**
* @return The web.xml to use for testing.
* Assert that the text of the xpath node retrieved is equal to the value
* specified.
*
* @param xpath
* @param value
* @param node
*/
protected String getConfiguration() {
return "/org/apache/cxf/systest/servlet/web.xml";
public void assertXPathEquals(String xpath, String value, Node node) throws Exception {
testUtilities.assertXPathEquals(xpath, value, node);
}

protected ServletUnitClient newClient() {
return sr.newClient();
protected CloseableHttpClient newClient() {
return HttpClients.createDefault();
}

protected String uri(String path) {
return "http://localhost:" + getPort() + CONTEXT + path;
}

protected Collection<HTMLAnchorElementImpl> getLinks(HTMLDocumentImpl document) {
final Collection<HTMLAnchorElementImpl> links = new HashSet<>();

for (int i = 0; i < document.getLinks().getLength(); ++i) {
final HTMLAnchorElementImpl link = (HTMLAnchorElementImpl)document.getLinks().item(i);
links.add(link);
}

return links;
}

protected HTMLDocumentImpl parse(InputStream in) throws SAXException, IOException {
final DOMParser parser = new DOMParser();
parser.parse(new InputSource(in));
return (HTMLDocumentImpl)parser.getDocument();
}

protected String getContentType(CloseableHttpResponse response) {
return ContentType.parse(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()).getMimeType();
}

protected String getCharset(CloseableHttpResponse response) {
return ContentType.parse(response.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue()).getCharset().name();
}

protected abstract int getPort();

/**
* Here we expect an errorCode other than 200, and look for it checking for
* text is omitted as it doesn't work. It would never work on java1.3, but
@@ -88,20 +133,12 @@ protected ServletUnitClient newClient() {
* @throws IOException
* @throws SAXException
*/
protected void expectErrorCode(WebRequest request, int errorCode, String errorText)
protected void expectErrorCode(HttpUriRequest request, int errorCode, String errorText)
throws MalformedURLException, IOException, SAXException {
String failureText = "Expected error " + errorCode + " from " + request.getURL();

try {
newClient().getResponse(request);
fail(errorText + " -got success instead");
} catch (HttpException e) {
assertEquals(failureText, errorCode, e.getResponseCode());
/*
* checking for text omitted as it doesn't work. if(errorText!=null) {
* assertTrue( "Failed to find "+errorText+" in "+
* e.getResponseMessage(), e.getMessage().indexOf(errorText)>=0); }
*/
String failureText = "Expected error " + errorCode + " from " + request.getRequestLine().getUri();

try (CloseableHttpResponse response = newClient().execute(request)) {
assertEquals(failureText, errorCode, response.getStatusLine().getStatusCode());
}
}
}

0 comments on commit d3d370b

Please sign in to comment.