Skip to content

Commit

Permalink
Refine null-safety in spring-test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Mar 26, 2024
1 parent c7c9da5 commit 88b9c05
Show file tree
Hide file tree
Showing 21 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public void addInitParameter(String name, String value) {
}

@Override
@Nullable
public String getInitParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.initParameters.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ protected void checkActive() throws IllegalStateException {
// ---------------------------------------------------------------------

@Override
@Nullable
public Object getAttribute(String name) {
checkActive();
return this.attributes.get(name);
Expand Down Expand Up @@ -637,6 +638,7 @@ public Enumeration<String> getParameterNames() {
}

@Override
@Nullable
public String[] getParameterValues(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.parameters.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public int getMaxInactiveInterval() {
}

@Override
@Nullable
public Object getAttribute(String name) {
assertIsValid();
Assert.notNull(name, "Attribute name must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public Iterator<String> getFileNames() {
}

@Override
@Nullable
public MultipartFile getFile(String name) {
return this.multipartFiles.getFirst(name);
}
Expand All @@ -117,6 +118,7 @@ public MultiValueMap<String, MultipartFile> getMultiFileMap() {
}

@Override
@Nullable
public String getMultipartContentType(String paramOrFileName) {
MultipartFile file = getFile(paramOrFileName);
if (file != null) {
Expand Down Expand Up @@ -154,6 +156,7 @@ public HttpHeaders getRequestHeaders() {
}

@Override
@Nullable
public HttpHeaders getMultipartHeaders(String paramOrFileName) {
MultipartFile file = getFile(paramOrFileName);
if (file != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void addInitParameter(String name, String value) {
}

@Override
@Nullable
public String getInitParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.initParameters.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ public void registerContext(String contextPath, ServletContext context) {
}

@Override
@Nullable
public ServletContext getContext(String contextPath) {
if (this.contextPath.equals(contextPath)) {
return this;
Expand Down Expand Up @@ -375,6 +376,7 @@ public RequestDispatcher getRequestDispatcher(String path) {
}

@Override
@Nullable
public RequestDispatcher getNamedDispatcher(String path) {
return this.namedRequestDispatchers.get(path);
}
Expand Down Expand Up @@ -464,6 +466,7 @@ public String getServerInfo() {
}

@Override
@Nullable
public String getInitParameter(String name) {
Assert.notNull(name, "Parameter name must not be null");
return this.initParameters.get(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public void setAttribute(String name, String value) {
}

@Override
@Nullable
public String getAttribute(String name) {
return this.attributes.get(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,7 @@ public Optional<MediaType> contentType() {
}

@Override
@Nullable
public InetSocketAddress host() {
return delegate().getHost();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.test.context;

import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.springframework.test.context;

import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;

/**
* Strategy interface for loading an {@link ApplicationContext} for an integration
Expand Down Expand Up @@ -157,7 +156,7 @@ public interface SmartContextLoader extends ContextLoader {
*/
@Override
@SuppressWarnings("deprecation")
default String[] processLocations(Class<?> clazz, @Nullable String... locations) {
default String[] processLocations(Class<?> clazz, String... locations) {
throw new UnsupportedOperationException("""
SmartContextLoader does not support the ContextLoader SPI. \
Call processContextConfiguration(ContextConfigurationAttributes) instead.""");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.aot.hint.TypeHint;
import org.springframework.aot.hint.TypeReference;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;

/**
Expand All @@ -39,7 +40,7 @@
class TestContextRuntimeHints implements RuntimeHintsRegistrar {

@Override
public void registerHints(RuntimeHints runtimeHints, ClassLoader classLoader) {
public void registerHints(RuntimeHints runtimeHints, @Nullable ClassLoader classLoader) {
boolean servletPresent = ClassUtils.isPresent("jakarta.servlet.Servlet", classLoader);
boolean groovyPresent = ClassUtils.isPresent("groovy.lang.Closure", classLoader);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Supplier;

import org.springframework.core.env.MapPropertySource;
import org.springframework.lang.Nullable;
import org.springframework.util.function.SupplierUtils;

/**
Expand All @@ -38,6 +39,7 @@ class DynamicValuesPropertySource extends MapPropertySource {
}

@Override
@Nullable
public Object getProperty(String name) {
return SupplierUtils.resolve(super.getProperty(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ private static class SequencedProperties extends Properties {
private final LinkedHashMap<String, Object> map = new LinkedHashMap<>();

@Override
@Nullable
public Object put(Object key, Object value) {
if (key instanceof String str) {
return this.map.put(str, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ protected void springTestContextAfterTestClass() throws Exception {
}


@Nullable
private Throwable getTestResultException(ITestResult testResult) {
Throwable testResultException = testResult.getThrowable();
if (testResultException instanceof InvocationTargetException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
protected final TransactionAttributeSource attributeSource = new AnnotationTransactionAttributeSource(false) {

@Override
@Nullable
protected TransactionAttribute findTransactionAttribute(Class<?> clazz) {
// @Transactional present in inheritance hierarchy?
TransactionAttribute result = super.findTransactionAttribute(clazz);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.test.web.client.ResponseCreator;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -59,7 +60,7 @@ public ExecutingResponseCreator(ClientHttpRequestFactory requestFactory) {


@Override
public ClientHttpResponse createResponse(ClientHttpRequest request) throws IOException {
public ClientHttpResponse createResponse(@Nullable ClientHttpRequest request) throws IOException {
Assert.state(request instanceof MockClientHttpRequest, "Expected a MockClientHttpRequest");
MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
ClientHttpRequest newRequest = this.requestFactory.createRequest(mockRequest.getURI(), mockRequest.getMethod());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public FlashMap getFlashMap() {
return RequestContextUtils.getOutputFlashMap(this.mockRequest);
}

public void setAsyncResult(Object asyncResult) {
public void setAsyncResult(@Nullable Object asyncResult) {
this.asyncResult.set(asyncResult);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void registerAsyncResultInterceptors(HttpServletRequest request) {
WebAsyncUtils.getAsyncManager(request).registerCallableInterceptor(KEY,
new CallableProcessingInterceptor() {
@Override
public <T> void postProcess(NativeWebRequest r, Callable<T> task, Object value) {
public <T> void postProcess(NativeWebRequest r, Callable<T> task, @Nullable Object value) {
// We got the result, must also wait for the dispatch
getMvcResult(request).setAsyncResult(value);
}
Expand All @@ -107,7 +107,7 @@ public <T> void postProcess(NativeWebRequest r, Callable<T> task, Object value)
WebAsyncUtils.getAsyncManager(request).registerDeferredResultInterceptor(KEY,
new DeferredResultProcessingInterceptor() {
@Override
public <T> void postProcess(NativeWebRequest r, DeferredResult<T> result, Object value) {
public <T> void postProcess(NativeWebRequest r, DeferredResult<T> result, @Nullable Object value) {
getMvcResult(request).setAsyncResult(value);
}
});
Expand All @@ -118,6 +118,7 @@ protected DefaultMvcResult getMvcResult(ServletRequest request) {
}

@Override
@Nullable
protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
HandlerExecutionChain chain = super.getHandler(request);
if (chain != null) {
Expand All @@ -138,6 +139,7 @@ protected void render(ModelAndView mv, HttpServletRequest request, HttpServletRe
}

@Override
@Nullable
protected ModelAndView processHandlerException(HttpServletRequest request, HttpServletResponse response,
@Nullable Object handler, Exception ex) throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ public HtmlUnitMockHttpServletRequest(ServletContext servletContext, String meth
}

@Override
@Nullable
public HttpSession getSession(boolean create) {
HttpSession session = super.getSession(false);
if (session == null && create) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public long getStartupDate() {
}

@Override
@Nullable
public ApplicationContext getParent() {
return null;
}
Expand Down Expand Up @@ -218,11 +219,13 @@ public boolean isTypeMatch(String name, Class<?> typeToMatch) throws NoSuchBeanD
}

@Override
@Nullable
public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return this.beanFactory.getType(name);
}

@Override
@Nullable
public Class<?> getType(String name, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {
return this.beanFactory.getType(name, allowFactoryBeanInit);
}
Expand Down Expand Up @@ -337,6 +340,7 @@ public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
//---------------------------------------------------------------------

@Override
@Nullable
public BeanFactory getParentBeanFactory() {
return null;
}
Expand All @@ -352,6 +356,7 @@ public boolean containsLocalBean(String name) {
//---------------------------------------------------------------------

@Override
@Nullable
public String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale) {
return this.messageSource.getMessage(code, args, defaultMessage, locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.lang.Nullable;
import org.springframework.test.context.ApplicationContextFailureProcessor;

/**
Expand All @@ -37,10 +38,10 @@ public class TrackingApplicationContextFailureProcessor implements ApplicationCo


@Override
public void processLoadFailure(ApplicationContext context, Throwable exception) {
public void processLoadFailure(ApplicationContext context, @Nullable Throwable exception) {
loadFailures.add(new LoadFailure(context, exception));
}

public record LoadFailure(ApplicationContext context, Throwable exception) {}
public record LoadFailure(ApplicationContext context, @Nullable Throwable exception) {}

}

0 comments on commit 88b9c05

Please sign in to comment.