Skip to content

Commit 0479d6f

Browse files
committed
Add nullability annotations to module/spring-boot-web-server-test
See gh-46587
1 parent 9a48595 commit 0479d6f

13 files changed

+100
-49
lines changed

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/SpringBootTestRandomPortEnvironmentPostProcessor.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818

1919
import java.util.Objects;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.SpringApplication;
2224
import org.springframework.boot.env.EnvironmentPostProcessor;
2325
import org.springframework.core.convert.ConversionFailedException;
2426
import org.springframework.core.env.ConfigurableEnvironment;
2527
import org.springframework.core.env.MapPropertySource;
2628
import org.springframework.core.env.PropertySource;
29+
import org.springframework.lang.Contract;
2730
import org.springframework.test.context.support.TestPropertySourceUtils;
2831
import org.springframework.util.ClassUtils;
2932

@@ -69,7 +72,9 @@ private boolean isTestManagementPortConfigured(PropertySource<?> source) {
6972
return source.getProperty(MANAGEMENT_PORT_PROPERTY) != null;
7073
}
7174

72-
private Integer getPropertyAsInteger(ConfigurableEnvironment environment, String property, Integer defaultValue) {
75+
@Contract("_, _, !null -> !null")
76+
private @Nullable Integer getPropertyAsInteger(ConfigurableEnvironment environment, String property,
77+
@Nullable Integer defaultValue) {
7378
return environment.getPropertySources()
7479
.stream()
7580
.filter((source) -> !source.getName()
@@ -80,7 +85,7 @@ private Integer getPropertyAsInteger(ConfigurableEnvironment environment, String
8085
.orElse(defaultValue);
8186
}
8287

83-
private Integer getPropertyAsInteger(PropertySource<?> source, String property,
88+
private @Nullable Integer getPropertyAsInteger(PropertySource<?> source, String property,
8489
ConfigurableEnvironment environment) {
8590
Object value = source.getProperty(property);
8691
if (value == null) {
@@ -100,7 +105,7 @@ private Integer getPropertyAsInteger(PropertySource<?> source, String property,
100105
}
101106
}
102107

103-
private Integer getResolvedValueIfPossible(ConfigurableEnvironment environment, String value) {
108+
private @Nullable Integer getResolvedValueIfPossible(ConfigurableEnvironment environment, String value) {
104109
String resolvedValue = environment.resolveRequiredPlaceholders(value);
105110
return environment.getConversionService().convert(resolvedValue, Integer.class);
106111
}

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/TestRestTemplate.java

Lines changed: 45 additions & 38 deletions
Large diffs are not rendered by default.

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/TestRestTemplateContextCustomizer.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.web.server.test.client;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.aot.AotDetector;
2022
import org.springframework.beans.BeansException;
2123
import org.springframework.beans.factory.BeanFactory;
@@ -41,6 +43,7 @@
4143
import org.springframework.test.context.ContextCustomizer;
4244
import org.springframework.test.context.MergedContextConfiguration;
4345
import org.springframework.test.context.TestContextAnnotationUtils;
46+
import org.springframework.util.Assert;
4447

4548
/**
4649
* {@link ContextCustomizer} for {@link TestRestTemplate}.
@@ -58,6 +61,7 @@ public void customizeContext(ConfigurableApplicationContext context,
5861
}
5962
SpringBootTest springBootTest = TestContextAnnotationUtils
6063
.findMergedAnnotation(mergedContextConfiguration.getTestClass(), SpringBootTest.class);
64+
Assert.state(springBootTest != null, "'springBootTest' must not be null");
6165
if (springBootTest.webEnvironment().isEmbedded()) {
6266
registerTestRestTemplate(context);
6367
}
@@ -77,7 +81,7 @@ private void registerTestRestTemplate(BeanDefinitionRegistry registry) {
7781
}
7882

7983
@Override
80-
public boolean equals(Object obj) {
84+
public boolean equals(@Nullable Object obj) {
8185
return (obj != null) && (obj.getClass() == getClass());
8286
}
8387

@@ -93,6 +97,7 @@ public int hashCode() {
9397
*/
9498
static class TestRestTemplateRegistrar implements BeanDefinitionRegistryPostProcessor, Ordered, BeanFactoryAware {
9599

100+
@SuppressWarnings("NullAway.Init")
96101
private BeanFactory beanFactory;
97102

98103
@Override
@@ -133,6 +138,7 @@ public static class TestRestTemplateFactory implements FactoryBean<TestRestTempl
133138

134139
private static final HttpClientOption[] SSL_OPTIONS = { HttpClientOption.SSL };
135140

141+
@SuppressWarnings("NullAway.Init")
136142
private TestRestTemplate template;
137143

138144
@Override

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/TestRestTemplateContextCustomizerFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.test.context.SpringBootTest;
2224
import org.springframework.test.context.ContextConfigurationAttributes;
2325
import org.springframework.test.context.ContextCustomizer;
@@ -38,7 +40,7 @@ class TestRestTemplateContextCustomizerFactory implements ContextCustomizerFacto
3840
TestRestTemplateContextCustomizerFactory.class.getClassLoader());
3941

4042
@Override
41-
public ContextCustomizer createContextCustomizer(Class<?> testClass,
43+
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
4244
List<ContextConfigurationAttributes> configAttributes) {
4345
if (!REST_TEMPLATE_BUILDER_PRESENT) {
4446
return null;

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Client-side support for testing embedded web servers.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.web.server.test.client;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/reactive/WebTestClientContextCustomizer.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
import java.util.Collection;
2020

21+
import jakarta.servlet.ServletContext;
22+
import org.jspecify.annotations.Nullable;
23+
2124
import org.springframework.aot.AotDetector;
2225
import org.springframework.beans.BeansException;
2326
import org.springframework.beans.factory.BeanFactory;
@@ -44,6 +47,7 @@
4447
import org.springframework.test.context.MergedContextConfiguration;
4548
import org.springframework.test.context.TestContextAnnotationUtils;
4649
import org.springframework.test.web.reactive.server.WebTestClient;
50+
import org.springframework.util.Assert;
4751
import org.springframework.util.ClassUtils;
4852
import org.springframework.util.CollectionUtils;
4953
import org.springframework.util.StringUtils;
@@ -71,6 +75,7 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
7175
}
7276
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(mergedConfig.getTestClass(),
7377
SpringBootTest.class);
78+
Assert.state(springBootTest != null, "'springBootTest' must not be null");
7479
if (springBootTest.webEnvironment().isEmbedded()) {
7580
registerWebTestClient(context);
7681
}
@@ -90,7 +95,7 @@ private void registerWebTestClient(BeanDefinitionRegistry registry) {
9095
}
9196

9297
@Override
93-
public boolean equals(Object obj) {
98+
public boolean equals(@Nullable Object obj) {
9499
return (obj != null) && (obj.getClass() == getClass());
95100
}
96101

@@ -106,6 +111,7 @@ public int hashCode() {
106111
*/
107112
static class WebTestClientRegistrar implements BeanDefinitionRegistryPostProcessor, Ordered, BeanFactoryAware {
108113

114+
@SuppressWarnings("NullAway.Init")
109115
private BeanFactory beanFactory;
110116

111117
@Override
@@ -142,9 +148,10 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
142148
*/
143149
public static class WebTestClientFactory implements FactoryBean<WebTestClient>, ApplicationContextAware {
144150

151+
@SuppressWarnings("NullAway.Init")
145152
private ApplicationContext applicationContext;
146153

147-
private WebTestClient object;
154+
private @Nullable WebTestClient object;
148155

149156
private static final String SERVLET_APPLICATION_CONTEXT_CLASS = "org.springframework.web.context.WebApplicationContext";
150157

@@ -191,13 +198,15 @@ private String getBaseUrl(boolean sslEnabled, String port) {
191198
return (sslEnabled ? "https" : "http") + "://localhost:" + port + pathSegment;
192199
}
193200

194-
private String deduceBasePath() {
201+
private @Nullable String deduceBasePath() {
195202
WebApplicationType webApplicationType = deduceFromApplicationContext(this.applicationContext.getClass());
196203
if (webApplicationType == WebApplicationType.REACTIVE) {
197204
return this.applicationContext.getEnvironment().getProperty("spring.webflux.base-path");
198205
}
199206
else if (webApplicationType == WebApplicationType.SERVLET) {
200-
return ((WebApplicationContext) this.applicationContext).getServletContext().getContextPath();
207+
ServletContext servletContext = ((WebApplicationContext) this.applicationContext).getServletContext();
208+
Assert.state(servletContext != null, "'servletContext' must not be null");
209+
return servletContext.getContextPath();
201210
}
202211
return null;
203212
}

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/reactive/WebTestClientContextCustomizerFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.List;
2020

21+
import org.jspecify.annotations.Nullable;
22+
2123
import org.springframework.boot.test.context.SpringBootTest;
2224
import org.springframework.test.context.ContextConfigurationAttributes;
2325
import org.springframework.test.context.ContextCustomizer;
@@ -42,7 +44,7 @@ class WebTestClientContextCustomizerFactory implements ContextCustomizerFactory
4244
}
4345

4446
@Override
45-
public ContextCustomizer createContextCustomizer(Class<?> testClass,
47+
public @Nullable ContextCustomizer createContextCustomizer(Class<?> testClass,
4648
List<ContextConfigurationAttributes> configAttributes) {
4749
SpringBootTest springBootTest = TestContextAnnotationUtils.findMergedAnnotation(testClass,
4850
SpringBootTest.class);

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/client/reactive/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
* Spring Boot support for testing Spring WebFlux server endpoints via
1919
* {@link org.springframework.test.web.reactive.server.WebTestClient}.
2020
*/
21+
@NullMarked
2122
package org.springframework.boot.web.server.test.client.reactive;
23+
24+
import org.jspecify.annotations.NullMarked;

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/htmlunit/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* HtmlUnit support classes.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.web.server.test.htmlunit;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-web-server-test/src/main/java/org/springframework/boot/web/server/test/htmlunit/webdriver/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Selenium support classes.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.web.server.test.htmlunit.webdriver;
22+
23+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)