diff --git a/spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java b/spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java
index 4657e976821..d13d767466a 100644
--- a/spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java
+++ b/spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java
@@ -18,7 +18,9 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
- * Mock implementation of the {@link javax.servlet.FilterChain} interface.
+ *
Mock implementation of the {@link javax.servlet.FilterChain} interface. Used
+ * for testing the web framework; also useful for testing custom
+ * {@link javax.servlet.Filter} implementations.
*
- *
Used for testing the web framework; also useful for testing
- * custom {@link javax.servlet.Filter} implementations.
+ *
A {@link MockFilterChain} can be configured with one or more filters and a
+ * Servlet to invoke. The first time the chain is called, it invokes all filters
+ * and the Servlet, and saves the request and response. Subsequent invocations
+ * raise an {@link IllegalStateException} unless {@link #reset()} is called.
*
* @author Juergen Hoeller
* @author Rob Winch
+ * @author Rossen Stoyanchev
*
* @since 2.0.3
* @see MockFilterConfig
@@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
- private final Iterator iterator;
+ private final List filters;
+
+ private Iterator iterator;
/**
* Register a single do-nothing {@link Filter} implementation. The first
* invocation saves the request and response. Subsequent invocations raise
- * an {@link IllegalStateException}.
+ * an {@link IllegalStateException} unless {@link #reset()} is called.
*/
public MockFilterChain() {
- this.iterator = null;
+ this.filters = Collections.emptyList();
}
/**
- * Create a FilterChain with a {@link Servlet} but without filters.
+ * Create a FilterChain with a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
+ * @param servlet the Servlet to invoke
* @since 3.2
*/
public MockFilterChain(Servlet servlet) {
- this(new ServletFilterProxy(servlet));
+ this.filters = initFilterList(servlet);
}
/**
- * Create a FilterChain with one or more {@link Filter} instances and a {@link Servlet}.
+ * Create a {@code FilterChain} with Filter's and a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
+ * @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
+ * @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 3.2
*/
public MockFilterChain(Servlet servlet, Filter... filters) {
- this(ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet)));
+ Assert.notNull(filters, "filters cannot be null");
+ Assert.noNullElements(filters, "filters cannot contain null values");
+ this.filters = initFilterList(servlet, filters);
+ }
+
+ private static List initFilterList(Servlet servlet, Filter... filters) {
+ Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet));
+ return Arrays.asList(allFilters);
}
/**
- * Create a {@link FilterChain} with one or more {@link Filter} instances.
- *
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
- * @since 3.2
+ * Return the request that {@link #doFilter} has been called with.
*/
- private MockFilterChain(Filter... filters) {
- Assert.notNull(filters, "filters cannot be null");
- Assert.notEmpty(filters, "filters cannot be empty");
- Assert.noNullElements(filters, "filters cannot contain null values");
- this.iterator = Arrays.asList(filters).iterator();
+ public ServletRequest getRequest() {
+ return this.request;
+ }
+
+ /**
+ * Return the response that {@link #doFilter} has been called with.
+ */
+ public ServletResponse getResponse() {
+ return this.response;
}
/**
@@ -108,7 +125,11 @@ public class MockFilterChain implements FilterChain {
throw new IllegalStateException("This FilterChain has already been called!");
}
- if ((this.iterator != null) && (this.iterator.hasNext())) {
+ if (this.iterator == null) {
+ this.iterator = this.filters.iterator();
+ }
+
+ if (this.iterator.hasNext()) {
Filter nextFilter = this.iterator.next();
nextFilter.doFilter(request, response, this);
}
@@ -118,17 +139,12 @@ public class MockFilterChain implements FilterChain {
}
/**
- * Return the request that {@link #doFilter} has been called with.
- */
- public ServletRequest getRequest() {
- return this.request;
- }
-
- /**
- * Return the response that {@link #doFilter} has been called with.
+ * Reset the {@link MockFilterChain} allowing it to be invoked again.
*/
- public ServletResponse getResponse() {
- return this.response;
+ public void reset() {
+ this.request = null;
+ this.response = null;
+ this.iterator = null;
}
diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java b/spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java
index 4657e976821..a80a2fb454a 100644
--- a/spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java
+++ b/spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java
@@ -18,7 +18,9 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -28,17 +30,24 @@ import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import org.springframework.mock.web.MockFilterConfig;
+import org.springframework.mock.web.PassThroughFilterChain;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
- * Mock implementation of the {@link javax.servlet.FilterChain} interface.
+ * Mock implementation of the {@link javax.servlet.FilterChain} interface. Used
+ * for testing the web framework; also useful for testing custom
+ * {@link javax.servlet.Filter} implementations.
*
- *
Used for testing the web framework; also useful for testing
- * custom {@link javax.servlet.Filter} implementations.
+ *
A {@link MockFilterChain} can be configured with one or more filters and a
+ * Servlet to invoke. The first time the chain is called, it invokes all filters
+ * and the Servlet, and saves the request and response. Subsequent invocations
+ * raise an {@link IllegalStateException} unless {@link #reset()} is called.
*
* @author Juergen Hoeller
* @author Rob Winch
+ * @author Rossen Stoyanchev
*
* @since 2.0.3
* @see MockFilterConfig
@@ -50,50 +59,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
- private final Iterator iterator;
+ private final List filters;
+
+ private Iterator iterator;
/**
* Register a single do-nothing {@link Filter} implementation. The first
* invocation saves the request and response. Subsequent invocations raise
- * an {@link IllegalStateException}.
+ * an {@link IllegalStateException} unless {@link #reset()} is called.
*/
public MockFilterChain() {
- this.iterator = null;
+ this.filters = Collections.emptyList();
}
/**
- * Create a FilterChain with a {@link Servlet} but without filters.
+ * Create a FilterChain with a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
+ * @param servlet the Servlet to invoke
* @since 3.2
*/
public MockFilterChain(Servlet servlet) {
- this(new ServletFilterProxy(servlet));
+ this.filters = initFilterList(servlet);
}
/**
- * Create a FilterChain with one or more {@link Filter} instances and a {@link Servlet}.
+ * Create a {@code FilterChain} with Filter's and a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
+ * @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
+ * @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 3.2
*/
public MockFilterChain(Servlet servlet, Filter... filters) {
- this(ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet)));
+ Assert.notNull(filters, "filters cannot be null");
+ Assert.noNullElements(filters, "filters cannot contain null values");
+ this.filters = initFilterList(servlet, filters);
+ }
+
+ private static List initFilterList(Servlet servlet, Filter... filters) {
+ Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet));
+ return Arrays.asList(allFilters);
}
/**
- * Create a {@link FilterChain} with one or more {@link Filter} instances.
- *
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
- * @since 3.2
+ * Return the request that {@link #doFilter} has been called with.
*/
- private MockFilterChain(Filter... filters) {
- Assert.notNull(filters, "filters cannot be null");
- Assert.notEmpty(filters, "filters cannot be empty");
- Assert.noNullElements(filters, "filters cannot contain null values");
- this.iterator = Arrays.asList(filters).iterator();
+ public ServletRequest getRequest() {
+ return this.request;
+ }
+
+ /**
+ * Return the response that {@link #doFilter} has been called with.
+ */
+ public ServletResponse getResponse() {
+ return this.response;
}
/**
@@ -108,7 +127,11 @@ public class MockFilterChain implements FilterChain {
throw new IllegalStateException("This FilterChain has already been called!");
}
- if ((this.iterator != null) && (this.iterator.hasNext())) {
+ if (this.iterator == null) {
+ this.iterator = this.filters.iterator();
+ }
+
+ if (this.iterator.hasNext()) {
Filter nextFilter = this.iterator.next();
nextFilter.doFilter(request, response, this);
}
@@ -118,17 +141,12 @@ public class MockFilterChain implements FilterChain {
}
/**
- * Return the request that {@link #doFilter} has been called with.
- */
- public ServletRequest getRequest() {
- return this.request;
- }
-
- /**
- * Return the response that {@link #doFilter} has been called with.
+ * Reset the {@link MockFilterChain} allowing it to be invoked again.
*/
- public ServletResponse getResponse() {
- return this.response;
+ public void reset() {
+ this.request = null;
+ this.response = null;
+ this.iterator = null;
}
diff --git a/spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java b/spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java
index 4657e976821..d13d767466a 100644
--- a/spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java
+++ b/spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java
@@ -18,7 +18,9 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
- * Mock implementation of the {@link javax.servlet.FilterChain} interface.
+ * Mock implementation of the {@link javax.servlet.FilterChain} interface. Used
+ * for testing the web framework; also useful for testing custom
+ * {@link javax.servlet.Filter} implementations.
*
- *
Used for testing the web framework; also useful for testing
- * custom {@link javax.servlet.Filter} implementations.
+ *
A {@link MockFilterChain} can be configured with one or more filters and a
+ * Servlet to invoke. The first time the chain is called, it invokes all filters
+ * and the Servlet, and saves the request and response. Subsequent invocations
+ * raise an {@link IllegalStateException} unless {@link #reset()} is called.
*
* @author Juergen Hoeller
* @author Rob Winch
+ * @author Rossen Stoyanchev
*
* @since 2.0.3
* @see MockFilterConfig
@@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
- private final Iterator iterator;
+ private final List filters;
+
+ private Iterator iterator;
/**
* Register a single do-nothing {@link Filter} implementation. The first
* invocation saves the request and response. Subsequent invocations raise
- * an {@link IllegalStateException}.
+ * an {@link IllegalStateException} unless {@link #reset()} is called.
*/
public MockFilterChain() {
- this.iterator = null;
+ this.filters = Collections.emptyList();
}
/**
- * Create a FilterChain with a {@link Servlet} but without filters.
+ * Create a FilterChain with a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
+ * @param servlet the Servlet to invoke
* @since 3.2
*/
public MockFilterChain(Servlet servlet) {
- this(new ServletFilterProxy(servlet));
+ this.filters = initFilterList(servlet);
}
/**
- * Create a FilterChain with one or more {@link Filter} instances and a {@link Servlet}.
+ * Create a {@code FilterChain} with Filter's and a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
+ * @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
+ * @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 3.2
*/
public MockFilterChain(Servlet servlet, Filter... filters) {
- this(ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet)));
+ Assert.notNull(filters, "filters cannot be null");
+ Assert.noNullElements(filters, "filters cannot contain null values");
+ this.filters = initFilterList(servlet, filters);
+ }
+
+ private static List initFilterList(Servlet servlet, Filter... filters) {
+ Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet));
+ return Arrays.asList(allFilters);
}
/**
- * Create a {@link FilterChain} with one or more {@link Filter} instances.
- *
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
- * @since 3.2
+ * Return the request that {@link #doFilter} has been called with.
*/
- private MockFilterChain(Filter... filters) {
- Assert.notNull(filters, "filters cannot be null");
- Assert.notEmpty(filters, "filters cannot be empty");
- Assert.noNullElements(filters, "filters cannot contain null values");
- this.iterator = Arrays.asList(filters).iterator();
+ public ServletRequest getRequest() {
+ return this.request;
+ }
+
+ /**
+ * Return the response that {@link #doFilter} has been called with.
+ */
+ public ServletResponse getResponse() {
+ return this.response;
}
/**
@@ -108,7 +125,11 @@ public class MockFilterChain implements FilterChain {
throw new IllegalStateException("This FilterChain has already been called!");
}
- if ((this.iterator != null) && (this.iterator.hasNext())) {
+ if (this.iterator == null) {
+ this.iterator = this.filters.iterator();
+ }
+
+ if (this.iterator.hasNext()) {
Filter nextFilter = this.iterator.next();
nextFilter.doFilter(request, response, this);
}
@@ -118,17 +139,12 @@ public class MockFilterChain implements FilterChain {
}
/**
- * Return the request that {@link #doFilter} has been called with.
- */
- public ServletRequest getRequest() {
- return this.request;
- }
-
- /**
- * Return the response that {@link #doFilter} has been called with.
+ * Reset the {@link MockFilterChain} allowing it to be invoked again.
*/
- public ServletResponse getResponse() {
- return this.response;
+ public void reset() {
+ this.request = null;
+ this.response = null;
+ this.iterator = null;
}
diff --git a/spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java b/spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java
index 4657e976821..d13d767466a 100644
--- a/spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java
+++ b/spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java
@@ -18,7 +18,9 @@ package org.springframework.mock.web;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.Iterator;
+import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
- * Mock implementation of the {@link javax.servlet.FilterChain} interface.
+ * Mock implementation of the {@link javax.servlet.FilterChain} interface. Used
+ * for testing the web framework; also useful for testing custom
+ * {@link javax.servlet.Filter} implementations.
*
- *
Used for testing the web framework; also useful for testing
- * custom {@link javax.servlet.Filter} implementations.
+ *
A {@link MockFilterChain} can be configured with one or more filters and a
+ * Servlet to invoke. The first time the chain is called, it invokes all filters
+ * and the Servlet, and saves the request and response. Subsequent invocations
+ * raise an {@link IllegalStateException} unless {@link #reset()} is called.
*
* @author Juergen Hoeller
* @author Rob Winch
+ * @author Rossen Stoyanchev
*
* @since 2.0.3
* @see MockFilterConfig
@@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
- private final Iterator iterator;
+ private final List filters;
+
+ private Iterator iterator;
/**
* Register a single do-nothing {@link Filter} implementation. The first
* invocation saves the request and response. Subsequent invocations raise
- * an {@link IllegalStateException}.
+ * an {@link IllegalStateException} unless {@link #reset()} is called.
*/
public MockFilterChain() {
- this.iterator = null;
+ this.filters = Collections.emptyList();
}
/**
- * Create a FilterChain with a {@link Servlet} but without filters.
+ * Create a FilterChain with a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
+ * @param servlet the Servlet to invoke
* @since 3.2
*/
public MockFilterChain(Servlet servlet) {
- this(new ServletFilterProxy(servlet));
+ this.filters = initFilterList(servlet);
}
/**
- * Create a FilterChain with one or more {@link Filter} instances and a {@link Servlet}.
+ * Create a {@code FilterChain} with Filter's and a Servlet.
*
- * @param servlet the {@link Servlet} to use in this {@link FilterChain}
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
+ * @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
+ * @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
* @since 3.2
*/
public MockFilterChain(Servlet servlet, Filter... filters) {
- this(ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet)));
+ Assert.notNull(filters, "filters cannot be null");
+ Assert.noNullElements(filters, "filters cannot contain null values");
+ this.filters = initFilterList(servlet, filters);
+ }
+
+ private static List initFilterList(Servlet servlet, Filter... filters) {
+ Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet));
+ return Arrays.asList(allFilters);
}
/**
- * Create a {@link FilterChain} with one or more {@link Filter} instances.
- *
- * @param filters the {@link Filter}'s to use in this {@link FilterChain}
- * @since 3.2
+ * Return the request that {@link #doFilter} has been called with.
*/
- private MockFilterChain(Filter... filters) {
- Assert.notNull(filters, "filters cannot be null");
- Assert.notEmpty(filters, "filters cannot be empty");
- Assert.noNullElements(filters, "filters cannot contain null values");
- this.iterator = Arrays.asList(filters).iterator();
+ public ServletRequest getRequest() {
+ return this.request;
+ }
+
+ /**
+ * Return the response that {@link #doFilter} has been called with.
+ */
+ public ServletResponse getResponse() {
+ return this.response;
}
/**
@@ -108,7 +125,11 @@ public class MockFilterChain implements FilterChain {
throw new IllegalStateException("This FilterChain has already been called!");
}
- if ((this.iterator != null) && (this.iterator.hasNext())) {
+ if (this.iterator == null) {
+ this.iterator = this.filters.iterator();
+ }
+
+ if (this.iterator.hasNext()) {
Filter nextFilter = this.iterator.next();
nextFilter.doFilter(request, response, this);
}
@@ -118,17 +139,12 @@ public class MockFilterChain implements FilterChain {
}
/**
- * Return the request that {@link #doFilter} has been called with.
- */
- public ServletRequest getRequest() {
- return this.request;
- }
-
- /**
- * Return the response that {@link #doFilter} has been called with.
+ * Reset the {@link MockFilterChain} allowing it to be invoked again.
*/
- public ServletResponse getResponse() {
- return this.response;
+ public void reset() {
+ this.request = null;
+ this.response = null;
+ this.iterator = null;
}