提交 fa7472c0 编写于 作者: J Juergen Hoeller

MockHttpServletResponse supports multiple includes (SPR-7188)

上级 bd1b559d
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -98,7 +98,7 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -98,7 +98,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
private String forwardedUrl; private String forwardedUrl;
private String includedUrl; private final List<String> includedUrls = new ArrayList<String>();
//--------------------------------------------------------------------- //---------------------------------------------------------------------
...@@ -443,11 +443,28 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -443,11 +443,28 @@ public class MockHttpServletResponse implements HttpServletResponse {
} }
public void setIncludedUrl(String includedUrl) { public void setIncludedUrl(String includedUrl) {
this.includedUrl = includedUrl; this.includedUrls.clear();
if (includedUrl != null) {
this.includedUrls.add(includedUrl);
}
} }
public String getIncludedUrl() { public String getIncludedUrl() {
return this.includedUrl; int count = this.includedUrls.size();
if (count > 1) {
throw new IllegalStateException(
"More than 1 URL included - check getIncludedUrls instead: " + this.includedUrls);
}
return (count == 1 ? this.includedUrls.get(0) : null);
}
public void addIncludedUrl(String includedUrl) {
Assert.notNull(includedUrl, "Included URL must not be null");
this.includedUrls.add(includedUrl);
}
public List<String> getIncludedUrls() {
return this.includedUrls;
} }
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -68,7 +68,7 @@ public class MockRequestDispatcher implements RequestDispatcher { ...@@ -68,7 +68,7 @@ public class MockRequestDispatcher implements RequestDispatcher {
public void include(ServletRequest request, ServletResponse response) { public void include(ServletRequest request, ServletResponse response) {
Assert.notNull(request, "Request must not be null"); Assert.notNull(request, "Request must not be null");
Assert.notNull(response, "Response must not be null"); Assert.notNull(response, "Response must not be null");
getMockHttpServletResponse(response).setIncludedUrl(this.url); getMockHttpServletResponse(response).addIncludedUrl(this.url);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("MockRequestDispatcher: including URL [" + this.url + "]"); logger.debug("MockRequestDispatcher: including URL [" + this.url + "]");
} }
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -98,7 +98,7 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -98,7 +98,7 @@ public class MockHttpServletResponse implements HttpServletResponse {
private String forwardedUrl; private String forwardedUrl;
private String includedUrl; private final List<String> includedUrls = new ArrayList<String>();
//--------------------------------------------------------------------- //---------------------------------------------------------------------
...@@ -443,11 +443,28 @@ public class MockHttpServletResponse implements HttpServletResponse { ...@@ -443,11 +443,28 @@ public class MockHttpServletResponse implements HttpServletResponse {
} }
public void setIncludedUrl(String includedUrl) { public void setIncludedUrl(String includedUrl) {
this.includedUrl = includedUrl; this.includedUrls.clear();
if (includedUrl != null) {
this.includedUrls.add(includedUrl);
}
} }
public String getIncludedUrl() { public String getIncludedUrl() {
return this.includedUrl; int count = this.includedUrls.size();
if (count > 1) {
throw new IllegalStateException(
"More than 1 URL included - check getIncludedUrls instead: " + this.includedUrls);
}
return (count == 1 ? this.includedUrls.get(0) : null);
}
public void addIncludedUrl(String includedUrl) {
Assert.notNull(includedUrl, "Included URL must not be null");
this.includedUrls.add(includedUrl);
}
public List<String> getIncludedUrls() {
return this.includedUrls;
} }
......
/* /*
* Copyright 2002-2009 the original author or authors. * Copyright 2002-2010 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -68,7 +68,7 @@ public class MockRequestDispatcher implements RequestDispatcher { ...@@ -68,7 +68,7 @@ public class MockRequestDispatcher implements RequestDispatcher {
public void include(ServletRequest request, ServletResponse response) { public void include(ServletRequest request, ServletResponse response) {
Assert.notNull(request, "Request must not be null"); Assert.notNull(request, "Request must not be null");
Assert.notNull(response, "Response must not be null"); Assert.notNull(response, "Response must not be null");
getMockHttpServletResponse(response).setIncludedUrl(this.url); getMockHttpServletResponse(response).addIncludedUrl(this.url);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("MockRequestDispatcher: including URL [" + this.url + "]"); logger.debug("MockRequestDispatcher: including URL [" + this.url + "]");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册