Skip to content

Commit

Permalink
#18318 Remove deployment conf field from VaadinSession
Browse files Browse the repository at this point in the history
  • Loading branch information
tepi committed Dec 15, 2023
1 parent cdfde35 commit 8f7b4b2
Show file tree
Hide file tree
Showing 19 changed files with 89 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 17 additions & 27 deletions flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 8 additions & 7 deletions flow-server/src/test/java/com/vaadin/flow/component/UITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -221,7 +220,6 @@ public VaadinContext getContext() {
};

MockVaadinSession session = new AlwaysLockedVaadinSession(service);
session.setConfiguration(new MockDeploymentConfiguration());

MockUI ui = new MockUI(session);
return ui;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));

Expand Down Expand Up @@ -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();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand All @@ -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) {
Expand Down Expand Up @@ -1239,5 +1246,10 @@ protected Lock getSessionLock(WrappedSession wrappedSession) {
protected RouteRegistry getRouteRegistry() {
return appRegistry;
}

@Override
public DeploymentConfiguration getDeploymentConfiguration() {
return new MockDeploymentConfiguration();
}
}
}
Loading

0 comments on commit 8f7b4b2

Please sign in to comment.