Skip to content

Commit c92617a

Browse files
committed
Add nullability annotations to tests in module/spring-boot-jetty
See gh-47263
1 parent a9bf744 commit c92617a

File tree

10 files changed

+44
-17
lines changed

10 files changed

+44
-17
lines changed

module/spring-boot-jetty/build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ dependencies {
4848
testImplementation(testFixtures(project(":core:spring-boot-autoconfigure")))
4949
testImplementation("org.apache.httpcomponents.client5:httpclient5")
5050

51+
testCompileOnly("com.google.code.findbugs:jsr305")
52+
5153
testRuntimeOnly("ch.qos.logback:logback-classic")
5254
testRuntimeOnly("io.projectreactor:reactor-test")
5355
testRuntimeOnly("io.projectreactor.netty:reactor-netty-http")
@@ -61,3 +63,6 @@ test {
6163
jvmArgs += "--add-opens=java.base/java.net=ALL-UNNAMED"
6264
}
6365

66+
tasks.named("compileTestJava") {
67+
options.nullability.checking = "tests"
68+
}

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/SslServerCustomizerTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ void alpnConnectionFactoryHasNullDefaultProtocolToAllowNegotiationToHttp11() {
9696
void configureSslWhenSslIsEnabledWithNoKeyStoreAndNotPkcs11ThrowsException() {
9797
Ssl ssl = new Ssl();
9898
assertThatIllegalStateException().isThrownBy(() -> {
99-
SslServerCustomizer customizer = new SslServerCustomizer(null, null, null, WebServerSslBundle.get(ssl));
99+
SslServerCustomizer customizer = new SslServerCustomizer(null, new InetSocketAddress(0), null,
100+
WebServerSslBundle.get(ssl));
100101
customizer.configureSsl(new SslContextFactory.Server(), ssl.getClientAuth());
101102
}).withMessageContaining("SSL is enabled but no trust material is configured");
102103
}
@@ -110,7 +111,8 @@ void configureSslWhenSslIsEnabledWithPkcs11AndKeyStoreThrowsException() {
110111
ssl.setKeyStore("classpath:test.jks");
111112
ssl.setKeyPassword("password");
112113
assertThatIllegalStateException().isThrownBy(() -> {
113-
SslServerCustomizer customizer = new SslServerCustomizer(null, null, null, WebServerSslBundle.get(ssl));
114+
SslServerCustomizer customizer = new SslServerCustomizer(null, new InetSocketAddress(0), null,
115+
WebServerSslBundle.get(ssl));
114116
customizer.configureSsl(new SslContextFactory.Server(), ssl.getClientAuth());
115117
}).withMessageContaining("must be empty or null for PKCS11 hardware key stores");
116118
}
@@ -122,7 +124,8 @@ void customizeWhenSslIsEnabledWithPkcs11AndKeyStoreProvider() {
122124
ssl.setKeyStoreProvider(MockPkcs11SecurityProvider.NAME);
123125
ssl.setKeyStorePassword("1234");
124126
assertThatNoException().isThrownBy(() -> {
125-
SslServerCustomizer customizer = new SslServerCustomizer(null, null, null, WebServerSslBundle.get(ssl));
127+
SslServerCustomizer customizer = new SslServerCustomizer(null, new InetSocketAddress(0), null,
128+
WebServerSslBundle.get(ssl));
126129
customizer.configureSsl(new SslContextFactory.Server(), ssl.getClientAuth());
127130
});
128131
}

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/autoconfigure/JettyWebServerFactoryCustomizerTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,9 @@ private void startAndStopToMakeInternalsAvailable(JettyWebServer server) {
378378
}
379379

380380
private BlockingQueue<?> getQueue(ThreadPool threadPool) {
381-
return ReflectionTestUtils.invokeMethod(threadPool, "getQueue");
381+
BlockingQueue<?> queue = ReflectionTestUtils.invokeMethod(threadPool, "getQueue");
382+
assertThat(queue).isNotNull();
383+
return queue;
382384
}
383385

384386
private void bind(String... inlinedProperties) {

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/autoconfigure/metrics/JettyMetricsAutoConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ void doesNotAutoConfigureSslHandshakeMetricsWhenSslEnabledPropertySetToFalse() {
217217
}
218218

219219
private ApplicationStartedEvent createApplicationStartedEvent(ConfigurableApplicationContext context) {
220-
return new ApplicationStartedEvent(new SpringApplication(), null, context, null);
220+
return new ApplicationStartedEvent(new SpringApplication(), new String[0], context, null);
221221
}
222222

223223
@Configuration(proxyBeanMethods = false)

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/autoconfigure/reactive/JettyReactiveWebServerAutoConfigurationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ void webSocketServerContainerIsAvailableFromServletContext() {
7676
this.serverRunner.run((context) -> {
7777
WebServer webServer = ((ReactiveWebServerApplicationContext) context.getSourceApplicationContext())
7878
.getWebServer();
79+
assertThat(webServer).isNotNull();
7980
ServletContextHandler servletContextHandler = (ServletContextHandler) ((StatisticsHandler) ((JettyWebServer) webServer)
8081
.getServer()
8182
.getHandler()).getHandler();

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/autoconfigure/servlet/JettyServletWebServerAutoConfigurationTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import jakarta.servlet.Filter;
22+
import jakarta.servlet.ServletContext;
2223
import org.eclipse.jetty.ee11.websocket.servlet.WebSocketUpgradeFilter;
2324
import org.eclipse.jetty.server.Server;
2425
import org.junit.jupiter.api.Test;
@@ -75,9 +76,11 @@ void jettyServerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
7576

7677
@Test
7778
void jettyWebSocketUpgradeFilterIsAddedToServletContex() {
78-
this.serverRunner.run((context) -> assertThat(
79-
context.getServletContext().getFilterRegistration(WebSocketUpgradeFilter.class.getName()))
80-
.isNotNull());
79+
this.serverRunner.run((context) -> {
80+
ServletContext servletContext = context.getServletContext();
81+
assertThat(servletContext).isNotNull();
82+
assertThat(servletContext.getFilterRegistration(WebSocketUpgradeFilter.class.getName())).isNotNull();
83+
});
8184
}
8285

8386
@Test

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/reactive/JettyReactiveWebServerFactoryTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,15 @@ protected void noCompressionForUserAgent() {
7272
}
7373

7474
@Test
75+
@SuppressWarnings("NullAway") // Test null check
7576
void setNullServerCustomizersShouldThrowException() {
7677
JettyReactiveWebServerFactory factory = getFactory();
7778
assertThatIllegalArgumentException().isThrownBy(() -> factory.setServerCustomizers(null))
7879
.withMessageContaining("'customizers' must not be null");
7980
}
8081

8182
@Test
83+
@SuppressWarnings("NullAway") // Test null check
8284
void addNullServerCustomizersShouldThrowException() {
8385
JettyReactiveWebServerFactory factory = getFactory();
8486
assertThatIllegalArgumentException()

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/servlet/JettyServletWebServerFactoryTests.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.eclipse.jetty.util.ssl.SslContextFactory;
5959
import org.eclipse.jetty.util.thread.QueuedThreadPool;
6060
import org.eclipse.jetty.util.thread.ThreadPool;
61+
import org.jspecify.annotations.Nullable;
6162
import org.junit.jupiter.api.Disabled;
6263
import org.junit.jupiter.api.Test;
6364
import org.mockito.InOrder;
@@ -123,7 +124,7 @@ protected void addConnector(int port, ConfigurableServletWebServerFactory factor
123124
}
124125

125126
@Override
126-
protected JspServlet getJspServlet() throws Exception {
127+
protected @Nullable JspServlet getJspServlet() throws Exception {
127128
WebAppContext context = findWebAppContext((JettyWebServer) this.webServer);
128129
ServletHolder holder = context.getServletHandler().getServlet("jsp");
129130
if (holder == null) {
@@ -141,7 +142,7 @@ protected Map<String, String> getActualMimeMappings() {
141142
}
142143

143144
@Override
144-
protected Charset getCharset(Locale locale) {
145+
protected @Nullable Charset getCharset(Locale locale) {
145146
WebAppContext context = findWebAppContext((JettyWebServer) this.webServer);
146147
String charsetName = context.getLocaleEncoding(locale);
147148
return (charsetName != null) ? Charset.forName(charsetName) : null;
@@ -314,7 +315,11 @@ private SslContextFactory extractSslContextFactory(SslConnectionFactory connecti
314315
catch (NoSuchMethodError ex) {
315316
Method getSslContextFactory = ReflectionUtils.findMethod(connectionFactory.getClass(),
316317
"getSslContextFactory");
317-
return (SslContextFactory) ReflectionUtils.invokeMethod(getSslContextFactory, connectionFactory);
318+
assertThat(getSslContextFactory).isNotNull();
319+
SslContextFactory sslContextFactory = (SslContextFactory) ReflectionUtils.invokeMethod(getSslContextFactory,
320+
connectionFactory);
321+
assertThat(sslContextFactory).isNotNull();
322+
return sslContextFactory;
318323
}
319324
}
320325

@@ -528,6 +533,7 @@ public void contextDestroyed(ServletContextEvent event) {
528533
// Jetty 10
529534
Method addEventListener = ReflectionUtils.findMethod(context.getClass(), "addEventListener",
530535
EventListener.class);
536+
assertThat(addEventListener).isNotNull();
531537
ReflectionUtils.invokeMethod(addEventListener, context, eventListener);
532538
}
533539
});

module/spring-boot-jetty/src/test/java/org/springframework/boot/jetty/servlet/LoaderHidingResourceTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,14 @@ void resolveHidesLoaderResources(@TempDir File temp) throws IOException {
7070
URI warUri = createExampleWar(temp);
7171
Resource resource = new PathResourceFactory().newResource(warUri);
7272
LoaderHidingResource loaderHidingResource = new LoaderHidingResource(resource, resource);
73-
assertThat(loaderHidingResource.resolve("/assets/image.jpg").exists()).isTrue();
74-
assertThat(loaderHidingResource.resolve("/assets/image.jpg")).isInstanceOf(LoaderHidingResource.class);
75-
assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg").exists()).isFalse();
76-
assertThat(loaderHidingResource.resolve("/assets/non-existent.jpg")).isInstanceOf(LoaderHidingResource.class);
73+
Resource image = loaderHidingResource.resolve("/assets/image.jpg");
74+
assertThat(image).isNotNull();
75+
assertThat(image.exists()).isTrue();
76+
assertThat(image).isInstanceOf(LoaderHidingResource.class);
77+
Resource doesntExist = loaderHidingResource.resolve("/assets/non-existent.jpg");
78+
assertThat(doesntExist).isNotNull();
79+
assertThat(doesntExist.exists()).isFalse();
80+
assertThat(doesntExist).isInstanceOf(LoaderHidingResource.class);
7781
assertThat(loaderHidingResource.resolve("/org/springframework/boot/Loader.class")).isNull();
7882
}
7983

module/spring-boot-web-server/src/testFixtures/java/org/springframework/boot/web/server/servlet/AbstractServletWebServerFactoryTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
import org.eclipse.jetty.client.ContentResponse;
107107
import org.eclipse.jetty.http2.client.HTTP2Client;
108108
import org.eclipse.jetty.http2.client.transport.HttpClientTransportOverHTTP2;
109+
import org.jspecify.annotations.Nullable;
109110
import org.junit.jupiter.api.AfterEach;
110111
import org.junit.jupiter.api.Assumptions;
111112
import org.junit.jupiter.api.Test;
@@ -1507,7 +1508,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
15071508

15081509
protected abstract Map<String, String> getActualMimeMappings();
15091510

1510-
protected abstract Charset getCharset(Locale locale);
1511+
protected abstract @Nullable Charset getCharset(Locale locale);
15111512

15121513
protected void addTestTxtFile(ConfigurableServletWebServerFactory factory) throws IOException {
15131514
FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.txt")));
@@ -1588,7 +1589,7 @@ protected void assertForwardHeaderIsUsed(ServletWebServerFactory factory) throws
15881589

15891590
protected abstract ConfigurableServletWebServerFactory getFactory();
15901591

1591-
protected abstract org.apache.jasper.servlet.JspServlet getJspServlet() throws Exception;
1592+
protected abstract org.apache.jasper.servlet.@Nullable JspServlet getJspServlet() throws Exception;
15921593

15931594
protected ServletContextInitializer exampleServletRegistration() {
15941595
return new ServletRegistrationBean<>(new ExampleServlet(), "/hello");

0 commit comments

Comments
 (0)