Author: kkolinko
Date: Tue Jan  8 08:38:58 2013
New Revision: 1430171

URL: http://svn.apache.org/viewvc?rev=1430171&view=rev
Log:
Merged revisions r1429687 r1429745 from tomcat/trunk:
Amended the test for bug 46243:
Test that a context can be started again after a failed start. There is no need 
to redeploy it.
........
Added comments and a pair of additional checks.
Renamed a variable to correct a typo.
........

Modified:
    tomcat/tc7.0.x/trunk/   (props changed)
    tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java
    tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java

Propchange: tomcat/tc7.0.x/trunk/
------------------------------------------------------------------------------
  Merged /tomcat/trunk:r1429687,1429745

Modified: 
tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java?rev=1430171&r1=1430170&r2=1430171&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java 
(original)
+++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/core/TestStandardContext.java 
Tue Jan  8 08:38:58 2013
@@ -49,6 +49,7 @@ import static org.junit.Assert.fail;
 import org.junit.Test;
 
 import org.apache.catalina.Context;
+import org.apache.catalina.LifecycleState;
 import org.apache.catalina.Wrapper;
 import org.apache.catalina.authenticator.BasicAuthenticator;
 import org.apache.catalina.deploy.FilterDef;
@@ -70,6 +71,10 @@ public class TestStandardContext extends
 
     @Test
     public void testBug46243() throws Exception {
+        // This tests that if a Filter init() fails then the web application
+        // is not put into service. (BZ 46243)
+        // This also tests that if the cause of the failure is gone,
+        // the context can be started without a need to redeploy it.
 
         // Set up a container
         Tomcat tomcat = getTomcatInstance();
@@ -80,23 +85,7 @@ public class TestStandardContext extends
         }
 
         Context root = tomcat.addContext("", "ROOT");
-
-        // Add test a filter that fails
-        FilterDef filterDef = new FilterDef();
-        filterDef.setFilterClass(Bug46243Filter.class.getName());
-        filterDef.setFilterName("Bug46243");
-        root.addFilterDef(filterDef);
-        FilterMap filterMap = new FilterMap();
-        filterMap.setFilterName("Bug46243");
-        filterMap.addURLPattern("*");
-        root.addFilterMap(filterMap);
-
-        // Add a test servlet so there is something to generate a response if
-        // it works (although it shouldn't)
-        Tomcat.addServlet(root, "Bug46243", new HelloWorldServlet());
-        root.addServletMapping("/", "Bug46243");
-
-
+        configureTest46243Context(root, true);
         tomcat.start();
 
         // Configure the client
@@ -107,6 +96,41 @@ public class TestStandardContext extends
         client.connect();
         client.processRequest();
         assertTrue(client.isResponse404());
