Skip to content

Commit

Permalink
Merge pull request #197 from arjantijms/tck_migrate_servlet_old
Browse files Browse the repository at this point in the history
Tck migrate servlet old
  • Loading branch information
arjantijms committed Apr 16, 2024
2 parents e6d32e0 + 05962cf commit 97611c2
Show file tree
Hide file tree
Showing 73 changed files with 16,659 additions and 7,434 deletions.
7 changes: 3 additions & 4 deletions tck/common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>1.1.14.Final</version>
<version>1.8.0.Final</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -54,13 +54,12 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<groupId>org.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.37.0</version>
<version>3.11.0</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2022-2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2024 Contributors to the Eclipse Foundation.
* Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -13,10 +14,13 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package ee.jakarta.tck.authentication.test.common;

import static java.lang.Boolean.getBoolean;
import static java.util.logging.Level.INFO;
import static java.util.logging.Level.SEVERE;
import static org.apache.http.HttpStatus.SC_MULTIPLE_CHOICES;
import static org.apache.http.HttpStatus.SC_OK;
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
import static org.jsoup.Jsoup.parse;
import static org.jsoup.parser.Parser.xmlParser;
Expand All @@ -26,37 +30,48 @@
import java.net.URL;
import java.util.Map;
import java.util.logging.Logger;

import org.htmlunit.DefaultCredentialsProvider;
import org.htmlunit.DefaultCssErrorHandler;
import org.htmlunit.FailingHttpStatusCodeException;
import org.htmlunit.Page;
import org.htmlunit.WebClient;
import org.htmlunit.WebResponse;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.arquillian.test.api.Secured;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Node;
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;

