Skip to content

Commit 6f047c8

Browse files
committed
Polish "Handle null RSocketServer address when setting port property"
See gh-23084
1 parent f08f948 commit 6f047c8

File tree

2 files changed

+35
-35
lines changed

2 files changed

+35
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
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.
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.
@@ -18,56 +18,56 @@
1818
import java.net.InetSocketAddress;
1919

2020
import org.junit.jupiter.api.Test;
21-
import org.junit.jupiter.api.extension.ExtendWith;
2221

23-
import org.springframework.beans.factory.annotation.Autowired;
2422
import org.springframework.boot.rsocket.server.RSocketServer;
25-
import org.springframework.boot.rsocket.server.RSocketServerException;
26-
import org.springframework.context.ApplicationEventPublisher;
2723
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2825
import org.springframework.context.annotation.Bean;
2926
import org.springframework.context.annotation.Configuration;
30-
import org.springframework.test.context.junit.jupiter.SpringExtension;
3127

32-
@ExtendWith(SpringExtension.class)
33-
public class RSocketPortInfoApplicationContextInitializerTests {
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;
3432

35-
@Autowired
36-
private RSocketPortInfoApplicationContextInitializer initializer;
37-
38-
@Autowired
39-
private ConfigurableApplicationContext context;
40-
41-
@Autowired
42-
private ApplicationEventPublisher publisher;
33+
/**
34+
* Tests for {@link RSocketPortInfoApplicationContextInitializer}.
35+
*
36+
* @author Spencer Gibb
37+
* @author Andy Wilkinson
38+
*/
39+
class RSocketPortInfoApplicationContextInitializerTests {
4340

4441
@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-
}
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+
}
5751

58-
@Override
59-
public InetSocketAddress address() {
60-
return null;
61-
}
62-
}));
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+
}
6361
}
6462

6563
@Configuration(proxyBeanMethods = false)
6664
static class Config {
6765

6866
@Bean
69-
public RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() {
67+
RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() {
7068
return new RSocketPortInfoApplicationContextInitializer();
7169
}
70+
7271
}
72+
7373
}

0 commit comments

Comments
 (0)