Skip to content

Commit e5c1dee

Browse files
committed
Polishing
1 parent 80a0cf7 commit e5c1dee

File tree

10 files changed

+36
-47
lines changed

10 files changed

+36
-47
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public MockMultipartFile(
101101
this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream));
102102
}
103103

104+
104105
@Override
105106
public String getName() {
106107
return this.name;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.Iterator;
2222
import java.util.List;
2323
import java.util.Map;
24-
2524
import javax.servlet.ServletContext;
2625

2726
import org.springframework.http.HttpHeaders;

spring-web/src/main/java/org/springframework/http/server/reactive/ServletHttpHandlerAdapter.java

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ public void init(ServletConfig config) {
121121
this.servletPath = getServletPath(config);
122122
}
123123

124-
@Nullable
125124
private String getServletPath(ServletConfig config) {
126125
String name = config.getServletName();
127126
ServletRegistration registration = config.getServletContext().getServletRegistration(name);
128-
Assert.notNull(registration, "ServletRegistration not found for Servlet '" + name + "'.");
127+
if (registration == null) {
128+
throw new IllegalStateException("ServletRegistration not found for Servlet '" + name + "'");
129+
}
129130

130131
Collection<String> mappings = registration.getMappings();
131132
if (mappings.size() == 1) {
@@ -136,16 +137,16 @@ private String getServletPath(ServletConfig config) {
136137
if (mapping.endsWith("/*")) {
137138
String path = mapping.substring(0, mapping.length() - 2);
138139
if (!path.isEmpty()) {
139-
logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'.");
140+
logger.info("Found Servlet mapping '" + path + "' for Servlet '" + name + "'");
140141
}
141142
return path;
142143
}
143144
}
144145

145-
throw new IllegalArgumentException("Expected a single Servlet mapping -- " +
146+
throw new IllegalArgumentException("Expected a single Servlet mapping: " +
146147
"either the default Servlet mapping (i.e. '/'), " +
147148
"or a path based mapping (e.g. '/*', '/foo/*'). " +
148-
"Actual mappings: " + mappings + " for Servlet '" + name + "'.");
149+
"Actual mappings: " + mappings + " for Servlet '" + name + "'");
149150
}
150151

151152

@@ -168,18 +169,13 @@ public void service(ServletRequest request, ServletResponse response) throws IOE
168169
this.httpHandler.handle(httpRequest, httpResponse).subscribe(subscriber);
169170
}
170171

171-
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context)
172-
throws IOException {
173-
174-
Assert.notNull(this.servletPath, "servletPath is not initialized.");
175-
172+
protected ServerHttpRequest createRequest(HttpServletRequest request, AsyncContext context) throws IOException {
173+
Assert.notNull(this.servletPath, "Servlet path is not initialized");
176174
return new ServletServerHttpRequest(
177175
request, context, this.servletPath, getDataBufferFactory(), getBufferSize());
178176
}
179177

180-
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context)
181-
throws IOException {
182-
178+
protected ServerHttpResponse createResponse(HttpServletResponse response, AsyncContext context) throws IOException {
183179
return new ServletServerHttpResponse(response, context, getDataBufferFactory(), getBufferSize());
184180
}
185181

@@ -219,25 +215,25 @@ private static void runIfAsyncNotComplete(AsyncContext asyncContext, Runnable ta
219215
private final static AsyncListener ERROR_LISTENER = new AsyncListener() {
220216

221217
@Override
222-
public void onTimeout(AsyncEvent event) throws IOException {
218+
public void onTimeout(AsyncEvent event) {
223219
AsyncContext context = event.getAsyncContext();
224220
runIfAsyncNotComplete(context, context::complete);
225221
}
226222

227223
@Override
228-
public void onError(AsyncEvent event) throws IOException {
224+
public void onError(AsyncEvent event) {
229225
AsyncContext context = event.getAsyncContext();
230226
runIfAsyncNotComplete(context, context::complete);
231227
}
232228

233229
@Override
234-
public void onStartAsync(AsyncEvent event) throws IOException {
235-
// No-op
230+
public void onStartAsync(AsyncEvent event) {
231+
// no-op
236232
}
237233

238234
@Override
239-
public void onComplete(AsyncEvent event) throws IOException {
240-
// No-op
235+
public void onComplete(AsyncEvent event) {
236+
// no-op
241237
}
242238
};
243239

@@ -246,7 +242,7 @@ private class HandlerResultSubscriber implements Subscriber<Void> {
246242

247243
private final AsyncContext asyncContext;
248244

249-
HandlerResultSubscriber(AsyncContext asyncContext) {
245+
public HandlerResultSubscriber(AsyncContext asyncContext) {
250246
this.asyncContext = asyncContext;
251247
}
252248

@@ -257,7 +253,7 @@ public void onSubscribe(Subscription subscription) {
257253

258254
@Override
259255
public void onNext(Void aVoid) {
260-
// no op
256+
// no-op
261257
}
262258

263259
@Override

spring-web/src/test/java/org/springframework/mock/web/test/HeaderValueHolder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ class HeaderValueHolder {
4040

4141
public void setValue(Object value) {
4242
this.values.clear();
43-
this.values.add(value);
43+
if (value != null) {
44+
this.values.add(value);
45+
}
4446
}
4547

4648
public void addValue(Object value) {

spring-web/src/test/java/org/springframework/mock/web/test/MockExpressionEvaluator.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
2323

24-
import org.springframework.util.Assert;
25-
2624
/**
2725
* Mock implementation of the JSP 2.0 {@link javax.servlet.jsp.el.ExpressionEvaluator}
2826
* interface, delegating to the Apache JSTL ExpressionEvaluatorManager.
@@ -57,36 +55,29 @@ public javax.servlet.jsp.el.Expression parseExpression(final String expression,
5755

5856
return new javax.servlet.jsp.el.Expression() {
5957
@Override
60-
public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver)
61-
throws javax.servlet.jsp.el.ELException {
62-
58+
public Object evaluate(javax.servlet.jsp.el.VariableResolver variableResolver) throws javax.servlet.jsp.el.ELException {
6359
return doEvaluate(expression, expectedType, functionMapper);
6460
}
6561
};
6662
}
6763

6864
@Override
6965
@SuppressWarnings("rawtypes")
70-
public Object evaluate(String expression, Class expectedType,
71-
javax.servlet.jsp.el.VariableResolver variableResolver,
66+
public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver,
7267
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
7368

74-
Assert.isNull(variableResolver, "Custom VariableResolver not supported");
7569
return doEvaluate(expression, expectedType, functionMapper);
7670
}
7771

7872
@SuppressWarnings("rawtypes")
79-
protected Object doEvaluate(String expression, Class expectedType,
80-
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
73+
protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper)
74+
throws javax.servlet.jsp.el.ELException {
8175

82-
Assert.isNull(functionMapper, "Custom FunctionMapper not supported");
8376
try {
84-
return ExpressionEvaluatorManager.evaluate(
85-
"JSP EL expression", expression, expectedType, this.pageContext);
77+
return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
8678
}
8779
catch (JspException ex) {
88-
throw new javax.servlet.jsp.el.ELException(
89-
"Parsing of JSP EL expression \"" + expression + "\" failed", ex);
80+
throw new javax.servlet.jsp.el.ELException("Parsing of JSP EL expression \"" + expression + "\" failed", ex);
9081
}
9182
}
9283

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
* @author Juergen Hoeller
4444
* @author Rob Winch
4545
* @author Rossen Stoyanchev
46-
*
4746
* @since 2.0.3
4847
* @see MockFilterConfig
4948
* @see PassThroughFilterChain
@@ -70,7 +69,6 @@ public MockFilterChain() {
7069

7170
/**
7271
* Create a FilterChain with a Servlet.
73-
*
7472
* @param servlet the Servlet to invoke
7573
* @since 3.2
7674
*/
@@ -80,7 +78,6 @@ public MockFilterChain(Servlet servlet) {
8078

8179
/**
8280
* Create a {@code FilterChain} with Filter's and a Servlet.
83-
*
8481
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
8582
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
8683
* @since 3.2
@@ -96,6 +93,7 @@ private static List<Filter> initFilterList(Servlet servlet, Filter... filters) {
9693
return Arrays.asList(allFilters);
9794
}
9895

96+
9997
/**
10098
* Return the request that {@link #doFilter} has been called with.
10199
*/

spring-web/src/test/java/org/springframework/mock/web/test/MockMultipartFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-web/src/test/java/org/springframework/mock/web/test/MockPageContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public Enumeration<String> getAttributeNamesInScope(int scope) {
267267
return this.request.getAttributeNames();
268268
case SESSION_SCOPE:
269269
HttpSession session = this.request.getSession(false);
270-
return (session != null ? session.getAttributeNames() : null);
270+
return (session != null ? session.getAttributeNames() : Collections.emptyEnumeration());
271271
case APPLICATION_SCOPE:
272272
return this.servletContext.getAttributeNames();
273273
default:

spring-web/src/test/java/org/springframework/mock/web/test/MockServletContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public class MockServletContext implements ServletContext {
141141

142142
private final Map<String, MediaType> mimeTypes = new LinkedHashMap<>();
143143

144+
144145
/**
145146
* Create a new {@code MockServletContext}, using no base path and a
146147
* {@link DefaultResourceLoader} (i.e. the classpath root as WAR root).
@@ -179,7 +180,7 @@ public MockServletContext(ResourceLoader resourceLoader) {
179180
*/
180181
public MockServletContext(String resourceBasePath, ResourceLoader resourceLoader) {
181182
this.resourceLoader = (resourceLoader != null ? resourceLoader : new DefaultResourceLoader());
182-
this.resourceBasePath = (resourceBasePath != null ? resourceBasePath : "");
183+
this.resourceBasePath = resourceBasePath;
183184

184185
// Use JVM temp dir as ServletContext temp dir.
185186
String tempDir = System.getProperty(TEMP_DIR_SYSTEM_PROPERTY);
@@ -204,7 +205,7 @@ protected String getResourceLocation(String path) {
204205
}
205206

206207
public void setContextPath(String contextPath) {
207-
this.contextPath = (contextPath != null ? contextPath : "");
208+
this.contextPath = contextPath;
208209
}
209210

210211
@Override

spring-web/src/test/java/org/springframework/mock/web/test/PassThroughFilterChain.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,7 +28,7 @@
2828

2929
/**
3030
* Implementation of the {@link javax.servlet.FilterConfig} interface which
31-
* simply passes the call through to a given Filter/FilterChain combo
31+
* simply passes the call through to a given Filter/FilterChain combination
3232
* (indicating the next Filter in the chain along with the FilterChain that it is
3333
* supposed to work on) or to a given Servlet (indicating the end of the chain).
3434
*
@@ -79,6 +79,7 @@ public void doFilter(ServletRequest request, ServletResponse response) throws Se
7979
this.filter.doFilter(request, response, this.nextFilterChain);
8080
}
8181
else {
82+
Assert.state(this.servlet != null, "Neither a Filter not a Servlet set");
8283
this.servlet.service(request, response);
8384
}
8485
}

0 commit comments

Comments
 (0)