]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix setValue() methods behavior null argument value + javadocs
authorTeemu Suo-Anttila <teemusa@vaadin.com>
Wed, 19 Oct 2016 11:54:33 +0000 (14:54 +0300)
committerDenis Anisimov <denis@vaadin.com>
Tue, 25 Oct 2016 07:19:36 +0000 (10:19 +0300)
Change-Id: I0000c1caf7c129634473161fe4876931f3c8dabf

20 files changed:
server/src/main/java/com/vaadin/data/HasValue.java
server/src/main/java/com/vaadin/ui/AbstractColorPicker.java
server/src/main/java/com/vaadin/ui/AbstractDateField.java
server/src/main/java/com/vaadin/ui/AbstractField.java
server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java
server/src/main/java/com/vaadin/ui/AbstractTextField.java
server/src/main/java/com/vaadin/ui/CheckBox.java
server/src/main/java/com/vaadin/ui/ComboBox.java
server/src/main/java/com/vaadin/ui/RichTextArea.java
server/src/main/java/com/vaadin/ui/Slider.java
server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerGradient.java
server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerGrid.java
server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPopup.java
server/src/main/java/com/vaadin/ui/components/colorpicker/ColorPickerPreview.java
server/src/test/java/com/vaadin/tests/server/component/abstracttextfield/AbstractTextFieldTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorPickerComponentsTest.java [new file with mode: 0644]
server/src/test/java/com/vaadin/tests/server/component/slider/SliderTest.java
server/src/test/java/com/vaadin/ui/CheckBoxTest.java
server/src/test/java/com/vaadin/ui/RichTextAreaTest.java

