// 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");
}
// 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");
}
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();
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());
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 + "]");
}
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 + "]");
}
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.
*/
/**
* 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()}.
+ *
+ * <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;
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
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();
}
/**