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
9a6d52e3
Commit
9a6d52e3
authored
Oct 02, 2020
by
Madhura Bhave
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '2.2.x' into 2.3.x
parents
121ba2b0
3118ca93
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
64 deletions
+47
-64
H2ConsoleAutoConfigurationTests.java
...oot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
+47
-64
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
View file @
9a6d52e3
...
@@ -17,27 +17,22 @@
...
@@ -17,27 +17,22 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
h2
;
package
org
.
springframework
.
boot
.
autoconfigure
.
h2
;
import
java.sql.Connection
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
import
javax.sql.DataSource
;
import
javax.sql.DataSource
;
import
org.junit.jupiter.api.AfterEach
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.Test
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.junit.jupiter.api.extension.ExtendWith
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
;
import
org.springframework.boot.test.context.runner.WebApplicationContextRunner
;
import
org.springframework.boot.test.system.CapturedOutput
;
import
org.springframework.boot.test.system.CapturedOutput
;
import
org.springframework.boot.test.system.OutputCaptureExtension
;
import
org.springframework.boot.test.system.OutputCaptureExtension
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.boot.web.servlet.ServletRegistrationBean
;
import
org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext
;
import
org.springframework.mock.web.MockServletContext
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThatExceptionOfType
;
/**
/**
* Tests for {@link H2ConsoleAutoConfiguration}
* Tests for {@link H2ConsoleAutoConfiguration}
...
@@ -48,90 +43,78 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
...
@@ -48,90 +43,78 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
*/
class
H2ConsoleAutoConfigurationTests
{
class
H2ConsoleAutoConfigurationTests
{
private
AnnotationConfigServletWebApplicationContext
context
=
new
AnnotationConfigServletWebApplicationContext
();
private
final
WebApplicationContextRunner
contextRunner
=
new
WebApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
H2ConsoleAutoConfiguration
.
class
));
@BeforeEach
void
setupContext
()
{
this
.
context
.
setServletContext
(
new
MockServletContext
());
}
@AfterEach
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
@Test
void
consoleIsDisabledByDefault
()
{
void
consoleIsDisabledByDefault
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
doesNotHaveBean
(
ServletRegistrationBean
.
class
));
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
isEmpty
();
}
}
@Test
@Test
void
propertyCanEnableConsole
()
{
void
propertyCanEnableConsole
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
context
Runner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
).
run
((
context
)
->
{
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
).
applyTo
(
this
.
context
);
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
this
.
context
.
refresh
(
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
ServletRegistrationBean
<?>
registrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"trace"
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*
"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAllowOthers
"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"trace
"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAdminPassword
"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAllowOthers"
);
}
);
}
}
@Test
@Test
void
customPathMustBeginWithASlash
()
{
void
customPathMustBeginWithASlash
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=custom"
)
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:custom"
).
applyTo
(
this
.
context
);
.
run
((
context
)
->
{
assertThatExceptionOfType
(
BeanCreationException
.
class
).
isThrownBy
(
this
.
context
::
refresh
)
assertThat
(
context
).
hasFailed
();
.
withMessageContaining
(
"Failed to bind properties under 'spring.h2.console'"
);
assertThat
(
context
.
getStartupFailure
()).
isInstanceOf
(
BeanCreationException
.
class
)
.
hasMessageContaining
(
"Failed to bind properties under 'spring.h2.console'"
);
});
}
}
@Test
@Test
void
customPathWithTrailingSlash
()
{
void
customPathWithTrailingSlash
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=/custom/"
)
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:/custom/"
)
.
run
((
context
)
->
{
.
applyTo
(
this
.
context
);
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
this
.
context
.
refresh
();
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
ServletRegistrationBean
<?>
servletRegistrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
});
assertThat
(
servletRegistrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
}
}
@Test
@Test
void
customPath
()
{
void
customPath
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
context
Runner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=/custom"
)
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:/custom"
).
applyTo
(
this
.
context
);
.
run
((
context
)
->
{
this
.
context
.
refresh
(
);
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
ServletRegistrationBean
<?>
servletRegistrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
assertThat
(
servletRegistrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
}
);
}
}
@Test
@Test
void
customInitParameters
()
{
void
customInitParameters
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.settings.trace=true"
,
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.settings.trace=true"
,
"spring.h2.console.settings.web-allow-others=true"
).
run
((
context
)
->
{
"spring.h2.console.settings.webAllowOthers=true"
).
applyTo
(
this
.
context
);
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
this
.
context
.
refresh
();
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
ServletRegistrationBean
<?>
registrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"trace"
,
""
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAllowOthers"
,
""
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"trace"
,
""
);
});
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAllowOthers"
,
""
);
}
}
@Test
@Test
@ExtendWith
(
OutputCaptureExtension
.
class
)
@ExtendWith
(
OutputCaptureExtension
.
class
)
void
dataSourceUrlIsLoggedWhenAvailable
(
CapturedOutput
output
)
throws
BeansException
,
SQLException
{
void
dataSourceUrlIsLoggedWhenAvailable
(
CapturedOutput
output
)
throws
BeansException
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
H2ConsoleAutoConfiguration
.
class
);
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
DataSourceAutoConfiguration
.
class
))
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
).
applyTo
(
this
.
context
);
.
withPropertyValues
(
"spring.h2.console.enabled=true"
).
run
((
context
)
->
{
this
.
context
.
refresh
();
try
(
Connection
connection
=
context
.
getBean
(
DataSource
.
class
).
getConnection
())
{
try
(
Connection
connection
=
this
.
context
.
getBean
(
DataSource
.
class
).
getConnection
())
{
assertThat
(
output
)
assertThat
(
output
).
contains
(
"Database available at '"
+
connection
.
getMetaData
().
getURL
()
+
"'"
);
.
contains
(
"Database available at '"
+
connection
.
getMetaData
().
getURL
()
+
"'"
);
}
}
});
}
}
}
}
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