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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

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

}

}
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
38 changes: 15 additions & 23 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 @@ -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();
}

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

/**
Expand Down Expand Up @@ -1117,6 +1107,8 @@ public void refreshTransients(WrappedSession wrappedSession,
VaadinService vaadinService) {
session = wrappedSession;
service = vaadinService;

refreshSessionLockCheckStrategy();
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 All @@ -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());
}
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
Loading