/**
*
* @author Arjan Tijms
*
*/
public class ArquillianBase {

private static final String WEBAPP_SRC = "src/main/webapp";
private static final Logger logger = Logger.getLogger(ArquillianBase.class.getName());
private static final String WEBAPP_SRC = "src/main/webapp";

private WebClient webClient;
private String response;
private boolean useBaseSecured;
private String responsePath;

@ArquillianResource
private URL base;

@Secured(port = 8181)
@ArquillianResource
private URL baseSecured;

@Rule
public TestWatcher ruleExample = new TestWatcher() {

@Override
protected void starting(Description description) {
logger.log(INFO, "Running " + description.getMethodName());
};

@Override
protected void failed(Throwable e, Description description) {
super.failed(e, description);
Expand All @@ -74,22 +89,18 @@ protected void failed(Throwable e, Description description) {
}
};

public static String formatHTML(String html) {
try {
return parse(html, "", xmlParser()).toString();
} catch (Exception e) {
return html;
}
public static WebArchive defaultArchive() {
return defaultWebArchive("test.war");
}

public static Archive<?> defaultArchive() {
return tryWrapEAR(defaultWebArchive());
public static WebArchive defaultWebArchive() {
return defaultWebArchive("test.war");
}

public static WebArchive defaultWebArchive() {
public static WebArchive defaultWebArchive(String name) {
return
removeTestClasses(
create(WebArchive.class, "test.war")
create(WebArchive.class, name + ".war")
.addPackages(true, "ee.jakarta.tck.authentication.test")
.addAsWebInfResource(resource("web.xml"))
);
Expand All @@ -106,22 +117,6 @@ private static WebArchive removeTestClasses(WebArchive archive) {
return archive;
}

public static Archive<?> tryWrapEAR(WebArchive webArchive) {
if (getBoolean("useEarForJaspic")) {
return
// EAR archive
create(EnterpriseArchive.class, "test.ear")

// Web module
// This is needed to prevent Arquillian generating an illegal application.xml
.addAsModule(
webArchive
);
} else {
return webArchive;
}
}

public static File resource(String name) {
return new File(WEBAPP_SRC + "/WEB-INF", name);
}
Expand All @@ -130,16 +125,31 @@ public static File web(String name) {
return new File(WEBAPP_SRC, name);
}

@ArquillianResource
private URL base;

@Before
public void setUp() {
webClient = new WebClient();
Logger logger = Logger.getLogger(DefaultCssErrorHandler.class.getName());
logger.setLevel(SEVERE);

useBaseSecured = false;
response = null;
webClient = new WebClient() {

private static final long serialVersionUID = 1L;

@Override
public void printContentIfNecessary(WebResponse webResponse) {
int statusCode = webResponse.getStatusCode();
if (getOptions().isPrintContentOnFailingStatusCode() && !(statusCode >= SC_OK && statusCode < SC_MULTIPLE_CHOICES)) {
logger.log(SEVERE, webResponse.getWebRequest().getUrl().toExternalForm());
}
super.printContentIfNecessary(webResponse);
}
};
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
if (System.getProperty("glassfish.suspend") != null) {
webClient.getOptions().setTimeout(0);
}
webClient.getOptions().setUseInsecureSSL(true);
}

@After
Expand All @@ -148,37 +158,142 @@ public void tearDown() {
webClient.close();
}

protected String readFromServerWithCredentials(String path, String username, String password) {
DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
credentialsProvider.addCredentials(username, password.toCharArray());

getWebClient().setCredentialsProvider(credentialsProvider);

return readFromServer(path);
}


protected String getFromServerPath(String path) {
return readFromServer(path);
}

protected String readFromServer(String path) {
response = "";
WebResponse localResponse = responseFromServer(path);
if (localResponse != null) {
response = localResponse.getContentAsString();
}

return response;
}

protected WebResponse responseFromServerWithCredentials(String path, String username, String password) {
DefaultCredentialsProvider credentialsProvider = new DefaultCredentialsProvider();
credentialsProvider.addCredentials(username, password.toCharArray());

getWebClient().setCredentialsProvider(credentialsProvider);

return responseFromServer(path);
}

protected WebResponse responseFromServer(String path) {

WebResponse webResponse = null;

Page page = pageFromServer(path);
if (page != null) {
webResponse = page.getWebResponse();
if (webResponse != null) {
response = webResponse.getContentAsString();
}
}

return webResponse;
}

protected <P extends Page> P pageFromServer(String path) {
URL currentBase = base;
if (useBaseSecured) {
currentBase = baseSecured;
}

if (currentBase.toString().endsWith("/") && path.startsWith("/")) {
path = path.substring(1);
}

try {
response = "";

P page = webClient.getPage(currentBase + path);

if (page != null) {
WebResponse localResponse = page.getWebResponse();
responsePath = page.getUrl().toString();
if (localResponse != null) {
response = localResponse.getContentAsString();

if (System.getProperty("tck.log.response") != null) {
printLastResponse();
}
}
}

return page;

} catch (FailingHttpStatusCodeException | IOException e) {
throw new IllegalStateException(e);
}
}

protected void printLastResponse() {
logger.info(
"\n\n" +
"Requested path:\n" + responsePath +
"\n\n" +

"Response :\n" + formatHTML(response) +
"\n\n\n");
}

protected void printPage(Page page) {
if (page != null) {
WebResponse localResponse = page.getWebResponse();
responsePath = page.getUrl().toString();
if (localResponse != null) {
response = localResponse.getContentAsString();
}

printLastResponse();
}
}

protected WebClient getWebClient() {
return webClient;
}

protected URL getBase() {
return base;
/**
* @return the useBaseSecured
*/
protected boolean isUseBaseSecured() {
return useBaseSecured;
}

/**
* Gets content from the path that's relative to the base URL on which the Arquillian test
* archive is deployed.
*
* @param path the path relative to the URL on which the Arquillian test is deployed
* @return the raw content as a string as returned by the server
* @param useBaseSecured the useBaseSecured to set
*/
protected String getFromServerPath(final String path) {
response = null;
for (int i=0; i<=3; i++) {
try {
response = webClient.getPage(base + path).getWebResponse().getContentAsString();
if (!response.contains("The response wrapper must wrap the response obtained from getResponse()")) {
return response;
}
} catch (FailingHttpStatusCodeException | IOException e) {
throw new IllegalStateException(e);
}
}
protected void setUseBaseSecured(boolean useBaseSecured) {
this.useBaseSecured = useBaseSecured;
}

return response;
/**
* @return the base
*/
protected URL getBase() {
return base;
}

public static String formatHTML(String html) {
try {
return parse(html, "", xmlParser()).toString();
} catch (Exception e) {
return html;
}
}

}

Loading

0 comments on commit 97611c2

Please sign in to comment.