|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2019 the original author or authors. |
| 2 | + * Copyright 2012-2020 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
18 | 18 | import java.net.InetSocketAddress;
|
19 | 19 |
|
20 | 20 | import org.junit.jupiter.api.Test;
|
21 |
| -import org.junit.jupiter.api.extension.ExtendWith; |
22 | 21 |
|
23 |
| -import org.springframework.beans.factory.annotation.Autowired; |
24 | 22 | import org.springframework.boot.rsocket.server.RSocketServer;
|
25 |
| -import org.springframework.boot.rsocket.server.RSocketServerException; |
26 |
| -import org.springframework.context.ApplicationEventPublisher; |
27 | 23 | import org.springframework.context.ConfigurableApplicationContext;
|
| 24 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
28 | 25 | import org.springframework.context.annotation.Bean;
|
29 | 26 | import org.springframework.context.annotation.Configuration;
|
30 |
| -import org.springframework.test.context.junit.jupiter.SpringExtension; |
31 | 27 |
|
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; |
34 | 32 |
|
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 { |
43 | 40 |
|
44 | 41 | @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 | + } |
57 | 51 |
|
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 | + } |
63 | 61 | }
|
64 | 62 |
|
65 | 63 | @Configuration(proxyBeanMethods = false)
|
66 | 64 | static class Config {
|
67 | 65 |
|
68 | 66 | @Bean
|
69 |
| - public RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() { |
| 67 | + RSocketPortInfoApplicationContextInitializer rSocketPortInfoApplicationContextInitializer() { |
70 | 68 | return new RSocketPortInfoApplicationContextInitializer();
|
71 | 69 | }
|
| 70 | + |
72 | 71 | }
|
| 72 | + |
73 | 73 | }
|
0 commit comments