ServletRegistrationBeanTests Class — spring-boot Architecture
Architecture documentation for the ServletRegistrationBeanTests class in ServletRegistrationBeanTests.java from the spring-boot codebase.
Entity Profile
Relationship Graph
Source Code
core/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletRegistrationBeanTests.java lines 49–202
@ExtendWith(MockitoExtension.class)
class ServletRegistrationBeanTests {
private final MockServlet servlet = new MockServlet();
@Mock
@SuppressWarnings("NullAway.Init")
private ServletContext servletContext;
@Mock
@SuppressWarnings("NullAway.Init")
private ServletRegistration.Dynamic registration;
@Test
void startupWithDefaults() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
bean.onStartup(this.servletContext);
then(this.servletContext).should().addServlet("mockServlet", this.servlet);
then(this.registration).should().setAsyncSupported(true);
then(this.registration).should().addMapping("/*");
}
@Test
void failsWithDoubleRegistration() {
assertThatIllegalStateException().isThrownBy(this::doubleRegistration)
.withMessage("Failed to register 'servlet double-registration' on "
+ "the servlet context. Possibly already registered?");
}
private void doubleRegistration() throws ServletException {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
bean.setName("double-registration");
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(null);
bean.onStartup(this.servletContext);
}
@Test
void startupWithSpecifiedValues() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>();
bean.setName("test");
bean.setServlet(this.servlet);
bean.setAsyncSupported(false);
bean.setInitParameters(Collections.singletonMap("a", "b"));
bean.addInitParameter("c", "d");
bean.setUrlMappings(new LinkedHashSet<>(Arrays.asList("/a", "/b")));
bean.addUrlMappings("/c");
bean.setLoadOnStartup(10);
bean.onStartup(this.servletContext);
then(this.servletContext).should().addServlet("test", this.servlet);
then(this.registration).should().setAsyncSupported(false);
Map<String, String> expectedInitParameters = new HashMap<>();
expectedInitParameters.put("a", "b");
expectedInitParameters.put("c", "d");
then(this.registration).should().setInitParameters(expectedInitParameters);
then(this.registration).should().addMapping("/a", "/b", "/c");
then(this.registration).should().setLoadOnStartup(10);
}
@Test
void specificName() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>();
bean.setName("specificName");
bean.setServlet(this.servlet);
bean.onStartup(this.servletContext);
then(this.servletContext).should().addServlet("specificName", this.servlet);
}
@Test
void deducedName() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>();
bean.setServlet(this.servlet);
bean.onStartup(this.servletContext);
then(this.servletContext).should().addServlet("mockServlet", this.servlet);
}
@Test
void disable() throws Exception {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>();
bean.setServlet(this.servlet);
bean.setEnabled(false);
bean.onStartup(this.servletContext);
then(this.servletContext).should(never()).addServlet("mockServlet", this.servlet);
}
@Test
void setServletMustNotBeNull() {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>();
assertThatIllegalStateException().isThrownBy(() -> bean.onStartup(this.servletContext))
.withMessageContaining("Unable to return description for null servlet");
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createServletMustNotBeNull() {
assertThatIllegalArgumentException().isThrownBy(() -> new ServletRegistrationBean<MockServlet>(null))
.withMessageContaining("'servlet' must not be null");
}
@Test
@SuppressWarnings("NullAway") // Test null check
void setMappingMustNotBeNull() {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
assertThatIllegalArgumentException().isThrownBy(() -> bean.setUrlMappings(null))
.withMessageContaining("'urlMappings' must not be null");
}
@Test
@SuppressWarnings("NullAway") // Test null check
void createMappingMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> new ServletRegistrationBean<>(this.servlet, (String[]) null))
.withMessageContaining("'urlMappings' must not be null");
}
@Test
@SuppressWarnings("NullAway") // Test null check
void addMappingMustNotBeNull() {
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet);
assertThatIllegalArgumentException().isThrownBy(() -> bean.addUrlMappings((String[]) null))
.withMessageContaining("'urlMappings' must not be null");
}
@Test
void setMappingReplacesValue() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet, "/a", "/b");
bean.setUrlMappings(new LinkedHashSet<>(Arrays.asList("/c", "/d")));
bean.onStartup(this.servletContext);
then(this.registration).should().addMapping("/c", "/d");
}
@Test
void modifyInitParameters() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet, "/a", "/b");
bean.addInitParameter("a", "b");
bean.getInitParameters().put("a", "c");
bean.onStartup(this.servletContext);
then(this.registration).should().setInitParameters(Collections.singletonMap("a", "c"));
}
@Test
void withoutDefaultMappings() throws Exception {
given(this.servletContext.addServlet(anyString(), any(Servlet.class))).willReturn(this.registration);
ServletRegistrationBean<MockServlet> bean = new ServletRegistrationBean<>(this.servlet, false);
bean.onStartup(this.servletContext);
then(this.registration).should(never()).addMapping(any(String[].class));
}
}
Domain
Source
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free