package org.displaytag.test;

import java.net.URL;

import junit.framework.TestCase;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.meterware.servletunit.ServletRunner;


/**
 * Base TestCase class for tests.
 * @author Fabrizio Giustina
 * @version $Revision$ ($Author$)
 */
public abstract class DisplaytagCase extends TestCase
{

    /**
     * logger.
     */
    protected final Log log = LogFactory.getLog(getClass());

    /**
     * HttpUnit ServletRunner.
     */
    protected ServletRunner runner;

    /**
     * Instantiates a new test case.
     * @param name test name
     */
    public DisplaytagCase(String name)
    {
        super(name);
    }

    /**
     * Returns the tested jsp name.
     * @return jsp name
     */
    public abstract String getJspName();

    /**
     * @see junit.framework.TestCase#setUp()
     */
    protected void setUp() throws Exception
    {
        // need to pass a web.xml file to setup servletunit working directory
        ClassLoader classLoader = getClass().getClassLoader();
        URL webXmlUrl = classLoader.getResource("WEB-INF/web.xml");
        String path = webXmlUrl.getFile();

        // start servletRunner
        runner = new ServletRunner(path, "");
        log.debug("ServletRunner setup OK");

        super.setUp();
    }

    /**
     * @see junit.framework.TestCase#tearDown()
     */
    protected void tearDown() throws Exception
    {
        // shutdown servlet engine
        runner.shutDown();
        super.tearDown();
    }

}
@@ -23,16 +23,37 @@
 .test;%0A%0A
+import java.io.File;%0A
 import j
@@ -65,16 +65,16 @@
 et.URL;%0A
-
 %0Aimport 
@@ -100,16 +100,104 @@
 tCase;%0A%0A
+import org.apache.commons.lang.StringUtils;%0Aimport org.apache.commons.lang.SystemUtils;%0A
 import o
@@ -1039,32 +1039,113 @@
 Exception%0A    %7B%0A
+        // removed compiled jsp from a previous run.%0A        cleanupTempFile();%0A%0A
         // need 
@@ -1709,24 +1709,24 @@
 shutDown();%0A
-
         supe
@@ -1746,9 +1746,851 @@
 %0A    %7D%0A%0A
+    /**%0A     * Clean up temporary files from a previous test.%0A     */%0A    private void cleanupTempFile()%0A    %7B%0A        if (SystemUtils.JAVA_IO_TMPDIR != null)%0A        %7B%0A            String path = SystemUtils.JAVA_IO_TMPDIR.substring(0, SystemUtils.JAVA_IO_TMPDIR.length() - 1)%0A                + getJspName();%0A%0A            File tempFile = new File(StringUtils.replace(path, %22.jsp%22, %22$jsp.java%22));%0A            if (tempFile.exists())%0A            %7B%0A                log.debug(%22Deleting temporary file %22 + tempFile.getPath());%0A                tempFile.delete();%0A            %7D%0A            tempFile = new File(StringUtils.replace(path, %22.jsp%22, %22$jsp.class%22));%0A            if (tempFile.exists())%0A            %7B%0A                log.debug(%22Deleting temporary file %22 + tempFile.getPath());%0A                tempFile.delete();%0A            %7D%0A        %7D%0A    %7D%0A%0A
 %7D

delete temp file before run, otherwise jasper wil never recompile the jsp