From e5c1deea637ca8b2ab4352857615e53b4cb07e87 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 5 Nov 2017 16:23:30 +0100 Subject: [PATCH] Polishing --- .../mock/web/MockMultipartFile.java | 1 + .../web/MockMultipartHttpServletRequest.java | 1 - .../reactive/ServletHttpHandlerAdapter.java | 38 +++++++++---------- .../mock/web/test/HeaderValueHolder.java | 4 +- .../web/test/MockExpressionEvaluator.java | 21 +++------- .../mock/web/test/MockFilterChain.java | 4 +- .../mock/web/test/MockMultipartFile.java | 2 +- .../mock/web/test/MockPageContext.java | 2 +- .../mock/web/test/MockServletContext.java | 5 ++- .../mock/web/test/PassThroughFilterChain.java | 5 ++- 10 files changed, 36 insertions(+), 47 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartFile.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartFile.java index 1e2fdb5e2a..66e3171224 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartFile.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartFile.java @@ -101,6 +101,7 @@ public class MockMultipartFile implements MultipartFile { this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); } + @Override public String getName() { return this.name; diff --git a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java index 49f9c1246c..572bf7a721 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/MockMultipartHttpServletRequest.java @@ -21,7 +21,6 @@ import java.util.Enumeration; import java.util.Iterator; import java.util.List; import java.util.Map; - import javax.servlet.ServletContext; import org.springframework.http.HttpHeaders; diff --git a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java index bac10b0ef4..ac209f7051 100644 --- a/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java +++ b/spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java @@ -121,11 +121,12 @@ public class ServletHttpHandlerAdapter implements Servlet { this.servletPath = getServletPath(config); } - @Nullable private String getServletPath(ServletConfig config) { String name = config.getServletName(); ServletRegistration registration = config.getServletContext().getServletRegistration(name); - Assert.notNull(registration, "ServletRegistration not found for Servlet '" + name + "'."); + if (registration == null) { + throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'"); + } Collection mappings = registration.getMappings(); if (mappings.size() == 1) { @@ -136,16 +137,16 @@ public class ServletHttpHandlerAdapter implements Servlet { if (mapping.endsWith("/*")) { String path = mapping.substring(0, mapping.length() - 2); if (!path.isEmpty()) { - logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'."); + logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'"); } return path; } } - throw new IllegalArgumentException("Expected a single Servlet mapping -- " + + throw new IllegalArgumentException("Expected a single Servlet mapping: " + "either the default Servlet mapping (i.e. '/'), " + "or a path based mapping (e.g. '/*', '/foo/*'). " + - "Actual mappings: " + mappings + " for Servlet '" + name + "'."); + "Actual mappings: " + mappings + " for Servlet '" + name + "'"); } @@ -168,18 +169,13 @@ public class ServletHttpHandlerAdapter implements Servlet { this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber); } - protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) - throws IOException { - - Assert.notNull(this.servletPath, "servletPath is not initialized."); - + protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException { + Assert.notNull(this.servletPath, "Servlet path is not initialized"); return new ServletServerHttpRequest( request, context, this.servletPath, getDataBufferFactory(), getBufferSize()); } - protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) - throws IOException { - + protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) throws IOException { return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize()); } @@ -219,25 +215,25 @@ public class ServletHttpHandlerAdapter implements Servlet { private final static AsyncListener ERROR_LISTENER = new AsyncListener() { @Override - public void onTimeout(AsyncEvent event) throws IOException { + public void onTimeout(AsyncEvent event) { AsyncContext context = event.getAsyncContext(); runIfAsyncNotComplete(context, context::complete); } @Override - public void onError(AsyncEvent event) throws IOException { + public void onError(AsyncEvent event) { AsyncContext context = event.getAsyncContext(); runIfAsyncNotComplete(context, context::complete); } @Override - public void onStartAsync(AsyncEvent event) throws IOException { - // No-op + public void onStartAsync(AsyncEvent event) { + // no-op } @Override - public void onComplete(AsyncEvent event) throws IOException { - // No-op + public void onComplete(AsyncEvent event) { + // no-op } }; @@ -246,7 +242,7 @@ public class ServletHttpHandlerAdapter implements Servlet { private final AsyncContext asyncContext; - HandlerResultSubscriber(AsyncContext asyncContext) { + public HandlerResultSubscriber(AsyncContext asyncContext) { this.asyncContext = asyncContext; } @@ -257,7 +253,7 @@ public class ServletHttpHandlerAdapter implements Servlet { @Override public void onNext(Void aVoid) { - // no op + // no-op } @Override diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/HeaderValueHolder.java b/spring-web/src/test/java/org/springframework/mock/web/test/HeaderValueHolder.java index 1ab85f56f1..6588a633e5 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/HeaderValueHolder.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/HeaderValueHolder.java @@ -40,7 +40,9 @@ class HeaderValueHolder { public void setValue(Object value) { this.values.clear(); - this.values.add(value); + if (value != null) { + this.values.add(value); + } } public void addValue(Object value) { diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java index 5fdbb3deaa..69e8ed4fd0 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java @@ -21,8 +21,6 @@ import javax.servlet.jsp.PageContext; import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager; -import org.springframework.util.Assert; - /** * Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator} * interface, delegating to the Apache JSTL ExpressionEvaluatorManager. @@ -57,9 +55,7 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval return new javax.servlet.jsp.el.Expression() { @Override - public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) - throws javax.servlet.jsp.el.ELException { - + public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException { return doEvaluate(expression, expectedType, functionMapper); } }; @@ -67,26 +63,21 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval @Override @SuppressWarnings("rawtypes") - public Object evaluate(String expression, Class expectedType, - javax.servlet.jsp.el.VariableResolver variableResolver, + public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver, javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException { - Assert.isNull(variableResolver, "Custom VariableResolver not supported"); return doEvaluate(expression, expectedType, functionMapper); } @SuppressWarnings("rawtypes") - protected Object doEvaluate(String expression, Class expectedType, - javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException { + protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper) + throws javax.servlet.jsp.el.ELException { - Assert.isNull(functionMapper, "Custom FunctionMapper not supported"); try { - return ExpressionEvaluatorManager.evaluate( - "JSP EL expression", expression, expectedType, this.pageContext); + return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext); } catch (JspException ex) { - throw new javax.servlet.jsp.el.ELException( - "Parsing of JSP EL expression \"" + expression + "\" failed", ex); + throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex); } } diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterChain.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterChain.java index da4a909310..7136d2160d 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterChain.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockFilterChain.java @@ -43,7 +43,6 @@ import org.springframework.util.ObjectUtils; * @author Juergen Hoeller * @author Rob Winch * @author Rossen Stoyanchev - * * @since 2.0.3 * @see MockFilterConfig * @see PassThroughFilterChain @@ -70,7 +69,6 @@ public class MockFilterChain implements FilterChain { /** * Create a FilterChain with a Servlet. - * * @param servlet the Servlet to invoke * @since 3.2 */ @@ -80,7 +78,6 @@ public class MockFilterChain implements FilterChain { /** * Create a {@code FilterChain} with Filter's and a Servlet. - * * @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 @@ -96,6 +93,7 @@ public class MockFilterChain implements FilterChain { return Arrays.asList(allFilters); } + /** * Return the request that {@link #doFilter} has been called with. */ diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartFile.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartFile.java index 65ff4da656..29193640f5 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartFile.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartFile.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java index fbb541a98b..16cc0f3413 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java @@ -267,7 +267,7 @@ public class MockPageContext extends PageContext { return this.request.getAttributeNames(); case SESSION_SCOPE: HttpSession session = this.request.getSession(false); - return (session != null ? session.getAttributeNames() : null); + return (session != null ? session.getAttributeNames() : Collections.emptyEnumeration()); case APPLICATION_SCOPE: return this.servletContext.getAttributeNames(); default: diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java b/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java index 2a7500551b..f6625a61b7 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java @@ -141,6 +141,7 @@ public class MockServletContext implements ServletContext { private final Map mimeTypes = new LinkedHashMap<>(); + /** * Create a new {@code MockServletContext}, using no base path and a * {@link DefaultResourceLoader} (i.e. the classpath root as WAR root). @@ -179,7 +180,7 @@ public class MockServletContext implements ServletContext { */ public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) { this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader()); - this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : ""); + this.resourceBasePath = resourceBasePath; // Use JVM temp dir as ServletContext temp dir. String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY); @@ -204,7 +205,7 @@ public class MockServletContext implements ServletContext { } public void setContextPath(String contextPath) { - this.contextPath = (contextPath != null ? contextPath : ""); + this.contextPath = contextPath; } @Override diff --git a/spring-web/src/test/java/org/springframework/mock/web/test/PassThroughFilterChain.java b/spring-web/src/test/java/org/springframework/mock/web/test/PassThroughFilterChain.java index 505aa3f6bc..3fb2b44a93 100644 --- a/spring-web/src/test/java/org/springframework/mock/web/test/PassThroughFilterChain.java +++ b/spring-web/src/test/java/org/springframework/mock/web/test/PassThroughFilterChain.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2017 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. @@ -28,7 +28,7 @@ import org.springframework.util.Assert; /** * Implementation of the {@link javax.servlet.FilterConfig} interface which - * simply passes the call through to a given Filter/FilterChain combo + * simply passes the call through to a given Filter/FilterChain combination * (indicating the next Filter in the chain along with the FilterChain that it is * supposed to work on) or to a given Servlet (indicating the end of the chain). * @@ -79,6 +79,7 @@ public class PassThroughFilterChain implements FilterChain { this.filter.doFilter(request, response, this.nextFilterChain); } else { + Assert.state(this.servlet != null, "Neither a Filter not a Servlet set"); this.servlet.service(request, response); } }