index 7a8bc7c296064459bd5bad37b5aec1eeec03064f..aa130344b2a7aa42544cbfdc73d0115d9ba3682e 100644 (file)
@@ -80,6 +80,7 @@ public interface HasValue<V> extends Serializable {
          *            {@code true} if this event originates from the client,
          *            {@code false} otherwise.
          */
+
         public ValueChangeEvent(Component component, HasValue<V> hasValue,
                 boolean userOriginated) {
             super(hasValue);
@@ -141,7 +142,6 @@ public interface HasValue<V> extends Serializable {
     @FunctionalInterface
     public interface ValueChangeListener<V>
             extends Consumer<ValueChangeEvent<V>>, Serializable {
-
         @Deprecated
         public static final Method VALUE_CHANGE_METHOD = ReflectTools
                 .findMethod(ValueChangeListener.class, "accept",
index f25663684528e3471f8acea4a0cd3621a204d045..a537df0b2139facb76a9a5c88d52b15e42cbe87f 100644 (file)
@@ -164,10 +164,12 @@ public abstract class AbstractColorPicker extends AbstractField<Color> {
 
     /**
      * Sets the selected color of this color picker. If the new color is not
-     * equal to getValue(), fires a value change event.
+     * equal to getValue(), fires a {@link ValueChangeEvent}.
      *
      * @param color
      *            the new selected color, not null
+     * @throws NullPointerException
+     *             if {@code color} is {@code null}
      */
     @Override
     public void setValue(Color color) {
index 4735d155d91de3f78efd6742033573497b40c38d..c62c76ac9c528d3c1a8dedd094ac2023288326dd 100644 (file)
@@ -484,6 +484,13 @@ public abstract class AbstractDateField extends AbstractField<LocalDate>
         return value;
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent} .
+     *
+     * @param value
+     *            the new value, may be {@code null}
+     */
     @Override
     public void setValue(LocalDate value) {
         /*
index 7d87053a2242ba55bff43d3a6b85585010da9b20..c87a72e4304e45ebdeefd48987d488dcb40794c0 100644 (file)
@@ -52,8 +52,8 @@ public abstract class AbstractField<T> extends AbstractComponent
         implements HasValue<T>, HasRequired, Focusable {
 
     @Deprecated
-    private static final Method VALUE_CHANGE_METHOD = ReflectTools
-            .findMethod(ValueChangeListener.class, "accept", ValueChangeEvent.class);
+    private static final Method VALUE_CHANGE_METHOD = ReflectTools.findMethod(
+            ValueChangeListener.class, "accept", ValueChangeEvent.class);
 
     @Override
     public void setValue(T value) {
index 5374b7d60824862ec4c3413b67bb2cb000ca6e1a..5ab6951f8d7d519c9270ce64fea763f831b09b5f 100644 (file)
@@ -366,7 +366,7 @@ public abstract class AbstractMultiSelect<T>
      *
      * @param value
      *            the items to select, not {@code null}
-     * @throws IllegalArgumentException
+     * @throws NullPointerException
      *             if the value is invalid
      */
     @Override
index 9c2720efd8bc1ab3857ea2bddd8a860e0f55a842..ee2ccc6604263afaf3d92f7fbeaf087ae318e83c 100644 (file)
@@ -79,9 +79,19 @@ public abstract class AbstractTextField extends AbstractField<String>
         registerRpc(new AbstractTextFieldFocusAndBlurRpcImpl());
     }
 
+    /**
+     * Sets the value of this text field. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is not null.
+     *
+     * @param value
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code value} is {@code null}
+     */
     @Override
     public void setValue(String value) {
-        Objects.requireNonNull(value, "Null value not supported");
+        Objects.requireNonNull(value, "value cannot be null");
         setValue(value, false);
     }
 
index c4bedbea0a8d695ed9c00f982063c995b6d55d6e..02d7b0db8b185a861518036601851a2d6ff675ef 100644 (file)
@@ -17,6 +17,7 @@
 package com.vaadin.ui;
 
 import java.util.Collection;
+import java.util.Objects;
 
 import org.jsoup.nodes.Attributes;
 import org.jsoup.nodes.Element;
@@ -115,21 +116,18 @@ public class CheckBox extends AbstractField<Boolean>
     }
 
     /**
-     * Sets the value of this ComboBox. If the new value is not equal to
-     * {@code getValue()}, fires a value change event. Throws
-     * {@code IllegalArgumentException} if the value is null.
+     * Sets the value of this CheckBox. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
      *
      * @param value
-     *            the new value
-     * @throws IllegalArgumentException
-     *             if the value is null
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code value} is {@code null}
      */
     @Override
     public void setValue(Boolean value) {
-        if (value == null) {
-            throw new IllegalArgumentException(
-                    "CheckBox value must not be null");
-        }
+        Objects.requireNonNull(value, "CheckBox value must not be null");
         super.setValue(value);
     }
 
index 9ebc035c122ce0a744fcbe319abd8e710bda2d03..5c40a744f77100242deaf21bcdfbf90882620620 100644 (file)
@@ -548,6 +548,13 @@ public class ComboBox<T> extends AbstractSingleSelect<T> implements HasValue<T>,
         this.filter = filter;
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}.
+     *
+     * @param value
+     *            the new value, may be {@code null}
+     */
     @Override
     public void setValue(T value) {
         getSelectionModel().setSelectedFromServer(value);
index 545166d33a7f5b4317022967b6ac54611c473c5c..96a639e285b3d7ff94874d87bbf98c4f30809278 100644 (file)
@@ -16,6 +16,8 @@
 
 package com.vaadin.ui;
 
+import java.util.Objects;
+
 import org.jsoup.nodes.Element;
 
 import com.vaadin.shared.ui.ValueChangeMode;
@@ -100,13 +102,20 @@ public class RichTextArea extends AbstractField<String>
         return (RichTextAreaState) super.getState(markAsDirty);
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     * 
+     * @param value
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code value} is {@code null}
+     */
     @Override
     public void setValue(String value) {
-        if (value == null) {
-            setValue("", false);
-        } else {
-            setValue(value, false);
-        }
+        Objects.requireNonNull(value, "value cannot be null");
+        setValue(value, false);
     }
 
     @Override
index 99ab476e2a3e4530af01fc506e944943bb4448de..397e33e7c39ecf5d47efc1f758848164a2a129c3 100644 (file)
@@ -17,6 +17,7 @@
 package com.vaadin.ui;
 
 import java.util.Collection;
+import java.util.Objects;
 
 import org.jsoup.nodes.Attributes;
 import org.jsoup.nodes.Element;
@@ -289,6 +290,22 @@ public class Slider extends AbstractField<Double> {
         getState().value = trimmedValue;
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     *
+     * @param value
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code value} is {@code null}
+     */
+    @Override
+    public void setValue(Double value) {
+        Objects.requireNonNull(value, "color cannot be null");
+        super.setValue(value);
+    }
+
     @Override
     public Double getValue() {
         return getState().value;
index ed8b12f80bcc91477bd9353c9166de10c5534b26..2e0b855ef9f08232c6f0e03890506d1554f005c4 100644 (file)
@@ -15,6 +15,8 @@
  */
 package com.vaadin.ui.components.colorpicker;
 
+import java.util.Objects;
+
 import com.vaadin.shared.ui.colorpicker.Color;
 import com.vaadin.shared.ui.colorpicker.ColorPickerGradientServerRpc;
 import com.vaadin.shared.ui.colorpicker.ColorPickerGradientState;
@@ -64,6 +66,22 @@ public class ColorPickerGradient extends AbstractField<Color> {
         this.converter = converter;
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     *
+     * @param color
+     *            the new color, not {@code null}
+     * @throws NullPointerException
+     *             if {@code color} is {@code null}
+     */
+    @Override
+    public void setValue(Color color) {
+        Objects.requireNonNull(color, "value must not be null");
+        super.setValue(color);
+    }
+
     @Override
     public Color getValue() {
         return color;
index c03c865d6d57d6dbeffde354f686a814d2c93ed3..95229a2d6a5a2214adcd0c1ad56a9ae0f6fa1fb8 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.ui.components.colorpicker;
 import java.awt.Point;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 import com.vaadin.shared.ui.colorpicker.Color;
 import com.vaadin.shared.ui.colorpicker.ColorPickerGridServerRpc;
@@ -174,6 +175,22 @@ public class ColorPickerGrid extends AbstractField<Color> {
         return new int[] { x, y };
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     *
+     * @param color
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code color} is {@code null}
+     */
+    @Override
+    public void setValue(Color color) {
+        Objects.requireNonNull(color, "value cannot be null");
+        super.setValue(color);
+    }
+
     @Override
     public Color getValue() {
         return colorGrid[x][y];
index 4cf79d862d4b050ad6cb04480724957d12c3317d..ba00773fec71ccf91964a4db9b94be0e9ffdf1be 100644 (file)
@@ -458,11 +458,19 @@ public class ColorPickerPopup extends Window implements HasValue<Color> {
         return history;
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     *
+     * @param color
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code color} is {@code null}
+     */
     @Override
     public void setValue(Color color) {
-        if (color == null) {
-            return;
-        }
+        Objects.requireNonNull(color, "color cannot be null");
 
         selectedColor = color;
 
index fd284b64b904c89370b86433feb18ef09b2e4efc..67724d134b2b1b49a47649648c992a769b33fd15 100644 (file)
@@ -63,8 +63,19 @@ public class ColorPickerPreview extends CssLayout implements HasValue<Color> {
         setValue(color);
     }
 
+    /**
+     * Sets the value of this object. If the new value is not equal to
+     * {@code getValue()}, fires a {@link ValueChangeEvent}. Throws
+     * {@code NullPointerException} if the value is null.
+     *
+     * @param color
+     *            the new value, not {@code null}
+     * @throws NullPointerException
+     *             if {@code color} is {@code null}
+     */
     @Override
     public void setValue(Color color) {
+        Objects.requireNonNull(color, "color cannot be null");
         this.color = color;
 
         // Unregister listener
@@ -160,7 +171,8 @@ public class ColorPickerPreview extends CssLayout implements HasValue<Color> {
                 }
 
                 oldValue = value;
-                fireEvent(new ValueChangeEvent<>(this, event.isUserOriginated()));
+                fireEvent(
+                        new ValueChangeEvent<>(this, event.isUserOriginated()));
             }
 
         } catch (NumberFormatException nfe) {
diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstracttextfield/AbstractTextFieldTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstracttextfield/AbstractTextFieldTest.java
new file mode 100644 (file)
index 0000000..5645c44
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2000-2016 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.server.component.abstracttextfield;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.vaadin.ui.AbstractTextField;
+
+/**
+ * @author Vaadin Ltd
+ *
+ */
+public class AbstractTextFieldTest {
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_throwsNPE() {
+        AbstractTextField field = Mockito.mock(AbstractTextField.class);
+        Mockito.doCallRealMethod().when(field).setValue(null);
+
+        field.setValue(null);
+    }
+}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerTest.java
new file mode 100644 (file)
index 0000000..f466366
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2000-2016 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.server.component.colorpicker;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.vaadin.ui.AbstractColorPicker;
+
+/**
+ * @author Vaadin Ltd
+ *
+ */
+public class AbstractColorPickerTest {
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_throwsNPE() {
+        AbstractColorPicker picker = Mockito.mock(AbstractColorPicker.class);
+        Mockito.doCallRealMethod().when(picker).setValue(null);
+
+        picker.setValue(null);
+    }
+}
diff --git a/server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorPickerComponentsTest.java b/server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorPickerComponentsTest.java
new file mode 100644 (file)
index 0000000..a655a3b
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2000-2016 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.server.component.colorpicker;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import com.vaadin.shared.ui.colorpicker.Color;
+import com.vaadin.ui.AbstractColorPicker.Coordinates2Color;
+import com.vaadin.ui.components.colorpicker.ColorPickerGradient;
+import com.vaadin.ui.components.colorpicker.ColorPickerGrid;
+import com.vaadin.ui.components.colorpicker.ColorPickerPopup;
+import com.vaadin.ui.components.colorpicker.ColorPickerPreview;
+
+/**
+ * @author Vaadin Ltd
+ *
+ */
+public class ColorPickerComponentsTest {
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_colorPickerGradientThrowsNPE() {
+        ColorPickerGradient gradient = new ColorPickerGradient("foo",
+                Mockito.mock(Coordinates2Color.class));
+        gradient.setValue(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_colorPickerGridThrowsNPE() {
+        ColorPickerGrid grid = new ColorPickerGrid();
+        grid.setValue(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_colorPickerPopupThrowsNPE() {
+        ColorPickerPopup popup = new ColorPickerPopup(Color.WHITE);
+        popup.setValue(null);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_colorPickerPreviewThrowsNPE() {
+        ColorPickerPreview preview = new ColorPickerPreview(Color.WHITE);
+        preview.setValue(null);
+    }
+}
index 0ce40cad39d740907d7f6e0478fd4de3ef8b91f6..4d178e618f27908d583a514f2c681a74d4f514c4 100644 (file)
@@ -126,4 +126,10 @@ public class SliderTest {
 
         assertThat(slider.getValue(), is(1.23));
     }
+
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_throwNPE() {
+        Slider slider = new Slider();
+        slider.setValue(null);
+    }
 }
index 04d626c7d1811cdfd2392421ad5e9aca881e108b..ee0ed943ebccfe3505e9ea508027091dcbb46c99 100644 (file)
@@ -34,4 +34,10 @@ public class CheckBoxTest {
         Assert.assertFalse(cb.getValue());
     }
 
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_throwsNPE() {
+        CheckBox cb = new CheckBox();
+        cb.setValue(null);
+    }
+
 }
index 554e0028488bccda3e9ff414965f19811f8d8fa3..6a4fd3217309226fda038016860025ded6960052 100644 (file)
@@ -88,11 +88,10 @@ public class RichTextAreaTest extends ComponentTest {
         Assert.assertEquals("bar", tf.getValue());
     }
 
-    @Test
-    public void setValueNullBecomesEmptyString() {
+    @Test(expected = NullPointerException.class)
+    public void setValue_nullValue_throwsNPE() {
         RichTextArea tf = new RichTextArea();
         tf.setValue(null);
-        Assert.assertEquals("", tf.getValue());
     }
 
     @Test