Skip to content

Commit

Permalink
Require compatibility mode setting (#6122)
Browse files Browse the repository at this point in the history
Treat absence of configuration as an error rather than as a hint to use compatibility mode. Also add a `flow-server-compatibility-mode` to make it easier to explicitly set compatibility mode.
  • Loading branch information
Johannes Eriksson authored and Legioth committed Aug 1, 2019
1 parent c177c2c commit 2b51a24
Show file tree
Hide file tree
Showing 25 changed files with 220 additions and 36 deletions.
19 changes: 19 additions & 0 deletions flow-server-compatibility-mode/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.vaadin</groupId>
<artifactId>flow-project</artifactId>
<version>2.0-SNAPSHOT</version>
</parent>
<artifactId>flow-server-compatibility-mode</artifactId>
<name>Flow Server Compatibility Mode</name>
<packaging>jar</packaging>

<description>
This is a wrapper project for the web-fragment.xml file that sets compatibilityMode=true. When this
jar is added to the classpath of a web application, it will run in Vaadin 10-13 compatibility mode.
</description>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd"
id="WebAppFragment_ID" version="3.0">

<name>flow_server_compatibility_mode_fragment</name>

<context-param>
<param-name>compatibilityMode</param-name>
<param-value>true</param-value>
</context-param>

</web-fragment>
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class DefaultDeploymentConfiguration
+ "and \"automatic\". The default of \"disabled\" will be used."
+ SEPARATOR;

public static final String ERROR_COMPATIBILITY_MODE_UNSET =
"Unable to determine mode of operation. To use npm mode, ensure "
+ "'flow-build-info.json' exists on the classpath. With Maven, "
+ "this is handled by the 'prepare-frontend goal'. To use "
+ "compatibility mode, add the 'flow-server-compatibility-mode' "
+ "dependency.";
/**
* Default value for {@link #getHeartbeatInterval()} = {@value} .
*/
Expand Down Expand Up @@ -244,19 +250,23 @@ private void checkProductionMode(boolean loggWarning) {
}

/**
* Log a warning if Vaadin is running in bower mode.
* Log a warning if Vaadin is running in compatibility mode. Throw
* {@link IllegalStateException} if the mode could not be determined from
* parameters.
*/
private void checkCompatibilityMode(boolean loggWarning) {
String bower = getStringProperty(Constants.SERVLET_PARAMETER_BOWER_MODE,
null);
if (bower == null) {
if (getStringProperty(Constants.SERVLET_PARAMETER_BOWER_MODE, null)
!= null) {
compatibilityMode = getBooleanProperty(
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE, true);
} else {
Constants.SERVLET_PARAMETER_BOWER_MODE, false);
} else if (getStringProperty(
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE, null) != null) {
compatibilityMode = getBooleanProperty(
Constants.SERVLET_PARAMETER_BOWER_MODE, true);
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE, false);
} else {
// neither parameter given -> throw exception
throw new IllegalStateException(ERROR_COMPATIBILITY_MODE_UNSET);
}

if (compatibilityMode && loggWarning) {
getLogger().warn(WARNING_COMPATIBILITY_MODE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public void testWithDefaultClassLoader() throws Exception {

private static DeploymentConfiguration createConfigurationMock() {
Properties properties = new Properties();
properties.put(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());
properties.put(VaadinSession.UI_PARAMETER, MyUI.class.getName());
return new DefaultDeploymentConfiguration(CustomUIClassLoaderTest.class,
properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,24 @@
*/
public class DefaultDeploymentConfigurationTest {

private static Properties DEFAULT_PARAMS = new Properties();

{
DEFAULT_PARAMS.setProperty(
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());
}

@Test
public void testGetSystemPropertyForDefaultPackage()
throws ClassNotFoundException {
Class<?> clazz = Class.forName("ClassInDefaultPackage");
String value = "value";
String prop = "prop";
System.setProperty(prop, value);
Properties initParameters = new Properties(DEFAULT_PARAMS);
DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(
clazz, new Properties());
clazz, initParameters);
assertEquals(value, config.getSystemProperty(prop));
}

Expand All @@ -54,14 +63,15 @@ public void testGetSystemProperty() throws ClassNotFoundException {
DefaultDeploymentConfigurationTest.class.getPackage().getName()
+ '.' + prop,
value);
Properties initParameters = new Properties(DEFAULT_PARAMS);
DefaultDeploymentConfiguration config = new DefaultDeploymentConfiguration(
DefaultDeploymentConfigurationTest.class, new Properties());
DefaultDeploymentConfigurationTest.class, initParameters);
assertEquals(value, config.getSystemProperty(prop));
}

@Test
public void booleanValueReadIgnoreTheCase_true() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(
Constants.SERVLET_PARAMETER_SEND_URLS_AS_PARAMETERS, "tRUe");

Expand All @@ -74,7 +84,7 @@ public void booleanValueReadIgnoreTheCase_true() {

@Test
public void booleanValueReadIgnoreTheCase_false() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(
Constants.SERVLET_PARAMETER_SEND_URLS_AS_PARAMETERS, "FaLsE");

Expand All @@ -88,7 +98,7 @@ public void booleanValueReadIgnoreTheCase_false() {

@Test
public void booleanValueRead_emptyIsTrue() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(
Constants.SERVLET_PARAMETER_SEND_URLS_AS_PARAMETERS, "");

Expand All @@ -101,7 +111,7 @@ public void booleanValueRead_emptyIsTrue() {

@Test(expected = IllegalArgumentException.class)
public void booleanValueRead_exceptionOnNonBooleanValue() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(
Constants.SERVLET_PARAMETER_SEND_URLS_AS_PARAMETERS,
"incorrectValue");
Expand All @@ -111,7 +121,7 @@ public void booleanValueRead_exceptionOnNonBooleanValue() {

@Test
public void frontendPrefixes_developmentMode() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(Constants.FRONTEND_URL_ES5,
"context://build/frontend-es5/");
initParameters.setProperty(Constants.FRONTEND_URL_ES6,
Expand All @@ -137,7 +147,7 @@ public void frontendPrefixes_productionMode() {
String es5Prefix = "context://build/frontend-es5/";
String es6Prefix = "context://build/frontend-es6/";

Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(Constants.FRONTEND_URL_ES5, es5Prefix);
initParameters.setProperty(Constants.FRONTEND_URL_ES6, es6Prefix);
initParameters.setProperty(Constants.SERVLET_PARAMETER_PRODUCTION_MODE,
Expand All @@ -164,15 +174,15 @@ private DefaultDeploymentConfiguration createDeploymentConfig(

@Test
public void defaultPushUrl() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
DefaultDeploymentConfiguration config = createDeploymentConfig(
initParameters);
assertThat(config.getPushURL(), is(""));
}

@Test
public void pushUrl() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(Constants.SERVLET_PARAMETER_PUSH_URL, "foo");

DefaultDeploymentConfiguration config = createDeploymentConfig(
Expand All @@ -182,7 +192,7 @@ public void pushUrl() {

@Test
public void bundleIsEnabledInProduction() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(Constants.SERVLET_PARAMETER_PRODUCTION_MODE,
"true");
DefaultDeploymentConfiguration config = createDeploymentConfig(
Expand All @@ -192,7 +202,7 @@ public void bundleIsEnabledInProduction() {

@Test
public void bundleCanBeDisabled() {
Properties initParameters = new Properties();
Properties initParameters = new Properties(DEFAULT_PARAMS);
initParameters.setProperty(Constants.SERVLET_PARAMETER_PRODUCTION_MODE,
"true");
initParameters.setProperty(Constants.USE_ORIGINAL_FRONTEND_RESOURCES,
Expand All @@ -201,4 +211,10 @@ public void bundleCanBeDisabled() {
initParameters);
Assert.assertFalse(config.useCompiledFrontendResources());
}

@Test(expected = IllegalStateException.class)
public void throwsIfCompatibilityModeNotExplicitlySet() {
Properties initParameters = new Properties();
createDeploymentConfig(initParameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class DeploymentConfigurationFactoryTest {
private File tokenFile;
private ServletContext contextMock;

private static Map<String, String> defaultServletParams =
Collections.singletonMap(
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());

private static class NoSettings extends VaadinServlet {
}

Expand Down Expand Up @@ -73,9 +78,11 @@ public void tearDown() {
public void servletWithEnclosingUI_hasItsNameInConfig() throws Exception {
Class<TestUI.ServletWithEnclosingUi> servlet = TestUI.ServletWithEnclosingUi.class;

Map<String, String> servletConfigParams = new HashMap<>(new HashMap<>(defaultServletParams));

DeploymentConfiguration config = DeploymentConfigurationFactory
.createDeploymentConfiguration(servlet,
createServletConfigMock(emptyMap(), emptyMap()));
createServletConfigMock(servletConfigParams, emptyMap()));

Class<?> customUiClass = servlet.getEnclosingClass();
assertTrue(String.format(
Expand All @@ -92,9 +99,11 @@ public void servletWithNoEnclosingUI_hasDefaultUiInConfig()
throws Exception {
Class<NoSettings> servlet = NoSettings.class;

Map<String, String> servletConfigParams = new HashMap<>(defaultServletParams);

DeploymentConfiguration config = DeploymentConfigurationFactory
.createDeploymentConfiguration(servlet,
createServletConfigMock(emptyMap(), emptyMap()));
createServletConfigMock(servletConfigParams, emptyMap()));

Class<?> notUiClass = servlet.getEnclosingClass();
assertFalse(String.format(
Expand All @@ -109,9 +118,11 @@ public void servletWithNoEnclosingUI_hasDefaultUiInConfig()
public void vaadinServletConfigurationRead() throws Exception {
Class<VaadinSettings> servlet = VaadinSettings.class;

Map<String, String> servletConfigParams = new HashMap<>(defaultServletParams);

DeploymentConfiguration config = DeploymentConfigurationFactory
.createDeploymentConfiguration(servlet,
createServletConfigMock(emptyMap(), emptyMap()));
createServletConfigMock(servletConfigParams, emptyMap()));

assertTrue(String.format(
"Unexpected value for production mode, check '%s' class annotation",
Expand All @@ -129,7 +140,7 @@ public void servletConfigParametersOverrideVaadinParameters()
boolean overridingProductionModeValue = false;
int overridingHeartbeatIntervalValue = 444;

Map<String, String> servletConfigParams = new HashMap<>();
Map<String, String> servletConfigParams = new HashMap<>(defaultServletParams);
servletConfigParams.put(SERVLET_PARAMETER_PRODUCTION_MODE,
Boolean.toString(overridingProductionModeValue));
servletConfigParams.put(Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL,
Expand All @@ -156,7 +167,7 @@ public void servletContextParametersOverrideVaadinParameters()
boolean overridingProductionModeValue = false;
int overridingHeartbeatIntervalValue = 444;

Map<String, String> servletContextParams = new HashMap<>();
Map<String, String> servletContextParams = new HashMap<>(defaultServletParams);
servletContextParams.put(SERVLET_PARAMETER_PRODUCTION_MODE,
Boolean.toString(overridingProductionModeValue));
servletContextParams.put(Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL,
Expand All @@ -183,7 +194,7 @@ public void servletConfigParametersOverrideServletContextParameters()
boolean servletConfigProductionModeValue = true;
int servletConfigHeartbeatIntervalValue = 333;

Map<String, String> servletConfigParams = new HashMap<>();
Map<String, String> servletConfigParams = new HashMap<>(defaultServletParams);
servletConfigParams.put(SERVLET_PARAMETER_PRODUCTION_MODE,
Boolean.toString(servletConfigProductionModeValue));
servletConfigParams.put(Constants.SERVLET_PARAMETER_HEARTBEAT_INTERVAL,
Expand Down Expand Up @@ -211,11 +222,9 @@ public void servletConfigParametersOverrideServletContextParameters()
config.getHeartbeatInterval());
}

@Test
public void should_beInBowerModeByDefault() throws Exception {
DeploymentConfiguration config = createConfig(emptyMap());
assertTrue(config.isCompatibilityMode());
assertFalse(config.isProductionMode());
@Test(expected = IllegalStateException.class)
public void should_throwIfModeNotSet() throws Exception {
createConfig(emptyMap());
}

@Test
Expand Down Expand Up @@ -282,4 +291,5 @@ public Enumeration<String> getInitParameterNames() {
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;

import com.sun.net.httpserver.HttpServer;
Expand Down Expand Up @@ -330,9 +331,14 @@ private VaadinServlet prepareServlet(int port) throws ServletException {
ServletContext ctx = mock(ServletContext.class);
Mockito.doAnswer(invocation -> ctx.getClass().getClassLoader()).when(ctx).getClassLoader();
Mockito.doAnswer(invocation -> ctx).when(cfg).getServletContext();

Mockito.doAnswer(
invocation -> Collections.enumeration(Collections.emptyList()))
invocation -> Collections.enumeration(Collections.singletonList(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE)))
.when(cfg).getInitParameterNames();
Mockito.doAnswer(
invocation -> Boolean.FALSE.toString())
.when(cfg).getInitParameter(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE);

Mockito.doAnswer(
invocation -> Collections.enumeration(Collections.emptyList()))
.when(ctx).getInitParameterNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@
@NotThreadSafe
public class I18NProviderTest {

private static Properties DEFAULT_PARAMS = new Properties();

{
DEFAULT_PARAMS.setProperty(
Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());
}

@Test
public void no_property_defined_should_leave_with_default_locale()
throws ServletException, ServiceException {
initServletAndService(new Properties());
final Properties initParams = new Properties();
initParams.setProperty(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());
initServletAndService(initParams);

Assert.assertEquals("Locale was not the expected default locale",
Locale.getDefault(), VaadinSession.getCurrent().getLocale());
Expand All @@ -52,6 +63,8 @@ public void property_defined_should_init_registy_with_provider()
Properties initParams = new Properties();
initParams.setProperty(Constants.I18N_PROVIDER,
TestProvider.class.getName());
initParams.setProperty(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());

initServletAndService(initParams);

Expand All @@ -66,6 +79,8 @@ public void with_defined_provider_locale_should_be_the_available_one()
Properties initParams = new Properties();
initParams.setProperty(Constants.I18N_PROVIDER,
TestProvider.class.getName());
initParams.setProperty(Constants.SERVLET_PARAMETER_COMPATIBILITY_MODE,
Boolean.FALSE.toString());

initServletAndService(initParams);

Expand Down Expand Up @@ -142,5 +157,4 @@ public WrappedSession getWrappedSession(

return servlet;
}

}
Loading

0 comments on commit 2b51a24

Please sign in to comment.