Author: markt Date: Sat Nov 1 21:04:20 2014 New Revision: 1636054 URL: http://svn.apache.org/r1636054 Log: Refactor tests to reduce boilerplate code. There is more to do here.
Modified: tomcat/trunk/test/javax/el/TestCompositeELResolver.java tomcat/trunk/test/javax/servlet/jsp/TestPageContext.java tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java tomcat/trunk/test/org/apache/el/TestELInJsp.java Modified: tomcat/trunk/test/javax/el/TestCompositeELResolver.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/el/TestCompositeELResolver.java?rev=1636054&r1=1636053&r2=1636054&view=diff ============================================================================== --- tomcat/trunk/test/javax/el/TestCompositeELResolver.java (original) +++ tomcat/trunk/test/javax/el/TestCompositeELResolver.java Sat Nov 1 21:04:20 2014 @@ -16,40 +16,20 @@ */ package javax.el; -import java.io.File; - import javax.servlet.http.HttpServletResponse; import static org.junit.Assert.assertEquals; import org.junit.Test; -import org.apache.catalina.WebResourceRoot; -import org.apache.catalina.core.StandardContext; -import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; -import org.apache.catalina.webresources.StandardRoot; import org.apache.tomcat.util.buf.ByteChunk; public class TestCompositeELResolver extends TomcatBaseTest { @Test public void testBug50408() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - StandardContext ctxt = (StandardContext) tomcat.addWebapp(null, - "/test", appDir.getAbsolutePath()); - - // This test needs the JSTL libraries - File lib = new File("webapps/examples/WEB-INF/lib"); - ctxt.setResources(new StandardRoot(ctxt)); - ctxt.getResources().createWebResourceSet( - WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", - lib.getAbsolutePath(), null, "/"); - - tomcat.start(); + getTomcatInstanceTestWebapp(true, true); int rc = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug50408.jsp", new ByteChunk(), null); Modified: tomcat/trunk/test/javax/servlet/jsp/TestPageContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/javax/servlet/jsp/TestPageContext.java?rev=1636054&r1=1636053&r2=1636054&view=diff ============================================================================== --- tomcat/trunk/test/javax/servlet/jsp/TestPageContext.java (original) +++ tomcat/trunk/test/javax/servlet/jsp/TestPageContext.java Sat Nov 1 21:04:20 2014 @@ -14,16 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package javax.servlet.jsp; -import java.io.File; - import static org.junit.Assert.assertTrue; import org.junit.Test; -import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; @@ -31,13 +27,7 @@ public class TestPageContext extends Tom @Test public void testBug49196() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49196.jsp"); Modified: tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java?rev=1636054&r1=1636053&r2=1636054&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java (original) +++ tomcat/trunk/test/org/apache/catalina/core/TestApplicationContext.java Sat Nov 1 21:04:20 2014 @@ -16,7 +16,6 @@ */ package org.apache.catalina.core; -import java.io.File; import java.util.Collection; import javax.servlet.Filter; @@ -29,6 +28,7 @@ import javax.servlet.http.HttpServletRes import org.junit.Assert; import org.junit.Test; +import org.apache.catalina.LifecycleException; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.tomcat.util.buf.ByteChunk; @@ -37,13 +37,7 @@ public class TestApplicationContext exte @Test public void testBug53257() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug53257/index.jsp"); @@ -60,13 +54,7 @@ public class TestApplicationContext exte @Test public void testBug53467() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = new ByteChunk(); int rc = getUrl("http://localhost:" + getPort() + @@ -78,37 +66,35 @@ public class TestApplicationContext exte @Test(expected = IllegalArgumentException.class) - public void testAddFilterWithFilterNameNull() { + public void testAddFilterWithFilterNameNull() throws LifecycleException { getServletContext().addFilter(null, (Filter) null); } @Test(expected = IllegalArgumentException.class) - public void testAddFilterWithFilterNameEmptyString() { + public void testAddFilterWithFilterNameEmptyString() throws LifecycleException { getServletContext().addFilter("", (Filter) null); } @Test(expected = IllegalArgumentException.class) - public void testAddServletWithServletNameNull() { + public void testAddServletWithServletNameNull() throws LifecycleException { getServletContext().addServlet(null, (Servlet) null); } @Test(expected = IllegalArgumentException.class) - public void testAddServletWithServletNameEmptyString() { + public void testAddServletWithServletNameEmptyString() throws LifecycleException { getServletContext().addServlet("", (Servlet) null); } @Test public void testGetJspConfigDescriptor() throws Exception { - Tomcat tomcat = getTomcatInstance(); + Tomcat tomcat = getTomcatInstanceTestWebapp(false, false); - File appDir = new File("test/webapp"); - // app dir is relative to server home - StandardContext standardContext = (StandardContext) tomcat.addWebapp( - null, "/test", appDir.getAbsolutePath()); + StandardContext standardContext = + (StandardContext) tomcat.getHost().findChildren()[0]; ServletContext servletContext = standardContext.getServletContext(); @@ -121,12 +107,10 @@ public class TestApplicationContext exte @Test public void testJspPropertyGroupsAreIsolated() throws Exception { - Tomcat tomcat = getTomcatInstance(); + Tomcat tomcat = getTomcatInstanceTestWebapp(false, false); - File appDir = new File("test/webapp"); - // app dir is relative to server home - StandardContext standardContext = (StandardContext) tomcat.addWebapp( - null, "/test", appDir.getAbsolutePath()); + StandardContext standardContext = + (StandardContext) tomcat.getHost().findChildren()[0]; ServletContext servletContext = standardContext.getServletContext(); @@ -147,13 +131,11 @@ public class TestApplicationContext exte } - private ServletContext getServletContext() { - Tomcat tomcat = getTomcatInstance(); + private ServletContext getServletContext() throws LifecycleException { + Tomcat tomcat = getTomcatInstanceTestWebapp(false, false); - File appDir = new File("test/webapp"); - // app dir is relative to server home - StandardContext standardContext = (StandardContext) tomcat.addWebapp( - null, "/test", appDir.getAbsolutePath()); + StandardContext standardContext = + (StandardContext) tomcat.getHost().findChildren()[0]; return standardContext.getServletContext(); } Modified: tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1636054&r1=1636053&r2=1636054&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java (original) +++ tomcat/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java Sat Nov 1 21:04:20 2014 @@ -45,12 +45,14 @@ import org.apache.catalina.LifecycleStat import org.apache.catalina.Manager; import org.apache.catalina.Server; import org.apache.catalina.Service; +import org.apache.catalina.WebResourceRoot; import org.apache.catalina.connector.Connector; import org.apache.catalina.core.AprLifecycleListener; import org.apache.catalina.core.StandardServer; import org.apache.catalina.session.ManagerBase; import org.apache.catalina.session.StandardManager; import org.apache.catalina.valves.AccessLogValve; +import org.apache.catalina.webresources.StandardRoot; import org.apache.coyote.http11.Http11NioProtocol; import org.apache.tomcat.util.buf.ByteChunk; @@ -64,13 +66,46 @@ public abstract class TomcatBaseTest ext public static final String TEMP_DIR = System.getProperty("java.io.tmpdir"); - /* - * Make Tomcat instance accessible to sub-classes. + /** + * Make the Tomcat instance available to sub-classes. + * + * @return A Tomcat instance without any pre-configured web applications */ public Tomcat getTomcatInstance() { return tomcat; } + /** + * Make the Tomcat instance preconfigured with test/webapp available to + * sub-classes. + * @param addJstl Should JSTL support be added to the test webapp + * @param start Should the Tomcat instance be started + * + * @return A Tomcat instance pre-configured with the web application located + * at test/webapp + * + * @throws LifecycleException If a problem occurs while starting the + * instance + */ + public Tomcat getTomcatInstanceTestWebapp(boolean addJstl, boolean start) + throws LifecycleException { + File appDir = new File("test/webapp"); + Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + if (addJstl) { + File lib = new File("webapps/examples/WEB-INF/lib"); + ctx.setResources(new StandardRoot(ctx)); + ctx.getResources().createWebResourceSet( + WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", + lib.getAbsolutePath(), null, "/"); + } + + if (start) { + tomcat.start(); + } + return tomcat; + } + /* * Sub-classes need to know port so they can connect */ Modified: tomcat/trunk/test/org/apache/el/TestELInJsp.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/el/TestELInJsp.java?rev=1636054&r1=1636053&r2=1636054&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/el/TestELInJsp.java (original) +++ tomcat/trunk/test/org/apache/el/TestELInJsp.java Sat Nov 1 21:04:20 2014 @@ -14,21 +14,14 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.el; -import java.io.File; - import static org.junit.Assert.assertTrue; import org.junit.Assert; import org.junit.Test; -import org.apache.catalina.Context; -import org.apache.catalina.WebResourceRoot; -import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; -import org.apache.catalina.webresources.StandardRoot; import org.apache.tomcat.util.buf.ByteChunk; /** @@ -40,13 +33,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug36923() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug36923.jsp"); @@ -56,13 +43,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug42565() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug42565.jsp"); @@ -87,13 +68,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug44994() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug44994.jsp"); @@ -105,13 +80,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug45427() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45427.jsp"); @@ -140,13 +109,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug45451() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45451a.jsp"); @@ -248,13 +211,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug45511() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug45nnn/bug45511.jsp"); @@ -266,13 +223,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug46596() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug46596.jsp"); String result = res.toString(); @@ -281,13 +232,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug47413() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug47413.jsp"); @@ -308,13 +253,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug48112() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug48nnn/bug48112.jsp"); @@ -324,13 +263,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug49555() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug49nnn/bug49555.jsp"); @@ -340,13 +273,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug51544() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug51544.jsp"); @@ -357,13 +284,7 @@ public class TestELInJsp extends TomcatB @Test public void testELMisc() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/el-misc.jsp"); String result = res.toString(); @@ -395,13 +316,7 @@ public class TestELInJsp extends TomcatB @Test public void testScriptingExpression() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/script-expr.jsp"); String result = res.toString(); @@ -421,13 +336,7 @@ public class TestELInJsp extends TomcatB @Test public void testELMethod() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/el-method.jsp"); String result = res.toString(); @@ -441,20 +350,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug56029() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - Context ctxt = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - // This test needs the JSTL libraries - File lib = new File("webapps/examples/WEB-INF/lib"); - ctxt.setResources(new StandardRoot(ctxt)); - ctxt.getResources().createWebResourceSet( - WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", - lib.getAbsolutePath(), null, "/"); - - tomcat.start(); + getTomcatInstanceTestWebapp(true, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56029.jspx"); @@ -467,21 +363,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug56147() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - ctx.setResources(new StandardRoot(ctx)); - - // Add the JSTL (we need the TLD) - File lib = new File("webapps/examples/WEB-INF/lib"); - ctx.getResources().createWebResourceSet( - WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", - lib.getAbsolutePath(), null, "/"); - - tomcat.start(); + getTomcatInstanceTestWebapp(true, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56147.jsp"); @@ -493,15 +375,7 @@ public class TestELInJsp extends TomcatB @Test public void testBug56612() throws Exception { - Tomcat tomcat = getTomcatInstance(); - - File appDir = new File("test/webapp"); - // app dir is relative to server home - Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); - - ctx.setResources(new StandardRoot(ctx)); - - tomcat.start(); + getTomcatInstanceTestWebapp(false, true); ByteChunk res = getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug56612.jsp"); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org