/* * 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.catalina.startup; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.URL; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import static org.junit.Assert.fail; import org.junit.After; import org.junit.Before; import org.apache.catalina.Container; import org.apache.catalina.LifecycleException; import org.apache.catalina.LifecycleState; import org.apache.catalina.Server; import org.apache.catalina.Service; import org.apache.catalina.connector.Connector; import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.StandardServer; import org.apache.catalina.session.StandardManager; import org.apache.catalina.valves.AccessLogValve; import org.apache.tomcat.util.buf.ByteChunk; /** * Base test case that provides a Tomcat instance for each test - mainly so we * don't have to keep writing the cleanup code. */ public abstract class TomcatBaseTest extends LoggingBaseTest { private Tomcat tomcat; private boolean accessLogEnabled = false; public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); /** * Make Tomcat instance accessible to sub-classes. */ public Tomcat getTomcatInstance() { return tomcat; } /** * Sub-classes need to know port so they can connect */ public int getPort() { return tomcat.getConnector().getLocalPort(); } /** * Sub-classes may want to check, whether an AccessLogValve is active */ public boolean isAccessLogEnabled() { return accessLogEnabled; } @Before @Override public void setUp() throws Exception { super.setUp(); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); File appBase = new File(getTemporaryDirectory(), "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } tomcat = new TomcatWithFastSessionIDs(); String protocol = getProtocol(); Connector connector = new Connector(protocol); // Listen only on localhost connector.setAttribute("address", InetAddress.getByName("localhost").getHostAddress()); // Use random free port connector.setPort(0); // Mainly set to reduce timeouts during async tests connector.setAttribute("connectionTimeout", "3000"); tomcat.getService().addConnector(connector); tomcat.setConnector(connector); // Add AprLifecycleListener if we are using the Apr connector if (protocol.contains("Apr")) { StandardServer server = (StandardServer) tomcat.getServer(); AprLifecycleListener listener = new AprLifecycleListener(); listener.setSSLRandomSeed("/dev/urandom"); server.addLifecycleListener(listener); connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } File catalinaBase = getTemporaryDirectory(); tomcat.setBaseDir(catalinaBase.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean( System.getProperty("tomcat.test.accesslog", "false")); if (accessLogEnabled) { AccessLogValve alv = new AccessLogValve(); alv.setDirectory(getBuildDirectory() + "/logs"); alv.setPattern("%h %l %u %t \"%r\" %s %b %I %D"); tomcat.getHost().getPipeline().addValve(alv); } // Cannot delete the whole tempDir, because logs are there, // but delete known subdirectories of it. addDeleteOnTearDown(new File(catalinaBase, "webapps")); addDeleteOnTearDown(new File(catalinaBase, "work")); } protected String getProtocol() { // Has a protocol been specified String protocol = System.getProperty("tomcat.test.protocol"); // Use BIO by default if (protocol == null) { protocol = "org.apache.coyote.http11.Http11Protocol"; } return protocol; } @After @Override public void tearDown() throws Exception { try { // Some tests may call tomcat.destroy(), some tests may just call // tomcat.stop(), some not call either method. Make sure that stop() // & destroy() are called as necessary. if (tomcat.server != null && tomcat.server.getState() != LifecycleState.DESTROYED) { if (tomcat.server.getState() != LifecycleState.STOPPED) { tomcat.stop(); } tomcat.destroy(); } } finally { super.tearDown(); } } /** * Simple Hello World servlet for use by test cases */ public static final class HelloWorldServlet extends HttpServlet { private static final long serialVersionUID = 1L; public static final String RESPONSE_TEXT = "
Hello World
"; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { PrintWriter out = resp.getWriter(); out.print(RESPONSE_TEXT); } } /** * Wrapper for getting the response. */ public static ByteChunk getUrl(String path) throws IOException { ByteChunk out = new ByteChunk(); getUrl(path, out, null); return out; } public static int getUrl(String path, ByteChunk out, Map