From 8f7b4b208d239ba6fb1ca231f5fbe8deecd45fa3 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Fri, 15 Dec 2023 15:25:04 +0200 Subject: [PATCH 1/7] #18318 Remove deployment conf field from VaadinSession --- .../com/vaadin/flow/server/VaadinService.java | 2 - .../com/vaadin/flow/server/VaadinSession.java | 44 +++++++------------ .../vaadin/flow/component/InvalidUrlTest.java | 15 ++++--- .../flow/component/LocationObserverTest.java | 4 +- .../com/vaadin/flow/component/UITest.java | 15 ++++--- .../component/internal/UIInternalsTest.java | 5 +++ .../webcomponent/WebComponentWrapperTest.java | 10 ++++- .../flow/router/RouteConfigurationTest.java | 4 ++ .../RouterConfigurationUrlResolvingTest.java | 1 - .../com/vaadin/flow/router/RouterTest.java | 1 - .../internal/ErrorStateRendererTest.java | 2 - .../internal/NavigationStateRendererTest.java | 7 --- .../flow/server/CustomUIClassLoaderTest.java | 13 ------ .../flow/server/ErrorHandlerUtilTest.java | 5 +++ .../flow/server/SessionRouteRegistryTest.java | 12 +++++ .../vaadin/flow/server/VaadinSessionTest.java | 21 +++++---- .../WebComponentBootstrapHandlerViteTest.java | 4 -- .../tests/server/SerializationTest.java | 14 +++--- .../java/com/vaadin/tests/util/MockUI.java | 8 ++-- 19 files changed, 89 insertions(+), 98 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java b/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java index e7a96c7c6e4..f7cd4a5f1f5 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java @@ -936,8 +936,6 @@ private VaadinSession createAndRegisterSession(VaadinRequest request) { // Initial WebBrowser data comes from the request session.setBrowser(new WebBrowser(request)); - session.setConfiguration(getDeploymentConfiguration()); - // Initial locale comes from the request if (getInstantiator().getI18NProvider() != null) { setLocale(request, session); diff --git a/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java b/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java index 3c8d940fef9..c8ec7545b4e 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java @@ -78,11 +78,6 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { volatile boolean sessionClosedExplicitly = false; - /** - * Configuration for the session. - */ - private DeploymentConfiguration configuration; - /** * Default locale of the session. */ @@ -145,6 +140,14 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { */ public VaadinSession(VaadinService service) { this.service = service; + + if (service != null) { + sessionLockCheckStrategy = getConfiguration().isProductionMode() + ? getConfiguration().getSessionLockCheckStrategy() + : SessionLockCheckStrategy.THROW; + assert sessionLockCheckStrategy != null; + } + resourceRegistry = createStreamResourceRegistry(); } @@ -344,33 +347,14 @@ private void refreshLock() { lock = service.getSessionLock(session); } - public void setConfiguration(DeploymentConfiguration configuration) { - checkHasLock(); - if (configuration == null) { - throw new IllegalArgumentException("Can not set to null"); - } - checkSetConfiguration(); - this.configuration = configuration; - - sessionLockCheckStrategy = configuration.isProductionMode() - ? configuration.getSessionLockCheckStrategy() - : SessionLockCheckStrategy.THROW; - assert sessionLockCheckStrategy != null; - } - - protected void checkSetConfiguration() { - assert this.configuration == null - : "Configuration can only be set once"; - } - /** - * Gets the configuration for this session. + * Gets the deployment configuration. Delegates the call to + * {@link VaadinService#getDeploymentConfiguration()} * * @return the deployment configuration */ public DeploymentConfiguration getConfiguration() { - checkHasLock(); - return configuration; + return service.getDeploymentConfiguration(); } /** @@ -1117,6 +1101,12 @@ public void refreshTransients(WrappedSession wrappedSession, VaadinService vaadinService) { session = wrappedSession; service = vaadinService; + + sessionLockCheckStrategy = getConfiguration().isProductionMode() + ? getConfiguration().getSessionLockCheckStrategy() + : SessionLockCheckStrategy.THROW; + assert sessionLockCheckStrategy != null; + refreshLock(); } diff --git a/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java b/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java index b1fc3a341b8..0144b0fb63a 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java @@ -80,18 +80,19 @@ private static void initUI(UI ui, String initialLocation, public VaadinContext getContext() { return new MockVaadinContext(); } + + @Override + public DeploymentConfiguration getDeploymentConfiguration() { + DeploymentConfiguration config = Mockito + .mock(DeploymentConfiguration.class); + Mockito.when(config.isProductionMode()).thenReturn(false); + return config; + } }; service.setCurrentInstances(request, response); MockVaadinSession session = new AlwaysLockedVaadinSession(service); - DeploymentConfiguration config = Mockito - .mock(DeploymentConfiguration.class); - Mockito.when(config.isProductionMode()).thenReturn(false); - - session.lock(); - session.setConfiguration(config); - ui.getInternals().setSession(session); RouteConfiguration routeConfiguration = RouteConfiguration diff --git a/flow-server/src/test/java/com/vaadin/flow/component/LocationObserverTest.java b/flow-server/src/test/java/com/vaadin/flow/component/LocationObserverTest.java index 1ae7145ff94..0c9bde1dc6a 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/LocationObserverTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/LocationObserverTest.java @@ -71,9 +71,7 @@ private static VaadinSession createMockSession(Router router) { MockVaadinServletService service = new MockVaadinServletService(); service.setRouter(router); - VaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(service.getDeploymentConfiguration()); - return session; + return new AlwaysLockedVaadinSession(service); } @Override diff --git a/flow-server/src/test/java/com/vaadin/flow/component/UITest.java b/flow-server/src/test/java/com/vaadin/flow/component/UITest.java index 8215436088b..0ae67679bf4 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/UITest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/UITest.java @@ -230,18 +230,19 @@ private static void initUI(UI ui, String initialLocation, public VaadinContext getContext() { return new MockVaadinContext(); } + + @Override + public DeploymentConfiguration getDeploymentConfiguration() { + DeploymentConfiguration config = Mockito + .mock(DeploymentConfiguration.class); + Mockito.when(config.isProductionMode()).thenReturn(false); + return config; + } }; service.setCurrentInstances(request, response); MockVaadinSession session = new AlwaysLockedVaadinSession(service); - - DeploymentConfiguration config = Mockito - .mock(DeploymentConfiguration.class); - Mockito.when(config.isProductionMode()).thenReturn(false); - session.lock(); - session.setConfiguration(config); - ui.getInternals().setSession(session); RouteConfiguration routeConfiguration = RouteConfiguration diff --git a/flow-server/src/test/java/com/vaadin/flow/component/internal/UIInternalsTest.java b/flow-server/src/test/java/com/vaadin/flow/component/internal/UIInternalsTest.java index 15d49b51253..4080f628215 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/internal/UIInternalsTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/internal/UIInternalsTest.java @@ -39,6 +39,7 @@ import com.vaadin.flow.shared.Registration; import com.vaadin.flow.shared.communication.PushMode; import com.vaadin.tests.util.AlwaysLockedVaadinSession; +import com.vaadin.tests.util.MockDeploymentConfiguration; public class UIInternalsTest { @@ -118,6 +119,10 @@ public void init() { Element body = new Element("body"); Mockito.when(ui.getElement()).thenReturn(body); + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + Mockito.when(vaadinService.getDeploymentConfiguration()) + .thenReturn(config); + internals = new UIInternals(ui); AlwaysLockedVaadinSession session = new AlwaysLockedVaadinSession( vaadinService); diff --git a/flow-server/src/test/java/com/vaadin/flow/component/webcomponent/WebComponentWrapperTest.java b/flow-server/src/test/java/com/vaadin/flow/component/webcomponent/WebComponentWrapperTest.java index 5d43203e231..096df558355 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/webcomponent/WebComponentWrapperTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/webcomponent/WebComponentWrapperTest.java @@ -23,6 +23,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.Tag; @@ -36,6 +37,7 @@ import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.server.webcomponent.WebComponentBinding; import com.vaadin.tests.util.AlwaysLockedVaadinSession; +import com.vaadin.tests.util.MockDeploymentConfiguration; import elemental.json.Json; import static org.mockito.Mockito.mock; @@ -242,9 +244,13 @@ private static WebComponentUI constructWebComponentUI( Element body = new Element("body"); when(ui.getElement()).thenReturn(body); + VaadinService vaadinService = Mockito.mock(VaadinService.class); + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + Mockito.when(vaadinService.getDeploymentConfiguration()) + .thenReturn(config); + UIInternals internals = new UIInternals(ui); - internals.setSession( - new AlwaysLockedVaadinSession(mock(VaadinService.class))); + internals.setSession(new AlwaysLockedVaadinSession(vaadinService)); when(ui.getInternals()).thenReturn(internals); Component parent = new Parent(); diff --git a/flow-server/src/test/java/com/vaadin/flow/router/RouteConfigurationTest.java b/flow-server/src/test/java/com/vaadin/flow/router/RouteConfigurationTest.java index f14a490c6fd..7854d26498d 100644 --- a/flow-server/src/test/java/com/vaadin/flow/router/RouteConfigurationTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/router/RouteConfigurationTest.java @@ -31,6 +31,7 @@ import com.vaadin.flow.server.VaadinServletService; import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.server.startup.ApplicationRouteRegistry; +import com.vaadin.tests.util.MockDeploymentConfiguration; @NotThreadSafe public class RouteConfigurationTest { @@ -50,6 +51,9 @@ public void init() { vaadinService = Mockito.mock(MockService.class); Mockito.when(vaadinService.getRouteRegistry()).thenReturn(registry); Mockito.when(vaadinService.getContext()).thenReturn(vaadinContext); + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + Mockito.when(vaadinService.getDeploymentConfiguration()) + .thenReturn(config); VaadinService.setCurrent(vaadinService); diff --git a/flow-server/src/test/java/com/vaadin/flow/router/RouterConfigurationUrlResolvingTest.java b/flow-server/src/test/java/com/vaadin/flow/router/RouterConfigurationUrlResolvingTest.java index 24ac6120d7a..fda7231193d 100644 --- a/flow-server/src/test/java/com/vaadin/flow/router/RouterConfigurationUrlResolvingTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/router/RouterConfigurationUrlResolvingTest.java @@ -52,7 +52,6 @@ public void init() throws NoSuchFieldException, IllegalAccessException { super.init(); ui = new RouterTestMockUI(router); ui.getSession().lock(); - ui.getSession().setConfiguration(configuration); VaadinService.setCurrent(service); diff --git a/flow-server/src/test/java/com/vaadin/flow/router/RouterTest.java b/flow-server/src/test/java/com/vaadin/flow/router/RouterTest.java index 36030de0ff1..60c6cd7e944 100644 --- a/flow-server/src/test/java/com/vaadin/flow/router/RouterTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/router/RouterTest.java @@ -1825,7 +1825,6 @@ public void init() throws NoSuchFieldException, SecurityException, super.init(); ui = new RouterTestMockUI(router); ui.getSession().lock(); - ui.getSession().setConfiguration(configuration); VaadinService.setCurrent(service); diff --git a/flow-server/src/test/java/com/vaadin/flow/router/internal/ErrorStateRendererTest.java b/flow-server/src/test/java/com/vaadin/flow/router/internal/ErrorStateRendererTest.java index 705fc13103b..93477472281 100644 --- a/flow-server/src/test/java/com/vaadin/flow/router/internal/ErrorStateRendererTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/router/internal/ErrorStateRendererTest.java @@ -46,7 +46,6 @@ import com.vaadin.flow.server.VaadinContext; import com.vaadin.flow.server.startup.ApplicationRouteRegistry; import com.vaadin.tests.util.AlwaysLockedVaadinSession; -import com.vaadin.tests.util.MockDeploymentConfiguration; import com.vaadin.tests.util.MockUI; import elemental.json.Json; @@ -221,7 +220,6 @@ public VaadinContext getContext() { }; MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); MockUI ui = new MockUI(session); return ui; diff --git a/flow-server/src/test/java/com/vaadin/flow/router/internal/NavigationStateRendererTest.java b/flow-server/src/test/java/com/vaadin/flow/router/internal/NavigationStateRendererTest.java index 241c1415e7a..552aff33f27 100644 --- a/flow-server/src/test/java/com/vaadin/flow/router/internal/NavigationStateRendererTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/router/internal/NavigationStateRendererTest.java @@ -68,7 +68,6 @@ import com.vaadin.flow.server.ServiceException; import com.vaadin.flow.server.startup.ApplicationRouteRegistry; import com.vaadin.tests.util.AlwaysLockedVaadinSession; -import com.vaadin.tests.util.MockDeploymentConfiguration; import com.vaadin.tests.util.MockUI; import elemental.json.Json; @@ -287,7 +286,6 @@ public void handle_preserveOnRefreshAndWindowNameKnown_componentIsCachedRetrieve // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a UI that contain a window name ROOT.123 MockUI ui1 = new MockUI(session); @@ -356,7 +354,6 @@ public void handle_preserveOnRefresh_refreshIsFlaggedInEvent() { // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a UI that contain a window name ROOT.123 MockUI ui = new MockUI(session); @@ -409,7 +406,6 @@ public void handle_preserveOnRefresh_otherUIChildrenAreMoved() { // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a NavigationStateRenderer mapping to PreservedView NavigationStateRenderer renderer = new NavigationStateRenderer( @@ -456,7 +452,6 @@ public void handle_preserveOnRefreshView_routerLayoutIsPreserved_oldUiIsClosed() // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a NavigationStateRenderer mapping to PreservedNestedView Router router = session.getService().getRouter(); @@ -510,7 +505,6 @@ public void handle_preserveOnRefresh_sameUI_uiIsNotClosed_childrenAreNotRemoved( // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a NavigationStateRenderer mapping to PreservedView NavigationStateRenderer renderer = new NavigationStateRenderer( @@ -582,7 +576,6 @@ public void handle_variousInputs_checkPushStateShouldBeCalledOrNot() { // given a locked session MockVaadinSession session = new AlwaysLockedVaadinSession(service); - session.setConfiguration(new MockDeploymentConfiguration()); // given a NavigationStateRenderer mapping to RegularView new NavigationStateBuilder(router).withTarget(RegularView.class) diff --git a/flow-server/src/test/java/com/vaadin/flow/server/CustomUIClassLoaderTest.java b/flow-server/src/test/java/com/vaadin/flow/server/CustomUIClassLoaderTest.java index 5cc5cbb60c3..c639cc8df63 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/CustomUIClassLoaderTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/CustomUIClassLoaderTest.java @@ -10,7 +10,6 @@ import com.vaadin.flow.component.UI; import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.server.startup.ApplicationConfiguration; -import com.vaadin.tests.util.AlwaysLockedVaadinSession; import org.junit.Test; import org.mockito.Mockito; @@ -52,9 +51,6 @@ protected synchronized Class<?> loadClass(String name, boolean resolve) */ @Test public void testWithDefaultClassLoader() throws Exception { - VaadinSession application = createStubApplication(); - application.setConfiguration(createConfigurationMock()); - Class<? extends UI> uiClass = BootstrapHandler .getUIClass(createRequestMock(getClass().getClassLoader())); @@ -113,13 +109,4 @@ public void testWithClassLoader() throws Exception { loggingClassLoader.requestedClasses.get(0)); } - - private VaadinSession createStubApplication() { - return new AlwaysLockedVaadinSession(new MockVaadinServletService()) { - @Override - public DeploymentConfiguration getConfiguration() { - return createConfigurationMock(); - } - }; - } } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/ErrorHandlerUtilTest.java b/flow-server/src/test/java/com/vaadin/flow/server/ErrorHandlerUtilTest.java index a2c7b89d199..94efdfb4868 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/ErrorHandlerUtilTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/ErrorHandlerUtilTest.java @@ -44,6 +44,7 @@ import com.vaadin.flow.router.RouterLayout; import com.vaadin.flow.server.startup.ApplicationRouteRegistry; import com.vaadin.tests.util.AlwaysLockedVaadinSession; +import com.vaadin.tests.util.MockDeploymentConfiguration; @NotThreadSafe public class ErrorHandlerUtilTest { @@ -129,6 +130,10 @@ public void init() { Mockito.when(ui.getUI()).thenReturn(Optional.of(ui)); Mockito.when(ui.getInternals()).thenReturn(internals); + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + Mockito.when(vaadinService.getDeploymentConfiguration()) + .thenReturn(config); + session = new AlwaysLockedVaadinSession(vaadinService); VaadinContext context = new MockVaadinContext(); Mockito.when(vaadinService.getContext()).thenReturn(context); diff --git a/flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java b/flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java index 79eb5e4108b..fdc950571a2 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java @@ -31,6 +31,7 @@ import com.vaadin.flow.component.Component; import com.vaadin.flow.component.HtmlContainer; import com.vaadin.flow.component.Tag; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.internal.CurrentInstance; import com.vaadin.flow.router.BeforeEnterEvent; import com.vaadin.flow.router.BeforeEvent; @@ -50,6 +51,7 @@ import com.vaadin.flow.server.startup.ApplicationConfiguration; import com.vaadin.flow.server.startup.ApplicationRouteRegistry; import com.vaadin.flow.shared.Registration; +import com.vaadin.tests.util.MockDeploymentConfiguration; public class SessionRouteRegistryTest { @@ -76,6 +78,11 @@ public void init() { Mockito.when(applicationConfiguration.isProductionMode()) .thenReturn(true); + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + config.setProductionMode(true); + Mockito.when(vaadinService.getDeploymentConfiguration()) + .thenReturn(config); + VaadinService.setCurrent(vaadinService); session = new MockVaadinSession(vaadinService) { @@ -1239,5 +1246,10 @@ protected Lock getSessionLock(WrappedSession wrappedSession) { protected RouteRegistry getRouteRegistry() { return appRegistry; } + + @Override + public DeploymentConfiguration getDeploymentConfiguration() { + return new MockDeploymentConfiguration(); + } } } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java b/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java index 1e7045f46ea..d0be0f59efb 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java @@ -34,6 +34,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.server.frontend.MockLogger; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; @@ -70,6 +71,8 @@ public UIDetachEvent(UI source) { public static class VaadinSessionWithMockLogger extends VaadinSession { public final MockLogger mockLogger = new MockLogger(); + private DeploymentConfiguration config; + public VaadinSessionWithMockLogger(VaadinService service) { super(service); mockLogger.includeStackTrace = true; @@ -81,7 +84,11 @@ Logger getLogger() { } @Override - protected void checkSetConfiguration() { + public DeploymentConfiguration getConfiguration() { + if (config == null) { + config = new MockDeploymentConfiguration(); + } + return config; } } @@ -140,11 +147,6 @@ public Object getAttribute(String name) { session = new VaadinSessionWithMockLogger(mockService); mockService.storeSession(session, mockWrappedSession); - MockDeploymentConfiguration configuration = new MockDeploymentConfiguration(); - session.lock(); - session.setConfiguration(configuration); - session.unlock(); - ui = new UI(); vaadinRequest = new VaadinServletRequest( Mockito.mock(HttpServletRequest.class), mockService) { @@ -545,13 +547,11 @@ public void checkHasLock_noCheckInDevMode() { session.checkHasLock(); configuration.setLockCheckStrategy(SessionLockCheckStrategy.LOG); - session.setConfiguration(configuration); session.mockLogger.clearLogs(); session.checkHasLock(); Assert.assertEquals("", session.mockLogger.getLogs()); configuration.setLockCheckStrategy(SessionLockCheckStrategy.ASSERT); - session.setConfiguration(configuration); session.checkHasLock(); } @@ -560,7 +560,7 @@ public void checkHasLock_assert() { final MockDeploymentConfiguration configuration = (MockDeploymentConfiguration) session .getConfiguration(); configuration.setProductionMode(true); - session.setConfiguration(configuration); + session.refreshTransients(mockWrappedSession, mockService); session.unlock(); Assume.assumeFalse(session.hasLock()); @@ -581,7 +581,6 @@ public void checkHasLock_throw() { .getConfiguration(); configuration.setProductionMode(true); configuration.setLockCheckStrategy(SessionLockCheckStrategy.THROW); - session.setConfiguration(configuration); session.unlock(); Assume.assumeFalse(session.hasLock()); @@ -600,7 +599,7 @@ public void checkHasLock_log() { .getConfiguration(); configuration.setProductionMode(true); configuration.setLockCheckStrategy(SessionLockCheckStrategy.LOG); - session.setConfiguration(configuration); + session.refreshTransients(mockWrappedSession, mockService); session.unlock(); Assume.assumeFalse(session.hasLock()); diff --git a/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandlerViteTest.java b/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandlerViteTest.java index ab341e66569..5d439c9efc0 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandlerViteTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/communication/WebComponentBootstrapHandlerViteTest.java @@ -199,7 +199,6 @@ protected PwaRegistry getPwaRegistry() { VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setFrontendHotdeploy(false); @@ -233,7 +232,6 @@ public void writeBootstrapPage_devToolsDisabled() VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setFrontendHotdeploy(false); @@ -298,7 +296,6 @@ public void writeBootstrapPage_withExportChunk() VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setFrontendHotdeploy(false); @@ -332,7 +329,6 @@ public void writeBootstrapPage_noExportChunk() VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setApplicationOrSystemProperty(SERVLET_PARAMETER_STATISTICS_JSON, diff --git a/flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java b/flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java index 57be740c9e1..d7b38ea2b11 100644 --- a/flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java +++ b/flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java @@ -23,10 +23,9 @@ import com.vaadin.flow.server.VaadinSession; import com.vaadin.flow.server.WrappedSession; import com.vaadin.flow.server.startup.ApplicationConfiguration; +import com.vaadin.tests.util.MockDeploymentConfiguration; import com.vaadin.tests.util.MockUI; -import static org.mockito.Mockito.withSettings; - public class SerializationTest { @Test @@ -64,8 +63,6 @@ public void testSerializeVaadinSession_notProductionMode_disableDevModeSerializa // should be called by Flow internally as soon as the session has // been created. session.refreshTransients(null, vaadinService); - session.setConfiguration(Mockito.mock(DeploymentConfiguration.class, - Mockito.withSettings().serializable())); MockUI ui = new MockUI(session); ui.doInit(null, 42); session.addUI(ui); @@ -131,8 +128,6 @@ private static VaadinSession serializeAndDeserializeWithUI( // should be called by Flow internally as soon as the session has // been created. session.refreshTransients(null, vaadinService); - session.setConfiguration(Mockito.mock(DeploymentConfiguration.class, - withSettings().serializable())); MockUI ui = new MockUI(session); ui.doInit(null, 42); session.addUI(ui); @@ -225,6 +220,13 @@ public MockVaadinService(boolean productionMode, boolean serialize) { this.serialize = serialize; } + @Override + public DeploymentConfiguration getDeploymentConfiguration() { + MockDeploymentConfiguration config = new MockDeploymentConfiguration(); + config.setProductionMode(productionMode); + return config; + } + @Override public VaadinContext getContext() { ApplicationConfiguration applicationConfiguration = Mockito diff --git a/flow-server/src/test/java/com/vaadin/tests/util/MockUI.java b/flow-server/src/test/java/com/vaadin/tests/util/MockUI.java index 2cb30d0f000..c592bf9a85d 100644 --- a/flow-server/src/test/java/com/vaadin/tests/util/MockUI.java +++ b/flow-server/src/test/java/com/vaadin/tests/util/MockUI.java @@ -68,7 +68,8 @@ private static VaadinSession createSession() { private static VaadinSession createSession(Router router) { VaadinService service = Mockito.mock(VaadinService.class); - + Mockito.when(service.getDeploymentConfiguration()) + .thenReturn(createConfiguration()); if (router != null) { Mockito.when(service.getRouter()).thenReturn(router); } @@ -85,9 +86,6 @@ private static DeploymentConfiguration createConfiguration() { } public static MockUI createUI() { - DeploymentConfiguration configuration = createConfiguration(); - VaadinSession session = createSession(); - session.setConfiguration(configuration); - return new MockUI(session); + return new MockUI(createSession()); } } From 24ed149373bd5a4692597289aefc2a124836a6f2 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Fri, 15 Dec 2023 15:57:18 +0200 Subject: [PATCH 2/7] Fix failing test --- .../flow/data/provider/DataCommunicatorAsyncTest.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorAsyncTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorAsyncTest.java index ff66b99e5a6..f74b54296ff 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorAsyncTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorAsyncTest.java @@ -17,10 +17,12 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import com.vaadin.flow.component.UI; import com.vaadin.flow.dom.Element; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.internal.Range; import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinService; @@ -253,8 +255,10 @@ protected void init(VaadinRequest request) { private static VaadinSession findOrcreateSession() { VaadinSession session = VaadinSession.getCurrent(); if (session == null) { + DeploymentConfiguration config = Mockito + .mock(DeploymentConfiguration.class); session = new AlwaysLockedVaadinSession( - new VaadinServletService(new VaadinServlet(), null)); + new VaadinServletService(new VaadinServlet(), config)); VaadinSession.setCurrent(session); } return session; From 43ba53b54f14b9e6ca69cef211d60d74cc8b06f8 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Mon, 18 Dec 2023 08:42:39 +0200 Subject: [PATCH 3/7] fix spring tests --- .../vaadin/flow/spring/scopes/AbstractScopeTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/vaadin-spring/src/test/java/com/vaadin/flow/spring/scopes/AbstractScopeTest.java b/vaadin-spring/src/test/java/com/vaadin/flow/spring/scopes/AbstractScopeTest.java index 8ee248caa07..b88763bb736 100644 --- a/vaadin-spring/src/test/java/com/vaadin/flow/spring/scopes/AbstractScopeTest.java +++ b/vaadin-spring/src/test/java/com/vaadin/flow/spring/scopes/AbstractScopeTest.java @@ -29,6 +29,7 @@ import org.springframework.beans.factory.config.Scope; import com.vaadin.flow.di.Lookup; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.server.DefaultDeploymentConfiguration; import com.vaadin.flow.server.VaadinContext; import com.vaadin.flow.server.VaadinService; @@ -48,10 +49,19 @@ public abstract class AbstractScopeTest { public static class TestSession extends SpringVaadinSession { + private DeploymentConfiguration configuration; + public TestSession() { super(Mockito.mock(VaadinService.class)); } + @Override + public DeploymentConfiguration getConfiguration() { + if (configuration == null) { + configuration = Mockito.mock(DeploymentConfiguration.class); + } + return configuration; + } } @After From 6320eef767bfaa18f10f74d1b9671ba7939347d5 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Tue, 19 Dec 2023 12:06:14 +0200 Subject: [PATCH 4/7] fixes for review feedback --- .../com/vaadin/flow/server/VaadinSession.java | 24 ++++++++++--------- .../vaadin/flow/component/InvalidUrlTest.java | 2 -- .../vaadin/flow/server/VaadinSessionTest.java | 11 ++++++++- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java b/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java index c8ec7545b4e..f80070d34a3 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java @@ -139,14 +139,10 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable { * the Vaadin service for the new session */ public VaadinSession(VaadinService service) { + Objects.requireNonNull(service, "VaadinService can not be null."); this.service = service; - if (service != null) { - sessionLockCheckStrategy = getConfiguration().isProductionMode() - ? getConfiguration().getSessionLockCheckStrategy() - : SessionLockCheckStrategy.THROW; - assert sessionLockCheckStrategy != null; - } + refreshSessionLockCheckStrategy(); resourceRegistry = createStreamResourceRegistry(); } @@ -347,6 +343,16 @@ private void refreshLock() { lock = service.getSessionLock(session); } + /** + * Updates the session lock check strategy. + */ + private void refreshSessionLockCheckStrategy() { + sessionLockCheckStrategy = getConfiguration().isProductionMode() + ? getConfiguration().getSessionLockCheckStrategy() + : SessionLockCheckStrategy.THROW; + assert sessionLockCheckStrategy != null; + } + /** * Gets the deployment configuration. Delegates the call to * {@link VaadinService#getDeploymentConfiguration()} @@ -1102,11 +1108,7 @@ public void refreshTransients(WrappedSession wrappedSession, session = wrappedSession; service = vaadinService; - sessionLockCheckStrategy = getConfiguration().isProductionMode() - ? getConfiguration().getSessionLockCheckStrategy() - : SessionLockCheckStrategy.THROW; - assert sessionLockCheckStrategy != null; - + refreshSessionLockCheckStrategy(); refreshLock(); } diff --git a/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java b/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java index 0144b0fb63a..23473ffa8bd 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/InvalidUrlTest.java @@ -107,8 +107,6 @@ public DeploymentConfiguration getDeploymentConfiguration() { ui.doInit(request, 0); ui.getRouter().initializeUI(ui, UITest.requestToLocation(request)); - session.unlock(); - if (statusCodeCaptor != null) { Mockito.verify(response).setStatus(statusCodeCaptor.capture()); } diff --git a/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java b/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java index d0be0f59efb..1c0903a75a3 100644 --- a/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/server/VaadinSessionTest.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.lang.reflect.Field; import java.util.EventObject; import java.util.HashMap; import java.util.Iterator; @@ -498,7 +499,15 @@ public void valueUnbound_sessionIsNotInitialized_noAnyInteractions() { public static class TestVaadinSession extends VaadinSession { public TestVaadinSession() { - super(null); + super(new MockVaadinServletService()); + try { + Field serviceField = getClass().getSuperclass() + .getDeclaredField("service"); + serviceField.setAccessible(true); + serviceField.set(this, null); + } catch (NoSuchFieldException | IllegalAccessException e) { + throw new RuntimeException(e); + } } } From 8a83f4d5aa799cfdd4cc8ffb603c22854526a70c Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Tue, 19 Dec 2023 13:15:08 +0200 Subject: [PATCH 5/7] fix flow-data tests --- .../data/provider/DataCommunicatorTest.java | 12 ++- .../HierarchicalCommunicatorDataTest.java | 82 +++++++++---------- 2 files changed, 50 insertions(+), 44 deletions(-) diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorTest.java index 9db8449c794..c439c375b79 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/DataCommunicatorTest.java @@ -47,10 +47,12 @@ import com.vaadin.flow.component.Tag; import com.vaadin.flow.component.UI; import com.vaadin.flow.dom.Element; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.function.SerializableConsumer; import com.vaadin.flow.function.SerializablePredicate; import com.vaadin.flow.internal.Range; import com.vaadin.flow.internal.StateNode; +import com.vaadin.flow.server.SessionLockCheckStrategy; import com.vaadin.flow.server.VaadinRequest; import com.vaadin.flow.server.VaadinService; import com.vaadin.flow.server.VaadinSession; @@ -1805,7 +1807,15 @@ protected void init(VaadinRequest request) { private static VaadinSession findOrcreateSession() { VaadinSession session = VaadinSession.getCurrent(); if (session == null) { - session = new AlwaysLockedVaadinSession(null); + VaadinService service = Mockito.mock(VaadinService.class); + DeploymentConfiguration conf = Mockito + .mock(DeploymentConfiguration.class); + Mockito.when(conf.isProductionMode()).thenReturn(true); + Mockito.when(conf.getSessionLockCheckStrategy()) + .thenReturn(SessionLockCheckStrategy.ASSERT); + Mockito.when(service.getDeploymentConfiguration()) + .thenReturn(conf); + session = new AlwaysLockedVaadinSession(service); VaadinSession.setCurrent(session); } return session; diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java index 85a1c4a738b..3ab7c6c8e25 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java @@ -20,22 +20,18 @@ import java.util.List; import java.util.stream.IntStream; -import com.vaadin.flow.function.SerializablePredicate; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; -import com.vaadin.flow.component.UI; import com.vaadin.flow.data.provider.CompositeDataGenerator; import com.vaadin.flow.data.provider.DataCommunicatorTest; import com.vaadin.flow.data.provider.hierarchy.HierarchicalArrayUpdater.HierarchicalUpdate; import com.vaadin.flow.dom.Element; +import com.vaadin.flow.function.SerializablePredicate; import com.vaadin.flow.function.ValueProvider; -import com.vaadin.flow.server.VaadinRequest; -import com.vaadin.flow.server.VaadinService; -import com.vaadin.flow.server.VaadinSession; import elemental.json.JsonValue; @@ -79,7 +75,7 @@ public int hashCode() { private TreeDataProvider<Item> dataProvider; private HierarchicalDataCommunicator<Item> communicator; private TreeData<Item> treeData; - private MockUI ui; + private DataCommunicatorTest.MockUI ui; private Element element; private boolean parentClearCalled = false; @@ -137,7 +133,7 @@ public void initialize() { @Before public void setUp() { MockitoAnnotations.initMocks(this); - ui = new MockUI(); + ui = new DataCommunicatorTest.MockUI(); element = new Element("div"); ui.getElement().appendChild(element); @@ -289,41 +285,41 @@ private void fakeClientCommunication() { }); } - public static class MockUI extends UI { - - public MockUI() { - this(findOrCreateSession()); - } - - public MockUI(VaadinSession session) { - getInternals().setSession(session); - setCurrent(this); - } - - @Override - protected void init(VaadinRequest request) { - // Do nothing - } - - private static VaadinSession findOrCreateSession() { - VaadinSession session = VaadinSession.getCurrent(); - if (session == null) { - session = new DataCommunicatorTest.AlwaysLockedVaadinSession( - null); - VaadinSession.setCurrent(session); - } - return session; - } - } - - public static class AlwaysLockedVaadinSession - extends DataCommunicatorTest.MockVaadinSession { - - public AlwaysLockedVaadinSession(VaadinService service) { - super(service); - lock(); - } - - } + // public static class MockUI extends UI { + // + // public MockUI() { + // this(findOrCreateSession()); + // } + // + // public MockUI(VaadinSession session) { + // getInternals().setSession(session); + // setCurrent(this); + // } + // + // @Override + // protected void init(VaadinRequest request) { + // // Do nothing + // } + // + // private static VaadinSession findOrCreateSession() { + // VaadinSession session = VaadinSession.getCurrent(); + // if (session == null) { + // session = new DataCommunicatorTest.AlwaysLockedVaadinSession( + // null); + // VaadinSession.setCurrent(session); + // } + // return session; + // } + // } + // + // public static class AlwaysLockedVaadinSession + // extends DataCommunicatorTest.MockVaadinSession { + // + // public AlwaysLockedVaadinSession(VaadinService service) { + // super(service); + // lock(); + // } + // + // } } From c904e233989a79b3f3737707b992db294d479aa0 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Tue, 19 Dec 2023 13:17:08 +0200 Subject: [PATCH 6/7] remove commented code --- .../HierarchicalCommunicatorDataTest.java | 37 ------------------- 1 file changed, 37 deletions(-) diff --git a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java index 3ab7c6c8e25..1033bde33d6 100644 --- a/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java +++ b/flow-data/src/test/java/com/vaadin/flow/data/provider/hierarchy/HierarchicalCommunicatorDataTest.java @@ -285,41 +285,4 @@ private void fakeClientCommunication() { }); } - // public static class MockUI extends UI { - // - // public MockUI() { - // this(findOrCreateSession()); - // } - // - // public MockUI(VaadinSession session) { - // getInternals().setSession(session); - // setCurrent(this); - // } - // - // @Override - // protected void init(VaadinRequest request) { - // // Do nothing - // } - // - // private static VaadinSession findOrCreateSession() { - // VaadinSession session = VaadinSession.getCurrent(); - // if (session == null) { - // session = new DataCommunicatorTest.AlwaysLockedVaadinSession( - // null); - // VaadinSession.setCurrent(session); - // } - // return session; - // } - // } - // - // public static class AlwaysLockedVaadinSession - // extends DataCommunicatorTest.MockVaadinSession { - // - // public AlwaysLockedVaadinSession(VaadinService service) { - // super(service); - // lock(); - // } - // - // } - } From c107a81d7e51a8e23d5f416c4d68caeca2e27446 Mon Sep 17 00:00:00 2001 From: Teppo Kurki <teppo.kurki@vaadin.com> Date: Tue, 19 Dec 2023 13:43:09 +0200 Subject: [PATCH 7/7] Fix more tests --- .../themeeditor/AbstractThemeEditorTest.java | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/vaadin-dev-server/src/test/java/com/vaadin/base/devserver/themeeditor/AbstractThemeEditorTest.java b/vaadin-dev-server/src/test/java/com/vaadin/base/devserver/themeeditor/AbstractThemeEditorTest.java index c53bbdf48d3..1a447b97fa4 100644 --- a/vaadin-dev-server/src/test/java/com/vaadin/base/devserver/themeeditor/AbstractThemeEditorTest.java +++ b/vaadin-dev-server/src/test/java/com/vaadin/base/devserver/themeeditor/AbstractThemeEditorTest.java @@ -1,12 +1,12 @@ package com.vaadin.base.devserver.themeeditor; import com.vaadin.base.devserver.MockVaadinContext; -import com.vaadin.experimental.FeatureFlags; import com.vaadin.flow.component.Component; import com.vaadin.flow.component.html.Span; import com.vaadin.flow.component.internal.ComponentTracker; import com.vaadin.flow.di.Lookup; import com.vaadin.flow.dom.Element; +import com.vaadin.flow.function.DeploymentConfiguration; import com.vaadin.flow.server.Command; import com.vaadin.flow.server.VaadinContext; import com.vaadin.flow.server.VaadinService; @@ -44,6 +44,8 @@ public abstract class AbstractThemeEditorTest { protected final int INLINEADD_CREATE = 48; protected final int INLINEADD_ATTACH = 48; + private VaadinSession session; + protected class TestThemeModifier extends ThemeModifier { public TestThemeModifier() { @@ -58,8 +60,6 @@ protected File getFrontendFolder() { protected class TestJavaSourceModifier extends JavaSourceModifier { - private VaadinSession session = new MockVaadinSession(null); - public TestJavaSourceModifier() { super(mockContext); } @@ -145,6 +145,13 @@ public void prepare() { VaadinService.setCurrent(service); Mockito.when(service.getContext()).thenReturn(mockContext); + DeploymentConfiguration depConf = Mockito + .mock(DeploymentConfiguration.class); + Mockito.when(depConf.isProductionMode()).thenReturn(false); + Mockito.when(service.getDeploymentConfiguration()).thenReturn(depConf); + + session = new MockVaadinSession(service); + ApplicationConfiguration configuration = Mockito .mock(ApplicationConfiguration.class); ApplicationConfigurationFactory factory = Mockito