11package de .codecentric .boot .admin .config ;
22
33import static org .hamcrest .Matchers .is ;
4+ import static org .junit .Assert .assertFalse ;
45import static org .junit .Assert .assertThat ;
6+ import static org .junit .Assert .assertTrue ;
57import static org .mockito .Mockito .mock ;
68import static org .mockito .Mockito .when ;
79
1618import org .springframework .boot .context .embedded .EmbeddedServletContainerInitializedEvent ;
1719import org .springframework .boot .context .embedded .EmbeddedWebApplicationContext ;
1820import org .springframework .boot .test .EnvironmentTestUtils ;
21+ import org .springframework .context .event .ContextRefreshedEvent ;
1922import org .springframework .web .context .support .AnnotationConfigWebApplicationContext ;
2023
2124public class AdminClientPropertiesTest {
@@ -29,40 +32,66 @@ public void close() {
2932 }
3033 }
3134
35+ @ Test
36+ public void test_isServerStarted_false () {
37+ assertFalse (new AdminClientProperties ().isServerInitialized ());
38+ }
39+
40+ @ Test
41+ public void test_isServerStarted_true_embedded () {
42+ AdminClientProperties clientProperties = new AdminClientProperties ();
43+ clientProperties .setUrl ("http://localhost" );
44+ publishServletContainerInitializedEvent (clientProperties , 8080 , null );
45+ assertTrue (clientProperties .isServerInitialized ());
46+ }
47+
48+ @ Test
49+ public void test_isServerStarted_true_war () {
50+ AdminClientProperties clientProperties = new AdminClientProperties ();
51+ clientProperties .setUrl ("http://localhost" );
52+ publishContextRefreshedEvent (clientProperties );
53+ assertTrue (clientProperties .isServerInitialized ());
54+ }
55+
56+ @ Test (expected = RuntimeException .class )
57+ public void test_isServerStarted_exception_war () {
58+ AdminClientProperties clientProperties = new AdminClientProperties ();
59+ publishContextRefreshedEvent (clientProperties );
60+ }
61+
3262 @ Test
3363 public void test_mgmtPortPath () {
34- load ("spring.boot.admin.url:http://localhost:8081" ,
35- "management.contextPath=/admin" );
36- AdminClientProperties clientProperties = context
37- .getBean (AdminClientProperties .class );
64+ load ("management.contextPath=/admin" );
65+ AdminClientProperties clientProperties = new AdminClientProperties ();
66+ context .getAutowireCapableBeanFactory ().autowireBean (clientProperties );
3867
39- publishEvent ( 8080 , null );
40- publishEvent ( 8081 , "management" );
68+ publishServletContainerInitializedEvent ( clientProperties , 8080 , null );
69+ publishServletContainerInitializedEvent ( clientProperties , 8081 , "management" );
4170
4271 assertThat (clientProperties .getUrl (), is ("http://" + getHostname ()
4372 + ":8081/admin" ));
4473 }
4574
4675 @ Test
4776 public void test_mgmtPort () {
48- load ("spring.boot.admin.url:http://localhost:8081" );
49- AdminClientProperties clientProperties = context
50- . getBean ( AdminClientProperties . class );
77+ load ();
78+ AdminClientProperties clientProperties = new AdminClientProperties ();
79+ context . getAutowireCapableBeanFactory (). autowireBean ( clientProperties );
5180
52- publishEvent ( 8080 , null );
53- publishEvent ( 8081 , "management" );
81+ publishServletContainerInitializedEvent ( clientProperties , 8080 , null );
82+ publishServletContainerInitializedEvent ( clientProperties , 8081 , "management" );
5483
5584 assertThat (clientProperties .getUrl (), is ("http://" + getHostname () + ":8081" ));
5685 }
5786
5887 @ Test
5988 public void test_contextPath_mgmtPath () {
60- load ("spring.boot.admin.url:http://localhost:8081" , " server.context-path=app" ,
89+ load ("server.context-path=app" ,
6190 "management.context-path=/admin" );
62- AdminClientProperties clientProperties = context
63- . getBean ( AdminClientProperties . class );
91+ AdminClientProperties clientProperties = new AdminClientProperties ();
92+ context . getAutowireCapableBeanFactory (). autowireBean ( clientProperties );
6493
65- publishEvent ( 8080 , null );
94+ publishServletContainerInitializedEvent ( clientProperties , 8080 , null );
6695
6796 assertThat (clientProperties .getUrl (), is ("http://" + getHostname ()
6897 + ":8080/app/admin" ));
@@ -71,23 +100,23 @@ public void test_contextPath_mgmtPath() {
71100
72101 @ Test
73102 public void test_contextPath () {
74- load ("spring.boot.admin.url:http://localhost:8081" , " server.context-path=app" );
75- AdminClientProperties clientProperties = context
76- . getBean ( AdminClientProperties . class );
103+ load ("server.context-path=app" );
104+ AdminClientProperties clientProperties = new AdminClientProperties ();
105+ context . getAutowireCapableBeanFactory (). autowireBean ( clientProperties );
77106
78- publishEvent ( 8080 , null );
107+ publishServletContainerInitializedEvent ( clientProperties , 8080 , null );
79108
80109 assertThat (clientProperties .getUrl (), is ("http://" + getHostname () + ":8080/app" ));
81110 }
82111
83112
84113 @ Test
85114 public void test_default () {
86- load ("spring.boot.admin.url:http://localhost:8081" );
87- AdminClientProperties clientProperties = context
88- . getBean ( AdminClientProperties . class );
115+ load ();
116+ AdminClientProperties clientProperties = new AdminClientProperties ();
117+ context . getAutowireCapableBeanFactory (). autowireBean ( clientProperties );
89118
90- publishEvent ( 8080 , null );
119+ publishServletContainerInitializedEvent ( clientProperties , 8080 , null );
91120
92121 assertThat (clientProperties .getUrl (), is ("http://" + getHostname () + ":8080" ));
93122 }
@@ -101,20 +130,27 @@ private String getHostname() {
101130 }
102131 }
103132
104- private void publishEvent (int port , String namespace ) {
133+ private void publishServletContainerInitializedEvent (AdminClientProperties client ,
134+ int port , String namespace ) {
105135 EmbeddedServletContainer eventSource = mock (EmbeddedServletContainer .class );
106136 when (eventSource .getPort ()).thenReturn (port );
107137 EmbeddedWebApplicationContext eventContext = mock (EmbeddedWebApplicationContext .class );
108138 when (eventContext .getNamespace ()).thenReturn (namespace );
109- context .publishEvent (new EmbeddedServletContainerInitializedEvent (eventContext ,
139+ when (eventContext .getEmbeddedServletContainer ()).thenReturn (eventSource );
140+ client .onApplicationEvent (new EmbeddedServletContainerInitializedEvent (
141+ eventContext ,
110142 eventSource ));
111143 }
112144
145+ private void publishContextRefreshedEvent (AdminClientProperties client ) {
146+ client .onApplicationEvent (new ContextRefreshedEvent (
147+ mock (EmbeddedWebApplicationContext .class )));
148+ }
149+
113150 private void load (String ... environment ) {
114151 AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext ();
115152 applicationContext .register (ServerPropertiesAutoConfiguration .class );
116153 applicationContext .register (ManagementServerPropertiesAutoConfiguration .class );
117- applicationContext .register (SpringBootAdminClientAutoConfiguration .class );
118154 EnvironmentTestUtils .addEnvironment (applicationContext , environment );
119155 applicationContext .refresh ();
120156 this .context = applicationContext ;
0 commit comments