Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

refactor!: Remove deployment conf field from VaadinSession #18319

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
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) {
mcollovati marked this conversation as resolved.
Show resolved Hide resolved
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;
mcollovati marked this conversation as resolved.
Show resolved Hide resolved

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();
mcollovati marked this conversation as resolved.
Show resolved Hide resolved
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
Loading
Loading