diff options
Diffstat (limited to 'compatibility-server/src/test/java/com')
28 files changed, 3068 insertions, 0 deletions
diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/AbstractFilterTestBase.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/AbstractFilterTestBase.java new file mode 100644 index 0000000000..979f472e20 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/AbstractFilterTestBase.java @@ -0,0 +1,97 @@ +package com.vaadin.data.util.filter; + +import junit.framework.TestCase; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.Property; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.util.PropertysetItem; + +public abstract class AbstractFilterTestBase<FILTERTYPE extends Filter> + extends TestCase { + + protected static final String PROPERTY1 = "property1"; + protected static final String PROPERTY2 = "property2"; + + protected static class TestItem<T1, T2> extends PropertysetItem { + + public TestItem(T1 value1, T2 value2) { + addItemProperty(PROPERTY1, new ObjectProperty<T1>(value1)); + addItemProperty(PROPERTY2, new ObjectProperty<T2>(value2)); + } + } + + protected static class NullProperty implements Property<String> { + + @Override + public String getValue() { + return null; + } + + @Override + public void setValue(String newValue) throws ReadOnlyException { + throw new ReadOnlyException(); + } + + @Override + public Class<String> getType() { + return String.class; + } + + @Override + public boolean isReadOnly() { + return true; + } + + @Override + public void setReadOnly(boolean newStatus) { + // do nothing + } + + } + + public static class SameItemFilter implements Filter { + + private final Item item; + private final Object propertyId; + + public SameItemFilter(Item item) { + this(item, ""); + } + + public SameItemFilter(Item item, Object propertyId) { + this.item = item; + this.propertyId = propertyId; + } + + @Override + public boolean passesFilter(Object itemId, Item item) + throws UnsupportedOperationException { + return this.item == item; + } + + @Override + public boolean appliesToProperty(Object propertyId) { + return this.propertyId != null ? this.propertyId.equals(propertyId) + : true; + } + + @Override + public boolean equals(Object obj) { + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } + SameItemFilter other = (SameItemFilter) obj; + return item == other.item + && (propertyId == null ? other.propertyId == null + : propertyId.equals(other.propertyId)); + } + + @Override + public int hashCode() { + return item.hashCode(); + } + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/AndOrFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/AndOrFilterTest.java new file mode 100644 index 0000000000..f825ef64c6 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/AndOrFilterTest.java @@ -0,0 +1,246 @@ +package com.vaadin.data.util.filter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.BeanItem; + +public class AndOrFilterTest + extends AbstractFilterTestBase<AbstractJunctionFilter> { + + protected Item item1 = new BeanItem<Integer>(1); + protected Item item2 = new BeanItem<Integer>(2); + + @Test + public void testNoFilterAnd() { + Filter filter = new And(); + + Assert.assertTrue(filter.passesFilter(null, item1)); + } + + @Test + public void testSingleFilterAnd() { + Filter filter = new And(new SameItemFilter(item1)); + + Assert.assertTrue(filter.passesFilter(null, item1)); + Assert.assertFalse(filter.passesFilter(null, item2)); + } + + @Test + public void testTwoFilterAnd() { + Filter filter1 = new And(new SameItemFilter(item1), + new SameItemFilter(item1)); + Filter filter2 = new And(new SameItemFilter(item1), + new SameItemFilter(item2)); + + Assert.assertTrue(filter1.passesFilter(null, item1)); + Assert.assertFalse(filter1.passesFilter(null, item2)); + + Assert.assertFalse(filter2.passesFilter(null, item1)); + Assert.assertFalse(filter2.passesFilter(null, item2)); + } + + @Test + public void testThreeFilterAnd() { + Filter filter1 = new And(new SameItemFilter(item1), + new SameItemFilter(item1), new SameItemFilter(item1)); + Filter filter2 = new And(new SameItemFilter(item1), + new SameItemFilter(item1), new SameItemFilter(item2)); + + Assert.assertTrue(filter1.passesFilter(null, item1)); + Assert.assertFalse(filter1.passesFilter(null, item2)); + + Assert.assertFalse(filter2.passesFilter(null, item1)); + Assert.assertFalse(filter2.passesFilter(null, item2)); + } + + @Test + public void testNoFilterOr() { + Filter filter = new Or(); + + Assert.assertFalse(filter.passesFilter(null, item1)); + } + + @Test + public void testSingleFilterOr() { + Filter filter = new Or(new SameItemFilter(item1)); + + Assert.assertTrue(filter.passesFilter(null, item1)); + Assert.assertFalse(filter.passesFilter(null, item2)); + } + + @Test + public void testTwoFilterOr() { + Filter filter1 = new Or(new SameItemFilter(item1), + new SameItemFilter(item1)); + Filter filter2 = new Or(new SameItemFilter(item1), + new SameItemFilter(item2)); + + Assert.assertTrue(filter1.passesFilter(null, item1)); + Assert.assertFalse(filter1.passesFilter(null, item2)); + + Assert.assertTrue(filter2.passesFilter(null, item1)); + Assert.assertTrue(filter2.passesFilter(null, item2)); + } + + @Test + public void testThreeFilterOr() { + Filter filter1 = new Or(new SameItemFilter(item1), + new SameItemFilter(item1), new SameItemFilter(item1)); + Filter filter2 = new Or(new SameItemFilter(item1), + new SameItemFilter(item1), new SameItemFilter(item2)); + + Assert.assertTrue(filter1.passesFilter(null, item1)); + Assert.assertFalse(filter1.passesFilter(null, item2)); + + Assert.assertTrue(filter2.passesFilter(null, item1)); + Assert.assertTrue(filter2.passesFilter(null, item2)); + } + + @Test + public void testAndEqualsHashCode() { + Filter filter0 = new And(); + Filter filter0b = new And(); + Filter filter1a = new And(new SameItemFilter(item1)); + Filter filter1a2 = new And(new SameItemFilter(item1)); + Filter filter1b = new And(new SameItemFilter(item2)); + Filter filter2a = new And(new SameItemFilter(item1), + new SameItemFilter(item1)); + Filter filter2b = new And(new SameItemFilter(item1), + new SameItemFilter(item2)); + Filter filter2b2 = new And(new SameItemFilter(item1), + new SameItemFilter(item2)); + Filter other0 = new Or(); + Filter other1 = new Or(new SameItemFilter(item1)); + + Assert.assertEquals(filter0, filter0); + Assert.assertEquals(filter0, filter0b); + Assert.assertFalse(filter0.equals(filter1a)); + Assert.assertFalse(filter0.equals(other0)); + Assert.assertFalse(filter0.equals(other1)); + + Assert.assertFalse(filter1a.equals(filter1b)); + Assert.assertFalse(filter1a.equals(other1)); + + Assert.assertFalse(filter1a.equals(filter2a)); + Assert.assertFalse(filter2a.equals(filter1a)); + + Assert.assertFalse(filter2a.equals(filter2b)); + Assert.assertEquals(filter2b, filter2b2); + + // hashCode() + Assert.assertEquals(filter0.hashCode(), filter0.hashCode()); + Assert.assertEquals(filter0.hashCode(), filter0b.hashCode()); + Assert.assertEquals(filter1a.hashCode(), filter1a.hashCode()); + Assert.assertEquals(filter1a.hashCode(), filter1a2.hashCode()); + Assert.assertEquals(filter2a.hashCode(), filter2a.hashCode()); + Assert.assertEquals(filter2b.hashCode(), filter2b2.hashCode()); + } + + @Test + public void testOrEqualsHashCode() { + Filter filter0 = new Or(); + Filter filter0b = new Or(); + Filter filter1a = new Or(new SameItemFilter(item1)); + Filter filter1a2 = new Or(new SameItemFilter(item1)); + Filter filter1b = new Or(new SameItemFilter(item2)); + Filter filter2a = new Or(new SameItemFilter(item1), + new SameItemFilter(item1)); + Filter filter2b = new Or(new SameItemFilter(item1), + new SameItemFilter(item2)); + Filter filter2b2 = new Or(new SameItemFilter(item1), + new SameItemFilter(item2)); + Filter other0 = new And(); + Filter other1 = new And(new SameItemFilter(item1)); + + Assert.assertEquals(filter0, filter0); + Assert.assertEquals(filter0, filter0b); + Assert.assertFalse(filter0.equals(filter1a)); + Assert.assertFalse(filter0.equals(other0)); + Assert.assertFalse(filter0.equals(other1)); + + Assert.assertFalse(filter1a.equals(filter1b)); + Assert.assertFalse(filter1a.equals(other1)); + + Assert.assertFalse(filter1a.equals(filter2a)); + Assert.assertFalse(filter2a.equals(filter1a)); + + Assert.assertFalse(filter2a.equals(filter2b)); + Assert.assertEquals(filter2b, filter2b2); + + // hashCode() + Assert.assertEquals(filter0.hashCode(), filter0.hashCode()); + Assert.assertEquals(filter0.hashCode(), filter0b.hashCode()); + Assert.assertEquals(filter1a.hashCode(), filter1a.hashCode()); + Assert.assertEquals(filter1a.hashCode(), filter1a2.hashCode()); + Assert.assertEquals(filter2a.hashCode(), filter2a.hashCode()); + Assert.assertEquals(filter2b.hashCode(), filter2b2.hashCode()); + } + + @Test + public void testAndAppliesToProperty() { + Filter filter0 = new And(); + Filter filter1a = new And(new SameItemFilter(item1, "a")); + Filter filter1b = new And(new SameItemFilter(item1, "b")); + Filter filter2aa = new And(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "a")); + Filter filter2ab = new And(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "b")); + Filter filter3abc = new And(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "b"), new SameItemFilter(item1, "c")); + + // empty And does not filter out anything + Assert.assertFalse(filter0.appliesToProperty("a")); + Assert.assertFalse(filter0.appliesToProperty("d")); + + Assert.assertTrue(filter1a.appliesToProperty("a")); + Assert.assertFalse(filter1a.appliesToProperty("b")); + Assert.assertFalse(filter1b.appliesToProperty("a")); + Assert.assertTrue(filter1b.appliesToProperty("b")); + + Assert.assertTrue(filter2aa.appliesToProperty("a")); + Assert.assertFalse(filter2aa.appliesToProperty("b")); + Assert.assertTrue(filter2ab.appliesToProperty("a")); + Assert.assertTrue(filter2ab.appliesToProperty("b")); + + Assert.assertTrue(filter3abc.appliesToProperty("a")); + Assert.assertTrue(filter3abc.appliesToProperty("b")); + Assert.assertTrue(filter3abc.appliesToProperty("c")); + Assert.assertFalse(filter3abc.appliesToProperty("d")); + } + + @Test + public void testOrAppliesToProperty() { + Filter filter0 = new Or(); + Filter filter1a = new Or(new SameItemFilter(item1, "a")); + Filter filter1b = new Or(new SameItemFilter(item1, "b")); + Filter filter2aa = new Or(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "a")); + Filter filter2ab = new Or(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "b")); + Filter filter3abc = new Or(new SameItemFilter(item1, "a"), + new SameItemFilter(item1, "b"), new SameItemFilter(item1, "c")); + + // empty Or filters out everything + Assert.assertTrue(filter0.appliesToProperty("a")); + Assert.assertTrue(filter0.appliesToProperty("d")); + + Assert.assertTrue(filter1a.appliesToProperty("a")); + Assert.assertFalse(filter1a.appliesToProperty("b")); + Assert.assertFalse(filter1b.appliesToProperty("a")); + Assert.assertTrue(filter1b.appliesToProperty("b")); + + Assert.assertTrue(filter2aa.appliesToProperty("a")); + Assert.assertFalse(filter2aa.appliesToProperty("b")); + Assert.assertTrue(filter2ab.appliesToProperty("a")); + Assert.assertTrue(filter2ab.appliesToProperty("b")); + + Assert.assertTrue(filter3abc.appliesToProperty("a")); + Assert.assertTrue(filter3abc.appliesToProperty("b")); + Assert.assertTrue(filter3abc.appliesToProperty("c")); + Assert.assertFalse(filter3abc.appliesToProperty("d")); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java new file mode 100644 index 0000000000..7c3dba9db3 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterDateTest.java @@ -0,0 +1,142 @@ +package com.vaadin.data.util.filter; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.util.PropertysetItem; +import com.vaadin.data.util.filter.Compare.Equal; +import com.vaadin.data.util.filter.Compare.Greater; +import com.vaadin.data.util.filter.Compare.GreaterOrEqual; +import com.vaadin.data.util.filter.Compare.Less; +import com.vaadin.data.util.filter.Compare.LessOrEqual; + +public class CompareFilterDateTest extends AbstractFilterTestBase<Compare> { + + protected Item itemNullUtilDate; + protected Item itemNullSqlDate; + protected Item itemUtilDate; + protected Item itemSqlDate; + + protected SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy"); + + protected Filter equalCompUtilDate; + protected Filter greaterCompUtilDate; + protected Filter lessCompUtilDate; + protected Filter greaterEqualCompUtilDate; + protected Filter lessEqualCompUtilDate; + + protected Filter equalCompSqlDate; + protected Filter greaterCompSqlDate; + protected Filter lessCompSqlDate; + protected Filter greaterEqualCompSqlDate; + protected Filter lessEqualCompSqlDate; + + @Override + protected void setUp() throws Exception { + super.setUp(); + + equalCompUtilDate = new Equal(PROPERTY1, formatter.parse("26072016")); + greaterCompUtilDate = new Greater(PROPERTY1, + formatter.parse("26072016")); + lessCompUtilDate = new Less(PROPERTY1, formatter.parse("26072016")); + greaterEqualCompUtilDate = new GreaterOrEqual(PROPERTY1, + formatter.parse("26072016")); + lessEqualCompUtilDate = new LessOrEqual(PROPERTY1, + formatter.parse("26072016")); + + equalCompSqlDate = new Equal(PROPERTY1, + new java.sql.Date(formatter.parse("26072016").getTime())); + greaterCompSqlDate = new Greater(PROPERTY1, + new java.sql.Date(formatter.parse("26072016").getTime())); + lessCompSqlDate = new Less(PROPERTY1, + new java.sql.Date(formatter.parse("26072016").getTime())); + greaterEqualCompSqlDate = new GreaterOrEqual(PROPERTY1, + new java.sql.Date(formatter.parse("26072016").getTime())); + lessEqualCompSqlDate = new LessOrEqual(PROPERTY1, + new java.sql.Date(formatter.parse("26072016").getTime())); + + itemNullUtilDate = new PropertysetItem(); + itemNullUtilDate.addItemProperty(PROPERTY1, + new ObjectProperty<Date>(null, Date.class)); + itemNullSqlDate = new PropertysetItem(); + itemNullSqlDate.addItemProperty(PROPERTY1, + new ObjectProperty<java.sql.Date>(null, java.sql.Date.class)); + itemUtilDate = new PropertysetItem(); + itemUtilDate.addItemProperty(PROPERTY1, new ObjectProperty<Date>( + formatter.parse("25072016"), Date.class)); + itemSqlDate = new PropertysetItem(); + itemSqlDate.addItemProperty(PROPERTY1, + new ObjectProperty<java.sql.Date>( + new java.sql.Date( + formatter.parse("25072016").getTime()), + java.sql.Date.class)); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + itemNullUtilDate = null; + itemNullSqlDate = null; + itemUtilDate = null; + itemSqlDate = null; + } + + @Test + public void testCompareUtilDatesAndUtilDates() { + Assert.assertFalse( + equalCompUtilDate.passesFilter(null, itemNullUtilDate)); + Assert.assertFalse(equalCompUtilDate.passesFilter(null, itemUtilDate)); + Assert.assertFalse( + greaterCompUtilDate.passesFilter(null, itemUtilDate)); + Assert.assertTrue(lessCompUtilDate.passesFilter(null, itemUtilDate)); + Assert.assertFalse( + greaterEqualCompUtilDate.passesFilter(null, itemUtilDate)); + Assert.assertTrue( + lessEqualCompUtilDate.passesFilter(null, itemUtilDate)); + } + + @Test + public void testCompareUtilDatesAndSqlDates() { + Assert.assertFalse( + equalCompUtilDate.passesFilter(null, itemNullSqlDate)); + Assert.assertFalse(equalCompUtilDate.passesFilter(null, itemSqlDate)); + Assert.assertFalse(greaterCompUtilDate.passesFilter(null, itemSqlDate)); + Assert.assertTrue(lessCompUtilDate.passesFilter(null, itemSqlDate)); + Assert.assertFalse( + greaterEqualCompUtilDate.passesFilter(null, itemSqlDate)); + Assert.assertTrue( + lessEqualCompUtilDate.passesFilter(null, itemSqlDate)); + } + + @Test + public void testCompareSqlDatesAndSqlDates() { + Assert.assertFalse( + equalCompSqlDate.passesFilter(null, itemNullSqlDate)); + Assert.assertFalse(equalCompSqlDate.passesFilter(null, itemSqlDate)); + Assert.assertFalse(greaterCompSqlDate.passesFilter(null, itemSqlDate)); + Assert.assertTrue(lessCompSqlDate.passesFilter(null, itemSqlDate)); + Assert.assertFalse( + greaterEqualCompSqlDate.passesFilter(null, itemSqlDate)); + Assert.assertTrue(lessEqualCompSqlDate.passesFilter(null, itemSqlDate)); + } + + @Test + public void testCompareSqlDatesAndUtilDates() { + Assert.assertFalse( + equalCompSqlDate.passesFilter(null, itemNullUtilDate)); + Assert.assertFalse(equalCompSqlDate.passesFilter(null, itemUtilDate)); + Assert.assertFalse(greaterCompSqlDate.passesFilter(null, itemUtilDate)); + Assert.assertTrue(lessCompSqlDate.passesFilter(null, itemUtilDate)); + Assert.assertFalse( + greaterEqualCompSqlDate.passesFilter(null, itemUtilDate)); + Assert.assertTrue( + lessEqualCompSqlDate.passesFilter(null, itemUtilDate)); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterTest.java new file mode 100644 index 0000000000..4bdad3c54d --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/CompareFilterTest.java @@ -0,0 +1,322 @@ +package com.vaadin.data.util.filter; + +import java.math.BigDecimal; +import java.util.Date; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.util.PropertysetItem; +import com.vaadin.data.util.filter.Compare.Equal; +import com.vaadin.data.util.filter.Compare.Greater; +import com.vaadin.data.util.filter.Compare.GreaterOrEqual; +import com.vaadin.data.util.filter.Compare.Less; +import com.vaadin.data.util.filter.Compare.LessOrEqual; + +public class CompareFilterTest extends AbstractFilterTestBase<Compare> { + + protected Item itemNull; + protected Item itemEmpty; + protected Item itemA; + protected Item itemB; + protected Item itemC; + + protected final Filter equalB = new Equal(PROPERTY1, "b"); + protected final Filter greaterB = new Greater(PROPERTY1, "b"); + protected final Filter lessB = new Less(PROPERTY1, "b"); + protected final Filter greaterEqualB = new GreaterOrEqual(PROPERTY1, "b"); + protected final Filter lessEqualB = new LessOrEqual(PROPERTY1, "b"); + + protected final Filter equalNull = new Equal(PROPERTY1, null); + protected final Filter greaterNull = new Greater(PROPERTY1, null); + protected final Filter lessNull = new Less(PROPERTY1, null); + protected final Filter greaterEqualNull = new GreaterOrEqual(PROPERTY1, + null); + protected final Filter lessEqualNull = new LessOrEqual(PROPERTY1, null); + + @Override + protected void setUp() throws Exception { + super.setUp(); + itemNull = new PropertysetItem(); + itemNull.addItemProperty(PROPERTY1, + new ObjectProperty<String>(null, String.class)); + itemEmpty = new PropertysetItem(); + itemEmpty.addItemProperty(PROPERTY1, + new ObjectProperty<String>("", String.class)); + itemA = new PropertysetItem(); + itemA.addItemProperty(PROPERTY1, + new ObjectProperty<String>("a", String.class)); + itemB = new PropertysetItem(); + itemB.addItemProperty(PROPERTY1, + new ObjectProperty<String>("b", String.class)); + itemC = new PropertysetItem(); + itemC.addItemProperty(PROPERTY1, + new ObjectProperty<String>("c", String.class)); + } + + @Override + protected void tearDown() throws Exception { + super.tearDown(); + itemNull = null; + itemEmpty = null; + itemA = null; + itemB = null; + } + + @Test + public void testCompareString() { + Assert.assertFalse(equalB.passesFilter(null, itemEmpty)); + Assert.assertFalse(equalB.passesFilter(null, itemA)); + Assert.assertTrue(equalB.passesFilter(null, itemB)); + Assert.assertFalse(equalB.passesFilter(null, itemC)); + + Assert.assertFalse(greaterB.passesFilter(null, itemEmpty)); + Assert.assertFalse(greaterB.passesFilter(null, itemA)); + Assert.assertFalse(greaterB.passesFilter(null, itemB)); + Assert.assertTrue(greaterB.passesFilter(null, itemC)); + + Assert.assertTrue(lessB.passesFilter(null, itemEmpty)); + Assert.assertTrue(lessB.passesFilter(null, itemA)); + Assert.assertFalse(lessB.passesFilter(null, itemB)); + Assert.assertFalse(lessB.passesFilter(null, itemC)); + + Assert.assertFalse(greaterEqualB.passesFilter(null, itemEmpty)); + Assert.assertFalse(greaterEqualB.passesFilter(null, itemA)); + Assert.assertTrue(greaterEqualB.passesFilter(null, itemB)); + Assert.assertTrue(greaterEqualB.passesFilter(null, itemC)); + + Assert.assertTrue(lessEqualB.passesFilter(null, itemEmpty)); + Assert.assertTrue(lessEqualB.passesFilter(null, itemA)); + Assert.assertTrue(lessEqualB.passesFilter(null, itemB)); + Assert.assertFalse(lessEqualB.passesFilter(null, itemC)); + } + + @Test + public void testCompareWithNull() { + // null comparisons: null is less than any other value + Assert.assertFalse(equalB.passesFilter(null, itemNull)); + Assert.assertTrue(greaterB.passesFilter(null, itemNull)); + Assert.assertFalse(lessB.passesFilter(null, itemNull)); + Assert.assertTrue(greaterEqualB.passesFilter(null, itemNull)); + Assert.assertFalse(lessEqualB.passesFilter(null, itemNull)); + + Assert.assertTrue(equalNull.passesFilter(null, itemNull)); + Assert.assertFalse(greaterNull.passesFilter(null, itemNull)); + Assert.assertFalse(lessNull.passesFilter(null, itemNull)); + Assert.assertTrue(greaterEqualNull.passesFilter(null, itemNull)); + Assert.assertTrue(lessEqualNull.passesFilter(null, itemNull)); + + Assert.assertFalse(equalNull.passesFilter(null, itemA)); + Assert.assertFalse(greaterNull.passesFilter(null, itemA)); + Assert.assertTrue(lessNull.passesFilter(null, itemA)); + Assert.assertFalse(greaterEqualNull.passesFilter(null, itemA)); + Assert.assertTrue(lessEqualNull.passesFilter(null, itemA)); + } + + @Test + public void testCompareInteger() { + int negative = -1; + int zero = 0; + int positive = 1; + + Item itemNegative = new PropertysetItem(); + itemNegative.addItemProperty(PROPERTY1, + new ObjectProperty<Integer>(negative, Integer.class)); + Item itemZero = new PropertysetItem(); + itemZero.addItemProperty(PROPERTY1, + new ObjectProperty<Integer>(zero, Integer.class)); + Item itemPositive = new PropertysetItem(); + itemPositive.addItemProperty(PROPERTY1, + new ObjectProperty<Integer>(positive, Integer.class)); + + Filter equalZero = new Equal(PROPERTY1, zero); + Assert.assertFalse(equalZero.passesFilter(null, itemNegative)); + Assert.assertTrue(equalZero.passesFilter(null, itemZero)); + Assert.assertFalse(equalZero.passesFilter(null, itemPositive)); + + Filter isPositive = new Greater(PROPERTY1, zero); + Assert.assertFalse(isPositive.passesFilter(null, itemNegative)); + Assert.assertFalse(isPositive.passesFilter(null, itemZero)); + Assert.assertTrue(isPositive.passesFilter(null, itemPositive)); + + Filter isNegative = new Less(PROPERTY1, zero); + Assert.assertTrue(isNegative.passesFilter(null, itemNegative)); + Assert.assertFalse(isNegative.passesFilter(null, itemZero)); + Assert.assertFalse(isNegative.passesFilter(null, itemPositive)); + + Filter isNonNegative = new GreaterOrEqual(PROPERTY1, zero); + Assert.assertFalse(isNonNegative.passesFilter(null, itemNegative)); + Assert.assertTrue(isNonNegative.passesFilter(null, itemZero)); + Assert.assertTrue(isNonNegative.passesFilter(null, itemPositive)); + + Filter isNonPositive = new LessOrEqual(PROPERTY1, zero); + Assert.assertTrue(isNonPositive.passesFilter(null, itemNegative)); + Assert.assertTrue(isNonPositive.passesFilter(null, itemZero)); + Assert.assertFalse(isNonPositive.passesFilter(null, itemPositive)); + } + + @Test + public void testCompareBigDecimal() { + BigDecimal negative = new BigDecimal(-1); + BigDecimal zero = new BigDecimal(0); + BigDecimal positive = new BigDecimal(1); + positive.setScale(1); + BigDecimal positiveScaleTwo = new BigDecimal(1).setScale(2); + + Item itemNegative = new PropertysetItem(); + itemNegative.addItemProperty(PROPERTY1, + new ObjectProperty<BigDecimal>(negative, BigDecimal.class)); + Item itemZero = new PropertysetItem(); + itemZero.addItemProperty(PROPERTY1, + new ObjectProperty<BigDecimal>(zero, BigDecimal.class)); + Item itemPositive = new PropertysetItem(); + itemPositive.addItemProperty(PROPERTY1, + new ObjectProperty<BigDecimal>(positive, BigDecimal.class)); + Item itemPositiveScaleTwo = new PropertysetItem(); + itemPositiveScaleTwo.addItemProperty(PROPERTY1, + new ObjectProperty<BigDecimal>(positiveScaleTwo, + BigDecimal.class)); + + Filter equalZero = new Equal(PROPERTY1, zero); + Assert.assertFalse(equalZero.passesFilter(null, itemNegative)); + Assert.assertTrue(equalZero.passesFilter(null, itemZero)); + Assert.assertFalse(equalZero.passesFilter(null, itemPositive)); + + Filter isPositive = new Greater(PROPERTY1, zero); + Assert.assertFalse(isPositive.passesFilter(null, itemNegative)); + Assert.assertFalse(isPositive.passesFilter(null, itemZero)); + Assert.assertTrue(isPositive.passesFilter(null, itemPositive)); + + Filter isNegative = new Less(PROPERTY1, zero); + Assert.assertTrue(isNegative.passesFilter(null, itemNegative)); + Assert.assertFalse(isNegative.passesFilter(null, itemZero)); + Assert.assertFalse(isNegative.passesFilter(null, itemPositive)); + + Filter isNonNegative = new GreaterOrEqual(PROPERTY1, zero); + Assert.assertFalse(isNonNegative.passesFilter(null, itemNegative)); + Assert.assertTrue(isNonNegative.passesFilter(null, itemZero)); + Assert.assertTrue(isNonNegative.passesFilter(null, itemPositive)); + + Filter isNonPositive = new LessOrEqual(PROPERTY1, zero); + Assert.assertTrue(isNonPositive.passesFilter(null, itemNegative)); + Assert.assertTrue(isNonPositive.passesFilter(null, itemZero)); + Assert.assertFalse(isNonPositive.passesFilter(null, itemPositive)); + + Filter isPositiveScaleTwo = new Equal(PROPERTY1, positiveScaleTwo); + Assert.assertTrue( + isPositiveScaleTwo.passesFilter(null, itemPositiveScaleTwo)); + Assert.assertTrue(isPositiveScaleTwo.passesFilter(null, itemPositive)); + + } + + @Test + public void testCompareDate() { + Date now = new Date(); + // new Date() is only accurate to the millisecond, so repeating it gives + // the same date + Date earlier = new Date(now.getTime() - 1); + Date later = new Date(now.getTime() + 1); + + Item itemEarlier = new PropertysetItem(); + itemEarlier.addItemProperty(PROPERTY1, + new ObjectProperty<Date>(earlier, Date.class)); + Item itemNow = new PropertysetItem(); + itemNow.addItemProperty(PROPERTY1, + new ObjectProperty<Date>(now, Date.class)); + Item itemLater = new PropertysetItem(); + itemLater.addItemProperty(PROPERTY1, + new ObjectProperty<Date>(later, Date.class)); + + Filter equalNow = new Equal(PROPERTY1, now); + Assert.assertFalse(equalNow.passesFilter(null, itemEarlier)); + Assert.assertTrue(equalNow.passesFilter(null, itemNow)); + Assert.assertFalse(equalNow.passesFilter(null, itemLater)); + + Filter after = new Greater(PROPERTY1, now); + Assert.assertFalse(after.passesFilter(null, itemEarlier)); + Assert.assertFalse(after.passesFilter(null, itemNow)); + Assert.assertTrue(after.passesFilter(null, itemLater)); + + Filter before = new Less(PROPERTY1, now); + Assert.assertTrue(before.passesFilter(null, itemEarlier)); + Assert.assertFalse(before.passesFilter(null, itemNow)); + Assert.assertFalse(before.passesFilter(null, itemLater)); + + Filter afterOrNow = new GreaterOrEqual(PROPERTY1, now); + Assert.assertFalse(afterOrNow.passesFilter(null, itemEarlier)); + Assert.assertTrue(afterOrNow.passesFilter(null, itemNow)); + Assert.assertTrue(afterOrNow.passesFilter(null, itemLater)); + + Filter beforeOrNow = new LessOrEqual(PROPERTY1, now); + Assert.assertTrue(beforeOrNow.passesFilter(null, itemEarlier)); + Assert.assertTrue(beforeOrNow.passesFilter(null, itemNow)); + Assert.assertFalse(beforeOrNow.passesFilter(null, itemLater)); + } + + @Test + public void testCompareAppliesToProperty() { + Filter filterA = new Equal("a", 1); + Filter filterB = new Equal("b", 1); + + Assert.assertTrue(filterA.appliesToProperty("a")); + Assert.assertFalse(filterA.appliesToProperty("b")); + Assert.assertFalse(filterB.appliesToProperty("a")); + Assert.assertTrue(filterB.appliesToProperty("b")); + } + + @Test + public void testCompareEqualsHashCode() { + // most checks with Equal filter, then only some with others + Filter equalNull2 = new Equal(PROPERTY1, null); + Filter equalNullProperty2 = new Equal(PROPERTY2, null); + Filter equalEmpty = new Equal(PROPERTY1, ""); + Filter equalEmpty2 = new Equal(PROPERTY1, ""); + Filter equalEmptyProperty2 = new Equal(PROPERTY2, ""); + Filter equalA = new Equal(PROPERTY1, "a"); + Filter equalB2 = new Equal(PROPERTY1, "b"); + Filter equalBProperty2 = new Equal(PROPERTY2, "b"); + + Filter greaterEmpty = new Greater(PROPERTY1, ""); + + // equals() + Assert.assertEquals(equalNull, equalNull); + Assert.assertEquals(equalNull, equalNull2); + Assert.assertFalse(equalNull.equals(equalNullProperty2)); + Assert.assertFalse(equalNull.equals(equalEmpty)); + Assert.assertFalse(equalNull.equals(equalB)); + + Assert.assertEquals(equalEmpty, equalEmpty); + Assert.assertFalse(equalEmpty.equals(equalNull)); + Assert.assertEquals(equalEmpty, equalEmpty2); + Assert.assertFalse(equalEmpty.equals(equalEmptyProperty2)); + Assert.assertFalse(equalEmpty.equals(equalB)); + + Assert.assertEquals(equalB, equalB); + Assert.assertFalse(equalB.equals(equalNull)); + Assert.assertFalse(equalB.equals(equalEmpty)); + Assert.assertEquals(equalB, equalB2); + Assert.assertFalse(equalB.equals(equalBProperty2)); + Assert.assertFalse(equalB.equals(equalA)); + + Assert.assertEquals(greaterB, greaterB); + Assert.assertFalse(greaterB.equals(lessB)); + Assert.assertFalse(greaterB.equals(greaterEqualB)); + Assert.assertFalse(greaterB.equals(lessEqualB)); + + Assert.assertFalse(greaterNull.equals(greaterEmpty)); + Assert.assertFalse(greaterNull.equals(greaterB)); + Assert.assertFalse(greaterEmpty.equals(greaterNull)); + Assert.assertFalse(greaterEmpty.equals(greaterB)); + Assert.assertFalse(greaterB.equals(greaterNull)); + Assert.assertFalse(greaterB.equals(greaterEmpty)); + + // hashCode() + Assert.assertEquals(equalNull.hashCode(), equalNull2.hashCode()); + Assert.assertEquals(equalEmpty.hashCode(), equalEmpty2.hashCode()); + Assert.assertEquals(equalB.hashCode(), equalB2.hashCode()); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/IsNullFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/IsNullFilterTest.java new file mode 100644 index 0000000000..18013cd41c --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/IsNullFilterTest.java @@ -0,0 +1,61 @@ +package com.vaadin.data.util.filter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.util.PropertysetItem; + +public class IsNullFilterTest extends AbstractFilterTestBase<IsNull> { + + @Test + public void testIsNull() { + Item item1 = new PropertysetItem(); + item1.addItemProperty("a", + new ObjectProperty<String>(null, String.class)); + item1.addItemProperty("b", + new ObjectProperty<String>("b", String.class)); + Item item2 = new PropertysetItem(); + item2.addItemProperty("a", + new ObjectProperty<String>("a", String.class)); + item2.addItemProperty("b", + new ObjectProperty<String>(null, String.class)); + + Filter filter1 = new IsNull("a"); + Filter filter2 = new IsNull("b"); + + Assert.assertTrue(filter1.passesFilter(null, item1)); + Assert.assertFalse(filter1.passesFilter(null, item2)); + Assert.assertFalse(filter2.passesFilter(null, item1)); + Assert.assertTrue(filter2.passesFilter(null, item2)); + } + + @Test + public void testIsNullAppliesToProperty() { + Filter filterA = new IsNull("a"); + Filter filterB = new IsNull("b"); + + Assert.assertTrue(filterA.appliesToProperty("a")); + Assert.assertFalse(filterA.appliesToProperty("b")); + Assert.assertFalse(filterB.appliesToProperty("a")); + Assert.assertTrue(filterB.appliesToProperty("b")); + } + + @Test + public void testIsNullEqualsHashCode() { + Filter filter1 = new IsNull("a"); + Filter filter1b = new IsNull("a"); + Filter filter2 = new IsNull("b"); + + // equals() + Assert.assertEquals(filter1, filter1b); + Assert.assertFalse(filter1.equals(filter2)); + Assert.assertFalse(filter1.equals(new And())); + + // hashCode() + Assert.assertEquals(filter1.hashCode(), filter1b.hashCode()); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/LikeFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/LikeFilterTest.java new file mode 100644 index 0000000000..39054008cd --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/LikeFilterTest.java @@ -0,0 +1,48 @@ +/* + * 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.data.util.filter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Item; +import com.vaadin.data.util.ObjectProperty; +import com.vaadin.data.util.PropertysetItem; + +public class LikeFilterTest extends AbstractFilterTestBase<Like> { + + protected Item item1 = new PropertysetItem(); + protected Item item2 = new PropertysetItem(); + protected Item item3 = new PropertysetItem(); + + @Test + public void testLikeWithNulls() { + + Like filter = new Like("value", "a"); + + item1.addItemProperty("value", new ObjectProperty<String>("a")); + item2.addItemProperty("value", new ObjectProperty<String>("b")); + item3.addItemProperty("value", + new ObjectProperty<String>(null, String.class)); + + Assert.assertTrue(filter.passesFilter(null, item1)); + Assert.assertFalse(filter.passesFilter(null, item2)); + Assert.assertFalse(filter.passesFilter(null, item3)); + + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/NotFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/NotFilterTest.java new file mode 100644 index 0000000000..e797b484f8 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/NotFilterTest.java @@ -0,0 +1,54 @@ +package com.vaadin.data.util.filter; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.Container.Filter; +import com.vaadin.data.Item; +import com.vaadin.data.util.BeanItem; + +public class NotFilterTest extends AbstractFilterTestBase<Not> { + + protected Item item1 = new BeanItem<Integer>(1); + protected Item item2 = new BeanItem<Integer>(2); + + @Test + public void testNot() { + Filter origFilter = new SameItemFilter(item1); + Filter filter = new Not(origFilter); + + Assert.assertTrue(origFilter.passesFilter(null, item1)); + Assert.assertFalse(origFilter.passesFilter(null, item2)); + Assert.assertFalse(filter.passesFilter(null, item1)); + Assert.assertTrue(filter.passesFilter(null, item2)); + } + + @Test + public void testANotAppliesToProperty() { + Filter filterA = new Not(new SameItemFilter(item1, "a")); + Filter filterB = new Not(new SameItemFilter(item1, "b")); + + Assert.assertTrue(filterA.appliesToProperty("a")); + Assert.assertFalse(filterA.appliesToProperty("b")); + Assert.assertFalse(filterB.appliesToProperty("a")); + Assert.assertTrue(filterB.appliesToProperty("b")); + } + + @Test + public void testNotEqualsHashCode() { + Filter origFilter = new SameItemFilter(item1); + Filter filter1 = new Not(origFilter); + Filter filter1b = new Not(new SameItemFilter(item1)); + Filter filter2 = new Not(new SameItemFilter(item2)); + + // equals() + Assert.assertEquals(filter1, filter1b); + Assert.assertFalse(filter1.equals(filter2)); + Assert.assertFalse(filter1.equals(origFilter)); + Assert.assertFalse(filter1.equals(new And())); + + // hashCode() + Assert.assertEquals(filter1.hashCode(), filter1b.hashCode()); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/data/util/filter/SimpleStringFilterTest.java b/compatibility-server/src/test/java/com/vaadin/data/util/filter/SimpleStringFilterTest.java new file mode 100644 index 0000000000..dd8267107a --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/data/util/filter/SimpleStringFilterTest.java @@ -0,0 +1,139 @@ +package com.vaadin.data.util.filter; + +import org.junit.Assert; +import org.junit.Test; + +public class SimpleStringFilterTest + extends AbstractFilterTestBase<SimpleStringFilter> { + + protected static TestItem<String, String> createTestItem() { + return new TestItem<String, String>("abcde", "TeSt"); + } + + protected TestItem<String, String> getTestItem() { + return createTestItem(); + } + + protected SimpleStringFilter f(Object propertyId, String filterString, + boolean ignoreCase, boolean onlyMatchPrefix) { + return new SimpleStringFilter(propertyId, filterString, ignoreCase, + onlyMatchPrefix); + } + + protected boolean passes(Object propertyId, String filterString, + boolean ignoreCase, boolean onlyMatchPrefix) { + return f(propertyId, filterString, ignoreCase, onlyMatchPrefix) + .passesFilter(null, getTestItem()); + } + + @Test + public void testStartsWithCaseSensitive() { + Assert.assertTrue(passes(PROPERTY1, "ab", false, true)); + Assert.assertTrue(passes(PROPERTY1, "", false, true)); + + Assert.assertFalse(passes(PROPERTY2, "ab", false, true)); + Assert.assertFalse(passes(PROPERTY1, "AB", false, true)); + } + + @Test + public void testStartsWithCaseInsensitive() { + Assert.assertTrue(passes(PROPERTY1, "AB", true, true)); + Assert.assertTrue(passes(PROPERTY2, "te", true, true)); + Assert.assertFalse(passes(PROPERTY2, "AB", true, true)); + } + + @Test + public void testContainsCaseSensitive() { + Assert.assertTrue(passes(PROPERTY1, "ab", false, false)); + Assert.assertTrue(passes(PROPERTY1, "abcde", false, false)); + Assert.assertTrue(passes(PROPERTY1, "cd", false, false)); + Assert.assertTrue(passes(PROPERTY1, "e", false, false)); + Assert.assertTrue(passes(PROPERTY1, "", false, false)); + + Assert.assertFalse(passes(PROPERTY2, "ab", false, false)); + Assert.assertFalse(passes(PROPERTY1, "es", false, false)); + } + + @Test + public void testContainsCaseInsensitive() { + Assert.assertTrue(passes(PROPERTY1, "AB", true, false)); + Assert.assertTrue(passes(PROPERTY1, "aBcDe", true, false)); + Assert.assertTrue(passes(PROPERTY1, "CD", true, false)); + Assert.assertTrue(passes(PROPERTY1, "", true, false)); + + Assert.assertTrue(passes(PROPERTY2, "es", true, false)); + + Assert.assertFalse(passes(PROPERTY2, "ab", true, false)); + } + + @Test + public void testAppliesToProperty() { + SimpleStringFilter filter = f(PROPERTY1, "ab", false, true); + Assert.assertTrue(filter.appliesToProperty(PROPERTY1)); + Assert.assertFalse(filter.appliesToProperty(PROPERTY2)); + Assert.assertFalse(filter.appliesToProperty("other")); + } + + @Test + public void testEqualsHashCode() { + SimpleStringFilter filter = f(PROPERTY1, "ab", false, true); + + SimpleStringFilter f1 = f(PROPERTY2, "ab", false, true); + SimpleStringFilter f1b = f(PROPERTY2, "ab", false, true); + SimpleStringFilter f2 = f(PROPERTY1, "cd", false, true); + SimpleStringFilter f2b = f(PROPERTY1, "cd", false, true); + SimpleStringFilter f3 = f(PROPERTY1, "ab", true, true); + SimpleStringFilter f3b = f(PROPERTY1, "ab", true, true); + SimpleStringFilter f4 = f(PROPERTY1, "ab", false, false); + SimpleStringFilter f4b = f(PROPERTY1, "ab", false, false); + + // equal but not same instance + Assert.assertEquals(f1, f1b); + Assert.assertEquals(f2, f2b); + Assert.assertEquals(f3, f3b); + Assert.assertEquals(f4, f4b); + + // more than one property differ + Assert.assertFalse(f1.equals(f2)); + Assert.assertFalse(f1.equals(f3)); + Assert.assertFalse(f1.equals(f4)); + Assert.assertFalse(f2.equals(f1)); + Assert.assertFalse(f2.equals(f3)); + Assert.assertFalse(f2.equals(f4)); + Assert.assertFalse(f3.equals(f1)); + Assert.assertFalse(f3.equals(f2)); + Assert.assertFalse(f3.equals(f4)); + Assert.assertFalse(f4.equals(f1)); + Assert.assertFalse(f4.equals(f2)); + Assert.assertFalse(f4.equals(f3)); + + // only one property differs + Assert.assertFalse(filter.equals(f1)); + Assert.assertFalse(filter.equals(f2)); + Assert.assertFalse(filter.equals(f3)); + Assert.assertFalse(filter.equals(f4)); + + Assert.assertFalse(f1.equals(null)); + Assert.assertFalse(f1.equals(new Object())); + + Assert.assertEquals(f1.hashCode(), f1b.hashCode()); + Assert.assertEquals(f2.hashCode(), f2b.hashCode()); + Assert.assertEquals(f3.hashCode(), f3b.hashCode()); + Assert.assertEquals(f4.hashCode(), f4b.hashCode()); + } + + @Test + public void testNonExistentProperty() { + Assert.assertFalse(passes("other1", "ab", false, true)); + } + + @Test + public void testNullValueForProperty() { + TestItem<String, String> item = createTestItem(); + item.addItemProperty("other1", new NullProperty()); + + Assert.assertFalse( + f("other1", "ab", false, true).passesFilter(null, item)); + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractBeanContainerListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractBeanContainerListenersTest.java new file mode 100644 index 0000000000..98a2513515 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractBeanContainerListenersTest.java @@ -0,0 +1,16 @@ +package com.vaadin.tests.server; + +import com.vaadin.data.Container.PropertySetChangeEvent; +import com.vaadin.data.Container.PropertySetChangeListener; +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; + +public class AbstractBeanContainerListenersTest + extends AbstractListenerMethodsTestBase { + public void testPropertySetChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(BeanItemContainer.class, + PropertySetChangeEvent.class, PropertySetChangeListener.class, + new BeanItemContainer<PropertySetChangeListener>( + PropertySetChangeListener.class)); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractContainerListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractContainerListenersTest.java new file mode 100644 index 0000000000..5e0d464fe8 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractContainerListenersTest.java @@ -0,0 +1,26 @@ +package com.vaadin.tests.server; + +import org.junit.Test; + +import com.vaadin.data.Container.ItemSetChangeEvent; +import com.vaadin.data.Container.ItemSetChangeListener; +import com.vaadin.data.Container.PropertySetChangeEvent; +import com.vaadin.data.Container.PropertySetChangeListener; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; + +public class AbstractContainerListenersTest + extends AbstractListenerMethodsTestBase { + + @Test + public void testItemSetChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(IndexedContainer.class, + ItemSetChangeEvent.class, ItemSetChangeListener.class); + } + + @Test + public void testPropertySetChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(IndexedContainer.class, + PropertySetChangeEvent.class, PropertySetChangeListener.class); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractInMemoryContainerListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractInMemoryContainerListenersTest.java new file mode 100644 index 0000000000..d5a7131182 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/AbstractInMemoryContainerListenersTest.java @@ -0,0 +1,14 @@ +package com.vaadin.tests.server; + +import com.vaadin.data.Container.ItemSetChangeEvent; +import com.vaadin.data.Container.ItemSetChangeListener; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; + +public class AbstractInMemoryContainerListenersTest + extends AbstractListenerMethodsTestBase { + public void testItemSetChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(IndexedContainer.class, + ItemSetChangeEvent.class, ItemSetChangeListener.class); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/IndexedContainerListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/IndexedContainerListenersTest.java new file mode 100644 index 0000000000..d8a1290b68 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/IndexedContainerListenersTest.java @@ -0,0 +1,26 @@ +package com.vaadin.tests.server; + +import org.junit.Test; + +import com.vaadin.data.Container.PropertySetChangeEvent; +import com.vaadin.data.Container.PropertySetChangeListener; +import com.vaadin.data.Property.ValueChangeEvent; +import com.vaadin.data.Property.ValueChangeListener; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; + +public class IndexedContainerListenersTest + extends AbstractListenerMethodsTestBase { + + @Test + public void testValueChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(IndexedContainer.class, ValueChangeEvent.class, + ValueChangeListener.class); + } + + @Test + public void testPropertySetChangeListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(IndexedContainer.class, + PropertySetChangeEvent.class, PropertySetChangeListener.class); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java new file mode 100644 index 0000000000..30b0729ace --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/SerializationTest.java @@ -0,0 +1,135 @@ +package com.vaadin.tests.server; + +import static org.junit.Assert.assertNotNull; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +import org.junit.Test; + +import com.vaadin.data.Item; +import com.vaadin.data.Property; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.data.util.MethodProperty; +import com.vaadin.server.VaadinSession; +import com.vaadin.v7.data.validator.LegacyRegexpValidator; + +public class SerializationTest { + + @Test + public void testValidators() throws Exception { + LegacyRegexpValidator validator = new LegacyRegexpValidator(".*", + "Error"); + validator.validate("aaa"); + LegacyRegexpValidator validator2 = serializeAndDeserialize(validator); + validator2.validate("aaa"); + } + + @Test + public void testIndedexContainerItemIds() throws Exception { + IndexedContainer ic = new IndexedContainer(); + ic.addContainerProperty("prop1", String.class, null); + Object id = ic.addItem(); + ic.getItem(id).getItemProperty("prop1").setValue("1"); + + Item item2 = ic.addItem("item2"); + item2.getItemProperty("prop1").setValue("2"); + + serializeAndDeserialize(ic); + } + + @Test + public void testMethodPropertyGetter() throws Exception { + MethodProperty<?> mp = new MethodProperty<Object>(new Data(), + "dummyGetter"); + serializeAndDeserialize(mp); + } + + @Test + public void testMethodPropertyGetterAndSetter() throws Exception { + MethodProperty<?> mp = new MethodProperty<Object>(new Data(), + "dummyGetterAndSetter"); + serializeAndDeserialize(mp); + } + + @Test + public void testMethodPropertyInt() throws Exception { + MethodProperty<?> mp = new MethodProperty<Object>(new Data(), + "dummyInt"); + serializeAndDeserialize(mp); + } + + @Test + public void testVaadinSession() throws Exception { + VaadinSession session = new VaadinSession(null); + + session = serializeAndDeserialize(session); + + assertNotNull( + "Pending access queue was not recreated after deserialization", + session.getPendingAccessQueue()); + } + + private static <S extends Serializable> S serializeAndDeserialize(S s) + throws IOException, ClassNotFoundException { + // Serialize and deserialize + + ByteArrayOutputStream bs = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(bs); + out.writeObject(s); + byte[] data = bs.toByteArray(); + ObjectInputStream in = new ObjectInputStream( + new ByteArrayInputStream(data)); + @SuppressWarnings("unchecked") + S s2 = (S) in.readObject(); + + // using special toString(Object) method to avoid calling + // Property.toString(), which will be temporarily disabled + // TODO This is hilariously broken (#12723) + if (s.equals(s2)) { + System.out.println(toString(s) + " equals " + toString(s2)); + } else { + System.out.println(toString(s) + " does NOT equal " + toString(s2)); + } + + return s2; + } + + private static String toString(Object o) { + if (o instanceof Property) { + return String.valueOf(((Property<?>) o).getValue()); + } else { + return String.valueOf(o); + } + } + + public static class Data implements Serializable { + private String dummyGetter; + private String dummyGetterAndSetter; + private int dummyInt; + + public String getDummyGetterAndSetter() { + return dummyGetterAndSetter; + } + + public void setDummyGetterAndSetter(String dummyGetterAndSetter) { + this.dummyGetterAndSetter = dummyGetterAndSetter; + } + + public int getDummyInt() { + return dummyInt; + } + + public void setDummyInt(int dummyInt) { + this.dummyInt = dummyInt; + } + + public String getDummyGetter() { + return dummyGetter; + } + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java new file mode 100644 index 0000000000..7d011afc8b --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarBasicsTest.java @@ -0,0 +1,290 @@ +/* + * 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.calendar; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.TimeZone; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.ui.Calendar; +import com.vaadin.ui.Calendar.TimeFormat; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.BackwardEvent; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.DateClickEvent; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.EventResize; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.ForwardEvent; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.MoveEvent; +import com.vaadin.ui.components.calendar.CalendarComponentEvents.WeekClick; +import com.vaadin.ui.components.calendar.event.BasicEventProvider; +import com.vaadin.ui.components.calendar.event.CalendarEventProvider; + +/** + * Basic API tests for the calendar + */ +public class CalendarBasicsTest { + + @Test + public void testEmptyConstructorInitialization() { + + Calendar calendar = new Calendar(); + + // The calendar should have a basic event provider with no events + CalendarEventProvider provider = calendar.getEventProvider(); + assertNotNull("Event provider should not be null", provider); + + // Basic event handlers should be registered + assertNotNull(calendar.getHandler(BackwardEvent.EVENT_ID)); + assertNotNull(calendar.getHandler(ForwardEvent.EVENT_ID)); + assertNotNull(calendar.getHandler(WeekClick.EVENT_ID)); + assertNotNull(calendar.getHandler(DateClickEvent.EVENT_ID)); + assertNotNull(calendar.getHandler(MoveEvent.EVENT_ID)); + assertNotNull(calendar.getHandler(EventResize.EVENT_ID)); + + // Calendar should have undefined size + assertTrue(calendar.getWidth() < 0); + assertTrue(calendar.getHeight() < 0); + } + + @Test + public void testConstructorWithCaption() { + final String caption = "My Calendar Caption"; + Calendar calendar = new Calendar(caption); + assertEquals(caption, calendar.getCaption()); + } + + @Test + public void testConstructorWithCustomEventProvider() { + BasicEventProvider myProvider = new BasicEventProvider(); + Calendar calendar = new Calendar(myProvider); + assertEquals(myProvider, calendar.getEventProvider()); + } + + @Test + public void testConstructorWithCustomEventProviderAndCaption() { + BasicEventProvider myProvider = new BasicEventProvider(); + final String caption = "My Calendar Caption"; + Calendar calendar = new Calendar(caption, myProvider); + assertEquals(caption, calendar.getCaption()); + assertEquals(myProvider, calendar.getEventProvider()); + } + + @Test + public void testDefaultStartAndEndDates() { + Calendar calendar = new Calendar(); + + // If no start and end date is set the calendar will display the current + // week + java.util.Calendar c = new GregorianCalendar(); + java.util.Calendar c2 = new GregorianCalendar(); + + c2.setTime(calendar.getStartDate()); + assertEquals(c.getFirstDayOfWeek(), + c2.get(java.util.Calendar.DAY_OF_WEEK)); + c2.setTime(calendar.getEndDate()); + + c.set(java.util.Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); + assertEquals(c.get(java.util.Calendar.DAY_OF_WEEK), + c2.get(java.util.Calendar.DAY_OF_WEEK)); + } + + @Test + public void testCustomStartAndEndDates() { + Calendar calendar = new Calendar(); + java.util.Calendar c = new GregorianCalendar(); + + Date start = c.getTime(); + c.add(java.util.Calendar.DATE, 3); + Date end = c.getTime(); + + calendar.setStartDate(start); + calendar.setEndDate(end); + + assertEquals(start.getTime(), calendar.getStartDate().getTime()); + assertEquals(end.getTime(), calendar.getEndDate().getTime()); + } + + @Test + public void testCustomLocale() { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.CANADA_FRENCH); + + // Setting the locale should set the internal calendars locale + assertEquals(Locale.CANADA_FRENCH, calendar.getLocale()); + java.util.Calendar c = new GregorianCalendar(Locale.CANADA_FRENCH); + assertEquals(c.getTimeZone().getRawOffset(), + calendar.getInternalCalendar().getTimeZone().getRawOffset()); + } + + @Test + public void testTimeFormat() { + Calendar calendar = new Calendar(); + + // The default timeformat depends on the current locale + calendar.setLocale(Locale.ENGLISH); + assertEquals(TimeFormat.Format12H, calendar.getTimeFormat()); + + calendar.setLocale(Locale.ITALIAN); + assertEquals(TimeFormat.Format24H, calendar.getTimeFormat()); + + // Setting a specific time format overrides the locale + calendar.setTimeFormat(TimeFormat.Format12H); + assertEquals(TimeFormat.Format12H, calendar.getTimeFormat()); + } + + @Test + public void testTimeZone() { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.CANADA_FRENCH); + + // By default the calendars timezone is returned + assertEquals(calendar.getInternalCalendar().getTimeZone(), + calendar.getTimeZone()); + + // One can override the default behaviour by specifying a timezone + TimeZone customTimeZone = TimeZone.getTimeZone("Europe/Helsinki"); + calendar.setTimeZone(customTimeZone); + assertEquals(customTimeZone, calendar.getTimeZone()); + } + + @Test + public void testVisibleDaysOfWeek() { + Calendar calendar = new Calendar(); + + // The defaults are the whole week + assertEquals(1, calendar.getFirstVisibleDayOfWeek()); + assertEquals(7, calendar.getLastVisibleDayOfWeek()); + + calendar.setFirstVisibleDayOfWeek(0); // Invalid input + assertEquals(1, calendar.getFirstVisibleDayOfWeek()); + + calendar.setLastVisibleDayOfWeek(0); // Invalid input + assertEquals(7, calendar.getLastVisibleDayOfWeek()); + + calendar.setFirstVisibleDayOfWeek(8); // Invalid input + assertEquals(1, calendar.getFirstVisibleDayOfWeek()); + + calendar.setLastVisibleDayOfWeek(8); // Invalid input + assertEquals(7, calendar.getLastVisibleDayOfWeek()); + + calendar.setFirstVisibleDayOfWeek(4); + assertEquals(4, calendar.getFirstVisibleDayOfWeek()); + + calendar.setLastVisibleDayOfWeek(6); + assertEquals(6, calendar.getLastVisibleDayOfWeek()); + + calendar.setFirstVisibleDayOfWeek(7); // Invalid since last day is 6 + assertEquals(4, calendar.getFirstVisibleDayOfWeek()); + + calendar.setLastVisibleDayOfWeek(2); // Invalid since first day is 4 + assertEquals(6, calendar.getLastVisibleDayOfWeek()); + } + + @Test + public void testVisibleHoursInDay() { + Calendar calendar = new Calendar(); + + // Defaults are the whole day + assertEquals(0, calendar.getFirstVisibleHourOfDay()); + assertEquals(23, calendar.getLastVisibleHourOfDay()); + } + + @Test + public void isClientChangeAllowed_connectorEnabled() { + TestCalendar calendar = new TestCalendar(true); + Assert.assertTrue( + "Calendar with enabled connector doesn't allow client change", + calendar.isClientChangeAllowed()); + } + + // regression test to ensure old functionality is not broken + @Test + public void defaultFirstDayOfWeek() { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.GERMAN); + // simulating consequences of markAsDirty + calendar.beforeClientResponse(true); + assertEquals(java.util.Calendar.MONDAY, + calendar.getInternalCalendar().getFirstDayOfWeek()); + } + + @Test + public void customFirstDayOfWeek() { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.GERMAN); + calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY); + + // simulating consequences of markAsDirty + calendar.beforeClientResponse(true); + assertEquals(java.util.Calendar.SUNDAY, + calendar.getInternalCalendar().getFirstDayOfWeek()); + } + + @Test + public void customFirstDayOfWeekCanSetEvenBeforeLocale() { + Calendar calendar = new Calendar(); + calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY); + + calendar.setLocale(Locale.GERMAN); + // simulating consequences of markAsDirty + calendar.beforeClientResponse(true); + assertEquals(java.util.Calendar.SUNDAY, + calendar.getInternalCalendar().getFirstDayOfWeek()); + } + + @Test + public void customFirstDayOfWeekSetNullRestoresDefault() { + Calendar calendar = new Calendar(); + calendar.setLocale(Locale.GERMAN); + calendar.setFirstDayOfWeek(java.util.Calendar.SUNDAY); + calendar.setFirstDayOfWeek(null); + // simulating consequences of markAsDirty + calendar.beforeClientResponse(true); + assertEquals(java.util.Calendar.MONDAY, + calendar.getInternalCalendar().getFirstDayOfWeek()); + } + + @Test(expected = IllegalArgumentException.class) + public void customFirstDayOfWeekValidation() { + Calendar calendar = new Calendar(); + int someWrongDayOfWeek = 10; + calendar.setFirstDayOfWeek(someWrongDayOfWeek); + } + + private static class TestCalendar extends Calendar { + TestCalendar(boolean connectorEnabled) { + isConnectorEnabled = connectorEnabled; + } + + @Override + public boolean isConnectorEnabled() { + return isConnectorEnabled; + } + + @Override + public boolean isClientChangeAllowed() { + return super.isClientChangeAllowed(); + } + + private final boolean isConnectorEnabled; + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarDeclarativeTest.java new file mode 100644 index 0000000000..f6896ff15d --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/CalendarDeclarativeTest.java @@ -0,0 +1,62 @@ +/* + * 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.calendar; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Calendar; +import com.vaadin.ui.Calendar.TimeFormat; + +public class CalendarDeclarativeTest extends DeclarativeTestBase<Calendar> { + + @Test + public void testEmpty() { + verifyDeclarativeDesign("<vaadin-calendar></vaadin-calendar>", + new Calendar()); + } + + @Test + public void testCalendarAllFeatures() throws ParseException { + String design = "<vaadin-calendar start-date='2014-11-17' end-date='2014-11-23' " + + "first-visible-day-of-week=2 last-visible-day-of-week=5 " + + "time-zone='EST' time-format='12h' first-visible-hour-of-day=8 " + + "last-visible-hour-of-day=18 weekly-caption-format='mmm MM/dd' />"; + + DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); + Calendar calendar = new Calendar(); + calendar.setStartDate(format.parse("2014-11-17")); + calendar.setEndDate(format.parse("2014-11-23")); + calendar.setFirstVisibleDayOfWeek(2); + calendar.setLastVisibleDayOfWeek(5); + calendar.setTimeZone(TimeZone.getTimeZone("EST")); + calendar.setTimeFormat(TimeFormat.Format12H); + calendar.setFirstVisibleHourOfDay(8); + calendar.setLastVisibleHourOfDay(18); + calendar.setWeeklyCaptionFormat("mmm MM/dd"); + verifyDeclarativeDesign(design, calendar); + } + + protected void verifyDeclarativeDesign(String design, Calendar expected) { + testRead(design, expected); + testWrite(design, expected); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerDataSourceTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerDataSourceTest.java new file mode 100644 index 0000000000..9cc78269f8 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerDataSourceTest.java @@ -0,0 +1,396 @@ +/* + * 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.calendar; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.Date; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.data.Container.Indexed; +import com.vaadin.data.Container.Sortable; +import com.vaadin.data.Item; +import com.vaadin.data.Property; +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.ui.Calendar; +import com.vaadin.ui.components.calendar.ContainerEventProvider; +import com.vaadin.ui.components.calendar.event.BasicEvent; +import com.vaadin.ui.components.calendar.event.CalendarEvent; + +public class ContainerDataSourceTest { + + private Calendar calendar; + + @Before + public void setUp() { + calendar = new Calendar(); + } + + /** + * Tests adding a bean item container to the Calendar + */ + @Test + public void testWithBeanItemContainer() { + + // Create a container to use as a datasource + Indexed container = createTestBeanItemContainer(); + + // Set datasource + calendar.setContainerDataSource(container); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart()); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Test the all events are returned + List<CalendarEvent> events = calendar.getEventProvider() + .getEvents(start, end); + assertEquals(container.size(), events.size()); + + // Test that a certain range is returned + cal.setTime(((CalendarEvent) container.getIdByIndex(6)).getStart()); + end = cal.getTime(); + events = calendar.getEventProvider().getEvents(start, end); + assertEquals(6, events.size()); + } + + /** + * This tests tests that if you give the Calendar an unsorted (== not sorted + * by starting date) container then the calendar should gracefully handle + * it. In this case the size of the container will be wrong. The test is + * exactly the same as {@link #testWithBeanItemContainer()} except that the + * beans has been intentionally sorted by caption instead of date. + */ + @Test + public void testWithUnsortedBeanItemContainer() { + // Create a container to use as a datasource + Indexed container = createTestBeanItemContainer(); + + // Make the container sorted by caption + ((Sortable) container).sort(new Object[] { "caption" }, + new boolean[] { true }); + + // Set data source + calendar.setContainerDataSource(container); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart()); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Test the all events are returned + List<CalendarEvent> events = calendar.getEventProvider() + .getEvents(start, end); + assertEquals(container.size(), events.size()); + + // Test that a certain range is returned + cal.setTime(((CalendarEvent) container.getIdByIndex(6)).getStart()); + end = cal.getTime(); + events = calendar.getEventProvider().getEvents(start, end); + + // The events size is 1 since the getEvents returns the wrong range + assertEquals(1, events.size()); + } + + /** + * Tests adding a Indexed container to the Calendar + */ + @Test + public void testWithIndexedContainer() { + + // Create a container to use as a datasource + Indexed container = createTestIndexedContainer(); + + // Set datasource + calendar.setContainerDataSource(container, "testCaption", + "testDescription", "testStartDate", "testEndDate", null); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + cal.setTime((Date) container.getItem(container.getIdByIndex(0)) + .getItemProperty("testStartDate").getValue()); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Test the all events are returned + List<CalendarEvent> events = calendar.getEventProvider() + .getEvents(start, end); + assertEquals(container.size(), events.size()); + + // Check that event values are present + CalendarEvent e = events.get(0); + assertEquals("Test 1", e.getCaption()); + assertEquals("Description 1", e.getDescription()); + assertTrue(e.getStart().compareTo(start) == 0); + + // Test that a certain range is returned + cal.setTime((Date) container.getItem(container.getIdByIndex(6)) + .getItemProperty("testStartDate").getValue()); + end = cal.getTime(); + events = calendar.getEventProvider().getEvents(start, end); + assertEquals(6, events.size()); + } + + @Test + public void testNullLimitsBeanItemContainer() { + // Create a container to use as a datasource + Indexed container = createTestBeanItemContainer(); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + cal.setTime(((CalendarEvent) container.getIdByIndex(0)).getStart()); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Set datasource + calendar.setContainerDataSource(container); + + // Test null start time + List<CalendarEvent> events = calendar.getEventProvider().getEvents(null, + end); + assertEquals(container.size(), events.size()); + + // Test null end time + events = calendar.getEventProvider().getEvents(start, null); + assertEquals(container.size(), events.size()); + + // Test both null times + events = calendar.getEventProvider().getEvents(null, null); + assertEquals(container.size(), events.size()); + } + + @Test + public void testNullLimitsIndexedContainer() { + // Create a container to use as a datasource + Indexed container = createTestIndexedContainer(); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + cal.setTime((Date) container.getItem(container.getIdByIndex(0)) + .getItemProperty("testStartDate").getValue()); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Set datasource + calendar.setContainerDataSource(container, "testCaption", + "testDescription", "testStartDate", "testEndDate", null); + + // Test null start time + List<CalendarEvent> events = calendar.getEventProvider().getEvents(null, + end); + assertEquals(container.size(), events.size()); + + // Test null end time + events = calendar.getEventProvider().getEvents(start, null); + assertEquals(container.size(), events.size()); + + // Test both null times + events = calendar.getEventProvider().getEvents(null, null); + assertEquals(container.size(), events.size()); + } + + /** + * Tests the addEvent convenience method with the default event provider + */ + @Test + public void testAddEventConvinienceMethod() { + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + + // Add an event + BasicEvent event = new BasicEvent("Test", "Test", start); + calendar.addEvent(event); + + // Ensure event exists + List<CalendarEvent> events = calendar.getEvents(start, end); + assertEquals(1, events.size()); + assertEquals(events.get(0).getCaption(), event.getCaption()); + assertEquals(events.get(0).getDescription(), event.getDescription()); + assertEquals(events.get(0).getStart(), event.getStart()); + } + + /** + * Test the removeEvent convenience method with the default event provider + */ + @Test + public void testRemoveEventConvinienceMethod() { + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + + // Add an event + CalendarEvent event = new BasicEvent("Test", "Test", start); + calendar.addEvent(event); + + // Ensure event exists + assertEquals(1, calendar.getEvents(start, end).size()); + + // Remove event + calendar.removeEvent(event); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + } + + @Test + public void testAddEventConvinienceMethodWithCustomEventProvider() { + + // Use a container data source + calendar.setEventProvider(new ContainerEventProvider( + new BeanItemContainer<BasicEvent>(BasicEvent.class))); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + + // Add an event + BasicEvent event = new BasicEvent("Test", "Test", start); + calendar.addEvent(event); + + // Ensure event exists + List<CalendarEvent> events = calendar.getEvents(start, end); + assertEquals(1, events.size()); + assertEquals(events.get(0).getCaption(), event.getCaption()); + assertEquals(events.get(0).getDescription(), event.getDescription()); + assertEquals(events.get(0).getStart(), event.getStart()); + } + + @Test + public void testRemoveEventConvinienceMethodWithCustomEventProvider() { + + // Use a container data source + calendar.setEventProvider(new ContainerEventProvider( + new BeanItemContainer<BasicEvent>(BasicEvent.class))); + + // Start and end dates to query for + java.util.Calendar cal = java.util.Calendar.getInstance(); + Date start = cal.getTime(); + cal.add(java.util.Calendar.MONTH, 1); + Date end = cal.getTime(); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + + // Add an event + BasicEvent event = new BasicEvent("Test", "Test", start); + calendar.addEvent(event); + + // Ensure event exists + List<CalendarEvent> events = calendar.getEvents(start, end); + assertEquals(1, events.size()); + + // Remove event + calendar.removeEvent(event); + + // Ensure no events + assertEquals(0, calendar.getEvents(start, end).size()); + } + + @Test + public void testStyleNamePropertyRetrieved() { + IndexedContainer ic = (IndexedContainer) createTestIndexedContainer(); + ic.addContainerProperty("testStyleName", String.class, ""); + for (int i = 0; i < 10; i++) { + Item item = ic.getItem(ic.getIdByIndex(i)); + @SuppressWarnings("unchecked") + Property<String> itemProperty = item + .getItemProperty("testStyleName"); + itemProperty.setValue("testStyle"); + } + + ContainerEventProvider provider = new ContainerEventProvider(ic); + provider.setCaptionProperty("testCaption"); + provider.setDescriptionProperty("testDescription"); + provider.setStartDateProperty("testStartDate"); + provider.setEndDateProperty("testEndDate"); + provider.setStyleNameProperty("testStyleName"); + + calendar.setEventProvider(provider); + java.util.Calendar cal = java.util.Calendar.getInstance(); + Date now = cal.getTime(); + cal.add(java.util.Calendar.DAY_OF_MONTH, 20); + Date then = cal.getTime(); + List<CalendarEvent> events = calendar.getEventProvider().getEvents(now, + then); + for (CalendarEvent ce : events) { + assertEquals("testStyle", ce.getStyleName()); + } + } + + private static Indexed createTestBeanItemContainer() { + BeanItemContainer<CalendarEvent> eventContainer = new BeanItemContainer<CalendarEvent>( + CalendarEvent.class); + java.util.Calendar cal = java.util.Calendar.getInstance(); + for (int i = 1; i <= 10; i++) { + eventContainer.addBean(new BasicEvent("Test " + i, + "Description " + i, cal.getTime())); + cal.add(java.util.Calendar.DAY_OF_MONTH, 2); + } + return eventContainer; + } + + private static Indexed createTestIndexedContainer() { + IndexedContainer container = new IndexedContainer(); + container.addContainerProperty("testCaption", String.class, ""); + container.addContainerProperty("testDescription", String.class, ""); + container.addContainerProperty("testStartDate", Date.class, null); + container.addContainerProperty("testEndDate", Date.class, null); + + java.util.Calendar cal = java.util.Calendar.getInstance(); + for (int i = 1; i <= 10; i++) { + Item item = container.getItem(container.addItem()); + item.getItemProperty("testCaption").setValue("Test " + i); + item.getItemProperty("testDescription") + .setValue("Description " + i); + item.getItemProperty("testStartDate").setValue(cal.getTime()); + item.getItemProperty("testEndDate").setValue(cal.getTime()); + cal.add(java.util.Calendar.DAY_OF_MONTH, 2); + } + return container; + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerEventProviderTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerEventProviderTest.java new file mode 100644 index 0000000000..401b5861ce --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/calendar/ContainerEventProviderTest.java @@ -0,0 +1,88 @@ +/* + * 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.calendar; + +import java.util.Calendar; +import java.util.Date; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.util.BeanItemContainer; +import com.vaadin.ui.components.calendar.ContainerEventProvider; +import com.vaadin.ui.components.calendar.event.CalendarEvent; + +/** + * + * @author Vaadin Ltd + */ +public class ContainerEventProviderTest { + + @Test + public void testDefaultAllDayProperty() { + ContainerEventProvider provider = new ContainerEventProvider(null); + Assert.assertEquals(ContainerEventProvider.ALL_DAY_PROPERTY, + provider.getAllDayProperty()); + + } + + @Test + public void testSetAllDayProperty() { + ContainerEventProvider provider = new ContainerEventProvider(null); + Object prop = new Object(); + provider.setAllDayProperty(prop); + Assert.assertEquals(prop, provider.getAllDayProperty()); + } + + @Test + public void testGetEvents() { + BeanItemContainer<EventBean> container = new BeanItemContainer<EventBean>( + EventBean.class); + EventBean bean = new EventBean(); + container.addBean(bean); + ContainerEventProvider provider = new ContainerEventProvider(container); + List<CalendarEvent> events = provider.getEvents(bean.getStart(), + bean.getEnd()); + Assert.assertTrue(events.get(0).isAllDay()); + } + + public static class EventBean { + + public boolean isAllDay() { + return true; + } + + public void setAllDay(boolean allDay) { + } + + public Date getStart() { + return Calendar.getInstance().getTime(); + } + + public Date getEnd() { + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.MINUTE, 10); + return calendar.getTime(); + } + + public void setStart(Date date) { + } + + public void setEnd(Date date) { + } + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerDeclarativeTest.java new file mode 100644 index 0000000000..2e3b8e412e --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/AbstractColorPickerDeclarativeTest.java @@ -0,0 +1,87 @@ +/* + * 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 com.vaadin.shared.ui.colorpicker.Color; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.AbstractColorPicker; +import com.vaadin.ui.AbstractColorPicker.PopupStyle; +import com.vaadin.ui.ColorPicker; +import com.vaadin.ui.ColorPickerArea; + +public class AbstractColorPickerDeclarativeTest + extends DeclarativeTestBase<AbstractColorPicker> { + + @Test + public void testAllAbstractColorPickerFeatures() { + String design = "<vaadin-color-picker color='#fafafa' default-caption-enabled position='100,100'" + + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'" + + " history-visibility=false textfield-visibility=false />"; + ColorPicker colorPicker = new ColorPicker(); + int colorInt = Integer.parseInt("fafafa", 16); + colorPicker.setColor(new Color(colorInt)); + colorPicker.setDefaultCaptionEnabled(true); + colorPicker.setPosition(100, 100); + colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE); + colorPicker.setRGBVisibility(false); + colorPicker.setHSVVisibility(false); + colorPicker.setSwatchesVisibility(true); + colorPicker.setHistoryVisibility(false); + colorPicker.setTextfieldVisibility(false); + + testWrite(design, colorPicker); + testRead(design, colorPicker); + } + + @Test + public void testEmptyColorPicker() { + String design = "<vaadin-color-picker />"; + ColorPicker colorPicker = new ColorPicker(); + testRead(design, colorPicker); + testWrite(design, colorPicker); + } + + @Test + public void testAllAbstractColorPickerAreaFeatures() { + String design = "<vaadin-color-picker-area color='#fafafa' default-caption-enabled position='100,100'" + + " popup-style='simple' rgb-visibility='false' hsv-visibility='false'" + + " history-visibility=false textfield-visibility=false />"; + AbstractColorPicker colorPicker = new ColorPickerArea(); + int colorInt = Integer.parseInt("fafafa", 16); + colorPicker.setColor(new Color(colorInt)); + colorPicker.setDefaultCaptionEnabled(true); + colorPicker.setPosition(100, 100); + colorPicker.setPopupStyle(PopupStyle.POPUP_SIMPLE); + colorPicker.setRGBVisibility(false); + colorPicker.setHSVVisibility(false); + colorPicker.setSwatchesVisibility(true); + colorPicker.setHistoryVisibility(false); + colorPicker.setTextfieldVisibility(false); + + testWrite(design, colorPicker); + testRead(design, colorPicker); + } + + @Test + public void testEmptyColorPickerArea() { + String design = "<vaadin-color-picker-area />"; + AbstractColorPicker colorPicker = new ColorPickerArea(); + testRead(design, colorPicker); + testWrite(design, colorPicker); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorConversionsTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorConversionsTest.java new file mode 100644 index 0000000000..a55ed89691 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/colorpicker/ColorConversionsTest.java @@ -0,0 +1,57 @@ +/* + * 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 static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.vaadin.shared.ui.colorpicker.Color; + +public class ColorConversionsTest { + + @Test + public void convertHSL2RGB() { + + int rgb = Color.HSLtoRGB(100, 50, 50); + Color c = new Color(rgb); + assertEquals(106, c.getRed()); + assertEquals(191, c.getGreen()); + assertEquals(64, c.getBlue()); + assertEquals("#6abf40", c.getCSS()); + + rgb = Color.HSLtoRGB(0, 50, 50); + c = new Color(rgb); + assertEquals(191, c.getRed()); + assertEquals(64, c.getGreen()); + assertEquals(64, c.getBlue()); + assertEquals("#bf4040", c.getCSS()); + + rgb = Color.HSLtoRGB(50, 0, 50); + c = new Color(rgb); + assertEquals(128, c.getRed()); + assertEquals(128, c.getGreen()); + assertEquals(128, c.getBlue()); + assertEquals("#808080", c.getCSS()); + + rgb = Color.HSLtoRGB(50, 100, 0); + c = new Color(rgb); + assertEquals(0, c.getRed(), 0); + assertEquals(0, c.getGreen(), 0); + assertEquals(0, c.getBlue(), 0); + assertEquals("#000000", c.getCSS()); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java new file mode 100644 index 0000000000..482267f63d --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxDeclarativeTest.java @@ -0,0 +1,88 @@ +/* + * 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.combobox; + +import org.junit.Test; + +import com.vaadin.shared.ui.combobox.FilteringMode; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.ComboBox; + +public class ComboBoxDeclarativeTest extends DeclarativeTestBase<ComboBox> { + + @Test + public void testReadOnlyWithOptionsRead() { + testRead(getReadOnlyWithOptionsDesign(), + getReadOnlyWithOptionsExpected()); + } + + private ComboBox getReadOnlyWithOptionsExpected() { + ComboBox cb = new ComboBox(); + cb.setTextInputAllowed(false); + cb.addItem("Hello"); + cb.addItem("World"); + return cb; + } + + private String getReadOnlyWithOptionsDesign() { + return "<vaadin-combo-box text-input-allowed='false'><option>Hello</option><option>World</option></vaadin-combo-box>"; + } + + @Test + public void testReadOnlyWithOptionsWrite() { + testWrite(stripOptionTags(getReadOnlyWithOptionsDesign()), + getReadOnlyWithOptionsExpected()); + } + + @Test + public void testBasicRead() { + testRead(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testBasicWrite() { + testWrite(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testReadOnlyValue() { + String design = "<vaadin-combo-box readonly value='foo'><option selected>foo</option></vaadin-combo-box>"; + + ComboBox comboBox = new ComboBox(); + comboBox.addItems("foo", "bar"); + comboBox.setValue("foo"); + comboBox.setReadOnly(true); + + testRead(design, comboBox); + + // Selects items are not written out by default + String design2 = "<vaadin-combo-box readonly></vaadin-combo-box>"; + testWrite(design2, comboBox); + } + + private String getBasicDesign() { + return "<vaadin-combo-box input-prompt=\"Select something\" filtering-mode=\"off\" scroll-to-selected-item='false'>"; + } + + private ComboBox getBasicExpected() { + ComboBox cb = new ComboBox(); + cb.setInputPrompt("Select something"); + cb.setTextInputAllowed(true); + cb.setFilteringMode(FilteringMode.OFF); + cb.setScrollToSelectedItem(false); + return cb; + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxStateTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxStateTest.java new file mode 100644 index 0000000000..e8ca64895a --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/combobox/ComboBoxStateTest.java @@ -0,0 +1,59 @@ +/* + * 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.combobox; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.shared.ui.combobox.ComboBoxState; +import com.vaadin.ui.ComboBox; + +/** + * Tests for ComboBox state. + * + */ +public class ComboBoxStateTest { + @Test + public void getState_comboboxHasCustomState() { + TestComboBox combobox = new TestComboBox(); + ComboBoxState state = combobox.getState(); + Assert.assertEquals("Unexpected state class", ComboBoxState.class, + state.getClass()); + } + + @Test + public void getPrimaryStyleName_comboboxHasCustomPrimaryStyleName() { + ComboBox combobox = new ComboBox(); + ComboBoxState state = new ComboBoxState(); + Assert.assertEquals("Unexpected primary style name", + state.primaryStyleName, combobox.getPrimaryStyleName()); + } + + @Test + public void comboboxStateHasCustomPrimaryStyleName() { + ComboBoxState state = new ComboBoxState(); + Assert.assertEquals("Unexpected primary style name", "v-filterselect", + state.primaryStyleName); + } + + private static class TestComboBox extends ComboBox { + + @Override + public ComboBoxState getState() { + return super.getState(); + } + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/ListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/ListenersTest.java new file mode 100644 index 0000000000..ade24a0df6 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/ListenersTest.java @@ -0,0 +1,143 @@ +package com.vaadin.tests.server.component.tree; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.ui.Tree; +import com.vaadin.ui.Tree.CollapseEvent; +import com.vaadin.ui.Tree.CollapseListener; +import com.vaadin.ui.Tree.ExpandEvent; +import com.vaadin.ui.Tree.ExpandListener; + +public class ListenersTest implements ExpandListener, CollapseListener { + private int expandCalled; + private int collapseCalled; + private Object lastExpanded; + private Object lastCollapsed; + + @Before + public void setUp() { + expandCalled = 0; + } + + @Test + public void testExpandListener() { + Tree tree = createTree(10, 20, false); + tree.addListener((ExpandListener) this); + List<Object> rootIds = new ArrayList<Object>(tree.rootItemIds()); + + assertEquals(10, rootIds.size()); + assertEquals(10 + 10 * 20 + 10, tree.size()); + + // Expanding should send one expand event for the root item id + tree.expandItem(rootIds.get(0)); + assertEquals(1, expandCalled); + assertEquals(rootIds.get(0), lastExpanded); + + // Expand should send one event for each expanded item id. + // In this case root + child 4 + expandCalled = 0; + tree.expandItemsRecursively(rootIds.get(1)); + assertEquals(2, expandCalled); + List<Object> c = new ArrayList<Object>( + tree.getChildren(rootIds.get(1))); + + assertEquals(c.get(4), lastExpanded); + + // Expanding an already expanded item should send no expand event + expandCalled = 0; + tree.expandItem(rootIds.get(0)); + assertEquals(0, expandCalled); + } + + /** + * Creates a tree with "rootItems" roots, each with "children" children, + * each with 1 child. + * + * @param rootItems + * @param children + * @param expand + * @return + */ + private Tree createTree(int rootItems, int children, boolean expand) { + Tree tree = new Tree(); + for (int i = 0; i < rootItems; i++) { + String rootId = "root " + i; + tree.addItem(rootId); + if (expand) { + tree.expandItemsRecursively(rootId); + } else { + tree.collapseItemsRecursively(rootId); + + } + for (int j = 0; j < children; j++) { + String childId = "child " + i + "/" + j; + tree.addItem(childId); + tree.setParent(childId, rootId); + tree.setChildrenAllowed(childId, false); + if (j == 4) { + tree.setChildrenAllowed(childId, true); + Object grandChildId = tree.addItem(); + tree.setParent(grandChildId, childId); + tree.setChildrenAllowed(grandChildId, false); + if (expand) { + tree.expandItemsRecursively(childId); + } else { + tree.collapseItemsRecursively(childId); + } + } + } + } + + return tree; + } + + @Test + public void testCollapseListener() { + Tree tree = createTree(7, 15, true); + tree.addListener((CollapseListener) this); + + List<Object> rootIds = new ArrayList<Object>(tree.rootItemIds()); + + assertEquals(7, rootIds.size()); + assertEquals(7 + 7 * 15 + 7, tree.size()); + + // Expanding should send one expand event for the root item id + tree.collapseItem(rootIds.get(0)); + assertEquals(1, collapseCalled); + assertEquals(rootIds.get(0), lastCollapsed); + + // Collapse sends one event for each collapsed node. + // In this case root + child 4 + collapseCalled = 0; + tree.collapseItemsRecursively(rootIds.get(1)); + assertEquals(2, collapseCalled); + List<Object> c = new ArrayList<Object>( + tree.getChildren(rootIds.get(1))); + assertEquals(c.get(4), lastCollapsed); + + // Collapsing an already expanded item should send no expand event + collapseCalled = 0; + tree.collapseItem(rootIds.get(0)); + assertEquals(0, collapseCalled); + } + + @Override + public void nodeExpand(ExpandEvent event) { + lastExpanded = event.getItemId(); + expandCalled++; + + } + + @Override + public void nodeCollapse(CollapseEvent event) { + lastCollapsed = event.getItemId(); + collapseCalled++; + + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java new file mode 100644 index 0000000000..1e73f953b2 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeDeclarativeTest.java @@ -0,0 +1,81 @@ +/* + * 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.tree; + +import org.junit.Test; + +import com.vaadin.server.ExternalResource; +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.Tree; +import com.vaadin.ui.Tree.TreeDragMode; + +/** + * Tests the declarative support for implementations of {@link Tree}. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TreeDeclarativeTest extends DeclarativeTestBase<Tree> { + + @Test + public void testDragMode() { + String design = "<vaadin-tree drag-mode='node' />"; + + Tree tree = new Tree(); + tree.setDragMode(TreeDragMode.NODE); + + testRead(design, tree); + testWrite(design, tree); + } + + @Test + public void testEmpty() { + testRead("<vaadin-tree />", new Tree()); + testWrite("<vaadin-tree />", new Tree()); + } + + @Test + public void testNodes() { + String design = "<vaadin-tree>" // + + " <node text='Node'/>" // + + " <node text='Parent'>" // + + " <node text='Child'>" // + + " <node text='Grandchild'/>" // + + " </node>" // + + " </node>" // + + " <node text='With icon' icon='http://example.com/icon.png'/>" // + + "</vaadin-tree>"; + + Tree tree = new Tree(); + + tree.addItem("Node"); + + tree.addItem("Parent"); + + tree.addItem("Child"); + tree.setParent("Child", "Parent"); + + tree.addItem("Grandchild"); + tree.setParent("Grandchild", "Child"); + + tree.addItem("With icon"); + tree.setItemIcon("With icon", + new ExternalResource("http://example.com/icon.png")); + + testRead(design, tree); + testWrite(design, tree, true); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeListenersTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeListenersTest.java new file mode 100644 index 0000000000..ec10c4fe39 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeListenersTest.java @@ -0,0 +1,33 @@ +package com.vaadin.tests.server.component.tree; + +import org.junit.Test; + +import com.vaadin.event.ItemClickEvent; +import com.vaadin.event.ItemClickEvent.ItemClickListener; +import com.vaadin.tests.server.component.AbstractListenerMethodsTestBase; +import com.vaadin.ui.Tree; +import com.vaadin.ui.Tree.CollapseEvent; +import com.vaadin.ui.Tree.CollapseListener; +import com.vaadin.ui.Tree.ExpandEvent; +import com.vaadin.ui.Tree.ExpandListener; + +public class TreeListenersTest extends AbstractListenerMethodsTestBase { + + @Test + public void testExpandListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(Tree.class, ExpandEvent.class, + ExpandListener.class); + } + + @Test + public void testItemClickListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(Tree.class, ItemClickEvent.class, + ItemClickListener.class); + } + + @Test + public void testCollapseListenerAddGetRemove() throws Exception { + testListenerAddGetRemove(Tree.class, CollapseEvent.class, + CollapseListener.class); + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeTest.java new file mode 100644 index 0000000000..ed455415ea --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/tree/TreeTest.java @@ -0,0 +1,178 @@ +package com.vaadin.tests.server.component.tree; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Field; +import java.util.HashSet; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.data.Container; +import com.vaadin.data.util.HierarchicalContainer; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.shared.ui.tree.TreeState; +import com.vaadin.ui.Tree; + +public class TreeTest { + + private Tree tree; + private Tree tree2; + private Tree tree3; + private Tree tree4; + + @Before + public void setUp() { + tree = new Tree(); + tree.addItem("parent"); + tree.addItem("child"); + tree.setChildrenAllowed("parent", true); + tree.setParent("child", "parent"); + + tree2 = new Tree("Caption"); + tree2.addItem("parent"); + tree2.addItem("child"); + tree2.setChildrenAllowed("parent", true); + tree2.setParent("child", "parent"); + + tree3 = new Tree("Caption", null); + tree3.addItem("parent"); + tree3.addItem("child"); + tree3.setChildrenAllowed("parent", true); + tree3.setParent("child", "parent"); + + tree4 = new Tree("Caption", new IndexedContainer()); + tree4.addItem("parent"); + tree4.addItem("child"); + tree4.setChildrenAllowed("parent", true); + tree4.setParent("child", "parent"); + } + + @Test + public void testRemoveChildren() { + assertTrue(tree.hasChildren("parent")); + tree.removeItem("child"); + assertFalse(tree.hasChildren("parent")); + + assertTrue(tree2.hasChildren("parent")); + tree2.removeItem("child"); + assertFalse(tree2.hasChildren("parent")); + + assertTrue(tree3.hasChildren("parent")); + tree3.removeItem("child"); + assertFalse(tree3.hasChildren("parent")); + + assertTrue(tree4.hasChildren("parent")); + tree4.removeItem("child"); + assertFalse(tree4.hasChildren("parent")); + } + + @Test + public void testContainerTypeIsHierarchical() { + assertTrue(HierarchicalContainer.class + .isAssignableFrom(tree.getContainerDataSource().getClass())); + assertTrue(HierarchicalContainer.class + .isAssignableFrom(tree2.getContainerDataSource().getClass())); + assertTrue(HierarchicalContainer.class + .isAssignableFrom(tree3.getContainerDataSource().getClass())); + assertFalse(HierarchicalContainer.class + .isAssignableFrom(tree4.getContainerDataSource().getClass())); + assertTrue(Container.Hierarchical.class + .isAssignableFrom(tree4.getContainerDataSource().getClass())); + } + + @Test + public void testRemoveExpandedItems() throws Exception { + tree.expandItem("parent"); + tree.expandItem("child"); + + Field expandedField = tree.getClass().getDeclaredField("expanded"); + Field expandedItemIdField = tree.getClass() + .getDeclaredField("expandedItemId"); + + expandedField.setAccessible(true); + expandedItemIdField.setAccessible(true); + + HashSet<Object> expanded = (HashSet<Object>) expandedField.get(tree); + Object expandedItemId = expandedItemIdField.get(tree); + + assertEquals(2, expanded.size()); + assertTrue("Contains parent", expanded.contains("parent")); + assertTrue("Contains child", expanded.contains("child")); + assertEquals("child", expandedItemId); + + tree.removeItem("parent"); + + expanded = (HashSet<Object>) expandedField.get(tree); + expandedItemId = expandedItemIdField.get(tree); + + assertEquals(1, expanded.size()); + assertTrue("Contains child", expanded.contains("child")); + assertEquals("child", expandedItemId); + + tree.removeItem("child"); + + expanded = (HashSet<Object>) expandedField.get(tree); + expandedItemId = expandedItemIdField.get(tree); + + assertEquals(0, expanded.size()); + assertNull(expandedItemId); + } + + @Test + public void testRemoveExpandedItemsOnContainerChange() throws Exception { + tree.expandItem("parent"); + tree.expandItem("child"); + + tree.setContainerDataSource(new HierarchicalContainer()); + + Field expandedField = tree.getClass().getDeclaredField("expanded"); + Field expandedItemIdField = tree.getClass() + .getDeclaredField("expandedItemId"); + + expandedField.setAccessible(true); + expandedItemIdField.setAccessible(true); + + HashSet<Object> expanded = (HashSet<Object>) expandedField.get(tree); + assertEquals(0, expanded.size()); + + Object expandedItemId = expandedItemIdField.get(tree); + assertNull(expandedItemId); + } + + @Test + public void getState_treeHasCustomState() { + TestTree table = new TestTree(); + TreeState state = table.getState(); + Assert.assertEquals("Unexpected state class", TreeState.class, + state.getClass()); + } + + @Test + public void getPrimaryStyleName_treeHasCustomPrimaryStyleName() { + Tree table = new Tree(); + TreeState state = new TreeState(); + Assert.assertEquals("Unexpected primary style name", + state.primaryStyleName, table.getPrimaryStyleName()); + } + + @Test + public void treeStateHasCustomPrimaryStyleName() { + TreeState state = new TreeState(); + Assert.assertEquals("Unexpected primary style name", "v-tree", + state.primaryStyleName); + } + + private static class TestTree extends Tree { + + @Override + public TreeState getState() { + return super.getState(); + } + } + +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectDeclarativeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectDeclarativeTest.java new file mode 100644 index 0000000000..02a2ebfd77 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectDeclarativeTest.java @@ -0,0 +1,74 @@ +/* + * 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.twincolselect; + +import java.util.Arrays; + +import org.junit.Test; + +import com.vaadin.tests.design.DeclarativeTestBase; +import com.vaadin.ui.TwinColSelect; + +/** + * Test cases for reading the properties of selection components. + * + * @author Vaadin Ltd + */ +public class TwinColSelectDeclarativeTest + extends DeclarativeTestBase<TwinColSelect> { + + public String getBasicDesign() { + return "<vaadin-twin-col-select rows=5 right-column-caption='Selected values' left-column-caption='Unselected values'>\n" + + " <option>First item</option>\n" + + " <option selected>Second item</option>\n" + + " <option selected>Third item</option>\n" + + "</vaadin-twin-col-select>"; + + } + + public TwinColSelect getBasicExpected() { + TwinColSelect s = new TwinColSelect(); + s.setRightColumnCaption("Selected values"); + s.setLeftColumnCaption("Unselected values"); + s.addItem("First item"); + s.addItem("Second item"); + s.addItem("Third item"); + s.setValue(Arrays.asList(new Object[] { "Second item", "Third item" })); + s.setRows(5); + return s; + } + + @Test + public void testReadBasic() { + testRead(getBasicDesign(), getBasicExpected()); + } + + @Test + public void testWriteBasic() { + testWrite(stripOptionTags(getBasicDesign()), getBasicExpected()); + } + + @Test + public void testReadEmpty() { + testRead("<vaadin-twin-col-select />", new TwinColSelect()); + } + + @Test + public void testWriteEmpty() { + testWrite("<vaadin-twin-col-select />", new TwinColSelect()); + } + +}
\ No newline at end of file diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectStateTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectStateTest.java new file mode 100644 index 0000000000..9348de63c2 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/component/twincolselect/TwinColSelectStateTest.java @@ -0,0 +1,60 @@ +/* + * 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.twincolselect; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.shared.ui.twincolselect.TwinColSelectState; +import com.vaadin.ui.TwinColSelect; + +/** + * Tests for TwinColSelectState. + * + */ +public class TwinColSelectStateTest { + + @Test + public void getState_selectHasCustomState() { + TestTwinColSelect select = new TestTwinColSelect(); + TwinColSelectState state = select.getState(); + Assert.assertEquals("Unexpected state class", TwinColSelectState.class, + state.getClass()); + } + + @Test + public void getPrimaryStyleName_selectHasCustomPrimaryStyleName() { + TwinColSelect table = new TwinColSelect(); + TwinColSelectState state = new TwinColSelectState(); + Assert.assertEquals("Unexpected primary style name", + state.primaryStyleName, table.getPrimaryStyleName()); + } + + @Test + public void selectStateHasCustomPrimaryStyleName() { + TwinColSelectState state = new TwinColSelectState(); + Assert.assertEquals("Unexpected primary style name", "v-select-twincol", + state.primaryStyleName); + } + + private static class TestTwinColSelect extends TwinColSelect { + + @Override + public TwinColSelectState getState() { + return super.getState(); + } + } +} diff --git a/compatibility-server/src/test/java/com/vaadin/tests/server/components/ComboBoxValueChangeTest.java b/compatibility-server/src/test/java/com/vaadin/tests/server/components/ComboBoxValueChangeTest.java new file mode 100644 index 0000000000..66a4ec2370 --- /dev/null +++ b/compatibility-server/src/test/java/com/vaadin/tests/server/components/ComboBoxValueChangeTest.java @@ -0,0 +1,46 @@ +package com.vaadin.tests.server.components; + +import org.junit.Before; + +import com.vaadin.server.ServerRpcManager; +import com.vaadin.server.ServerRpcMethodInvocation; +import com.vaadin.shared.ui.combobox.ComboBoxServerRpc; +import com.vaadin.ui.ComboBox; +import com.vaadin.v7.ui.LegacyAbstractField; + +/** + * Check that the value change listener for a combo box is triggered exactly + * once when setting the value, at the correct time. + * + * See <a href="http://dev.vaadin.com/ticket/4394">Ticket 4394</a>. + */ +public class ComboBoxValueChangeTest + extends AbstractFieldValueChangeTestBase<Object> { + + @Before + public void setUp() { + ComboBox combo = new ComboBox() { + @Override + public String getConnectorId() { + return "id"; + } + }; + combo.addItem("myvalue"); + super.setUp(combo); + } + + @Override + protected void setValue(LegacyAbstractField<Object> field) { + ComboBox combo = (ComboBox) field; + ServerRpcMethodInvocation invocation = new ServerRpcMethodInvocation( + combo.getConnectorId(), ComboBoxServerRpc.class, + "setSelectedItem", 1); + invocation.setParameters(new Object[] { "myvalue" }); + try { + ServerRpcManager.applyInvocation(combo, invocation); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} |