KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > httptest > HttpUnitTestCase


1 package info.magnolia.httptest;
2
3 import java.io.File JavaDoc;
4 import java.net.URL JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.apache.commons.lang.StringUtils;
9 import org.apache.commons.lang.SystemUtils;
10 import org.apache.log4j.Logger;
11
12 import com.meterware.servletunit.ServletRunner;
13
14
15 /**
16  * Base TestCase class for httpunit tests.
17  * @author Fabrizio Giustina
18  * @version $Revision: 365 $ ($Author: fgiust $)
19  */

20 public abstract class HttpUnitTestCase extends TestCase {
21
22     /**
23      * Context mapped to the test application.
24      */

25     public static final String JavaDoc CONTEXT = "/context";
26
27     /**
28      * logger.
29      */

30     protected final Logger log = Logger.getLogger(getClass());
31
32     /**
33      * HttpUnit ServletRunner.
34      */

35     protected ServletRunner runner;
36
37     /**
38      * Returns the tested jsp name.
39      * @return jsp name
40      */

41     public abstract String JavaDoc getJspName();
42
43     /**
44      * Runs the test.
45      * @param jspName jsp name, with full path
46      * @throws Exception any axception thrown during test.
47      */

48     public abstract void doTest(String JavaDoc jspName) throws Exception JavaDoc;
49
50     /**
51      * run the test with the jsp 11 tld.
52      * @throws Exception any axception thrown during test.
53      */

54     public void testHtmlPage() throws Exception JavaDoc {
55         doTest("http://localhost" + CONTEXT + "/tags/" + getJspName());
56     }
57
58     /**
59      * @see junit.framework.TestCase#setUp()
60      */

61     protected void setUp() throws Exception JavaDoc {
62         // remove any compiled jsp from a previous run.
63
cleanupTempFile("tags/" + getJspName());
64
65         // need to pass a web.xml file to setup servletunit working directory
66
ClassLoader JavaDoc classLoader = getClass().getClassLoader();
67         URL JavaDoc webXmlUrl = classLoader.getResource("WEB-INF/web.xml");
68         String JavaDoc path = webXmlUrl.getFile();
69
70         // start servletRunner
71
runner = new ServletRunner(new File JavaDoc(path), CONTEXT);
72         log.debug("ServletRunner setup OK");
73
74         super.setUp();
75     }
76
77     /**
78      * @see junit.framework.TestCase#tearDown()
79      */

80     protected void tearDown() throws Exception JavaDoc {
81         // shutdown servlet engine
82
runner.shutDown();
83         super.tearDown();
84     }
85
86     /**
87      * @see junit.framework.TestCase#getName()
88      */

89     public String JavaDoc getName() {
90         return getClass().getName() + "." + super.getName() + " (" + getJspName() + ")";
91     }
92
93     /**
94      * Clean up temporary files from a previous test.
95      * @param jspName jsp name, with full path
96      */

97     private void cleanupTempFile(String JavaDoc jspName) {
98         URL JavaDoc resourceUrl = getClass().getResource("/" + jspName);
99         if (resourceUrl != null && SystemUtils.JAVA_IO_TMPDIR != null) {
100             File JavaDoc jspFile = new File JavaDoc(resourceUrl.getFile());
101             long jspModified = jspFile.lastModified();
102
103             String JavaDoc path = SystemUtils.JAVA_IO_TMPDIR + jspName;
104
105             File JavaDoc tempFile = new File JavaDoc(StringUtils.replace(path, ".jsp", "$jsp.java"));
106
107             // delete file only if jsp has been modified
108
if (tempFile.exists() && tempFile.lastModified() < jspModified) {
109                 if (log.isDebugEnabled()) {
110                     log.debug("Deleting temporary file " + tempFile.getPath());
111                 }
112                 tempFile.delete();
113             }
114             tempFile = new File JavaDoc(StringUtils.replace(path, ".jsp", "$jsp.class"));
115             if (tempFile.exists() && tempFile.lastModified() < jspModified) {
116                 if (log.isDebugEnabled()) {
117                     log.debug("Deleting temporary file " + tempFile.getPath());
118                 }
119                 tempFile.delete();
120             }
121         }
122     }
123
124 }
Popular Tags