From: kkolinko Date: Sat, 24 Sep 2011 23:31:04 +0000 (+0000) Subject: Cleanup temporary files created by tests: X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=3619577b2ef8456a159724737a4805ea49139214;p=tomcat7.0 Cleanup temporary files created by tests: register them and delete them in tearDown(). In TestRegistration also aligned mkdir calls with their usage elsewhere. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1175283 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/test/org/apache/catalina/connector/TestCoyoteAdapter.java b/test/org/apache/catalina/connector/TestCoyoteAdapter.java index 53d6c8e09..8e05a0067 100644 --- a/test/org/apache/catalina/connector/TestCoyoteAdapter.java +++ b/test/org/apache/catalina/connector/TestCoyoteAdapter.java @@ -87,6 +87,7 @@ public class TestCoyoteAdapter extends TomcatBaseTest { // Create the folder that will trigger the redirect File foo = new File(docBase, "foo"); + addDeleteOnTearDown(foo); if (!foo.mkdirs() && !foo.isDirectory()) { fail("Unable to create foo directory in docBase"); } diff --git a/test/org/apache/catalina/core/TestAsyncContextImpl.java b/test/org/apache/catalina/core/TestAsyncContextImpl.java index 66d129ac5..3d9b9416c 100644 --- a/test/org/apache/catalina/core/TestAsyncContextImpl.java +++ b/test/org/apache/catalina/core/TestAsyncContextImpl.java @@ -411,6 +411,7 @@ public class TestAsyncContextImpl extends TomcatBaseTest { // Create the folder that will trigger the redirect File foo = new File(docBase, "async"); + addDeleteOnTearDown(foo); if (!foo.mkdirs() && !foo.isDirectory()) { fail("Unable to create async directory in docBase"); } diff --git a/test/org/apache/catalina/mbeans/TestRegistration.java b/test/org/apache/catalina/mbeans/TestRegistration.java index 893f0077a..7336c3338 100644 --- a/test/org/apache/catalina/mbeans/TestRegistration.java +++ b/test/org/apache/catalina/mbeans/TestRegistration.java @@ -116,9 +116,9 @@ public class TestRegistration extends TomcatBaseTest { final Tomcat tomcat = getTomcatInstance(); final File contextDir = new File(getTemporaryDirectory(), "webappFoo"); - if (!contextDir.exists()) { - if (!contextDir.mkdir()) - fail("Failed to create: [" + contextDir.toString() + "]"); + addDeleteOnTearDown(contextDir); + if (!contextDir.mkdirs() && !contextDir.isDirectory()) { + fail("Failed to create: [" + contextDir.toString() + "]"); } tomcat.addContext(contextName, contextDir.getAbsolutePath()); tomcat.start(); @@ -172,9 +172,9 @@ public class TestRegistration extends TomcatBaseTest { tomcat.getEngine().addChild(host); final File contextDir2 = new File(getTemporaryDirectory(), "webappFoo2"); - if (!contextDir2.exists()) { - if (!contextDir2.mkdir()) - fail("Failed to create: [" + contextDir2.toString() + "]"); + addDeleteOnTearDown(contextDir2); + if (!contextDir2.mkdirs() && !contextDir2.isDirectory()) { + fail("Failed to create: [" + contextDir2.toString() + "]"); } tomcat.addContext(host, contextName + "2", contextDir2.getAbsolutePath()); diff --git a/test/org/apache/catalina/servlets/TestDefaultServlet.java b/test/org/apache/catalina/servlets/TestDefaultServlet.java index 5ac2f11a8..d0339db71 100644 --- a/test/org/apache/catalina/servlets/TestDefaultServlet.java +++ b/test/org/apache/catalina/servlets/TestDefaultServlet.java @@ -164,6 +164,7 @@ public class TestDefaultServlet extends TomcatBaseTest { public void testCustomErrorPage() throws Exception { File appDir = new File(getTemporaryDirectory(), "MyApp"); File webInf = new File(appDir, "WEB-INF"); + addDeleteOnTearDown(appDir); if (!webInf.mkdirs() && !webInf.isDirectory()) { fail("Unable to create directory [" + webInf + "]"); } @@ -249,6 +250,7 @@ public class TestDefaultServlet extends TomcatBaseTest { public void testCustomErrorPageMissing() throws Exception { File appDir = new File(getTemporaryDirectory(), "MyApp"); File webInf = new File(appDir, "WEB-INF"); + addDeleteOnTearDown(appDir); if (!webInf.mkdirs() && !webInf.isDirectory()) { fail("Unable to create directory [" + webInf + "]"); } diff --git a/test/org/apache/catalina/startup/TomcatBaseTest.java b/test/org/apache/catalina/startup/TomcatBaseTest.java index 657e3093b..3720bf35a 100644 --- a/test/org/apache/catalina/startup/TomcatBaseTest.java +++ b/test/org/apache/catalina/startup/TomcatBaseTest.java @@ -24,6 +24,7 @@ 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; @@ -61,6 +62,8 @@ public abstract class TomcatBaseTest { public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); + private List deleteOnTearDown = new ArrayList(); + /** * Make Tomcat instance accessible to sub-classes. */ @@ -85,8 +88,15 @@ 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()} and is - * deleted at {@link #tearDown()}. + * the test runs. The directory is configured during {@link #setUp()}. + * + *

+ * It is used as ${catalina.base} for the instance of Tomcat + * that is being started, but can be used to store other temporary files as + * well. Its work and webapps 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; @@ -109,6 +119,17 @@ public abstract class TomcatBaseTest { 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 public void setUp() throws Exception { // Need to use JULI so log messages from the tests are visible @@ -190,10 +211,14 @@ public abstract class TomcatBaseTest { tomcat.destroy(); } // Cannot delete the whole tempDir, because logs are there, - // and they might be open for writing. + // and they might be open for writing. // Delete known subdirectories of it. - ExpandWar.delete(new File(tempDir, "webapps")); - ExpandWar.delete(new File(tempDir, "work")); + deleteOnTearDown.add(new File(tempDir, "webapps")); + deleteOnTearDown.add(new File(tempDir, "work")); + for (File file : deleteOnTearDown) { + ExpandWar.delete(file); + } + deleteOnTearDown.clear(); } /**