Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
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
DEMO
spring-boot
Commits
fc0a687e
Commit
fc0a687e
authored
Jul 20, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate test to ApplicationContextRunner
parent
660d284f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
64 deletions
+62
-64
ServletWebServerFactoryAutoConfigurationTests.java
...ervlet/ServletWebServerFactoryAutoConfigurationTests.java
+62
-64
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfigurationTests.java
View file @
fc0a687e
/*
/*
* Copyright 2012-201
7
the original author or authors.
* Copyright 2012-201
8
the original author or authors.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* you may not use this file except in compliance with the License.
...
@@ -25,16 +25,19 @@ import org.junit.Test;
...
@@ -25,16 +25,19 @@ import org.junit.Test;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.beans.factory.config.BeanPostProcessor
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.autoconfigure.condition.ConditionalOnExpression
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.test.context.assertj.AssertableWebApplicationContext
;
import
org.springframework.boot.test.context.runner.ContextConsumer
;
import
org.springframework.boot.test.context.runner.WebApplicationContextRunner
;
import
org.springframework.boot.web.server.WebServerFactoryCustomizer
;
import
org.springframework.boot.web.server.WebServerFactoryCustomizer
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
;
import
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
;
import
org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
;
import
org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory
;
import
org.springframework.boot.web.servlet.server.ServletWebServerFactory
;
import
org.springframework.boot.web.servlet.server.ServletWebServerFactory
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.servlet.DispatcherServlet
;
import
org.springframework.web.servlet.DispatcherServlet
;
import
org.springframework.web.servlet.FrameworkServlet
;
import
org.springframework.web.servlet.FrameworkServlet
;
...
@@ -47,112 +50,107 @@ import static org.mockito.Mockito.verify;
...
@@ -47,112 +50,107 @@ import static org.mockito.Mockito.verify;
*
*
* @author Dave Syer
* @author Dave Syer
* @author Phillip Webb
* @author Phillip Webb
* @author Stephane Nicoll
*/
*/
public
class
ServletWebServerFactoryAutoConfigurationTests
{
public
class
ServletWebServerFactoryAutoConfigurationTests
{
private
AnnotationConfigServletWebServerApplicationContext
context
;
private
WebApplicationContextRunner
contextRunner
=
new
WebApplicationContextRunner
(
AnnotationConfigServletWebServerApplicationContext:
:
new
).
withConfiguration
(
AutoConfigurations
.
of
(
ServletWebServerFactoryAutoConfiguration
.
class
,
DispatcherServletAutoConfiguration
.
class
))
.
withUserConfiguration
(
WebServerConfiguration
.
class
);
@Test
@Test
public
void
createFromConfigClass
()
{
public
void
createFromConfigClass
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
.
run
(
verifyContext
());
BaseConfiguration
.
class
);
verifyContext
();
}
}
@Test
@Test
public
void
contextAlreadyHasDispatcherServletWithDefaultName
()
{
public
void
contextAlreadyHasDispatcherServletWithDefaultName
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
.
withUserConfiguration
(
DispatcherServletConfiguration
.
class
)
DispatcherServletConfiguration
.
class
,
BaseConfiguration
.
class
);
.
run
(
verifyContext
());
verifyContext
();
}
}
@Test
@Test
public
void
contextAlreadyHasDispatcherServlet
()
{
public
void
contextAlreadyHasDispatcherServlet
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
.
withUserConfiguration
(
SpringServletConfiguration
.
class
)
SpringServletConfiguration
.
class
,
BaseConfiguration
.
class
);
.
run
((
context
)
->
{
verifyContext
();
verifyContext
(
context
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
)
assertThat
(
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
))
.
isEqualTo
(
2
);
.
hasSize
(
2
);
});
}
}
@Test
@Test
public
void
contextAlreadyHasNonDispatcherServlet
()
{
public
void
contextAlreadyHasNonDispatcherServlet
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
context
Runner
.
withUserConfiguration
(
NonSpringServletConfiguration
.
class
)
NonSpringServletConfiguration
.
class
,
BaseConfiguration
.
class
);
.
run
((
context
)
->
{
verifyContext
(
);
// the non default servlet is still registered
verifyContext
(
context
);
// the non default servlet is still registered
assertThat
(
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
)
assertThat
(
context
).
doesNotHaveBean
(
DispatcherServlet
.
class
);
.
isEqualTo
(
0
);
}
);
}
}
@Test
@Test
public
void
contextAlreadyHasNonServlet
()
{
public
void
contextAlreadyHasNonServlet
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
context
Runner
.
withUserConfiguration
(
NonServletConfiguration
.
class
)
NonServletConfiguration
.
class
,
BaseConfiguration
.
class
);
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
)
assertThat
(
context
).
doesNotHaveBean
(
DispatcherServlet
.
class
);
.
isEqualTo
(
0
);
assertThat
(
context
).
doesNotHaveBean
(
Servlet
.
class
);
assertThat
(
this
.
context
.
getBeanNamesForType
(
Servlet
.
class
).
length
).
isEqualTo
(
0
);
}
);
}
}
@Test
@Test
public
void
contextAlreadyHasDispatcherServletAndRegistration
()
{
public
void
contextAlreadyHasDispatcherServletAndRegistration
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
DispatcherServletWithRegistrationConfiguration
.
class
,
.
withUserConfiguration
(
BaseConfiguration
.
class
);
DispatcherServletWithRegistrationConfiguration
.
class
)
verifyContext
();
.
run
((
context
)
->
{
assertThat
(
this
.
context
.
getBeanNamesForType
(
DispatcherServlet
.
class
).
length
)
verifyContext
(
context
);
.
isEqualTo
(
1
);
assertThat
(
context
).
hasSingleBean
(
DispatcherServlet
.
class
);
});
}
}
@Test
@Test
public
void
webServerHasNoServletContext
()
{
public
void
webServerHasNoServletContext
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
.
withUserConfiguration
(
EnsureWebServerHasNoServletContext
.
class
)
EnsureWebServerHasNoServletContext
.
class
,
BaseConfiguration
.
class
);
.
run
(
verifyContext
());
verifyContext
();
}
}
@Test
@Test
public
void
customizeWebServerFactoryThroughCallback
()
{
public
void
customizeWebServerFactoryThroughCallback
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
(
this
.
contextRunner
CallbackEmbeddedServerFactoryCustomizer
.
class
,
BaseConfiguration
.
class
);
.
withUserConfiguration
(
CallbackEmbeddedServerFactoryCustomizer
.
class
)
verifyContext
();
.
run
((
context
)
->
{
assertThat
(
getWebServerFactory
().
getPort
()).
isEqualTo
(
9000
);
verifyContext
(
context
);
assertThat
(
context
.
getBean
(
MockServletWebServerFactory
.
class
).
getPort
())
.
isEqualTo
(
9000
);
});
}
}
@Test
@Test
public
void
initParametersAreConfiguredOnTheServletContext
()
{
public
void
initParametersAreConfiguredOnTheServletContext
()
{
this
.
context
=
new
AnnotationConfigServletWebServerApplicationContext
();
this
.
contextRunner
.
withPropertyValues
(
"server.servlet.context-parameters.a:alpha"
,
TestPropertyValues
"server.servlet.context-parameters.b:bravo"
).
run
((
context
)
->
{
.
of
(
"server.servlet.context-parameters.a:alpha"
,
ServletContext
servletContext
=
context
.
getServletContext
();
"server.servlet.context-parameters.b:bravo"
)
assertThat
(
servletContext
.
getInitParameter
(
"a"
)).
isEqualTo
(
"alpha"
);
.
applyTo
(
this
.
context
);
assertThat
(
servletContext
.
getInitParameter
(
"b"
)).
isEqualTo
(
"bravo"
);
this
.
context
.
register
(
BaseConfiguration
.
class
);
});
this
.
context
.
refresh
();
ServletContext
servletContext
=
this
.
context
.
getServletContext
();
assertThat
(
servletContext
.
getInitParameter
(
"a"
)).
isEqualTo
(
"alpha"
);
assertThat
(
servletContext
.
getInitParameter
(
"b"
)).
isEqualTo
(
"bravo"
);
}
}
private
void
verifyContext
()
{
private
ContextConsumer
<
AssertableWebApplicationContext
>
verifyContext
()
{
MockServletWebServerFactory
factory
=
getWebServerFactory
();
return
this
::
verifyContext
;
Servlet
servlet
=
this
.
context
.
getBean
(
}
private
void
verifyContext
(
ApplicationContext
context
)
{
MockServletWebServerFactory
factory
=
context
.
getBean
(
MockServletWebServerFactory
.
class
);
Servlet
servlet
=
context
.
getBean
(
DispatcherServletAutoConfiguration
.
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
,
DispatcherServletAutoConfiguration
.
DEFAULT_DISPATCHER_SERVLET_BEAN_NAME
,
Servlet
.
class
);
Servlet
.
class
);
verify
(
factory
.
getServletContext
()).
addServlet
(
"dispatcherServlet"
,
servlet
);
verify
(
factory
.
getServletContext
()).
addServlet
(
"dispatcherServlet"
,
servlet
);
}
}
private
MockServletWebServerFactory
getWebServerFactory
()
{
return
this
.
context
.
getBean
(
MockServletWebServerFactory
.
class
);
}
@Configuration
@Import
({
WebServerConfiguration
.
class
,
ServletWebServerFactoryAutoConfiguration
.
class
,
DispatcherServletAutoConfiguration
.
class
})
protected
static
class
BaseConfiguration
{
}
@Configuration
@Configuration
@ConditionalOnExpression
(
"true"
)
@ConditionalOnExpression
(
"true"
)
public
static
class
WebServerConfiguration
{
public
static
class
WebServerConfiguration
{
...
...
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