kusalk commented on code in PR #663:
URL: https://github.com/apache/struts/pull/663#discussion_r1121457278
##########
core/src/test/java/org/apache/struts2/dispatcher/TwoFilterIntegrationTest.java:
##########
@@ -19,87 +19,133 @@
package org.apache.struts2.dispatcher;
import com.opensymphony.xwork2.ActionContext;
-import junit.framework.TestCase;
-import org.apache.struts2.dispatcher.Dispatcher;
-import org.apache.struts2.dispatcher.PrepareOperations;
import org.apache.struts2.dispatcher.filter.StrutsExecuteFilter;
import org.apache.struts2.dispatcher.filter.StrutsPrepareFilter;
-import org.springframework.mock.web.*;
+import org.junit.Before;
+import org.junit.Test;
+import org.springframework.mock.web.MockFilterChain;
+import org.springframework.mock.web.MockFilterConfig;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
-import javax.servlet.*;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
import java.io.IOException;
-import java.util.LinkedList;
import java.util.Arrays;
+import java.util.LinkedList;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
/**
* Integration tests for the filter
*/
-public class TwoFilterIntegrationTest extends TestCase {
- StrutsExecuteFilter filterExecute;
- StrutsPrepareFilter filterPrepare;
- Filter failFilter;
+public class TwoFilterIntegrationTest {
+ private StrutsExecuteFilter filterExecute;
+ private StrutsPrepareFilter filterPrepare;
+ private Filter failFilter;
private Filter stringFilter;
+ @Before
public void setUp() {
filterPrepare = new StrutsPrepareFilter();
filterExecute = new StrutsExecuteFilter();
- failFilter = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- fail("Should never get here");
- }
- public void destroy() {}
- };
- stringFilter = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- response.getWriter().write("content");
- assertNotNull(ActionContext.getContext());
- assertNotNull(Dispatcher.getInstance());
- }
- public void destroy() {}
- };
+ failFilter = newFilter((req, res, chain) -> fail("Should never get
here"));
+ stringFilter = newFilter((req, res, chain) -> {
+ res.getWriter().write("content");
+ assertNotNull(ActionContext.getContext());
+ assertNotNull(Dispatcher.getInstance());
+ });
}
+ @Test
public void test404() throws ServletException, IOException {
MockHttpServletResponse response = run("/foo.action", filterPrepare,
filterExecute, failFilter);
assertEquals(404, response.getStatus());
}
+ @Test
public void test200() throws ServletException, IOException {
MockHttpServletResponse response = run("/hello.action", filterPrepare,
filterExecute, failFilter);
assertEquals(200, response.getStatus());
}
+ @Test
public void testStaticFallthrough() throws ServletException, IOException {
MockHttpServletResponse response = run("/foo.txt", filterPrepare,
filterExecute, stringFilter);
assertEquals(200, response.getStatus());
assertEquals("content", response.getContentAsString());
}
+ @Test
public void testStaticExecute() throws ServletException, IOException {
MockHttpServletResponse response = run("/static/utils.js",
filterPrepare, filterExecute, failFilter);
assertEquals(200, response.getStatus());
assertTrue(response.getContentAsString().contains("StrutsUtils"));
}
+ @Test
public void testFilterInMiddle() throws ServletException, IOException {
- Filter middle = new Filter() {
- public void init(FilterConfig filterConfig) throws
ServletException {}
- public void doFilter(ServletRequest request, ServletResponse
response, FilterChain chain) throws IOException, ServletException {
- assertNotNull(ActionContext.getContext());
- assertNotNull(Dispatcher.getInstance());
- assertNull(ActionContext.getContext().getActionInvocation());
- chain.doFilter(request, response);
- assertEquals("hello",
ActionContext.getContext().getActionInvocation().getProxy().getActionName());
- }
- public void destroy() {}
- };
+ Filter middle = newFilter((req, res, chain) -> {
+ assertNotNull(ActionContext.getContext());
+ assertNotNull(Dispatcher.getInstance());
+ assertNull(ActionContext.getContext().getActionInvocation());
+ chain.doFilter(req, res);
+ assertEquals("hello",
ActionContext.getContext().getActionInvocation().getProxy().getActionName());
+ });
MockHttpServletResponse response = run("/hello.action", filterPrepare,
middle, filterExecute, failFilter);
assertEquals(200, response.getStatus());
}
+ /**
+ * It is possible for a Struts excluded URL to be forwarded to a Struts
URL. If this happens, the ActionContext
+ * should not be cleared until the very first execution of the
StrutsPrepareFilter, otherwise SiteMesh will malfunction.
+ */
+ @Test
+ public void
testActionContextNotClearedUntilEndWhenForwardedFromExcludedUrl() throws
ServletException, IOException {
Review Comment:
This integration test is actually for
[WW-5270](https://issues.apache.org/jira/browse/WW-5270). (We already have
acceptance test coverage for that fix.)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]