Browse Source

Polish MockFilterChain

A reset method now allows it to be invoked more than once each time
storing the request and response with which it was invoked.
pull/154/head
Rossen Stoyanchev 14 years ago
parent
commit
4566db82f5
  1. 82
      spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java
  2. 84
      spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java
  3. 82
      spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java
  4. 82
      spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java

82
spring-orm/src/test/java/org/springframework/mock/web/MockFilterChain.java

@ -18,7 +18,9 @@ package org.springframework.mock.web; @@ -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; @@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Mock implementation of the {@link javax.servlet.FilterChain} interface.
* <p>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.
*
* <p>Used for testing the web framework; also useful for testing
* custom {@link javax.servlet.Filter} implementations.
* <p>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 { @@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
private final Iterator<Filter> iterator;
private final List<Filter> filters;
private Iterator<Filter> 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<Filter> 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 { @@ -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 { @@ -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;
}

84
spring-test/src/main/java/org/springframework/mock/web/MockFilterChain.java

@ -18,7 +18,9 @@ package org.springframework.mock.web; @@ -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; @@ -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.
* <p>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.
*
* <p>Used for testing the web framework; also useful for testing
* custom {@link javax.servlet.Filter} implementations.
* <p>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 { @@ -50,50 +59,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
private final Iterator<Filter> iterator;
private final List<Filter> filters;
private Iterator<Filter> 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<Filter> 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 { @@ -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 { @@ -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;
}

82
spring-web/src/test/java/org/springframework/mock/web/MockFilterChain.java

@ -18,7 +18,9 @@ package org.springframework.mock.web; @@ -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; @@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Mock implementation of the {@link javax.servlet.FilterChain} interface.
* <p>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.
*
* <p>Used for testing the web framework; also useful for testing
* custom {@link javax.servlet.Filter} implementations.
* <p>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 { @@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
private final Iterator<Filter> iterator;
private final List<Filter> filters;
private Iterator<Filter> 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<Filter> 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 { @@ -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 { @@ -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;
}

82
spring-webmvc/src/test/java/org/springframework/mock/web/MockFilterChain.java

@ -18,7 +18,9 @@ package org.springframework.mock.web; @@ -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; @@ -32,13 +34,18 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
/**
* Mock implementation of the {@link javax.servlet.FilterChain} interface.
* <p>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.
*
* <p>Used for testing the web framework; also useful for testing
* custom {@link javax.servlet.Filter} implementations.
* <p>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 { @@ -50,50 +57,60 @@ public class MockFilterChain implements FilterChain {
private ServletResponse response;
private final Iterator<Filter> iterator;
private final List<Filter> filters;
private Iterator<Filter> 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<Filter> 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 { @@ -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 { @@ -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;
}

Loading…
Cancel
Save