Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set load-on-startup for automatically registered Vaadin servlet #14501

Merged
merged 1 commit into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ private VaadinServletCreation createServletIfNotExists(

registration.setAsyncSupported(true);
registration.addMapping(path);
registration.setLoadOnStartup(1);
return VaadinServletCreation.SERVLET_CREATED;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

public class ServletDeployerTest {
private final ServletDeployer deployer = new ServletDeployer();

private List<String> servletNames;
private List<String> servletMappings;
private List<Integer> servletLoadOnStartup;

private boolean disableAutomaticServletRegistration = false;

Expand Down Expand Up @@ -84,6 +86,7 @@ private static class ComponentWithRoute extends Component {
public void clearCaptures() {
servletNames = new ArrayList<>();
servletMappings = new ArrayList<>();
servletLoadOnStartup = new ArrayList<>();
}

@Test
Expand All @@ -93,6 +96,7 @@ public void automaticallyRegisterTwoServletsWhenNoServletsPresent()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -114,6 +118,7 @@ public void registeredNonVaadinServlets_vaadinServletsAreRegistered()
singletonList("/test/*"), Collections.emptyMap())));

assertMappingsCount(1, 1);
assertLoadOnStartupSet();
}

@Test
Expand All @@ -126,6 +131,7 @@ public void frontendServletIsNotRegisteredWhenProductionModeIsActive()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -135,6 +141,7 @@ public void frontendServletIsNotRegistered_whenMainServletIsRegistered()

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

@Test
Expand All @@ -156,6 +163,7 @@ public void servletIsNotRegisteredWhenAnotherHasTheSamePathMapping_frontendServl

assertMappingsCount(1, 1);
assertMappingIsRegistered(ServletDeployer.class.getName(), "/*");
assertLoadOnStartupSet();
}

private void assertMappingsCount(int numServlets, int numMappings) {
Expand Down Expand Up @@ -184,6 +192,21 @@ private void assertMappingIsRegistered(String servletName,
pathIndex, servletNameIndex);
}

private void assertLoadOnStartupSet() {
assertEquals("Servlet loadOnStartup should be invoked only once", 1,
servletLoadOnStartup.size());
assertEquals(
String.format(
"Expected servlet loadOnStartup to be '%d' but was '%d",
1, servletLoadOnStartup.get(0)),
(Integer) 1, servletLoadOnStartup.get(0));
}

private void assertLoadOnStartupNotSet() {
assertTrue("Servlet loadOnStartup should not have been invoked ",
servletLoadOnStartup.isEmpty());
}

@SuppressWarnings({ "unchecked", "rawtypes" })
private ServletContextEvent getContextEvent(
ServletRegistration... servletRegistrations) throws Exception {
Expand All @@ -196,6 +219,8 @@ private ServletContextEvent getContextEvent(
this.servletMappings.addAll(Arrays.asList(mappings));
return Collections.emptySet();
});
Mockito.doAnswer(i -> this.servletLoadOnStartup.add(i.getArgument(0)))
.when(dynamicMock).setLoadOnStartup(ArgumentMatchers.anyInt());

ServletContext contextMock = Mockito.mock(ServletContext.class);

Expand Down