From 387649190cdbb93d11dc3714de73f0dcdc4c4059 Mon Sep 17 00:00:00 2001 From: Teemu Suo-Anttila Date: Thu, 18 Aug 2016 15:05:12 +0300 Subject: Add unit tests for adding and executing data generators Change-Id: Ica704e0b79524bd2038aeb048c8171810a27652c --- .../abstractlisting/AbstractListingTest.java | 74 ++++++++++++++++++++-- 1 file changed, 70 insertions(+), 4 deletions(-) diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java index cf9fcdab37..b3d606cc30 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/abstractlisting/AbstractListingTest.java @@ -9,26 +9,61 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.vaadin.data.Listing; import com.vaadin.server.data.BackEndDataSource; import com.vaadin.server.data.DataSource; import com.vaadin.server.data.ListDataSource; import com.vaadin.server.data.Query; +import com.vaadin.server.data.TypedDataGenerator; import com.vaadin.ui.AbstractListing; +import elemental.json.JsonObject; + public class AbstractListingTest { + private final class TestListing extends AbstractListing { + + @Override + public void addDataGenerator(TypedDataGenerator generator) { + super.addDataGenerator(generator); + } + + @Override + public void removeDataGenerator(TypedDataGenerator generator) { + super.removeDataGenerator(generator); + } + + /** + * Used to execute data generation + */ + public void runDataGeneration() { + super.getDataCommunicator().beforeClientResponse(true); + } + } + + private final class CountGenerator implements TypedDataGenerator { + + int callCount = 0; + + @Override + public void generateData(String data, JsonObject jsonObject) { + ++callCount; + } + + @Override + public void destroyData(String data) { + } + } + private static final String[] ITEM_ARRAY = new String[] { "Foo", "Bar", "Baz" }; - private Listing listing; + private TestListing listing; private List items; @Before public void setUp() { items = new ArrayList<>(Arrays.asList(ITEM_ARRAY)); - listing = new AbstractListing() { - }; + listing = new TestListing(); } @Test @@ -63,4 +98,35 @@ public class AbstractListingTest { Assert.assertNotEquals("setDataSource did not replace data source", dataSource, listing.getDataSource()); } + + @Test + public void testAddDataGeneartorBeforeDataSource() { + CountGenerator generator = new CountGenerator(); + listing.addDataGenerator(generator); + listing.setItems("Foo"); + listing.runDataGeneration(); + Assert.assertEquals("Generator should have been called once", 1, + generator.callCount); + } + + @Test + public void testAddDataGeneartorAfterDataSource() { + CountGenerator generator = new CountGenerator(); + listing.setItems("Foo"); + listing.addDataGenerator(generator); + listing.runDataGeneration(); + Assert.assertEquals("Generator should have been called once", 1, + generator.callCount); + } + + @Test + public void testRemoveDataGeneartor() { + listing.setItems("Foo"); + CountGenerator generator = new CountGenerator(); + listing.addDataGenerator(generator); + listing.removeDataGenerator(generator); + listing.runDataGeneration(); + Assert.assertEquals("Generator should not have been called", 0, + generator.callCount); + } } -- cgit v1.2.3