Backport further refinements from the nullability efforts

Issue: SPR-15656
This commit is contained in:
Juergen Hoeller
2017-09-27 15:20:17 +02:00
parent 5f167fd7f8
commit cc70fdcbeb
50 changed files with 382 additions and 532 deletions

View File

@@ -1,67 +0,0 @@
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.config.annotation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.util.ObjectUtils;
/**
* A simple configuration condition that checks for the absence of any beans
* of a given type.
*
* @author Rossen Stoyanchev
* @since 4.1
*/
class BeanTypeNotPresentCondition implements ConfigurationCondition {
private static final Log logger =
LogFactory.getLog("org.springframework.web.servlet.config.annotation.ViewResolution");
private final Class<?> beanType;
BeanTypeNotPresentCondition(Class<?> beanType) {
this.beanType = beanType;
}
@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.PARSE_CONFIGURATION;
}
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ListableBeanFactory factory = context.getBeanFactory();
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, this.beanType, false, false);
if (ObjectUtils.isEmpty(names)) {
logger.debug("No bean of type [" + this.beanType + "]. Conditional configuration applies.");
return true;
}
else {
logger.debug("Found bean of type [" + this.beanType + "]. Conditional configuration does not apply.");
return false;
}
}
}

View File

@@ -197,7 +197,7 @@ public class UrlTag extends HtmlEscapingAwareTag implements ParamAware {
* Build the URL for the tag from the tag attributes and parameters.
* @return the URL value as a String
*/
private String createUrl() throws JspException {
String createUrl() throws JspException {
HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.web.servlet.tags;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
@@ -30,7 +29,6 @@ import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockPageContext;
import org.springframework.util.ReflectionUtils;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
@@ -44,13 +42,15 @@ public class UrlTagTests extends AbstractTagTests {
private MockPageContext context;
@Before
public void setUp() throws Exception {
public void setup() throws Exception {
context = createPageContext();
tag = new UrlTag();
tag.setPageContext(context);
}
@Test
public void paramSupport() {
assertThat(tag, instanceOf(ParamAware.class));
@@ -66,7 +66,6 @@ public class UrlTagTests extends AbstractTagTests {
@Test
public void doEndTag() throws JspException {
tag.setValue("url/path");
tag.doStartTag();
int action = tag.doEndTag();
@@ -77,12 +76,10 @@ public class UrlTagTests extends AbstractTagTests {
public void varDefaultScope() throws JspException {
tag.setValue("url/path");
tag.setVar("var");
tag.doStartTag();
tag.doEndTag();
assertEquals("url/path", context.getAttribute("var",
PageContext.PAGE_SCOPE));
assertEquals("url/path", context.getAttribute("var", PageContext.PAGE_SCOPE));
}
@Test
@@ -90,19 +87,16 @@ public class UrlTagTests extends AbstractTagTests {
tag.setValue("url/path");
tag.setVar("var");
tag.setScope("request");
tag.doStartTag();
tag.doEndTag();
assertEquals("url/path", context.getAttribute("var",
PageContext.REQUEST_SCOPE));
assertEquals("url/path", context.getAttribute("var", PageContext.REQUEST_SCOPE));
}
@Test
public void setHtmlEscapeDefault() throws JspException {
tag.setValue("url/path");
tag.setVar("var");
tag.doStartTag();
Param param = new Param();
@@ -116,9 +110,7 @@ public class UrlTagTests extends AbstractTagTests {
tag.addParam(param);
tag.doEndTag();
assertEquals("url/path?n%20me=v%26l%3De&name=value2", context
.getAttribute("var"));
assertEquals("url/path?n%20me=v%26l%3De&name=value2", context.getAttribute("var"));
}
@Test
@@ -140,9 +132,7 @@ public class UrlTagTests extends AbstractTagTests {
tag.addParam(param);
tag.doEndTag();
assertEquals("url/path?n%20me=v%26l%3De&name=value2", context
.getAttribute("var"));
assertEquals("url/path?n%20me=v%26l%3De&name=value2", context.getAttribute("var"));
}
@Test
@@ -150,7 +140,6 @@ public class UrlTagTests extends AbstractTagTests {
tag.setValue("url/path");
tag.setVar("var");
tag.setHtmlEscape(true);
tag.doStartTag();
Param param = new Param();
@@ -164,9 +153,7 @@ public class UrlTagTests extends AbstractTagTests {
tag.addParam(param);
tag.doEndTag();
assertEquals("url/path?n%20me=v%26l%3De&amp;name=value2", context
.getAttribute("var"));
assertEquals("url/path?n%20me=v%26l%3De&amp;name=value2", context.getAttribute("var"));
}
@Test
@@ -174,7 +161,6 @@ public class UrlTagTests extends AbstractTagTests {
tag.setValue("url/path");
tag.setVar("var");
tag.setJavaScriptEscape(true);
tag.doStartTag();
Param param = new Param();
@@ -188,9 +174,7 @@ public class UrlTagTests extends AbstractTagTests {
tag.addParam(param);
tag.doEndTag();
assertEquals("url\\/path?n%20me=v%26l%3De&name=value2", context
.getAttribute("var"));
assertEquals("url\\/path?n%20me=v%26l%3De&name=value2", context.getAttribute("var"));
}
@Test
@@ -199,7 +183,6 @@ public class UrlTagTests extends AbstractTagTests {
tag.setVar("var");
tag.setHtmlEscape(true);
tag.setJavaScriptEscape(true);
tag.doStartTag();
Param param = new Param();
@@ -213,25 +196,22 @@ public class UrlTagTests extends AbstractTagTests {
tag.addParam(param);
tag.doEndTag();
assertEquals("url\\/path?n%20me=v%26l%3De&amp;name=value2", context
.getAttribute("var"));
assertEquals("url\\/path?n%20me=v%26l%3De&amp;name=value2", context.getAttribute("var"));
}
@Test
public void createQueryStringNoParams() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("", queryString);
}
@Test
public void createQueryStringOneParam() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -239,15 +219,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("?name=value", queryString);
}
@Test
public void createQueryStringOneParamForExsistingQueryString()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
public void createQueryStringOneParamForExsistingQueryString() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -255,14 +233,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, false);
assertEquals("&name=value", queryString);
}
@Test
public void createQueryStringOneParamEmptyValue() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -270,14 +247,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("?name=", queryString);
}
@Test
public void createQueryStringOneParamNullValue() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -285,31 +261,28 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("?name", queryString);
}
@Test
public void createQueryStringOneParamAlreadyUsed() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
param.setValue("value");
params.add(param);
usedParams.add("name");
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("", queryString);
}
@Test
public void createQueryStringTwoParams() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -322,14 +295,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("?name=value&name=value2", queryString);
}
@Test
public void createQueryStringUrlEncoding() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("n me");
@@ -342,14 +314,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("?n%20me=v%26l%3De&name=value2", queryString);
}
@Test
public void createQueryStringParamNullName() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName(null);
@@ -357,14 +328,13 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("", queryString);
}
@Test
public void createQueryStringParamEmptyName() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("");
@@ -372,78 +342,65 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String queryString = tag.createQueryString(params, usedParams, true);
assertEquals("", queryString);
}
@Test
public void replaceUriTemplateParamsNoParams() throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
String uri = tag.replaceUriTemplateParams("url/path", params,
usedParams);
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
String uri = tag.replaceUriTemplateParams("url/path", params, usedParams);
assertEquals("url/path", uri);
assertEquals(0, usedParams.size());
}
@Test
public void replaceUriTemplateParamsTemplateWithoutParamMatch()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
String uri = tag.replaceUriTemplateParams("url/{path}", params,
usedParams);
public void replaceUriTemplateParamsTemplateWithoutParamMatch() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
String uri = tag.replaceUriTemplateParams("url/{path}", params, usedParams);
assertEquals("url/{path}", uri);
assertEquals(0, usedParams.size());
}
@Test
public void replaceUriTemplateParamsTemplateWithParamMatch()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
public void replaceUriTemplateParamsTemplateWithParamMatch() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
param.setValue("value");
params.add(param);
String uri = tag.replaceUriTemplateParams("url/{name}", params,
usedParams);
String uri = tag.replaceUriTemplateParams("url/{name}", params, usedParams);
assertEquals("url/value", uri);
assertEquals(1, usedParams.size());
assertTrue(usedParams.contains("name"));
}
@Test
public void replaceUriTemplateParamsTemplateWithParamMatchNamePreEncoding()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
public void replaceUriTemplateParamsTemplateWithParamMatchNamePreEncoding() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("n me");
param.setValue("value");
params.add(param);
String uri = tag.replaceUriTemplateParams("url/{n me}", params,
usedParams);
String uri = tag.replaceUriTemplateParams("url/{n me}", params, usedParams);
assertEquals("url/value", uri);
assertEquals(1, usedParams.size());
assertTrue(usedParams.contains("n me"));
}
@Test
public void replaceUriTemplateParamsTemplateWithParamMatchValueEncoded()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
public void replaceUriTemplateParamsTemplateWithParamMatchValueEncoded() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -458,13 +415,10 @@ public class UrlTagTests extends AbstractTagTests {
assertTrue(usedParams.contains("name"));
}
// SPR-11401
@Test
public void replaceUriTemplateParamsTemplateWithPathSegment()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
@Test // SPR-11401
public void replaceUriTemplateParamsTemplateWithPathSegment() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -479,10 +433,9 @@ public class UrlTagTests extends AbstractTagTests {
}
@Test
public void replaceUriTemplateParamsTemplateWithPath()
throws JspException {
List<Param> params = new LinkedList<Param>();
Set<String> usedParams = new HashSet<String>();
public void replaceUriTemplateParamsTemplateWithPath() throws JspException {
List<Param> params = new LinkedList<>();
Set<String> usedParams = new HashSet<>();
Param param = new Param();
param.setName("name");
@@ -490,7 +443,6 @@ public class UrlTagTests extends AbstractTagTests {
params.add(param);
String uri = tag.replaceUriTemplateParams("url/{name}", params, usedParams);
assertEquals("url/my/Id", uri);
assertEquals(1, usedParams.size());
assertTrue(usedParams.contains("name"));
@@ -499,89 +451,71 @@ public class UrlTagTests extends AbstractTagTests {
@Test
public void createUrlRemoteServer() throws JspException {
tag.setValue("http://www.springframework.org/");
tag.doStartTag();
// String uri = tag.createUrl();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("http://www.springframework.org/", uri);
}
@Test
public void createUrlRelative() throws JspException {
tag.setValue("url/path");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("url/path", uri);
}
@Test
public void createUrlLocalContext() throws JspException {
((MockHttpServletRequest) context.getRequest())
.setContextPath("/app-context");
((MockHttpServletRequest) context.getRequest()).setContextPath("/app-context");
tag.setValue("/url/path");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("/app-context/url/path", uri);
}
@Test
public void createUrlRemoteContext() throws JspException {
((MockHttpServletRequest) context.getRequest())
.setContextPath("/app-context");
((MockHttpServletRequest) context.getRequest()).setContextPath("/app-context");
tag.setValue("/url/path");
tag.setContext("some-other-context");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("/some-other-context/url/path", uri);
}
@Test
public void createUrlRemoteContextWithSlash() throws JspException {
((MockHttpServletRequest) context.getRequest())
.setContextPath("/app-context");
((MockHttpServletRequest) context.getRequest()).setContextPath("/app-context");
tag.setValue("/url/path");
tag.setContext("/some-other-context");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("/some-other-context/url/path", uri);
}
@Test
public void createUrlRemoteContextSingleSlash() throws JspException {
((MockHttpServletRequest) context.getRequest())
.setContextPath("/app-context");
((MockHttpServletRequest) context.getRequest()).setContextPath("/app-context");
tag.setValue("/url/path");
tag.setContext("/");
tag.doStartTag();
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("/url/path", uri);
}
@Test
public void createUrlWithParams() throws JspException {
tag.setValue("url/path");
tag.doStartTag();
Param param = new Param();
@@ -594,15 +528,13 @@ public class UrlTagTests extends AbstractTagTests {
param.setValue("v lue");
tag.addParam(param);
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("url/path?name=value&n%20me=v%20lue", uri);
}
@Test
public void createUrlWithTemplateParams() throws JspException {
tag.setValue("url/{name}");
tag.doStartTag();
Param param = new Param();
@@ -615,16 +547,13 @@ public class UrlTagTests extends AbstractTagTests {
param.setValue("v lue");
tag.addParam(param);
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("url/value?n%20me=v%20lue", uri);
}
@Test
public void createUrlWithParamAndExsistingQueryString()
throws JspException {
public void createUrlWithParamAndExistingQueryString() throws JspException {
tag.setValue("url/path?foo=bar");
tag.doStartTag();
Param param = new Param();
@@ -632,29 +561,8 @@ public class UrlTagTests extends AbstractTagTests {
param.setValue("value");
tag.addParam(param);
String uri = invokeCreateUrl(tag);
String uri = tag.createUrl();
assertEquals("url/path?foo=bar&name=value", uri);
}
@Test
public void jspWriterOutput() {
// TODO assert that the output to the JspWriter is the expected output
}
@Test
public void servletRepsonseEncodeUrl() {
// TODO assert that HttpServletResponse.encodeURL(String) is invoked for
// non absolute urls
}
// support methods
private String invokeCreateUrl(UrlTag tag) {
Method createUrl = ReflectionUtils.findMethod(tag.getClass(),
"createUrl");
ReflectionUtils.makeAccessible(createUrl);
return (String) ReflectionUtils.invokeMethod(createUrl, tag);
}
}