]> source.dussan.org Git - vaadin-framework.git/commitdiff
Add clear() for fields and field group (#14755)
authorArtur Signell <artur@vaadin.com>
Wed, 24 Sep 2014 14:56:36 +0000 (17:56 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 26 Sep 2014 12:40:14 +0000 (12:40 +0000)
Change-Id: If9372ccceeaacd0826f8b1ed07f64af12bf47fc6

server/src/com/vaadin/data/fieldgroup/FieldGroup.java
server/src/com/vaadin/ui/AbstractField.java
server/src/com/vaadin/ui/Form.java
server/tests/src/com/vaadin/data/fieldgroup/FieldGroupDate.java
server/tests/src/com/vaadin/data/util/BeanItemContainerGenerator.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/TestClassesSerializable.java
server/tests/src/com/vaadin/ui/TableTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/ui/TextFieldTest.java [new file with mode: 0644]

index e647bdbf6d568f5f3e763d635478320216ca9965..2873d07c5dd53972c339d2af2dcdea181ce627be 100644 (file)
@@ -28,6 +28,7 @@ import com.vaadin.data.Item;
 import com.vaadin.data.Property;
 import com.vaadin.data.Validator.InvalidValueException;
 import com.vaadin.data.util.TransactionalPropertyWrapper;
+import com.vaadin.ui.AbstractField;
 import com.vaadin.ui.DefaultFieldFactory;
 import com.vaadin.ui.Field;
 import com.vaadin.ui.Form;
@@ -1095,4 +1096,18 @@ public class FieldGroup implements Serializable {
         }
         return memberFieldInOrder;
     }
+
+    /**
+     * Clears the value of all fields.
+     * 
+     * @since
+     */
+    public void clear() {
+        for (Field<?> f : getFields()) {
+            if (f instanceof AbstractField) {
+                ((AbstractField) f).clear();
+            }
+        }
+
+    }
 }
index bb70c0c57bf354c0f6299e34d8260c95bb9df2a9..369ad1253cfce522552918111e1ad58783500d16 100644 (file)
@@ -1512,6 +1512,19 @@ public abstract class AbstractField<T> extends AbstractComponent implements
         return (getFieldValue() == null);
     }
 
+    /**
+     * Clear the value of the field.
+     * <p>
+     * The field value is typically reset to the initial value of the field but
+     * this is not mandatory. Calling {@link #isEmpty()} on a cleared field must
+     * always returns true.
+     * 
+     * @since
+     */
+    public void clear() {
+        setValue(null);
+    }
+
     /**
      * Is automatic, visible validation enabled?
      * 
index 391ee45536a5f5d7b34915bbc7a3ba3be947cec4..7b0c49ec95727ccfcc71deca0b8d9a360390bdac 100644 (file)
@@ -1202,6 +1202,21 @@ public class Form extends AbstractField<Object> implements Item.Editor,
         return true;
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.ui.AbstractField#clear()
+     */
+    @Override
+    public void clear() {
+        for (Iterator<Field<?>> i = fields.values().iterator(); i.hasNext();) {
+            Field<?> f = i.next();
+            if (f instanceof AbstractField) {
+                ((AbstractField<?>) f).clear();
+            }
+        }
+    }
+
     /**
      * Adding validators directly to form is not supported.
      * 
index 07e36b93f91cffa899581523da9858800140b98c..fd5d47b32fc77c578ad3283f82596ce7406c528a 100644 (file)
@@ -78,4 +78,20 @@ public class FieldGroupDate {
         Assert.assertEquals(PopupDateField.class, f.getClass());
     }
 
+    @Test
+    public void clearFields() {
+        PopupDateField sqlDate = new PopupDateField();
+        PopupDateField javaDate = new PopupDateField();
+        fieldGroup.bind(sqlDate, "sqlDate");
+        fieldGroup.bind(javaDate, "javaDate");
+
+        Assert.assertEquals(new Date(2010, 5, 7), javaDate.getValue());
+        Assert.assertEquals(new Date(2011, 6, 8), sqlDate.getValue());
+
+        fieldGroup.clear();
+        Assert.assertEquals(null, javaDate.getValue());
+        Assert.assertEquals(null, sqlDate.getValue());
+
+    }
+
 }
diff --git a/server/tests/src/com/vaadin/data/util/BeanItemContainerGenerator.java b/server/tests/src/com/vaadin/data/util/BeanItemContainerGenerator.java
new file mode 100644 (file)
index 0000000..7400f0e
--- /dev/null
@@ -0,0 +1,153 @@
+package com.vaadin.data.util;
+
+import java.util.Date;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.junit.Ignore;
+
+@Ignore
+public class BeanItemContainerGenerator {
+
+    public static class PortableRandom {
+        private final static long multiplier = 0x5DEECE66DL;
+        private final static long addend = 0xBL;
+        private final static long mask = (1L << 48) - 1;
+        private AtomicLong seed;
+
+        public PortableRandom(long seed) {
+            this.seed = new AtomicLong(0L);
+            setSeed(seed);
+        }
+
+        synchronized public void setSeed(long seed) {
+            seed = (seed ^ multiplier) & mask;
+            this.seed.set(seed);
+        }
+
+        public int nextInt(int n) {
+            if (n <= 0) {
+                throw new IllegalArgumentException("n must be positive");
+            }
+
+            if ((n & -n) == n) {
+                return (int) ((n * (long) next(31)) >> 31);
+            }
+
+            int bits, val;
+            do {
+                bits = next(31);
+                val = bits % n;
+            } while (bits - val + (n - 1) < 0);
+            return val;
+        }
+
+        protected int next(int bits) {
+            long oldseed, nextseed;
+            AtomicLong seed = this.seed;
+            do {
+                oldseed = seed.get();
+                nextseed = (oldseed * multiplier + addend) & mask;
+            } while (!seed.compareAndSet(oldseed, nextseed));
+            return (int) (nextseed >>> (48 - bits));
+        }
+
+        public boolean nextBoolean() {
+            return next(1) != 0;
+        }
+
+    }
+
+    public static BeanItemContainer<TestBean> createContainer(int size) {
+        return createContainer(size, new Date().getTime());
+    }
+
+    public static BeanItemContainer<TestBean> createContainer(int size,
+            long seed) {
+
+        BeanItemContainer<TestBean> container = new BeanItemContainer<TestBean>(
+                TestBean.class);
+        PortableRandom r = new PortableRandom(seed);
+        for (int i = 0; i < size; i++) {
+            container.addBean(new TestBean(r));
+        }
+
+        return container;
+
+    }
+
+    public static class TestBean {
+        private String name, address, city, country;
+        private int age, shoesize;
+
+        public int getAge() {
+            return age;
+        }
+
+        public void setAge(int age) {
+            this.age = age;
+        }
+
+        public int getShoesize() {
+            return shoesize;
+        }
+
+        public void setShoesize(int shoesize) {
+            this.shoesize = shoesize;
+        }
+
+        public TestBean(PortableRandom r) {
+            age = r.nextInt(100) + 5;
+            shoesize = r.nextInt(10) + 35;
+            name = createRandomString(r, r.nextInt(5) + 5);
+            address = createRandomString(r, r.nextInt(15) + 5) + " "
+                    + r.nextInt(100) + 1;
+            city = createRandomString(r, r.nextInt(7) + 3);
+            if (r.nextBoolean()) {
+                country = createRandomString(r, r.nextInt(4) + 4);
+            }
+        }
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public String getAddress() {
+            return address;
+        }
+
+        public void setAddress(String address) {
+            this.address = address;
+        }
+
+        public String getCity() {
+            return city;
+        }
+
+        public void setCity(String city) {
+            this.city = city;
+        }
+
+        public String getCountry() {
+            return country;
+        }
+
+        public void setCountry(String country) {
+            this.country = country;
+        }
+
+    }
+
+    public static String createRandomString(PortableRandom r, int len) {
+        StringBuilder b = new StringBuilder();
+        for (int i = 0; i < len; i++) {
+            b.append((char) (r.nextInt('z' - 'a') + 'a'));
+        }
+
+        return b.toString();
+    }
+
+}
index 63f79504ffb0a9aa657d8fcf2d6d2a361fe11264..122020947994dd1519fb63083feddb34365c5586 100644 (file)
@@ -16,6 +16,7 @@ import java.util.jar.JarFile;
 
 import junit.framework.TestCase;
 
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class TestClassesSerializable extends TestCase {
@@ -95,15 +96,8 @@ public class TestClassesSerializable extends TestCase {
             if (cls.isAnnotation() || cls.isSynthetic()) {
                 continue;
             }
-            // Don't add classes that have a @Test annotation on any methods
-            boolean testPresent = false;
-            for (Method method : cls.getMethods()) {
-                if (method.isAnnotationPresent(Test.class)) {
-                    testPresent = true;
-                    break;
-                }
-            }
-            if (testPresent) {
+            // Don't add classes that have a @Ignore annotation on the class
+            if (isTestClass(cls)) {
                 continue;
             }
 
@@ -152,6 +146,27 @@ public class TestClassesSerializable extends TestCase {
         }
     }
 
+    private boolean isTestClass(Class<?> cls) {
+        // @Ignore is used on test util classes
+        if (cls.isAnnotationPresent(Ignore.class)) {
+            return true;
+        }
+
+        if (cls.getEnclosingClass() != null
+                && isTestClass(cls.getEnclosingClass())) {
+            return true;
+        }
+
+        // Test classes with a @Test annotation on some method
+        for (Method method : cls.getMethods()) {
+            if (method.isAnnotationPresent(Test.class)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
     /**
      * Lists all class path entries by splitting the class path string.
      * 
diff --git a/server/tests/src/com/vaadin/ui/TableTest.java b/server/tests/src/com/vaadin/ui/TableTest.java
new file mode 100644 (file)
index 0000000..86237ab
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * 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.ui;
+
+import java.util.Collection;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.util.BeanItemContainerGenerator;
+
+public class TableTest {
+
+    Table table;
+
+    @Before
+    public void init() {
+        table = new Table();
+    }
+
+    @Test
+    public void initiallyEmpty() {
+        Assert.assertTrue(table.isEmpty());
+    }
+
+    @Test
+    public void emptyAfterClearSingleSelect() {
+        table.setContainerDataSource(BeanItemContainerGenerator
+                .createContainer(100));
+        Assert.assertTrue(table.isEmpty());
+        Object first = table.getContainerDataSource().getItemIds().iterator()
+                .next();
+        table.setValue(first);
+        Assert.assertEquals(first, table.getValue());
+        Assert.assertFalse(table.isEmpty());
+        table.clear();
+        Assert.assertEquals(null, table.getValue());
+        Assert.assertTrue(table.isEmpty());
+    }
+
+    @Test
+    public void emptyAfterClearMultiSelect() {
+        table.setMultiSelect(true);
+        table.setContainerDataSource(BeanItemContainerGenerator
+                .createContainer(100));
+
+        Assert.assertTrue(table.isEmpty());
+        Assert.assertArrayEquals(new Object[] {},
+                ((Collection) table.getValue()).toArray());
+
+        Object first = table.getContainerDataSource().getItemIds().iterator()
+                .next();
+        table.select(first);
+        Assert.assertArrayEquals(new Object[] { first },
+                ((Collection) table.getValue()).toArray());
+        Assert.assertFalse(table.isEmpty());
+
+        table.clear();
+        Assert.assertArrayEquals(new Object[] {},
+                ((Collection) table.getValue()).toArray());
+        Assert.assertTrue(table.isEmpty());
+    }
+
+}
diff --git a/server/tests/src/com/vaadin/ui/TextFieldTest.java b/server/tests/src/com/vaadin/ui/TextFieldTest.java
new file mode 100644 (file)
index 0000000..bfd452b
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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.ui;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.vaadin.data.util.ObjectProperty;
+
+public class TextFieldTest {
+
+    @Test
+    public void initiallyEmpty() {
+        TextField tf = new TextField();
+        Assert.assertTrue(tf.isEmpty());
+    }
+
+    @Test
+    public void emptyAfterClearUsingPDS() {
+        TextField tf = new TextField(new ObjectProperty<String>("foo"));
+        Assert.assertFalse(tf.isEmpty());
+        tf.clear();
+        Assert.assertTrue(tf.isEmpty());
+    }
+
+    @Test
+    public void emptyAfterClear() {
+        TextField tf = new TextField();
+        tf.setValue("foobar");
+        Assert.assertFalse(tf.isEmpty());
+        tf.clear();
+        Assert.assertTrue(tf.isEmpty());
+    }
+
+}