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
86b96254
Commit
86b96254
authored
Apr 10, 2018
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate to ApplicationContextRunner
parent
d8b2a17e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
84 deletions
+97
-84
ControllerEndpointDiscovererTests.java
...int/web/annotation/ControllerEndpointDiscovererTests.java
+44
-39
ServletEndpointDiscovererTests.java
...dpoint/web/annotation/ServletEndpointDiscovererTests.java
+53
-45
No files found.
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/annotation/ControllerEndpointDiscovererTests.java
View file @
86b96254
...
...
@@ -31,7 +31,9 @@ import org.springframework.boot.actuate.endpoint.annotation.DiscoveredEndpoint;
import
org.springframework.boot.actuate.endpoint.annotation.Endpoint
;
import
org.springframework.boot.actuate.endpoint.annotation.ReadOperation
;
import
org.springframework.boot.actuate.endpoint.web.PathMapper
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.context.runner.ContextConsumer
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
...
...
@@ -41,74 +43,77 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ControllerEndpointDiscoverer}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public
class
ControllerEndpointDiscovererTests
{
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
();
@Test
public
void
getEndpointsWhenNoEndpointBeansShouldReturnEmptyCollection
()
{
load
(
EmptyConfiguration
.
class
,
(
discoverer
)
->
assertThat
(
discoverer
.
getEndpoints
()).
isEmpty
());
this
.
contextRunner
.
withUserConfiguration
(
EmptyConfiguration
.
class
).
run
(
assertDiscoverer
((
discoverer
)
->
assertThat
(
discoverer
.
getEndpoints
()).
isEmpty
()));
}
@Test
public
void
getEndpointsShouldIncludeControllerEndpoints
()
{
load
(
TestControllerEndpoint
.
class
,
(
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableControllerEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testcontroller"
);
assertThat
(
endpoint
.
getController
())
.
isInstanceOf
(
TestControllerEndpoint
.
class
);
assertThat
(
endpoint
).
isInstanceOf
(
DiscoveredEndpoint
.
class
);
});
this
.
contextRunner
.
withUserConfiguration
(
TestControllerEndpoint
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableControllerEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testcontroller"
);
assertThat
(
endpoint
.
getController
())
.
isInstanceOf
(
TestControllerEndpoint
.
class
);
assertThat
(
endpoint
).
isInstanceOf
(
DiscoveredEndpoint
.
class
);
}));
}
@Test
public
void
getEndpointsShouldIncludeRestControllerEndpoints
()
{
load
(
TestRestControllerEndpoint
.
class
,
(
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableControllerEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testrestcontroller"
);
assertThat
(
endpoint
.
getController
())
.
isInstanceOf
(
TestRestControllerEndpoint
.
class
);
});
this
.
contextRunner
.
withUserConfiguration
(
TestRestControllerEndpoint
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableControllerEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testrestcontroller"
);
assertThat
(
endpoint
.
getController
())
.
isInstanceOf
(
TestRestControllerEndpoint
.
class
);
}));
}
@Test
public
void
getEndpointsShouldNotDiscoverRegularEndpoints
()
{
load
(
WithRegularEndpointConfiguration
.
class
,
(
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
List
<
String
>
ids
=
endpoints
.
stream
().
map
(
ExposableEndpoint:
:
getId
)
.
collect
(
Collectors
.
toList
());
assertThat
(
ids
).
containsOnly
(
"testcontroller"
,
"testrestcontroller"
);
});
this
.
contextRunner
.
withUserConfiguration
(
WithRegularEndpointConfiguration
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
Collection
<
ExposableControllerEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
List
<
String
>
ids
=
endpoints
.
stream
().
map
(
ExposableEndpoint:
:
getId
)
.
collect
(
Collectors
.
toList
());
assertThat
(
ids
).
containsOnly
(
"testcontroller"
,
"testrestcontroller"
);
}));
}
@Test
public
void
getEndpointWhenEndpointHasOperationsShouldThrowException
()
{
load
(
TestControllerWithOperation
.
class
,
(
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"ControllerEndpoints must not declare operations"
);
discoverer
.
getEndpoints
();
});
this
.
contextRunner
.
withUserConfiguration
(
TestControllerWithOperation
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"ControllerEndpoints must not declare operations"
);
discoverer
.
getEndpoints
();
}));
}
private
void
load
(
Class
<?>
configuration
,
private
ContextConsumer
<
AssertableApplicationContext
>
assertDiscoverer
(
Consumer
<
ControllerEndpointDiscoverer
>
consumer
)
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
configuration
);
try
{
return
(
context
)
->
{
ControllerEndpointDiscoverer
discoverer
=
new
ControllerEndpointDiscoverer
(
context
,
PathMapper
.
useEndpointId
(),
Collections
.
emptyList
());
consumer
.
accept
(
discoverer
);
}
finally
{
context
.
close
();
}
};
}
@Configuration
...
...
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/annotation/ServletEndpointDiscovererTests.java
View file @
86b96254
...
...
@@ -40,7 +40,9 @@ import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import
org.springframework.boot.actuate.endpoint.web.EndpointServlet
;
import
org.springframework.boot.actuate.endpoint.web.ExposableServletEndpoint
;
import
org.springframework.boot.actuate.endpoint.web.PathMapper
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.boot.test.context.assertj.AssertableApplicationContext
;
import
org.springframework.boot.test.context.runner.ApplicationContextRunner
;
import
org.springframework.boot.test.context.runner.ContextConsumer
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
...
...
@@ -50,88 +52,94 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link ServletEndpointDiscoverer}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
public
class
ServletEndpointDiscovererTests
{
@Rule
public
final
ExpectedException
thrown
=
ExpectedException
.
none
();
private
final
ApplicationContextRunner
contextRunner
=
new
ApplicationContextRunner
();
@Test
public
void
getEndpointsWhenNoEndpointBeansShouldReturnEmptyCollection
()
{
load
(
EmptyConfiguration
.
class
,
(
discoverer
)
->
assertThat
(
discoverer
.
getEndpoints
()).
isEmpty
());
this
.
contextRunner
.
withUserConfiguration
(
EmptyConfiguration
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
assertThat
(
discoverer
.
getEndpoints
()).
isEmpty
()));
}
@Test
public
void
getEndpointsShouldIncludeServletEndpoints
()
{
load
(
TestServletEndpoint
.
class
,
(
discoverer
)
->
{
Collection
<
ExposableServletEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableServletEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testservlet"
);
assertThat
(
endpoint
.
getEndpointServlet
()).
isNotNull
();
assertThat
(
endpoint
).
isInstanceOf
(
DiscoveredEndpoint
.
class
);
});
this
.
contextRunner
.
withUserConfiguration
(
TestServletEndpoint
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
Collection
<
ExposableServletEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
assertThat
(
endpoints
).
hasSize
(
1
);
ExposableServletEndpoint
endpoint
=
endpoints
.
iterator
().
next
();
assertThat
(
endpoint
.
getId
()).
isEqualTo
(
"testservlet"
);
assertThat
(
endpoint
.
getEndpointServlet
()).
isNotNull
();
assertThat
(
endpoint
).
isInstanceOf
(
DiscoveredEndpoint
.
class
);
}));
}
@Test
public
void
getEndpointsShouldNotDiscoverRegularEndpoints
()
{
load
(
WithRegularEndpointConfiguration
.
class
,
(
discoverer
)
->
{
Collection
<
ExposableServletEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
List
<
String
>
ids
=
endpoints
.
stream
().
map
(
ExposableEndpoint:
:
getId
)
.
collect
(
Collectors
.
toList
());
assertThat
(
ids
).
containsOnly
(
"testservlet"
);
});
this
.
contextRunner
.
withUserConfiguration
(
WithRegularEndpointConfiguration
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
Collection
<
ExposableServletEndpoint
>
endpoints
=
discoverer
.
getEndpoints
();
List
<
String
>
ids
=
endpoints
.
stream
().
map
(
ExposableEndpoint:
:
getId
)
.
collect
(
Collectors
.
toList
());
assertThat
(
ids
).
containsOnly
(
"testservlet"
);
}));
}
@Test
public
void
getEndpointWhenEndpointHasOperationsShouldThrowException
()
{
load
(
TestServletEndpointWithOperation
.
class
,
(
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"ServletEndpoints must not declare operations"
);
discoverer
.
getEndpoints
();
});
this
.
contextRunner
.
withUserConfiguration
(
TestServletEndpointWithOperation
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"ServletEndpoints must not declare operations"
);
discoverer
.
getEndpoints
();
}));
}
@Test
public
void
getEndpointWhenEndpointNotASupplierShouldThrowException
()
{
load
(
TestServletEndpointNotASupplier
.
class
,
(
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must be a supplier"
);
discoverer
.
getEndpoints
();
});
this
.
contextRunner
.
withUserConfiguration
(
TestServletEndpointNotASupplier
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must be a supplier"
);
discoverer
.
getEndpoints
();
}));
}
@Test
public
void
getEndpointWhenEndpointSuppliesWrongTypeShouldThrowException
()
{
load
(
TestServletEndpointSupplierOfWrongType
.
class
,
(
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must supply an EndpointServlet"
);
discoverer
.
getEndpoints
();
});
this
.
contextRunner
.
withUserConfiguration
(
TestServletEndpointSupplierOfWrongType
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must supply an EndpointServlet"
);
discoverer
.
getEndpoints
();
}));
}
@Test
public
void
getEndpointWhenEndpointSuppliesNullShouldThrowException
()
{
load
(
TestServletEndpointSupplierOfNull
.
class
,
(
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must not supply null"
);
discoverer
.
getEndpoints
();
});
this
.
contextRunner
.
withUserConfiguration
(
TestServletEndpointSupplierOfNull
.
class
)
.
run
(
assertDiscoverer
((
discoverer
)
->
{
this
.
thrown
.
expect
(
IllegalStateException
.
class
);
this
.
thrown
.
expectMessage
(
"must not supply null"
);
discoverer
.
getEndpoints
();
}));
}
private
void
load
(
Class
<?>
configuration
,
private
ContextConsumer
<
AssertableApplicationContext
>
assertDiscoverer
(
Consumer
<
ServletEndpointDiscoverer
>
consumer
)
{
AnnotationConfigApplicationContext
context
=
new
AnnotationConfigApplicationContext
(
configuration
);
try
{
return
(
context
)
->
{
ServletEndpointDiscoverer
discoverer
=
new
ServletEndpointDiscoverer
(
context
,
PathMapper
.
useEndpointId
(),
Collections
.
emptyList
());
consumer
.
accept
(
discoverer
);
}
finally
{
context
.
close
();
}
};
}
@Configuration
...
...
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