|
| 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