Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-boot-admin
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-boot-admin
Commits
8a497e65
Commit
8a497e65
authored
Sep 22, 2017
by
Johannes Edmeier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable Client for reactive web applications
parent
7e32dfcc
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
255 additions
and
97 deletions
+255
-97
pom.xml
spring-boot-admin-starter-client/pom.xml
+5
-0
SpringBootAdminClientAutoConfiguration.java
...client/config/SpringBootAdminClientAutoConfiguration.java
+13
-0
DefaultApplicationFactory.java
.../admin/client/registration/DefaultApplicationFactory.java
+8
-11
RegistrationApplicationListener.java
.../client/registration/RegistrationApplicationListener.java
+6
-15
AbstractClientApplicationTest.java
...ot/admin/client/config/AbstractClientApplicationTest.java
+81
-0
ClientReactiveApplicationTest.java
...ot/admin/client/config/ClientReactiveApplicationTest.java
+65
-0
ClientServletApplicationTest.java
...oot/admin/client/config/ClientServletApplicationTest.java
+17
-47
DefaultApplicationFactoryTest.java
...in/client/registration/DefaultApplicationFactoryTest.java
+31
-12
ServletApplicationFactoryTest.java
...in/client/registration/ServletApplicationFactoryTest.java
+29
-12
No files found.
spring-boot-admin-starter-client/pom.xml
View file @
8a497e65
...
...
@@ -70,6 +70,11 @@
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-webflux
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
com.github.tomakehurst
</groupId>
<artifactId>
wiremock-standalone
</artifactId>
<version>
2.5.1
</version>
...
...
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/client/config/SpringBootAdminClientAutoConfiguration.java
View file @
8a497e65
...
...
@@ -62,6 +62,19 @@ public class SpringBootAdminClientAutoConfiguration {
}
}
@Configuration
@ConditionalOnWebApplication
(
type
=
Type
.
REACTIVE
)
public
static
class
ReactiveConfiguration
{
@Bean
@ConditionalOnMissingBean
public
ApplicationFactory
applicationFactory
(
InstanceProperties
instance
,
ManagementServerProperties
management
,
ServerProperties
server
,
EndpointPathProvider
endpointPathProvider
)
{
return
new
DefaultApplicationFactory
(
instance
,
management
,
server
,
endpointPathProvider
);
}
}
@Bean
@ConditionalOnMissingBean
public
ApplicationRegistrator
registrator
(
ClientProperties
client
,
...
...
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/client/registration/DefaultApplicationFactory.java
View file @
8a497e65
...
...
@@ -24,11 +24,10 @@ import java.util.Map;
import
org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider
;
import
org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.
context.event.ApplicationReady
Event
;
import
org.springframework.boot.
web.context.WebServerInitialized
Event
;
import
org.springframework.boot.web.server.Ssl
;
import
org.springframework.context.event.EventListener
;
import
org.springframework.util.StringUtils
;
import
org.springframework.web.context.WebApplicationContext
;
import
org.springframework.web.util.UriComponentsBuilder
;
/**
...
...
@@ -77,7 +76,7 @@ public class DefaultApplicationFactory implements ApplicationFactory {
String
baseUrl
=
instance
.
getServiceBaseUrl
();
if
(
getLocalServerPort
()
==
null
&&
StringUtils
.
isEmpty
(
baseUrl
))
{
throw
new
IllegalStateException
(
"
service-base-url must be set when deployed to servlet-container
"
);
throw
new
IllegalStateException
(
"
couldn't determine local port. Please supply service-base-url.
"
);
}
UriComponentsBuilder
builder
;
...
...
@@ -209,14 +208,12 @@ public class DefaultApplicationFactory implements ApplicationFactory {
}
@EventListener
public
void
onApplicationReady
(
ApplicationReadyEvent
event
)
{
if
(
event
.
getApplicationContext
()
instanceof
WebApplicationContext
)
{
localServerPort
=
event
.
getApplicationContext
()
.
getEnvironment
()
.
getProperty
(
"local.server.port"
,
Integer
.
class
);
localManagementPort
=
event
.
getApplicationContext
()
.
getEnvironment
()
.
getProperty
(
"local.management.port"
,
Integer
.
class
,
localServerPort
);
public
void
onWebServerInitialized
(
WebServerInitializedEvent
event
)
{
if
(
"server"
.
equals
(
event
.
getServerId
()))
{
localServerPort
=
event
.
getWebServer
().
getPort
();
}
if
(
"management"
.
equals
(
event
.
getServerId
()))
{
localManagementPort
=
event
.
getWebServer
().
getPort
();
}
}
}
spring-boot-admin-starter-client/src/main/java/de/codecentric/boot/admin/client/registration/RegistrationApplicationListener.java
View file @
8a497e65
/*
* Copyright 2014 the original author or authors.
* Copyright 2014
-2017
the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -16,7 +16,6 @@
package
de
.
codecentric
.
boot
.
admin
.
client
.
registration
;
import
java.util.concurrent.ScheduledFuture
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.boot.context.event.ApplicationReadyEvent
;
...
...
@@ -25,7 +24,6 @@ import org.springframework.context.event.EventListener;
import
org.springframework.core.Ordered
;
import
org.springframework.core.annotation.Order
;
import
org.springframework.scheduling.TaskScheduler
;
import
org.springframework.web.context.WebApplicationContext
;
/**
* Listener responsible for starting and stopping the registration task when the application is
...
...
@@ -34,8 +32,7 @@ import org.springframework.web.context.WebApplicationContext;
* @author Johannes Edmeier
*/
public
class
RegistrationApplicationListener
{
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
RegistrationApplicationListener
.
class
);
private
static
final
Logger
LOGGER
=
LoggerFactory
.
getLogger
(
RegistrationApplicationListener
.
class
);
private
final
ApplicationRegistrator
registrator
;
private
final
TaskScheduler
taskScheduler
;
private
boolean
autoDeregister
=
false
;
...
...
@@ -43,8 +40,7 @@ public class RegistrationApplicationListener {
private
long
registerPeriod
=
10_000L
;
private
volatile
ScheduledFuture
<?>
scheduledTask
;
public
RegistrationApplicationListener
(
ApplicationRegistrator
registrator
,
TaskScheduler
taskScheduler
)
{
public
RegistrationApplicationListener
(
ApplicationRegistrator
registrator
,
TaskScheduler
taskScheduler
)
{
this
.
registrator
=
registrator
;
this
.
taskScheduler
=
taskScheduler
;
}
...
...
@@ -52,7 +48,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order
(
Ordered
.
LOWEST_PRECEDENCE
)
public
void
onApplicationReady
(
ApplicationReadyEvent
event
)
{
if
(
event
.
getApplicationContext
()
instanceof
WebApplicationContext
&&
autoRegister
)
{
if
(
event
.
getApplicationContext
().
getParent
()
==
null
&&
autoRegister
)
{
startRegisterTask
();
}
}
...
...
@@ -60,7 +56,7 @@ public class RegistrationApplicationListener {
@EventListener
@Order
(
Ordered
.
LOWEST_PRECEDENCE
)
public
void
onClosedContext
(
ContextClosedEvent
event
)
{
if
(
event
.
getApplicationContext
()
instanceof
WebApplicationContext
)
{
if
(
event
.
getApplicationContext
().
getParent
()
==
null
)
{
stopRegisterTask
();
if
(
autoDeregister
)
{
...
...
@@ -74,12 +70,7 @@ public class RegistrationApplicationListener {
return
;
}
scheduledTask
=
taskScheduler
.
scheduleAtFixedRate
(
new
Runnable
()
{
@Override
public
void
run
()
{
registrator
.
register
();
}
},
registerPeriod
);
scheduledTask
=
taskScheduler
.
scheduleAtFixedRate
(
registrator:
:
register
,
registerPeriod
);
LOGGER
.
debug
(
"Scheduled registration task for every {}ms"
,
registerPeriod
);
}
...
...
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/client/config/AbstractClientApplicationTest.java
0 → 100644
View file @
8a497e65
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
de
.
codecentric
.
boot
.
admin
.
client
.
config
;
import
wiremock.org.apache.http.HttpStatus
;
import
org.junit.Test
;
import
com.github.tomakehurst.wiremock.WireMockServer
;
import
com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder
;
import
com.github.tomakehurst.wiremock.matching.RequestPatternBuilder
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
aResponse
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
configureFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
equalTo
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
equalToJson
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
moreThanOrExactly
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
post
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
postRequestedFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
stubFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
urlEqualTo
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
verify
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
core
.
WireMockConfiguration
.
wireMockConfig
;
public
abstract
class
AbstractClientApplicationTest
{
private
WireMockServer
wiremock
;
public
void
setUp
()
throws
Exception
{
wiremock
=
new
WireMockServer
(
wireMockConfig
().
dynamicPort
());
wiremock
.
start
();
configureFor
(
wiremock
.
port
());
ResponseDefinitionBuilder
response
=
aResponse
().
withStatus
(
HttpStatus
.
SC_CREATED
)
.
withHeader
(
"Content-Type"
,
"application/json"
)
.
withHeader
(
"Location"
,
"http://localhost:"
+
wiremock
.
port
()
+
"/instances/abcdef"
)
.
withBody
(
"{ \"id\" : \"abcdef\" }"
);
stubFor
(
post
(
urlEqualTo
(
"/instances"
)).
willReturn
(
response
));
}
@Test
public
void
test_context
()
throws
InterruptedException
{
Thread
.
sleep
(
1000L
);
String
serviceHost
=
"http://localhost:"
+
getServerPort
();
String
managementHost
=
"http://localhost:"
+
getManagementPort
();
String
body
=
"{ \"name\" : \"Test-Client\","
+
//
" \"managementUrl\" : \""
+
managementHost
+
"/mgmt\","
+
//
" \"healthUrl\" : \""
+
managementHost
+
"/mgmt/health\","
+
//
" \"serviceUrl\" : \""
+
serviceHost
+
"/\", "
+
//
" \"metadata\" : {} }"
;
RequestPatternBuilder
request
=
postRequestedFor
(
urlEqualTo
(
"/instances"
)).
withHeader
(
"Content-Type"
,
equalTo
(
"application/json"
)).
withRequestBody
(
equalToJson
(
body
));
verify
(
moreThanOrExactly
(
1
),
request
);
}
public
void
shutdown
()
{
wiremock
.
stop
();
}
protected
abstract
int
getServerPort
();
protected
abstract
int
getManagementPort
();
protected
int
getWirmockPort
()
{
return
wiremock
.
port
();
}
}
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/client/config/ClientReactiveApplicationTest.java
0 → 100644
View file @
8a497e65
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
de
.
codecentric
.
boot
.
admin
.
client
.
config
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
;
import
org.springframework.context.ConfigurableApplicationContext
;
public
class
ClientReactiveApplicationTest
extends
AbstractClientApplicationTest
{
private
ConfigurableApplicationContext
instance
;
@Override
@Before
public
void
setUp
()
throws
Exception
{
super
.
setUp
();
instance
=
SpringApplication
.
run
(
TestClientApplication
.
class
,
"--spring.main.web-application-type=reactive"
,
"--spring.application.name=Test-Client"
,
"--server.port=0"
,
"--management.context-path=/mgmt"
,
"--endpoints.health.enabled=true"
,
"--spring.boot.admin.client.url=http://localhost:"
+
getWirmockPort
());
}
@Override
@After
public
void
shutdown
()
{
instance
.
close
();
super
.
shutdown
();
}
@Override
protected
int
getServerPort
()
{
return
instance
.
getEnvironment
().
getProperty
(
"local.server.port"
,
Integer
.
class
,
0
);
}
@Override
protected
int
getManagementPort
()
{
return
instance
.
getEnvironment
().
getProperty
(
"local.management.port"
,
Integer
.
class
,
0
);
}
@SpringBootConfiguration
@EnableAutoConfiguration
(
exclude
=
WebMvcAutoConfiguration
.
class
)
public
static
class
TestClientApplication
{
}
}
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/client/config/ClientServletApplicationTest.java
View file @
8a497e65
...
...
@@ -16,73 +16,43 @@
package
de
.
codecentric
.
boot
.
admin
.
client
.
config
;
import
wiremock.org.apache.http.HttpStatus
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.SpringBootConfiguration
;
import
org.springframework.boot.autoconfigure.EnableAutoConfiguration
;
import
org.springframework.context.ConfigurableApplicationContext
;
import
com.github.tomakehurst.wiremock.WireMockServer
;
import
com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder
;
import
com.github.tomakehurst.wiremock.matching.RequestPatternBuilder
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
aResponse
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
configureFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
equalTo
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
equalToJson
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
moreThanOrExactly
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
post
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
postRequestedFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
stubFor
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
urlEqualTo
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
client
.
WireMock
.
verify
;
import
static
com
.
github
.
tomakehurst
.
wiremock
.
core
.
WireMockConfiguration
.
wireMockConfig
;
public
class
ClientServletApplicationTest
{
public
class
ClientServletApplicationTest
extends
AbstractClientApplicationTest
{
private
ConfigurableApplicationContext
instance
;
private
WireMockServer
wiremock
;
@Override
@Before
public
void
setUp
()
throws
Exception
{
wiremock
=
new
WireMockServer
(
wireMockConfig
().
dynamicPort
());
wiremock
.
start
();
configureFor
(
wiremock
.
port
());
ResponseDefinitionBuilder
response
=
aResponse
().
withStatus
(
HttpStatus
.
SC_CREATED
)
.
withHeader
(
"Content-Type"
,
"application/json"
)
.
withHeader
(
"Location"
,
"http://localhost:"
+
wiremock
.
port
()
+
"/instances/abcdef"
)
.
withBody
(
"{ \"id\" : \"abcdef\" }"
);
stubFor
(
post
(
urlEqualTo
(
"/instances"
)).
willReturn
(
response
));
super
.
setUp
();
instance
=
SpringApplication
.
run
(
TestClientApplication
.
class
,
"--spring.main.web-application-type=servlet"
,
"--spring.application.name=Test-Client"
,
"--server.port=0"
,
"--management.context-path=/mgmt"
,
"--endpoints.health.enabled=true"
,
"--spring.boot.admin.client.url=http://localhost:"
+
wiremock
.
port
());
}
@Test
public
void
test_context
()
throws
InterruptedException
{
Thread
.
sleep
(
1000L
);
String
serviceHost
=
"http://localhost:"
+
instance
.
getEnvironment
().
getProperty
(
"local.server.port"
);
String
managementHost
=
"http://localhost:"
+
instance
.
getEnvironment
().
getProperty
(
"local.management.port"
);
String
body
=
"{ \"name\" : \"Test-Client\","
+
//
" \"managementUrl\" : \""
+
managementHost
+
"/mgmt\","
+
//
" \"healthUrl\" : \""
+
managementHost
+
"/mgmt/health\","
+
//
" \"serviceUrl\" : \""
+
serviceHost
+
"/\", "
+
//
" \"metadata\" : {} }"
;
RequestPatternBuilder
request
=
postRequestedFor
(
urlEqualTo
(
"/instances"
)).
withHeader
(
"Content-Type"
,
equalTo
(
"application/json"
)).
withRequestBody
(
equalToJson
(
body
));
verify
(
moreThanOrExactly
(
1
),
request
);
"--spring.boot.admin.client.url=http://localhost:"
+
getWirmockPort
());
}
@Override
@After
public
void
shutdown
()
{
instance
.
close
();
wiremock
.
stop
();
super
.
shutdown
();
}
@Override
protected
int
getServerPort
()
{
return
instance
.
getEnvironment
().
getProperty
(
"local.server.port"
,
Integer
.
class
,
0
);
}
@Override
protected
int
getManagementPort
()
{
return
instance
.
getEnvironment
().
getProperty
(
"local.management.port"
,
Integer
.
class
,
0
);
}
@SpringBootConfiguration
...
...
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/client/registration/DefaultApplicationFactoryTest.java
View file @
8a497e65
...
...
@@ -22,14 +22,13 @@ import java.net.InetAddress;
import
java.net.UnknownHostException
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider
;
import
org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.
context.event.ApplicationReady
Event
;
import
org.springframework.boot.
web.context.WebServerInitialized
Event
;
import
org.springframework.boot.web.server.Ssl
;
import
org.springframework.
mock.env.MockEnvironment
;
import
org.springframework.
web.context.ConfigurableWeb
ApplicationContext
;
import
org.springframework.
boot.web.server.WebServer
;
import
org.springframework.
context.
ApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatThrownBy
;
...
...
@@ -187,16 +186,36 @@ public class DefaultApplicationFactoryTest {
private
void
publishApplicationReadyEvent
(
DefaultApplicationFactory
factory
,
Integer
serverport
,
Integer
managementport
)
{
MockEnvironment
env
=
new
MockEnvironment
(
);
if
(
serverport
!=
null
)
{
env
.
setProperty
(
"local.server.port"
,
serverport
.
toString
(
));
factory
.
onWebServerInitialized
(
new
TestWebServerInitializedEvent
(
"server"
,
serverport
)
);
factory
.
onWebServerInitialized
(
new
TestWebServerInitializedEvent
(
"management"
,
managementport
!=
null
?
managementport
:
serverport
));
}
if
(
managementport
!=
null
)
{
env
.
setProperty
(
"local.management.port"
,
managementport
.
toString
());
private
static
class
TestWebServerInitializedEvent
extends
WebServerInitializedEvent
{
private
final
String
serverId
;
private
final
WebServer
server
;
private
TestWebServerInitializedEvent
(
String
serverId
,
int
port
)
{
super
(
mock
(
WebServer
.
class
));
this
.
serverId
=
serverId
;
this
.
server
=
mock
(
WebServer
.
class
);
when
(
server
.
getPort
()).
thenReturn
(
port
);
}
@Override
public
ApplicationContext
getApplicationContext
()
{
return
null
;
}
ConfigurableWebApplicationContext
context
=
mock
(
ConfigurableWebApplicationContext
.
class
);
when
(
context
.
getEnvironment
()).
thenReturn
(
env
);
factory
.
onApplicationReady
(
new
ApplicationReadyEvent
(
mock
(
SpringApplication
.
class
),
new
String
[]{},
context
))
;
@Override
public
String
getServerId
()
{
return
this
.
serverId
;
}
@Override
public
WebServer
getWebServer
()
{
return
this
.
server
;
}
}
}
spring-boot-admin-starter-client/src/test/java/de/codecentric/boot/admin/client/registration/ServletApplicationFactoryTest.java
View file @
8a497e65
...
...
@@ -22,14 +22,13 @@ import java.net.InetAddress;
import
java.net.UnknownHostException
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.springframework.boot.SpringApplication
;
import
org.springframework.boot.actuate.autoconfigure.endpoint.web.EndpointPathProvider
;
import
org.springframework.boot.actuate.autoconfigure.web.server.ManagementServerProperties
;
import
org.springframework.boot.autoconfigure.web.ServerProperties
;
import
org.springframework.boot.context.event.ApplicationReadyEvent
;
import
org.springframework.mock.env.MockEnvironment
;
import
org.springframework.boot.web.context.WebServerInitializedEvent
;
import
org.springframework.boot.web.server.WebServer
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.mock.web.MockServletContext
;
import
org.springframework.web.context.ConfigurableWebApplicationContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
...
@@ -111,17 +110,35 @@ public class ServletApplicationFactoryTest {
private
void
publishApplicationReadyEvent
(
DefaultApplicationFactory
factory
,
Integer
serverport
,
Integer
managementport
)
{
MockEnvironment
env
=
new
MockEnvironment
(
);
if
(
serverport
!=
null
)
{
env
.
setProperty
(
"local.server.port"
,
serverport
.
toString
(
));
factory
.
onWebServerInitialized
(
new
TestWebServerInitializedEvent
(
"server"
,
serverport
)
);
factory
.
onWebServerInitialized
(
new
TestWebServerInitializedEvent
(
"management"
,
managementport
!=
null
?
managementport
:
serverport
));
}
if
(
managementport
!=
null
)
{
env
.
setProperty
(
"local.management.port"
,
managementport
.
toString
());
private
static
class
TestWebServerInitializedEvent
extends
WebServerInitializedEvent
{
private
final
String
serverId
;
private
final
WebServer
server
;
private
TestWebServerInitializedEvent
(
String
serverId
,
int
port
)
{
super
(
mock
(
WebServer
.
class
));
this
.
serverId
=
serverId
;
this
.
server
=
mock
(
WebServer
.
class
);
when
(
server
.
getPort
()).
thenReturn
(
port
);
}
@Override
public
ApplicationContext
getApplicationContext
()
{
return
null
;
}
ConfigurableWebApplicationContext
context
=
mock
(
ConfigurableWebApplicationContext
.
class
);
when
(
context
.
getEnvironment
()).
thenReturn
(
env
);
factory
.
onApplicationReady
(
new
ApplicationReadyEvent
(
mock
(
SpringApplication
.
class
),
new
String
[]{},
context
))
;
@Override
public
String
getServerId
()
{
return
this
.
serverId
;
}
@Override
public
WebServer
getWebServer
()
{
return
this
.
server
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment