From 77fd42425b59805b5c30dcafc3512eecaa78e24c Mon Sep 17 00:00:00 2001 From: Denis Date: Tue, 6 Dec 2016 21:34:59 +0200 Subject: Provide a shorthand for the HasValue::clear() method. (#94) * Provide a shorthand for the HasValue::clear() method. Fixes vaadin/framework8-issues#496 --- server/src/main/java/com/vaadin/data/HasValue.java | 13 ++++++ .../java/com/vaadin/server/data/HasValueTest.java | 47 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 server/src/test/java/com/vaadin/server/data/HasValueTest.java (limited to 'server') diff --git a/server/src/main/java/com/vaadin/data/HasValue.java b/server/src/main/java/com/vaadin/data/HasValue.java index 1a612dbb80..a7fe8901db 100644 --- a/server/src/main/java/com/vaadin/data/HasValue.java +++ b/server/src/main/java/com/vaadin/data/HasValue.java @@ -257,4 +257,17 @@ public interface HasValue extends Serializable { * not. */ public boolean isReadOnly(); + + /** + * Resets the value to the empty one. + *

+ * This is just a shorthand for resetting the value, see the methods + * {@link #setValue(Object)} and {@link #getEmptyValue()}. + * + * @see #setValue(Object) + * @see #getEmptyValue() + */ + public default void clear() { + setValue(getEmptyValue()); + } } diff --git a/server/src/test/java/com/vaadin/server/data/HasValueTest.java b/server/src/test/java/com/vaadin/server/data/HasValueTest.java new file mode 100644 index 0000000000..cc3815d0e5 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/HasValueTest.java @@ -0,0 +1,47 @@ +/* + * 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.server.data; + +import org.junit.Test; +import org.mockito.Mockito; + +import com.vaadin.data.HasValue; + +/** + * @author Vaadin Ltd + * + */ +public class HasValueTest { + + public abstract static class TestHasValue implements HasValue { + @Override + public void clear() { + HasValue.super.clear(); + } + } + + @Test + public void clear() { + TestHasValue hasValue = Mockito.mock(TestHasValue.class); + Mockito.doCallRealMethod().when(hasValue).clear(); + String value = "foo"; + Mockito.when(hasValue.getEmptyValue()).thenReturn(value); + + hasValue.clear(); + + Mockito.verify(hasValue).setValue(value); + } +} -- cgit v1.2.3