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; 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..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 @@ -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 dataProvider; private HierarchicalDataCommunicator communicator; private TreeData 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,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(); - } - - } - } 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 53a93669623..8339de02282 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..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 @@ -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. */ @@ -144,7 +139,11 @@ 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; + + refreshSessionLockCheckStrategy(); + resourceRegistry = createStreamResourceRegistry(); } @@ -344,33 +343,24 @@ 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() + /** + * Updates the session lock check strategy. + */ + private void refreshSessionLockCheckStrategy() { + sessionLockCheckStrategy = getConfiguration().isProductionMode() + ? getConfiguration().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 +1107,8 @@ public void refreshTransients(WrappedSession wrappedSession, VaadinService vaadinService) { session = wrappedSession; service = vaadinService; + + 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 b1fc3a341b8..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 @@ -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 @@ -106,8 +107,6 @@ public VaadinContext getContext() { 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/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 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..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; @@ -34,6 +35,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 +72,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 +85,11 @@ Logger getLogger() { } @Override - protected void checkSetConfiguration() { + public DeploymentConfiguration getConfiguration() { + if (config == null) { + config = new MockDeploymentConfiguration(); + } + return config; } } @@ -140,11 +148,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) { @@ -496,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); + } } } @@ -545,13 +556,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 +569,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 +590,6 @@ public void checkHasLock_throw() { .getConfiguration(); configuration.setProductionMode(true); configuration.setLockCheckStrategy(SessionLockCheckStrategy.THROW); - session.setConfiguration(configuration); session.unlock(); Assume.assumeFalse(session.hasLock()); @@ -600,7 +608,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 c07d55a4e5f..e367e75c36e 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); @@ -234,7 +233,6 @@ public void writeBootstrapPage_devToolsDisabled() VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setFrontendHotdeploy(false); @@ -300,7 +298,6 @@ public void writeBootstrapPage_withExportChunk() VaadinSession session = new MockVaadinSession(service); session.lock(); - session.setConfiguration(service.getDeploymentConfiguration()); MockDeploymentConfiguration config = (MockDeploymentConfiguration) service .getDeploymentConfiguration(); config.setFrontendHotdeploy(false); @@ -334,7 +331,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()); } } 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 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