1 package info.magnolia.httptest; 2 3 import java.io.File ; 4 import java.net.URL ; 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 20 public abstract class HttpUnitTestCase extends TestCase { 21 22 25 public static final String CONTEXT = "/context"; 26 27 30 protected final Logger log = Logger.getLogger(getClass()); 31 32 35 protected ServletRunner runner; 36 37 41 public abstract String getJspName(); 42 43 48 public abstract void doTest(String jspName) throws Exception ; 49 50 54 public void testHtmlPage() throws Exception { 55 doTest("http://localhost" + CONTEXT + "/tags/" + getJspName()); 56 } 57 58 61 protected void setUp() throws Exception { 62 cleanupTempFile("tags/" + getJspName()); 64 65 ClassLoader classLoader = getClass().getClassLoader(); 67 URL webXmlUrl = classLoader.getResource("WEB-INF/web.xml"); 68 String path = webXmlUrl.getFile(); 69 70 runner = new ServletRunner(new File (path), CONTEXT); 72 log.debug("ServletRunner setup OK"); 73 74 super.setUp(); 75 } 76 77 80 protected void tearDown() throws Exception { 81 runner.shutDown(); 83 super.tearDown(); 84 } 85 86 89 public String getName() { 90 return getClass().getName() + "." + super.getName() + " (" + getJspName() + ")"; 91 } 92 93 97 private void cleanupTempFile(String jspName) { 98 URL resourceUrl = getClass().getResource("/" + jspName); 99 if (resourceUrl != null && SystemUtils.JAVA_IO_TMPDIR != null) { 100 File jspFile = new File (resourceUrl.getFile()); 101 long jspModified = jspFile.lastModified(); 102 103 String path = SystemUtils.JAVA_IO_TMPDIR + jspName; 104 105 File tempFile = new File (StringUtils.replace(path, ".jsp", "$jsp.java")); 106 107 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 (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 |