[tomcat] branch 9.0.x updated: Parameterise test for LoadBalancerDrainingValve

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[tomcat] branch 9.0.x updated: Parameterise test for LoadBalancerDrainingValve

markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/9.0.x by this push:
     new 6e68b62  Parameterise test for LoadBalancerDrainingValve
6e68b62 is described below

commit 6e68b62cd11e2ecdd0658f9ed7bdd34eabf218bd
Author: Mark Thomas <[hidden email]>
AuthorDate: Thu Nov 19 14:37:02 2020 +0000

    Parameterise test for LoadBalancerDrainingValve
---
 .../valves/TestLoadBalancerDrainingValve.java      | 266 +++++++++++----------
 1 file changed, 136 insertions(+), 130 deletions(-)

diff --git a/test/org/apache/catalina/valves/TestLoadBalancerDrainingValve.java b/test/org/apache/catalina/valves/TestLoadBalancerDrainingValve.java
index 0c5694c..9f32f11 100644
--- a/test/org/apache/catalina/valves/TestLoadBalancerDrainingValve.java
+++ b/test/org/apache/catalina/valves/TestLoadBalancerDrainingValve.java
@@ -16,6 +16,7 @@
 package org.apache.catalina.valves;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.List;
 
 import javax.servlet.ServletContext;
@@ -23,6 +24,10 @@ import javax.servlet.SessionCookieConfig;
 import javax.servlet.http.Cookie;
 
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameter;
+import org.junit.runners.Parameterized.Parameters;
 
 import org.apache.catalina.Context;
 import org.apache.catalina.Valve;
@@ -32,28 +37,143 @@ import org.apache.catalina.core.StandardPipeline;
 import org.easymock.EasyMock;
 import org.easymock.IMocksControl;
 
