diff options
author | Matti Tahvonen <matti@vaadin.com> | 2015-07-16 18:12:57 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2016-05-02 08:38:24 +0000 |
commit | 85870ccd9f93cebd839d22d2bd63ec817bb90a3d (patch) | |
tree | a1397563807e3468429878d520874bbc8cc92d33 | |
parent | 29a6ed998041be42e2c4e986d9702fa2e798f1be (diff) | |
download | vaadin-framework-85870ccd9f93cebd839d22d2bd63ec817bb90a3d.tar.gz vaadin-framework-85870ccd9f93cebd839d22d2bd63ec817bb90a3d.zip |
Better default for nullRepresentation (#13221, #12877)
Change-Id: Ia4662c79b20ee699b3a9741ffa24c4de6645b775
5 files changed, 162 insertions, 2 deletions
diff --git a/server/src/main/java/com/vaadin/server/DefaultDeploymentConfiguration.java b/server/src/main/java/com/vaadin/server/DefaultDeploymentConfiguration.java index 1f22a9e33d..677be2e31b 100644 --- a/server/src/main/java/com/vaadin/server/DefaultDeploymentConfiguration.java +++ b/server/src/main/java/com/vaadin/server/DefaultDeploymentConfiguration.java @@ -21,6 +21,7 @@ import java.util.logging.Level; import java.util.logging.Logger; import com.vaadin.shared.communication.PushMode; +import com.vaadin.ui.AbstractTextField; /** * The default implementation of {@link DeploymentConfiguration} based on a base @@ -97,6 +98,17 @@ public class DefaultDeploymentConfiguration extends checkLegacyPropertyToString(); checkSyncIdCheck(); checkSendUrlsAsParameters(); + checkNullRepresentationLegacyMode(); + } + + private void checkNullRepresentationLegacyMode() { + final boolean legacyMode = getApplicationOrSystemProperty( + "com.vaadin.nullrepresentationlegacymode", + Boolean.toString(false)).equals("true"); + if (legacyMode) { + AbstractTextField.setNullRepresentationDefault("null"); + } + } private void checkLegacyPropertyToString() { diff --git a/server/src/main/java/com/vaadin/ui/AbstractTextField.java b/server/src/main/java/com/vaadin/ui/AbstractTextField.java index 5fbe60937a..ce1d6f638d 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractTextField.java +++ b/server/src/main/java/com/vaadin/ui/AbstractTextField.java @@ -41,10 +41,13 @@ import com.vaadin.ui.declarative.DesignContext; public abstract class AbstractTextField extends AbstractField<String> implements BlurNotifier, FocusNotifier, TextChangeNotifier, LegacyComponent { + private static String nullRepresentationDefault = ""; + /** * Null representation. */ - private String nullRepresentation = "null"; + private String nullRepresentation = nullRepresentationDefault; + /** * Is setting to null from non-null value allowed by setting with null * representation . @@ -810,4 +813,36 @@ public abstract class AbstractTextField extends AbstractField<String> implements getMaxLength(), def.getMaxLength(), Integer.class); } + /** + * @since 7.6 + * @return the default value used for nullRepresentation + */ + public static String getNullRepresentationDefault() { + return nullRepresentationDefault; + } + + /** + * A static helper to define the default value used for nullRepresentation. + * <p> + * In 7.6 the infamous default value "null" for + * AbstractTextField.nullRepresentation was changed to "", which may cause + * unexpected issues in certain applications that don't tackle null values. + * If there are several places in your application that depend on the old + * default, this method can be used to put new AbstractTextField instances + * into backwards compatibility mode by defining the default value as + * "null". The "legacy mode" can also be forced by setting system property + * "com.vaadin.nullrepresentationlegacymode" (before AbstractTextField class + * is loaded by your class loader). + * + * @since 7.6 + * @param nullRepresentationString + * the value that will be used as a default for + * {@link AbstractTextField#getNullRepresentation()} in new + * instances + */ + public static void setNullRepresentationDefault( + String nullRepresentationString) { + AbstractTextField.nullRepresentationDefault = nullRepresentationString; + } + } diff --git a/uitest/src/main/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyMode.java b/uitest/src/main/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyMode.java new file mode 100644 index 0000000000..ab0bc6e534 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyMode.java @@ -0,0 +1,84 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.tests.components.textfield; + +import com.vaadin.annotations.Theme; +import com.vaadin.data.fieldgroup.BeanFieldGroup; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.AbstractTextField; +import com.vaadin.ui.TextField; +import com.vaadin.ui.VerticalLayout; + +@SuppressWarnings("serial") +@Theme("valo") +public class NullRepresentationLegacyMode extends AbstractTestUI { + + public static class Entity { + + private String value; + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + } + + public static class Form extends VerticalLayout { + TextField value = new TextField(); + + public Form() { + setMargin(true); + setSpacing(true); + addComponent(value); + } + } + + @Override + protected void setup(VaadinRequest request) { + Form formWithoutNulls = new Form(); + formWithoutNulls.setCaption("No 'null's here please"); + formWithoutNulls.setId("without"); + BeanFieldGroup.bindFieldsUnbuffered(new Entity(), formWithoutNulls); + + // Use the legacy default + AbstractTextField.setNullRepresentationDefault("null"); + + Form formWithNulls = new Form(); + formWithNulls.setCaption("'null's please"); + formWithNulls.setId("with"); + BeanFieldGroup.bindFieldsUnbuffered(new Entity(), formWithNulls); + AbstractTextField.setNullRepresentationDefault(""); + + addComponents(formWithoutNulls, formWithNulls); + } + + @Override + protected String getTestDescription() { + return "Text field must not truncate underscores in modal dialogs."; + } + + @Override + protected Integer getTicketNumber() { + return 12974; + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/fieldgroup/MultipleValidationErrors.java b/uitest/src/main/java/com/vaadin/tests/fieldgroup/MultipleValidationErrors.java index 58f2292f84..f09b32d5c7 100644 --- a/uitest/src/main/java/com/vaadin/tests/fieldgroup/MultipleValidationErrors.java +++ b/uitest/src/main/java/com/vaadin/tests/fieldgroup/MultipleValidationErrors.java @@ -1,5 +1,7 @@ package com.vaadin.tests.fieldgroup; +import org.apache.commons.lang.StringEscapeUtils; + import com.vaadin.data.Validator; import com.vaadin.data.fieldgroup.FieldGroup; import com.vaadin.data.util.BeanItem; @@ -7,10 +9,10 @@ import com.vaadin.data.validator.BeanValidator; import com.vaadin.server.AbstractErrorMessage; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.AbstractTextField; import com.vaadin.ui.Button; import com.vaadin.ui.Label; import com.vaadin.ui.TextField; -import org.apache.commons.lang.StringEscapeUtils; public class MultipleValidationErrors extends AbstractTestUI { @@ -25,9 +27,15 @@ public class MultipleValidationErrors extends AbstractTestUI { new PersonBeanWithValidationAnnotations()); final FieldGroup fieldGroup = new FieldGroup(item); + // use old default that this test depends on + AbstractTextField.setNullRepresentationDefault("null"); + bindTextField(item, fieldGroup, "First Name", "firstName"); bindTextField(item, fieldGroup, "Last Name", "lastName"); + // Revert to new + AbstractTextField.setNullRepresentationDefault(""); + final Label validationErrors = new Label(); validationErrors.setId("validationErrors"); addComponent(validationErrors); diff --git a/uitest/src/test/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyModeTest.java b/uitest/src/test/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyModeTest.java new file mode 100644 index 0000000000..c303e8e9f1 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/textfield/NullRepresentationLegacyModeTest.java @@ -0,0 +1,21 @@ +package com.vaadin.tests.components.textfield; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +public class NullRepresentationLegacyModeTest extends MultiBrowserTest { + + @Test + public void testWindowRepositioning() throws Exception { + openTestURL(); + String without = getDriver().findElement( + By.xpath("//div[@id='without']//input")).getAttribute("value"); + String with = getDriver().findElement( + By.xpath("//div[@id='with']//input")).getAttribute("value"); + Assert.assertEquals("null", with); + Assert.assertEquals("", without); + } +} |