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
9e290213
Commit
9e290213
authored
Jun 15, 2020
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish
parent
ac0f175c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
76 deletions
+56
-76
H2ConsoleAutoConfigurationTests.java
...oot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
+49
-67
H2ConsolePropertiesTests.java
...ework/boot/autoconfigure/h2/H2ConsolePropertiesTests.java
+7
-9
No files found.
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsoleAutoConfigurationTests.java
View file @
9e290213
...
...
@@ -17,27 +17,22 @@
package
org
.
springframework
.
boot
.
autoconfigure
.
h2
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
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.extension.ExtendWith
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.BeanCreationException
;
import
org.springframework.boot.autoconfigure.AutoConfigurations
;
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.OutputCaptureExtension
;
import
org.springframework.boot.test.util.TestPropertyValues
;
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
.
assertThatExceptionOfType
;
/**
* Tests for {@link H2ConsoleAutoConfiguration}
...
...
@@ -48,93 +43,80 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
class
H2ConsoleAutoConfigurationTests
{
private
AnnotationConfigServletWebApplicationContext
context
=
new
AnnotationConfigServletWebApplicationContext
();
@BeforeEach
void
setupContext
()
{
this
.
context
.
setServletContext
(
new
MockServletContext
());
}
@AfterEach
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
private
final
WebApplicationContextRunner
contextRunner
=
new
WebApplicationContextRunner
()
.
withConfiguration
(
AutoConfigurations
.
of
(
H2ConsoleAutoConfiguration
.
class
));
@Test
void
consoleIsDisabledByDefault
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
isEmpty
();
this
.
contextRunner
.
run
((
context
)
->
assertThat
(
context
).
doesNotHaveBean
(
ServletRegistrationBean
.
class
));
}
@Test
void
propertyCanEnableConsole
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
).
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
ServletRegistrationBean
<?>
registrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"trace"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAllowOthers"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAdminPassword"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"trace"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAllowOthers"
);
assertThat
(
registrationBean
.
getInitParameters
()).
doesNotContainKey
(
"webAdminPassword"
);
});
}
@Test
void
customPathMustBeginWithASlash
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:custom"
).
applyTo
(
this
.
context
);
assertThatExceptionOfType
(
BeanCreationException
.
class
).
isThrownBy
(
this
.
context
::
refresh
)
.
withMessageContaining
(
"Failed to bind properties under 'spring.h2.console'"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=custom"
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasFailed
();
assertThat
(
context
.
getStartupFailure
()).
isInstanceOf
(
BeanCreationException
.
class
)
.
hasMessageContaining
(
"Failed to bind properties under 'spring.h2.console'"
);
});
}
@Test
void
customPathWithTrailingSlash
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:/custom/"
)
.
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
ServletRegistrationBean
<?>
servletRegistrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
servletRegistrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=/custom/"
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
});
}
@Test
void
customPath
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.path:/custom"
).
applyTo
(
this
.
context
);
this
.
context
.
refresh
(
);
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
ServletRegistrationBean
<?>
servletRegistrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
servletRegistrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
this
.
context
Runner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.path=/custom"
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/custom/*"
);
}
);
}
@Test
void
customInitParameters
()
{
this
.
context
.
register
(
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
,
"spring.h2.console.settings.trace=true"
,
"spring.h2.console.settings.webAllowOthers=true"
,
"spring.h2.console.settings.webAdminPassword=abcd"
)
.
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
assertThat
(
this
.
context
.
getBeansOfType
(
ServletRegistrationBean
.
class
)).
hasSize
(
1
);
ServletRegistrationBean
<?>
registrationBean
=
this
.
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"trace"
,
""
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAllowOthers"
,
""
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAdminPassword"
,
"abcd"
);
this
.
contextRunner
.
withPropertyValues
(
"spring.h2.console.enabled=true"
,
"spring.h2.console.settings.trace=true"
,
"spring.h2.console.settings.web-allow-others=true"
,
"spring.h2.console.settings.web-admin-password=abcd"
).
run
((
context
)
->
{
assertThat
(
context
).
hasSingleBean
(
ServletRegistrationBean
.
class
);
ServletRegistrationBean
<?>
registrationBean
=
context
.
getBean
(
ServletRegistrationBean
.
class
);
assertThat
(
registrationBean
.
getUrlMappings
()).
contains
(
"/h2-console/*"
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"trace"
,
""
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAllowOthers"
,
""
);
assertThat
(
registrationBean
.
getInitParameters
()).
containsEntry
(
"webAdminPassword"
,
"abcd"
);
});
}
@Test
@ExtendWith
(
OutputCaptureExtension
.
class
)
void
dataSourceUrlIsLoggedWhenAvailable
(
CapturedOutput
output
)
throws
BeansException
,
SQLException
{
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
H2ConsoleAutoConfiguration
.
class
);
TestPropertyValues
.
of
(
"spring.h2.console.enabled:true"
).
applyTo
(
this
.
context
);
this
.
context
.
refresh
();
try
(
Connection
connection
=
this
.
context
.
getBean
(
DataSource
.
class
).
getConnection
())
{
assertThat
(
output
).
contains
(
"Database available at '"
+
connection
.
getMetaData
().
getURL
()
+
"'"
);
}
void
dataSourceUrlIsLoggedWhenAvailable
(
CapturedOutput
output
)
throws
BeansException
{
this
.
contextRunner
.
withConfiguration
(
AutoConfigurations
.
of
(
DataSourceAutoConfiguration
.
class
))
.
withPropertyValues
(
"spring.h2.console.enabled=true"
).
run
((
context
)
->
{
try
(
Connection
connection
=
context
.
getBean
(
DataSource
.
class
).
getConnection
())
{
assertThat
(
output
)
.
contains
(
"Database available at '"
+
connection
.
getMetaData
().
getURL
()
+
"'"
);
}
});
}
}
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/h2/H2ConsolePropertiesTests.java
View file @
9e290213
/*
* Copyright 2012-20
19
the original author or authors.
* Copyright 2012-20
20
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.
...
...
@@ -27,26 +27,24 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
class
H2ConsolePropertiesTests
{
private
H2ConsoleProperties
properties
;
@Test
void
pathMustNotBeEmpty
()
{
this
.
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
properties
.
setPath
(
""
))
H2ConsoleProperties
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
properties
.
setPath
(
""
))
.
withMessageContaining
(
"Path must have length greater than 1"
);
}
@Test
void
pathMustHaveLengthGreaterThanOne
()
{
this
.
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
properties
.
setPath
(
"/"
))
H2ConsoleProperties
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
properties
.
setPath
(
"/"
))
.
withMessageContaining
(
"Path must have length greater than 1"
);
}
@Test
void
customPathMustBeginWithASlash
()
{
this
.
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
this
.
properties
.
setPath
(
"custom"
))
H2ConsoleProperties
properties
=
new
H2ConsoleProperties
();
assertThatIllegalArgumentException
().
isThrownBy
(()
->
properties
.
setPath
(
"custom"
))
.
withMessageContaining
(
"Path must start with '/'"
);
}
...
...
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