+@RunWith(Parameterized.class)
 public class TestLoadBalancerDrainingValve {
 
-    static class MockResponse extends Response {
-        private List<Cookie> cookies;
-        @Override
-        public boolean isCommitted() {
-            return false;
+    @Parameters(name = "{index}: activation[{0}], validSessionID[{1}], expectInvokeNext[{2}], enableIgnore[{3}], " +
+            "queryString[{4}]")
+    public static Collection<Object[]> parameters() {
+
+        String[] jkActivations = new String[] { "ACT", "DIS" };
+        Boolean[] booleans = new Boolean[] { Boolean.TRUE, Boolean.FALSE };
+        String[] queryStrings = new String[] { null, "foo=bar" };
+
+        List<Object[]> parameterSets = new ArrayList<>();
+            for (String jkActivation : jkActivations) {
+                for (Boolean validSessionId : booleans) {
+                    for (Boolean enableIgnore : booleans) {
+                        Boolean expectInvokeNext = Boolean.valueOf("ACT".equals(jkActivation) || enableIgnore.booleanValue() ||
+                                validSessionId.booleanValue());
+                        for (String queryString : queryStrings) {
+                            parameterSets.add(new Object[] { jkActivation, validSessionId, expectInvokeNext,
+                                    enableIgnore, queryString});
+                        }
+                    }
+                }
+            }
+        return parameterSets;
+    }
+
+
+    @Parameter(0)
+    public String jkActivation;
+
+    @Parameter(1)
+    public boolean validSessionId;
+
+    @Parameter(2)
+    public boolean expectInvokeNext;
+
+    @Parameter(3)
+    public boolean enableIgnore;
+
+    @Parameter(4)
+    public String queryString;
+
+    @Test
+    public void runValve() throws Exception {
+        IMocksControl control = EasyMock.createControl();
+        ServletContext servletContext = control.createMock(ServletContext.class);
+        Context ctx = control.createMock(Context.class);
+        Request request = control.createMock(Request.class);
+        Response response = control.createMock(Response.class);
+
+        String sessionCookieName = "JSESSIONID";
+        String sessionId = "cafebabe";
+        String requestURI = "/test/path";
+        SessionCookieConfig cookieConfig = new CookieConfig();
+        cookieConfig.setDomain("example.com");
+        cookieConfig.setName(sessionCookieName);
+        cookieConfig.setPath("/");
+
+        // Valve.init requires all of this stuff
+        EasyMock.expect(ctx.getMBeanKeyProperties()).andStubReturn("");
+        EasyMock.expect(ctx.getName()).andStubReturn("");
+        EasyMock.expect(ctx.getPipeline()).andStubReturn(new StandardPipeline());
+        EasyMock.expect(ctx.getDomain()).andStubReturn("foo");
+        EasyMock.expect(ctx.getLogger()).andStubReturn(org.apache.juli.logging.LogFactory.getLog(LoadBalancerDrainingValve.class));
+        EasyMock.expect(ctx.getServletContext()).andStubReturn(servletContext);
+
+        // Set up the actual test
+        EasyMock.expect(request.getAttribute(LoadBalancerDrainingValve.ATTRIBUTE_KEY_JK_LB_ACTIVATION)).andStubReturn(jkActivation);
+        EasyMock.expect(Boolean.valueOf(request.isRequestedSessionIdValid())).andStubReturn(Boolean.valueOf(validSessionId));
+
+        ArrayList<Cookie> cookies = new ArrayList<>();
+        if(enableIgnore) {
+            cookies.add(new Cookie("ignore", "true"));
         }
-        @Override
-        public void addCookie(Cookie cookie)
-        {
-            if(null == cookies)
-                cookies = new ArrayList<>(1);
+
+        if(!validSessionId && jkActivation.equals("DIS")) {
+            MyCookie cookie = new MyCookie(cookieConfig.getName(), sessionId);
+            cookie.setPath(cookieConfig.getPath());
+            cookie.setValue(sessionId);
+
             cookies.add(cookie);
+
+            EasyMock.expect(request.getRequestedSessionId()).andStubReturn(sessionId);
+            EasyMock.expect(request.getRequestURI()).andStubReturn(requestURI);
+            EasyMock.expect(request.getCookies()).andStubReturn(cookies.toArray(new Cookie[0]));
+            EasyMock.expect(request.getContext()).andStubReturn(ctx);
+            EasyMock.expect(ctx.getSessionCookieName()).andStubReturn(sessionCookieName);
+            EasyMock.expect(servletContext.getSessionCookieConfig()).andStubReturn(cookieConfig);
+            EasyMock.expect(request.getQueryString()).andStubReturn(queryString);
+            EasyMock.expect(ctx.getSessionCookiePath()).andStubReturn("/");
+
+            if (!enableIgnore) {
+                EasyMock.expect(Boolean.valueOf(ctx.getSessionCookiePathUsesTrailingSlash())).andStubReturn(Boolean.TRUE);
+                EasyMock.expect(request.getQueryString()).andStubReturn(queryString);
+                // Response will have cookie deleted
+                MyCookie expectedCookie = new MyCookie(cookieConfig.getName(), "");
+                expectedCookie.setPath(cookieConfig.getPath());
+                expectedCookie.setMaxAge(0);
+
+                // These two lines just mean EasyMock.expect(response.addCookie) but for a void method
+                response.addCookie(expectedCookie);
+                EasyMock.expect(ctx.getSessionCookieName()).andReturn(sessionCookieName); // Indirect call
+                String expectedRequestURI = requestURI;
+                if(null != queryString)
+                    expectedRequestURI = expectedRequestURI + '?' + queryString;
+                response.setHeader("Location", expectedRequestURI);
+                response.setStatus(307);
+            }
         }
-        @Override
-        public List<Cookie> getCookies() {
-            return cookies;
+
+        Valve next = control.createMock(Valve.class);
+
+        if(expectInvokeNext) {
+            // Expect the "next" Valve to fire
+            // Next 2 lines are basically EasyMock.expect(next.invoke(req,res)) but for a void method
+            next.invoke(request, response);
+            EasyMock.expectLastCall();
         }
+
+        // Get set to actually test
+        control.replay();
+
+        LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();
+        valve.setContainer(ctx);
+        valve.init();
+        valve.setNext(next);
+        valve.setIgnoreCookieName("ignore");
+        valve.setIgnoreCookieValue("true");
+
+        valve.invoke(request, response);
+
+        control.verify();
     }
 
-    static class CookieConfig implements SessionCookieConfig {
+
+    private static class CookieConfig implements SessionCookieConfig {
 
         private String name;
         private String domain;
@@ -121,8 +241,9 @@ public class TestLoadBalancerDrainingValve {
         }
     }
 
+
     // A Cookie subclass that knows how to compare itself to other Cookie objects
-    static class MyCookie extends Cookie {
+    private static class MyCookie extends Cookie {
         private static final long serialVersionUID = 1L;
 
         public MyCookie(String name, String value) { super(name, value); }
@@ -157,119 +278,4 @@ public class TestLoadBalancerDrainingValve {
             return "Cookie { name=" + getName() + ", value=" + getValue() + ", path=" + getPath() + ", maxAge=" + getMaxAge() + " }";
         }
     }
-
-    @Test
-    public void testNormalRequest() throws Exception {
-        runValve("ACT", true, true, false, null);
-    }
-
-    @Test
-    public void testDisabledValidSession() throws Exception {
-        runValve("DIS", true, true, false, null);
-    }
-
-    @Test
-    public void testDisabledInvalidSession() throws Exception {
-        runValve("DIS", false, false, false, "foo=bar");
-    }
-
-    @Test
-    public void testDisabledInvalidSessionWithIgnore() throws Exception {
-        runValve("DIS", false, true, true, "foo=bar");
-    }
-
-    private void runValve(String jkActivation,
-                          boolean validSessionId,
-                          boolean expectInvokeNext,
-                          boolean enableIgnore,
-                          String queryString) throws Exception {
-        IMocksControl control = EasyMock.createControl();
-        ServletContext servletContext = control.createMock(ServletContext.class);
-        Context ctx = control.createMock(Context.class);
-        Request request = control.createMock(Request.class);
-        Response response = control.createMock(Response.class);
-
-        String sessionCookieName = "JSESSIONID";
-        String sessionId = "cafebabe";
-        String requestURI = "/test/path";
-        SessionCookieConfig cookieConfig = new CookieConfig();
-        cookieConfig.setDomain("example.com");
-        cookieConfig.setName(sessionCookieName);
-        cookieConfig.setPath("/");
-
-        // Valve.init requires all of this stuff
-        EasyMock.expect(ctx.getMBeanKeyProperties()).andStubReturn("");
-        EasyMock.expect(ctx.getName()).andStubReturn("");
-        EasyMock.expect(ctx.getPipeline()).andStubReturn(new StandardPipeline());
-        EasyMock.expect(ctx.getDomain()).andStubReturn("foo");
-        EasyMock.expect(ctx.getLogger()).andStubReturn(org.apache.juli.logging.LogFactory.getLog(LoadBalancerDrainingValve.class));
-        EasyMock.expect(ctx.getServletContext()).andStubReturn(servletContext);
-
-        // Set up the actual test
-        EasyMock.expect(request.getAttribute(LoadBalancerDrainingValve.ATTRIBUTE_KEY_JK_LB_ACTIVATION)).andStubReturn(jkActivation);
-        EasyMock.expect(Boolean.valueOf(request.isRequestedSessionIdValid())).andStubReturn(Boolean.valueOf(validSessionId));
-
-        ArrayList<Cookie> cookies = new ArrayList<>();
-        if(enableIgnore) {
-            cookies.add(new Cookie("ignore", "true"));
-        }
-
-        if(!validSessionId) {
-            MyCookie cookie = new MyCookie(cookieConfig.getName(), sessionId);
-            cookie.setPath(cookieConfig.getPath());
-            cookie.setValue(sessionId);
-
-            cookies.add(cookie);
-
-            EasyMock.expect(request.getRequestedSessionId()).andStubReturn(sessionId);
-            EasyMock.expect(request.getRequestURI()).andStubReturn(requestURI);
-            EasyMock.expect(request.getCookies()).andStubReturn(cookies.toArray(new Cookie[0]));
-            EasyMock.expect(request.getContext()).andStubReturn(ctx);
-            EasyMock.expect(ctx.getSessionCookieName()).andStubReturn(sessionCookieName);
-            EasyMock.expect(servletContext.getSessionCookieConfig()).andStubReturn(cookieConfig);
-            EasyMock.expect(request.getQueryString()).andStubReturn(queryString);
-            EasyMock.expect(ctx.getSessionCookiePath()).andStubReturn("/");
-
-            if (!enableIgnore) {
-                EasyMock.expect(Boolean.valueOf(ctx.getSessionCookiePathUsesTrailingSlash())).andStubReturn(Boolean.TRUE);
-                EasyMock.expect(request.getQueryString()).andStubReturn(queryString);
-                // Response will have cookie deleted
-                MyCookie expectedCookie = new MyCookie(cookieConfig.getName(), "");
-                expectedCookie.setPath(cookieConfig.getPath());
-                expectedCookie.setMaxAge(0);
-
-                // These two lines just mean EasyMock.expect(response.addCookie) but for a void method
-                response.addCookie(expectedCookie);
-                EasyMock.expect(ctx.getSessionCookieName()).andReturn(sessionCookieName); // Indirect call
-                String expectedRequestURI = requestURI;
-                if(null != queryString)
-                    expectedRequestURI = expectedRequestURI + '?' + queryString;
-                response.setHeader("Location", expectedRequestURI);
-                response.setStatus(307);
-            }
-        }
-
-        Valve next = control.createMock(Valve.class);
-
-        if(expectInvokeNext) {
-            // Expect the "next" Valve to fire
-            // Next 2 lines are basically EasyMock.expect(next.invoke(req,res)) but for a void method
-            next.invoke(request, response);
-            EasyMock.expectLastCall();
-        }
-
-        // Get set to actually test
-        control.replay();
-
-        LoadBalancerDrainingValve valve = new LoadBalancerDrainingValve();
-        valve.setContainer(ctx);
-        valve.init();
-        valve.setNext(next);
-        valve.setIgnoreCookieName("ignore");
-        valve.setIgnoreCookieValue("true");
-
-        valve.invoke(request, response);
-
-        control.verify();
-    }
 }


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