Skip to content

Commit 4143c31

Browse files
committed
Merge branch '2.2.x' into 2.3.x
Closes gh-23087
2 parents f42da16 + eba2bc3 commit 4143c31

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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-2020 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+
22+
import org.springframework.boot.rsocket.server.RSocketServer;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.verify;
32+
33+
/**
34+
* Tests for {@link RSocketPortInfoApplicationContextInitializer}.
35+
*
36+
* @author Spencer Gibb
37+
* @author Andy Wilkinson
38+
*/
39+
class RSocketPortInfoApplicationContextInitializerTests {
40+
41+
@Test
42+
void whenServerHasAddressThenInitializerSetsPortProperty() {
43+
try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class)) {
44+
context.getBean(RSocketPortInfoApplicationContextInitializer.class).initialize(context);
45+
RSocketServer server = mock(RSocketServer.class);
46+
given(server.address()).willReturn(new InetSocketAddress(65535));
47+
context.publishEvent(new RSocketServerInitializedEvent(server));
48+
assertThat(context.getEnvironment().getProperty("local.rsocket.server.port")).isEqualTo("65535");
49+
}
50+
}
51+
52+
@Test
53+
void whenServerHasNoAddressThenInitializerDoesNotSetPortProperty() {
54+
try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(Config.class)) {
55+
context.getBean(RSocketPortInfoApplicationContextInitializer.class).initialize(context);
56+
RSocketServer server = mock(RSocketServer.class);
57+
context.publishEvent(new RSocketServerInitializedEvent(server));
58+
verify(server).address();
59+
assertThat(context.getEnvironment().getProperty("local.rsocket.server.port")).isNull();
60+
}
61+
}
62+
63+
@Configuration(proxyBeanMethods = false)
64+
static class Config {
65+
66+
@Bean
67+
RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() {
68+
return new RSocketPortInfoApplicationContextInitializer();
69+
}
70+
71+
}
72+
73+
}

0 commit comments

Comments
 (0)