+
+        // Context failed to start. This checks that automatic transition
+        // from FAILED to STOPPED state was successful.
+        assertEquals(LifecycleState.STOPPED, root.getState());
+
+        // Prepare context for the second attempt
+        // Configuration was cleared on stop() thanks to
+        // StandardContext.resetContext(), so we need to configure it again
+        // from scratch.
+        configureTest46243Context(root, false);
+        root.start();
+        // The same request is processed successfully
+        client.connect();
+        client.processRequest();
+        assertTrue(client.isResponse200());
+        assertEquals(Bug46243Filter.class.getName()
+                + HelloWorldServlet.RESPONSE_TEXT, client.getResponseBody());
+    }
+
+    private static void configureTest46243Context(Context context, boolean 
fail) {
+        // Add a test filter that fails
+        FilterDef filterDef = new FilterDef();
+        filterDef.setFilterClass(Bug46243Filter.class.getName());
+        filterDef.setFilterName("Bug46243");
+        filterDef.addInitParameter("fail", Boolean.toString(fail));
+        context.addFilterDef(filterDef);
+        FilterMap filterMap = new FilterMap();
+        filterMap.setFilterName("Bug46243");
+        filterMap.addURLPattern("*");
+        context.addFilterMap(filterMap);
+
+        // Add a test servlet so there is something to generate a response if
+        // it works (although it shouldn't)
+        Tomcat.addServlet(context, "Bug46243", new HelloWorldServlet());
+        context.addServletMapping("/", "Bug46243");
     }
 
     private static final class Bug46243Client extends SimpleHttpClient {
@@ -132,19 +156,28 @@ public class TestStandardContext extends
         @Override
         public void doFilter(ServletRequest request, ServletResponse response,
                 FilterChain chain) throws IOException, ServletException {
-            // If it works, do nothing
+            @SuppressWarnings("resource") // No need to close this writer
+            PrintWriter out = response.getWriter();
+            out.print(getClass().getName());
             chain.doFilter(request, response);
         }
 
         @Override
         public void init(FilterConfig filterConfig) throws ServletException {
-            throw new ServletException("Init fail", new 
ClassNotFoundException());
+            boolean fail = 
filterConfig.getInitParameter("fail").equals("true");
+            if (fail) {
+                throw new ServletException("Init fail",
+                        new ClassNotFoundException());
+            }
         }
 
     }
 
     @Test
     public void testBug49922() throws Exception {
+        // Test that filter mapping works. Test that the same filter is
+        // called only once, even if is selected by several mapping
+        // url-patterns or by both a url-pattern and a servlet-name.
 
         // Set up a container
         Tomcat tomcat = getTomcatInstance();
@@ -269,6 +302,9 @@ public class TestStandardContext extends
 
     @Test
     public void testBug50015() throws Exception {
+        // Test that configuring servlet security constraints programmatically
+        // does work.
+
         // Set up a container
         Tomcat tomcat = getTomcatInstance();
 
@@ -348,6 +384,9 @@ public class TestStandardContext extends
     }
 
     private void doTestBug51376(boolean loadOnStartUp) throws Exception {
+        // Test that for a servlet that was added programmatically its
+        // loadOnStartup property is honored and its init() and destroy()
+        // methods are called.
 
         // Set up a container
         Tomcat tomcat = getTomcatInstance();
@@ -368,6 +407,7 @@ public class TestStandardContext extends
 
         // Make sure that init() and destroy() were called correctly
         assertTrue(sci.getServlet().isOk());
+        assertTrue(loadOnStartUp == sci.getServlet().isInitCalled());
     }
 
     public static final class Bug51376SCI
@@ -402,11 +442,11 @@ public class TestStandardContext extends
         private static final long serialVersionUID = 1L;
 
         private Boolean initOk = null;
-        private Boolean destoryOk = null;
+        private Boolean destroyOk = null;
 
         @Override
         public void init() {
-            if (initOk == null && destoryOk == null) {
+            if (initOk == null && destroyOk == null) {
                 initOk = Boolean.TRUE;
             } else {
                 initOk = Boolean.FALSE;
@@ -415,10 +455,10 @@ public class TestStandardContext extends
 
         @Override
         public void destroy() {
-            if (initOk.booleanValue() && destoryOk == null) {
-                destoryOk = Boolean.TRUE;
+            if (initOk.booleanValue() && destroyOk == null) {
+                destroyOk = Boolean.TRUE;
             } else {
-                destoryOk = Boolean.FALSE;
+                destroyOk = Boolean.FALSE;
             }
         }
 
@@ -430,15 +470,19 @@ public class TestStandardContext extends
         }
 
         protected boolean isOk() {
-            if (initOk != null && initOk.booleanValue() && destoryOk != null &&
-                    destoryOk.booleanValue()) {
+            if (initOk != null && initOk.booleanValue() && destroyOk != null &&
+                    destroyOk.booleanValue()) {
                 return true;
-            } else if (initOk == null && destoryOk == null) {
+            } else if (initOk == null && destroyOk == null) {
                 return true;
             } else {
                 return false;
             }
         }
+
+        protected boolean isInitCalled() {
+            return initOk != null && initOk.booleanValue();
+        }
     }
 
     /**
@@ -499,6 +543,7 @@ public class TestStandardContext extends
             resp.setContentType("text/plain");
             resp.setCharacterEncoding("UTF-8");
 
+            @SuppressWarnings("resource") // No need to close this writer
             PrintWriter out = resp.getWriter();
 
             out.println("parts=" + (null == req.getParts()

Modified: 
tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java?rev=1430171&r1=1430170&r2=1430171&view=diff
==============================================================================
--- tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java 
(original)
+++ tomcat/tc7.0.x/trunk/test/org/apache/catalina/startup/TomcatBaseTest.java 
Tue Jan  8 08:38:58 2013
@@ -180,6 +180,7 @@ public abstract class TomcatBaseTest ext
         @Override
         protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                 throws ServletException, IOException {
+            @SuppressWarnings("resource") // No need to close this writer
             PrintWriter out = resp.getWriter();
             out.print(RESPONSE_TEXT);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to