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;
}
return memberFieldInOrder;
}
+
+ /**
+ * Clears the value of all fields.
+ *
+ * @since
+ */
+ public void clear() {
+ for (Field<?> f : getFields()) {
+ if (f instanceof AbstractField) {
+ ((AbstractField) f).clear();
+ }
+ }
+
+ }
}
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?
*
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.
*
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());
+
+ }
+
}
--- /dev/null
+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();
+ }
+
+}
import junit.framework.TestCase;
+import org.junit.Ignore;
import org.junit.Test;
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;
}
}
}
+ 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.
*
--- /dev/null
+/*
+ * 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());
+ }
+
+}
--- /dev/null
+/*
+ * 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());
+ }
+
+}