Author: kkolinko Date: Thu Nov 3 18:41:39 2011 New Revision: 1197261 URL: http://svn.apache.org/viewvc?rev=1197261&view=rev Log: Introduce a new class LoggingBaseTest and move the following features from TomcatBaseTest there: setting up logging, creating temporary directory, deleting files on tear down. This allows to use the same logging configuration in tests that do not use Tomcat.
Added: tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java (with props) Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Added: tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java?rev=1197261&view=auto ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java (added) +++ tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java Thu Nov 3 18:41:39 2011 @@ -0,0 +1,116 @@ +/* + * 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.File; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.fail; + +import org.junit.After; +import org.junit.Before; + +import org.apache.juli.logging.Log; +import org.apache.juli.logging.LogFactory; + +/** + * Base class that provides logging support for test cases that respects the + * standard conf/logging.properties configuration file. + * + * <p> + * It also provides support for cleaning up temporary files after shutdown. See + * {@link #addDeleteOnTearDown(File)}. + * + * <p> + * <em>Note</em> that the logging configuration uses + * <code>${catalina.base}</code> value and thus we take care about that property + * even if the tests do not use Tomcat. + */ +public abstract class LoggingBaseTest { + + protected Log log; + + private File tempDir; + + private List<File> deleteOnTearDown = new ArrayList<File>(); + + /** + * Helper method that returns the directory where Tomcat build resides. It + * is used to access resources that are part of default Tomcat deployment. + * E.g. the examples webapp. + */ + public File getBuildDirectory() { + return new File(System.getProperty("tomcat.test.tomcatbuild", + "output/build")); + } + + /** + * Helper method that returns the path of the temporary directory used by + * the test runs. The directory is configured during {@link #setUp()}. + * + * <p> + * It is used as <code>${catalina.base}</code> for the instance of Tomcat + * that is being started, but can be used to store other temporary files as + * well. Its <code>work</code> and <code>webapps</code> subdirectories are + * deleted at {@link #tearDown()}. If you have other files or directories + * that have to be deleted on cleanup, register them with + * {@link #addDeleteOnTearDown(File)}. + */ + public File getTemporaryDirectory() { + return tempDir; + } + + /** + * Schedule the given file or directory to be deleted during after-test + * cleanup. + * + * @param file + * File or directory + */ + public void addDeleteOnTearDown(File file) { + deleteOnTearDown.add(file); + } + + @Before + public void setUp() throws Exception { + // Create catalina.base directory + tempDir = new File(System.getProperty("tomcat.test.temp", "output/tmp")); + if (!tempDir.mkdirs() && !tempDir.isDirectory()) { + fail("Unable to create temporary directory for test"); + } + + System.setProperty("catalina.base", tempDir.getAbsolutePath()); + + // Configure logging + System.setProperty("java.util.logging.manager", + "org.apache.juli.ClassLoaderLogManager"); + System.setProperty("java.util.logging.config.file", new File( + getBuildDirectory(), "conf/logging.properties").toString()); + + // Get log instance after logging has been configured + log = LogFactory.getLog(getClass()); + } + + @After + public void tearDown() throws Exception { + for (File file : deleteOnTearDown) { + ExpandWar.delete(file); + } + deleteOnTearDown.clear(); + } +} Propchange: tomcat/trunk/test/org/apache/catalina/startup/LoggingBaseTest.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1197261&r1=1197260&r2=1197261&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java (original) +++ tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Thu Nov 3 18:41:39 2011 @@ -24,7 +24,6 @@ import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; -import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -54,16 +53,13 @@ import org.apache.tomcat.util.buf.ByteCh * 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 { +public abstract class TomcatBaseTest extends LoggingBaseTest { private Tomcat tomcat; - private File tempDir; private boolean accessLogEnabled = false; private static int port = 8000; public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); - private List<File> deleteOnTearDown = new ArrayList<File>(); - /** * Make Tomcat instance accessible to sub-classes. */ @@ -87,67 +83,21 @@ public abstract class TomcatBaseTest { } /** - * Helper method that returns the path of the temporary directory used by - * the test runs. The directory is configured during {@link #setUp()}. - * - * <p> - * It is used as <code>${catalina.base}</code> for the instance of Tomcat - * that is being started, but can be used to store other temporary files as - * well. Its <code>work</code> and <code>webapps</code> subdirectories are - * deleted at {@link #tearDown()}. If you have other files or directories - * that have to be deleted on cleanup, register them with - * {@link #addDeleteOnTearDown(File)}. - */ - public File getTemporaryDirectory() { - return tempDir; - } - - /** - * Helper method that returns the directory where Tomcat build resides. It - * is used to access resources that are part of default Tomcat deployment. - * E.g. the examples webapp. - */ - public File getBuildDirectory() { - return new File(System.getProperty("tomcat.test.tomcatbuild", - "output/build")); - } - - /** * Sub-classes may want to check, whether an AccessLogValve is active */ public boolean isAccessLogEnabled() { return accessLogEnabled; } - /** - * Schedule the given file or directory to be deleted during after-test - * cleanup. - * - * @param file - * File or directory - */ - public void addDeleteOnTearDown(File file) { - deleteOnTearDown.add(file); - } - @Before + @Override public void setUp() throws Exception { - // Need to use JULI so log messages from the tests are visible - System.setProperty("java.util.logging.manager", - "org.apache.juli.ClassLoaderLogManager"); - System.setProperty("java.util.logging.config.file", new File( - getBuildDirectory(), "conf/logging.properties").toString()); - - tempDir = new File(System.getProperty("tomcat.test.temp", "output/tmp")); - if (!tempDir.mkdirs() && !tempDir.isDirectory()) { - fail("Unable to create temporary directory for test"); - } + super.setUp(); - System.setProperty("catalina.base", tempDir.getAbsolutePath()); // Trigger loading of catalina.properties CatalinaProperties.getProperty("foo"); - File appBase = new File(tempDir, "webapps"); + File appBase = new File(getTemporaryDirectory(), "webapps"); if (!appBase.exists() && !appBase.mkdir()) { fail("Unable to create appBase for test"); } @@ -173,7 +123,8 @@ public abstract class TomcatBaseTest { connector.setAttribute("pollerThreadCount", Integer.valueOf(1)); } - tomcat.setBaseDir(tempDir.getAbsolutePath()); + File catalinaBase = getTemporaryDirectory(); + tomcat.setBaseDir(catalinaBase.getAbsolutePath()); tomcat.getHost().setAppBase(appBase.getAbsolutePath()); accessLogEnabled = Boolean.parseBoolean( @@ -184,6 +135,11 @@ public abstract class TomcatBaseTest { 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() { @@ -199,30 +155,26 @@ public abstract class TomcatBaseTest { } @After + @Override public void tearDown() throws Exception { - // Speed up Tomcat shutdown - if (tomcat.connector != null) { - tomcat.connector.setProperty("fastShutdown", "true"); - } - // 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(); + try { + // Speed up Tomcat shutdown + if (tomcat.connector != null) { + tomcat.connector.setProperty("fastShutdown", "true"); } - tomcat.destroy(); - } - // Cannot delete the whole tempDir, because logs are there, - // and they might be open for writing. - // Delete known subdirectories of it. - deleteOnTearDown.add(new File(tempDir, "webapps")); - deleteOnTearDown.add(new File(tempDir, "work")); - for (File file : deleteOnTearDown) { - ExpandWar.delete(file); + // 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(); } - deleteOnTearDown.clear(); } /** --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org