Skip to content

Commit 88b9c05

Browse files
committed
Refine null-safety in spring-test
See spring-projectsgh-32475
1 parent c7c9da5 commit 88b9c05

21 files changed

+39
-10
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public void addInitParameter(String name, String value) {
9797
}
9898

9999
@Override
100+
@Nullable
100101
public String getInitParameter(String name) {
101102
Assert.notNull(name, "Parameter name must not be null");
102103
return this.initParameters.get(name);

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

+2
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ protected void checkActive() throws IllegalStateException {
383383
// ---------------------------------------------------------------------
384384

385385
@Override
386+
@Nullable
386387
public Object getAttribute(String name) {
387388
checkActive();
388389
return this.attributes.get(name);
@@ -637,6 +638,7 @@ public Enumeration<String> getParameterNames() {
637638
}
638639

639640
@Override
641+
@Nullable
640642
public String[] getParameterValues(String name) {
641643
Assert.notNull(name, "Parameter name must not be null");
642644
return this.parameters.get(name);

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

+1
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public int getMaxInactiveInterval() {
148148
}
149149

150150
@Override
151+
@Nullable
151152
public Object getAttribute(String name) {
152153
assertIsValid();
153154
Assert.notNull(name, "Attribute name must not be null");

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

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public Iterator<String> getFileNames() {
9696
}
9797

9898
@Override
99+
@Nullable
99100
public MultipartFile getFile(String name) {
100101
return this.multipartFiles.getFirst(name);
101102
}
@@ -117,6 +118,7 @@ public MultiValueMap<String, MultipartFile> getMultiFileMap() {
117118
}
118119

119120
@Override
121+
@Nullable
120122
public String getMultipartContentType(String paramOrFileName) {
121123
MultipartFile file = getFile(paramOrFileName);
122124
if (file != null) {
@@ -154,6 +156,7 @@ public HttpHeaders getRequestHeaders() {
154156
}
155157

156158
@Override
159+
@Nullable
157160
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
158161
MultipartFile file = getFile(paramOrFileName);
159162
if (file != null) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public void addInitParameter(String name, String value) {
9393
}
9494

9595
@Override
96+
@Nullable
9697
public String getInitParameter(String name) {
9798
Assert.notNull(name, "Parameter name must not be null");
9899
return this.initParameters.get(name);

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

+3
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ public void registerContext(String contextPath, ServletContext context) {
223223
}
224224

225225
@Override
226+
@Nullable
226227
public ServletContext getContext(String contextPath) {
227228
if (this.contextPath.equals(contextPath)) {
228229
return this;
@@ -375,6 +376,7 @@ public RequestDispatcher getRequestDispatcher(String path) {
375376
}
376377

377378
@Override
379+
@Nullable
378380
public RequestDispatcher getNamedDispatcher(String path) {
379381
return this.namedRequestDispatchers.get(path);
380382
}
@@ -464,6 +466,7 @@ public String getServerInfo() {
464466
}
465467

466468
@Override
469+
@Nullable
467470
public String getInitParameter(String name) {
468471
Assert.notNull(name, "Parameter name must not be null");
469472
return this.initParameters.get(name);

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

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public void setAttribute(String name, String value) {
136136
}
137137

138138
@Override
139+
@Nullable
139140
public String getAttribute(String name) {
140141
return this.attributes.get(name);
141142
}

spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java

+1
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ public Optional<MediaType> contentType() {
569569
}
570570

571571
@Override
572+
@Nullable
572573
public InetSocketAddress host() {
573574
return delegate().getHost();
574575
}

spring-test/src/main/java/org/springframework/test/context/ApplicationContextFailureProcessor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.test.context;
1818

1919
import org.springframework.context.ApplicationContext;
20+
import org.springframework.lang.Nullable;
2021

2122
/**
2223
* Strategy for components that process failures related to application contexts
@@ -41,6 +42,6 @@ public interface ApplicationContextFailureProcessor {
4142
* @param context the application context that did not load successfully
4243
* @param exception the exception thrown while loading the application context
4344
*/
44-
void processLoadFailure(ApplicationContext context, Throwable exception);
45+
void processLoadFailure(ApplicationContext context, @Nullable Throwable exception);
4546

4647
}

spring-test/src/main/java/org/springframework/test/context/SmartContextLoader.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.test.context;
1818

1919
import org.springframework.context.ApplicationContext;
20-
import org.springframework.lang.Nullable;
2120

2221
/**
2322
* Strategy interface for loading an {@link ApplicationContext} for an integration
@@ -157,7 +156,7 @@ public interface SmartContextLoader extends ContextLoader {
157156
*/
158157
@Override
159158
@SuppressWarnings("deprecation")
160-
default String[] processLocations(Class<?> clazz, @Nullable String... locations) {
159+
default String[] processLocations(Class<?> clazz, String... locations) {
161160
throw new UnsupportedOperationException("""
162161
SmartContextLoader does not support the ContextLoader SPI. \
163162
Call processContextConfiguration(ContextConfigurationAttributes) instead.""");

spring-test/src/main/java/org/springframework/test/context/hint/TestContextRuntimeHints.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2727
import org.springframework.aot.hint.TypeHint;
2828
import org.springframework.aot.hint.TypeReference;
29+
import org.springframework.lang.Nullable;
2930
import org.springframework.util.ClassUtils;
3031

3132
/**
@@ -39,7 +40,7 @@
3940
class TestContextRuntimeHints implements RuntimeHintsRegistrar {
4041

4142
@Override
42-
public void registerHints(RuntimeHints runtimeHints, ClassLoader classLoader) {
43+
public void registerHints(RuntimeHints runtimeHints, @Nullable ClassLoader classLoader) {
4344
boolean servletPresent = ClassUtils.isPresent("jakarta.servlet.Servlet", classLoader);
4445
boolean groovyPresent = ClassUtils.isPresent("groovy.lang.Closure", classLoader);
4546

spring-test/src/main/java/org/springframework/test/context/support/DynamicValuesPropertySource.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.function.Supplier;
2121

2222
import org.springframework.core.env.MapPropertySource;
23+
import org.springframework.lang.Nullable;
2324
import org.springframework.util.function.SupplierUtils;
2425

2526
/**
@@ -38,6 +39,7 @@ class DynamicValuesPropertySource extends MapPropertySource {
3839
}
3940

4041
@Override
42+
@Nullable
4143
public Object getProperty(String name) {
4244
return SupplierUtils.resolve(super.getProperty(name));
4345
}

spring-test/src/main/java/org/springframework/test/context/support/TestPropertySourceUtils.java

+1
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ private static class SequencedProperties extends Properties {
459459
private final LinkedHashMap<String, Object> map = new LinkedHashMap<>();
460460

461461
@Override
462+
@Nullable
462463
public Object put(Object key, Object value) {
463464
if (key instanceof String str) {
464465
return this.map.put(str, value);

spring-test/src/main/java/org/springframework/test/context/testng/AbstractTestNGSpringContextTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ protected void springTestContextAfterTestClass() throws Exception {
198198
}
199199

200200

201+
@Nullable
201202
private Throwable getTestResultException(ITestResult testResult) {
202203
Throwable testResultException = testResult.getThrowable();
203204
if (testResultException instanceof InvocationTargetException) {

spring-test/src/main/java/org/springframework/test/context/transaction/TransactionalTestExecutionListener.java

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
155155
protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource(false) {
156156

157157
@Override
158+
@Nullable
158159
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
159160
// @Transactional present in inheritance hierarchy?
160161
TransactionAttribute result = super.findTransactionAttribute(clazz);

spring-test/src/main/java/org/springframework/test/web/client/response/ExecutingResponseCreator.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.http.client.ClientHttpRequest;
2222
import org.springframework.http.client.ClientHttpRequestFactory;
2323
import org.springframework.http.client.ClientHttpResponse;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.mock.http.client.MockClientHttpRequest;
2526
import org.springframework.test.web.client.ResponseCreator;
2627
import org.springframework.util.Assert;
@@ -59,7 +60,7 @@ public ExecutingResponseCreator(ClientHttpRequestFactory requestFactory) {
5960

6061

6162
@Override
62-
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
63+
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
6364
Assert.state(request instanceof MockClientHttpRequest, "Expected a MockClientHttpRequest");
6465
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
6566
ClientHttpRequest newRequest = this.requestFactory.createRequest(mockRequest.getURI(), mockRequest.getMethod());

spring-test/src/main/java/org/springframework/test/web/servlet/DefaultMvcResult.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public FlashMap getFlashMap() {
127127
return RequestContextUtils.getOutputFlashMap(this.mockRequest);
128128
}
129129

130-
public void setAsyncResult(Object asyncResult) {
130+
public void setAsyncResult(@Nullable Object asyncResult) {
131131
this.asyncResult.set(asyncResult);
132132
}
133133

spring-test/src/main/java/org/springframework/test/web/servlet/TestDispatcherServlet.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ private void registerAsyncResultInterceptors(HttpServletRequest request) {
9898
WebAsyncUtils.getAsyncManager(request).registerCallableInterceptor(KEY,
9999
new CallableProcessingInterceptor() {
100100
@Override
101-
public <T> void postProcess(NativeWebRequest r, Callable<T> task, Object value) {
101+
public <T> void postProcess(NativeWebRequest r, Callable<T> task, @Nullable Object value) {
102102
// We got the result, must also wait for the dispatch
103103
getMvcResult(request).setAsyncResult(value);
104104
}
@@ -107,7 +107,7 @@ public <T> void postProcess(NativeWebRequest r, Callable<T> task, Object value)
107107
WebAsyncUtils.getAsyncManager(request).registerDeferredResultInterceptor(KEY,
108108
new DeferredResultProcessingInterceptor() {
109109
@Override
110-
public <T> void postProcess(NativeWebRequest r, DeferredResult<T> result, Object value) {
110+
public <T> void postProcess(NativeWebRequest r, DeferredResult<T> result, @Nullable Object value) {
111111
getMvcResult(request).setAsyncResult(value);
112112
}
113113
});
@@ -118,6 +118,7 @@ protected DefaultMvcResult getMvcResult(ServletRequest request) {
118118
}
119119

120120
@Override
121+
@Nullable
121122
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
122123
HandlerExecutionChain chain = super.getHandler(request);
123124
if (chain != null) {
@@ -138,6 +139,7 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
138139
}
139140

140141
@Override
142+
@Nullable
141143
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
142144
@Nullable Object handler, Exception ex) throws Exception {
143145

spring-test/src/main/java/org/springframework/test/web/servlet/htmlunit/HtmlUnitRequestBuilder.java

+1
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ public HtmlUnitMockHttpServletRequest(ServletContext servletContext, String meth
451451
}
452452

453453
@Override
454+
@Nullable
454455
public HttpSession getSession(boolean create) {
455456
HttpSession session = super.getSession(false);
456457
if (session == null && create) {

spring-test/src/main/java/org/springframework/test/web/servlet/setup/StubWebApplicationContext.java

+5
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ public long getStartupDate() {
130130
}
131131

132132
@Override
133+
@Nullable
133134
public ApplicationContext getParent() {
134135
return null;
135136
}
@@ -218,11 +219,13 @@ public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanD
218219
}
219220

220221
@Override
222+
@Nullable
221223
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
222224
return this.beanFactory.getType(name);
223225
}
224226

225227
@Override
228+
@Nullable
226229
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
227230
return this.beanFactory.getType(name, allowFactoryBeanInit);
228231
}
@@ -337,6 +340,7 @@ public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
337340
//---------------------------------------------------------------------
338341

339342
@Override
343+
@Nullable
340344
public BeanFactory getParentBeanFactory() {
341345
return null;
342346
}
@@ -352,6 +356,7 @@ public boolean containsLocalBean(String name) {
352356
//---------------------------------------------------------------------
353357

354358
@Override
359+
@Nullable
355360
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
356361
return this.messageSource.getMessage(code, args, defaultMessage, locale);
357362
}

spring-test/src/test/java/org/springframework/test/context/failures/TrackingApplicationContextFailureProcessor.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222

2323
import org.springframework.context.ApplicationContext;
24+
import org.springframework.lang.Nullable;
2425
import org.springframework.test.context.ApplicationContextFailureProcessor;
2526

2627
/**
@@ -37,10 +38,10 @@ public class TrackingApplicationContextFailureProcessor implements ApplicationCo
3738

3839

3940
@Override
40-
public void processLoadFailure(ApplicationContext context, Throwable exception) {
41+
public void processLoadFailure(ApplicationContext context, @Nullable Throwable exception) {
4142
loadFailures.add(new LoadFailure(context, exception));
4243
}
4344

44-
public record LoadFailure(ApplicationContext context, Throwable exception) {}
45+
public record LoadFailure(ApplicationContext context, @Nullable Throwable exception) {}
4546

4647
}

0 commit comments

Comments
 (0)