|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * https://oss.oracle.com/licenses/CDDL+GPL-1.1 |
| 12 | + * or LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jaccApi.common; |
| 42 | + |
| 43 | +import static java.lang.Boolean.getBoolean; |
| 44 | +import static java.util.logging.Level.SEVERE; |
| 45 | +import static org.jboss.shrinkwrap.api.ShrinkWrap.create; |
| 46 | +import static org.jsoup.Jsoup.parse; |
| 47 | +import static org.jsoup.parser.Parser.xmlParser; |
| 48 | + |
| 49 | +import java.io.File; |
| 50 | +import java.io.IOException; |
| 51 | +import java.net.URL; |
| 52 | +import java.util.logging.Logger; |
| 53 | + |
| 54 | +import org.jboss.arquillian.test.api.ArquillianResource; |
| 55 | +import org.jboss.shrinkwrap.api.Archive; |
| 56 | +import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; |
| 57 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 58 | +import org.junit.After; |
| 59 | +import org.junit.Before; |
| 60 | +import org.junit.Rule; |
| 61 | +import org.junit.rules.TestWatcher; |
| 62 | +import org.junit.runner.Description; |
| 63 | +import static org.jboss.shrinkwrap.api.ShrinkWrap.create; |
| 64 | + |
| 65 | +import java.io.File; |
| 66 | + |
| 67 | +import org.jboss.shrinkwrap.api.importer.ZipImporter; |
| 68 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 69 | + |
| 70 | +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; |
| 71 | +import com.gargoylesoftware.htmlunit.WebClient; |
| 72 | + |
| 73 | +/** |
| 74 | + * |
| 75 | + * |
| 76 | + */ |
| 77 | +public class ArquillianBase { |
| 78 | + |
| 79 | + private static final String WEBAPP_SRC = "src/main/webapp"; |
| 80 | + private static final Logger logger = Logger.getLogger(ArquillianBase.class.getName()); |
| 81 | + |
| 82 | + private WebClient webClient; |
| 83 | + private String response; |
| 84 | + |
| 85 | + @Rule |
| 86 | + public TestWatcher ruleExample = new TestWatcher() { |
| 87 | + @Override |
| 88 | + protected void failed(Throwable e, Description description) { |
| 89 | + super.failed(e, description); |
| 90 | + |
| 91 | + logger.log(SEVERE, |
| 92 | + "\n\nTest failed: " + |
| 93 | + description.getClassName() + "." + description.getMethodName() + |
| 94 | + |
| 95 | + "\nMessage: " + e.getMessage() + |
| 96 | + |
| 97 | + "\nLast response: " + |
| 98 | + |
| 99 | + "\n\n" + formatHTML(response) + "\n\n"); |
| 100 | + |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + public static String formatHTML(String html) { |
| 105 | + try { |
| 106 | + return parse(html, "", xmlParser()).toString(); |
| 107 | + } catch (Exception e) { |
| 108 | + return html; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + public static Archive<?> defaultArchive() { |
| 113 | + return tryWrapEAR(defaultWebArchive()); |
| 114 | + } |
| 115 | + |
| 116 | + public static WebArchive defaultWebArchive() { |
| 117 | + return |
| 118 | + create(WebArchive.class, "test.war") |
| 119 | + .addPackages(true, "org.javaee7.jaspic") |
| 120 | + .deleteClass(ArquillianBase.class) |
| 121 | + .addAsWebInfResource(resource("web.xml")) |
| 122 | + .addAsWebInfResource(resource("jboss-web.xml")) |
| 123 | + .addAsWebInfResource(resource("glassfish-web.xml")); |
| 124 | + } |
| 125 | + |
| 126 | + public static WebArchive mavenWar() { |
| 127 | + return create(ZipImporter.class, System.getProperty("finalName") + ".war") |
| 128 | + .importFrom(new File("target/" + System.getProperty("finalName") + ".war")) |
| 129 | + .as(WebArchive.class); |
| 130 | + } |
| 131 | + |
| 132 | + public static Archive<?> tryWrapEAR(WebArchive webArchive) { |
| 133 | + if (getBoolean("useEarForJaspic")) { |
| 134 | + return |
| 135 | + // EAR archive |
| 136 | + create(EnterpriseArchive.class, "test.ear") |
| 137 | + |
| 138 | + // Liberty needs to have the binding file in an ear. |
| 139 | + // TODO: this is no longer the case and this code can be removed (-bnd.xml |
| 140 | + // needs to be moved to correct place) |
| 141 | + .addAsManifestResource(resource("ibm-application-bnd.xml")) |
| 142 | + |
| 143 | + // Web module |
| 144 | + // This is needed to prevent Arquillian generating an illegal application.xml |
| 145 | + .addAsModule( |
| 146 | + webArchive |
| 147 | + ); |
| 148 | + } else { |
| 149 | + return webArchive; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + public static File resource(String name) { |
| 154 | + return new File(WEBAPP_SRC + "/WEB-INF", name); |
| 155 | + } |
| 156 | + |
| 157 | + public static File web(String name) { |
| 158 | + return new File(WEBAPP_SRC, name); |
| 159 | + } |
| 160 | + |
| 161 | + @ArquillianResource |
| 162 | + private URL base; |
| 163 | + |
| 164 | + @Before |
| 165 | + public void setUp() { |
| 166 | + webClient = new WebClient(); |
| 167 | + webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); |
| 168 | + } |
| 169 | + |
| 170 | + @After |
| 171 | + public void tearDown() { |
| 172 | + webClient.getCookieManager().clearCookies(); |
| 173 | + webClient.close(); |
| 174 | + } |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + protected WebClient getWebClient() { |
| 179 | + return webClient; |
| 180 | + } |
| 181 | + |
| 182 | + protected URL getBase() { |
| 183 | + return base; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Gets content from the path that's relative to the base URL on which the Arquillian test |
| 188 | + * archive is deployed. |
| 189 | + * |
| 190 | + * @param path the path relative to the URL on which the Arquillian test is deployed |
| 191 | + * @return the raw content as a string as returned by the server |
| 192 | + */ |
| 193 | + protected String getFromServerPath(final String path) { |
| 194 | + response = null; |
| 195 | + for (int i=0; i<=3; i++) { |
| 196 | + try { |
| 197 | + response = webClient.getPage(base + path).getWebResponse().getContentAsString(); |
| 198 | + if (!response.contains("The response wrapper must wrap the response obtained from getResponse()")) { |
| 199 | + return response; |
| 200 | + } |
| 201 | + } catch (FailingHttpStatusCodeException | IOException e) { |
| 202 | + throw new IllegalStateException(e); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return response; |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments