Skip to content

Commit f08f948

Browse files
spencergibbwilkinsona
authored andcommitted
Handle null RSocketServer address when setting port property
See gh-23084
1 parent 066471b commit f08f948

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/rsocket/context/RSocketPortInfoApplicationContextInitializer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ private static class Listener implements ApplicationListener<RSocketServerInitia
6363

6464
@Override
6565
public void onApplicationEvent(RSocketServerInitializedEvent event) {
66-
setPortProperty(this.applicationContext, event.getServer().address().getPort());
66+
if (event.getServer().address() != null) {
67+
setPortProperty(this.applicationContext, event.getServer().address().getPort());
68+
}
6769
}
6870

6971
private void setPortProperty(ApplicationContext context, int port) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.boot.rsocket.context;
17+
18+
import java.net.InetSocketAddress;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.boot.rsocket.server.RSocketServer;
25+
import org.springframework.boot.rsocket.server.RSocketServerException;
26+
import org.springframework.context.ApplicationEventPublisher;
27+
import org.springframework.context.ConfigurableApplicationContext;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.test.context.junit.jupiter.SpringExtension;
31+
32+
@ExtendWith(SpringExtension.class)
33+
public class RSocketPortInfoApplicationContextInitializerTests {
34+
35+
@Autowired
36+
private RSocketPortInfoApplicationContextInitializer initializer;
37+
38+
@Autowired
39+
private ConfigurableApplicationContext context;
40+
41+
@Autowired
42+
private ApplicationEventPublisher publisher;
43+
44+
@Test
45+
void nullAddressDoesNotThrow() {
46+
initializer.initialize(context);
47+
publisher.publishEvent(new RSocketServerInitializedEvent(new RSocketServer() {
48+
@Override
49+
public void start() throws RSocketServerException {
50+
51+
}
52+
53+
@Override
54+
public void stop() throws RSocketServerException {
55+
56+
}
57+
58+
@Override
59+
public InetSocketAddress address() {
60+
return null;
61+
}
62+
}));
63+
}
64+
65+
@Configuration(proxyBeanMethods = false)
66+
static class Config {
67+
68+
@Bean
69+
public RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() {
70+
return new RSocketPortInfoApplicationContextInitializer();
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)