From da61fe3ab9611aeb8c44aed8466436f97c1c9966 Mon Sep 17 00:00:00 2001 From: Sauli Tähkäpää Date: Mon, 15 Dec 2014 10:48:51 +0200 Subject: Refactor SQLContainerTableQueryTest. Change-Id: Ie850614b8ef68996da5e4fb07a86235f536cb547 --- .../sqlcontainer/SQLContainerTableQueryTest.java | 534 ++++++++++----------- 1 file changed, 246 insertions(+), 288 deletions(-) (limited to 'server/tests/src') diff --git a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java index bef57fd7d1..c70462012e 100644 --- a/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java +++ b/server/tests/src/com/vaadin/data/util/sqlcontainer/SQLContainerTableQueryTest.java @@ -1,5 +1,14 @@ package com.vaadin.data.util.sqlcontainer; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.math.BigDecimal; import java.sql.Connection; import java.sql.SQLException; @@ -24,14 +33,19 @@ import com.vaadin.data.util.sqlcontainer.connection.SimpleJDBCConnectionPool; import com.vaadin.data.util.sqlcontainer.query.OrderBy; import com.vaadin.data.util.sqlcontainer.query.TableQuery; -import static org.junit.Assert.assertTrue; - public class SQLContainerTableQueryTest { private static final int offset = SQLTestsConstants.offset; + private final int numberOfRowsInContainer = 4; + private final int numberOfPropertiesInContainer = 3; + private final String NAME = "NAME"; + private final String ID = "ID"; + private final String AGE = "AGE"; private JDBCConnectionPool connectionPool; - private TableQuery peopleQuery; + private TableQuery query; private SQLContainer container; + private final RowId existingItemId = getRowId(1); + private final RowId nonExistingItemId = getRowId(1337); @Before public void setUp() throws SQLException { @@ -47,8 +61,8 @@ public class SQLContainerTableQueryTest { DataGenerator.addPeopleToDatabase(connectionPool); - peopleQuery = getTableQuery("people"); - container = new SQLContainer(peopleQuery); + query = getTableQuery("people"); + container = new SQLContainer(query); } private TableQuery getTableQuery(String tableName) { @@ -62,6 +76,14 @@ public class SQLContainerTableQueryTest { return new SQLContainer(getTableQuery("garbage")); } + private Item getItem(Object id) { + return container.getItem(id); + } + + private RowId getRowId(int id) { + return new RowId(new Object[] { id + offset }); + } + @After public void tearDown() { if (connectionPool != null) { @@ -72,7 +94,7 @@ public class SQLContainerTableQueryTest { @Test public void itemWithExistingVersionColumnIsRemoved() throws SQLException { container.setAutoCommit(true); - peopleQuery.setVersionColumn("ID"); + query.setVersionColumn(ID); assertTrue(container.removeItem(container.lastItemId())); } @@ -80,7 +102,7 @@ public class SQLContainerTableQueryTest { @Test(expected = IllegalArgumentException.class) public void itemWithNonExistingVersionColumnCannotBeRemoved() throws SQLException { - peopleQuery.setVersionColumn("version"); + query.setVersionColumn("version"); container.removeItem(container.lastItemId()); @@ -88,160 +110,113 @@ public class SQLContainerTableQueryTest { } @Test - public void constructor_withTableQuery_shouldSucceed() throws SQLException { - new SQLContainer(new TableQuery("people", connectionPool, - SQLTestsConstants.sqlGen)); + public void containerContainsId() { + assertTrue(container.containsId(existingItemId)); } @Test - public void containsId_withTableQueryAndExistingId_returnsTrue() - throws SQLException { - SQLContainer container = new SQLContainer(new TableQuery("people", - connectionPool, SQLTestsConstants.sqlGen)); - - assertTrue(container.containsId(new RowId(new Object[] { 1 + offset }))); - Assert.assertTrue(container.containsId(new RowId( - new Object[] { 1 + offset }))); - assertTrue(this.container.containsId(new RowId( - new Object[] { 1 + offset }))); + public void containerDoesNotContainId() { + assertFalse(container.containsId(nonExistingItemId)); } @Test - public void containsId_withTableQueryAndNonexistingId_returnsFalse() - throws SQLException { - Assert.assertFalse(container.containsId(new RowId( - new Object[] { 1337 + offset }))); + public void idPropertyHasCorrectType() { + if (SQLTestsConstants.db == DB.ORACLE) { + assertEquals(container.getType(ID), BigDecimal.class); + } else { + assertEquals(container.getType(ID), Integer.class); + } } @Test - public void getContainerProperty_tableExistingItemIdAndNonexistingPropertyId_returnsNull() - throws SQLException { - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1 + offset }), "asdf")); + public void namePropertyHasCorrectType() { + assertEquals(container.getType(NAME), String.class); } @Test - public void getContainerProperty_tableNonexistingItemId_returnsNull() - throws SQLException { - Assert.assertNull(container.getContainerProperty(new RowId( - new Object[] { 1337 + offset }), "NAME")); + public void nonExistingPropertyDoesNotHaveType() { + assertThat(container.getType("adsf"), is(nullValue())); } @Test - public void getType_tableIDPropertyId_returnsInteger() throws SQLException { - if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals(BigDecimal.class, container.getType("ID")); - } else { - Assert.assertEquals(Integer.class, container.getType("ID")); - } + public void sizeIsReturnedCorrectly() { + assertEquals(numberOfRowsInContainer, container.size()); } @Test - public void getType_tableNonexistingPropertyId_returnsNull() - throws SQLException { - Assert.assertNull(container.getType("asdf")); + public void propertyIsFetchedForExistingItem() { + assertThat(container.getContainerProperty(existingItemId, NAME) + .getValue().toString(), is("Kalle")); } @Test - public void size_table_returnsFour() throws SQLException { - Assert.assertEquals(4, container.size()); + public void containerDoesNotContainPropertyForExistingItem() { + assertThat(container.getContainerProperty(existingItemId, "asdf"), + is(nullValue())); } @Test - public void getContainerProperty_tableExistingItemIdAndPropertyId_returnsProperty() - throws SQLException { - if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - "Ville", - container - .getContainerProperty( - new RowId(new Object[] { new BigDecimal( - 0 + offset) }), "NAME").getValue()); - } else { - Assert.assertEquals( - "Ville", - container.getContainerProperty( - new RowId(new Object[] { 0 + offset }), "NAME") - .getValue()); - } + public void containerDoesNotContainExistingPropertyForNonExistingItem() { + assertThat(container.getContainerProperty(nonExistingItemId, NAME), + is(nullValue())); } @Test - public void getContainerPropertyIds_table_returnsIDAndNAME() - throws SQLException { - Collection propertyIds = container.getContainerPropertyIds(); - Assert.assertEquals(3, propertyIds.size()); - Assert.assertArrayEquals(new String[] { "ID", "NAME", "AGE" }, - propertyIds.toArray()); + public void propertyIdsAreFetched() { + ArrayList propertyIds = new ArrayList( + (Collection) container + .getContainerPropertyIds()); + + assertThat(propertyIds.size(), is(numberOfPropertiesInContainer)); + assertThat(propertyIds, hasItems(ID, NAME, AGE)); } @Test - public void getItem_tableExistingItemId_returnsItem() throws SQLException { - Item item; - if (SQLTestsConstants.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 0 + offset) })); - } else { - item = container.getItem(new RowId(new Object[] { 0 + offset })); - } - Assert.assertNotNull(item); - Assert.assertEquals("Ville", item.getItemProperty("NAME").getValue()); + public void existingItemIsFetched() { + Item item = container.getItem(existingItemId); + + assertThat(item.getItemProperty(NAME).getValue().toString(), + is("Kalle")); } @Test - public void getItem_commitedModifiedAndRefreshed() throws SQLException { - String OLD_VALUE = "SomeValue"; //$NON-NLS-1$ - String NEW_VALUE = "OtherValue"; //$NON-NLS-1$ + public void newItemIsAdded() throws SQLException { + Object id = container.addItem(); + getItem(id).getItemProperty(NAME).setValue("foo"); - Object itemID = container.addItem(); - Item item = container.getItem(itemID); - item.getItemProperty("NAME").setValue(OLD_VALUE); //$NON-NLS-1$ container.commit(); - itemID = container.getIdByIndex(container.size() - 1); - item = container.getItem(itemID); - Assert.assertEquals(OLD_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ - .getValue()); - item.getItemProperty("NAME").setValue(NEW_VALUE); //$NON-NLS-1$ + Item item = getItem(container.lastItemId()); + assertThat(item.getItemProperty(NAME).getValue().toString(), is("foo")); + } + + @Test + public void itemPropertyIsNotRevertedOnRefresh() { + getItem(existingItemId).getItemProperty(NAME).setValue("foo"); - // refresh the container which free's the caches - // and the modified cache keeps untouched which is a really powerful - // feature container.refresh(); - // access the item again will use the item from the modified cache. - item = container.getItem(itemID); - Assert.assertEquals(NEW_VALUE, item.getItemProperty("NAME") //$NON-NLS-1$ - .getValue()); + assertThat(getItem(existingItemId).getItemProperty(NAME).toString(), + is("foo")); } @Test - public void getItem_table5000RowsWithParameter1337_returnsItemWithId1337() - throws SQLException { + public void correctItemIsFetchedFromMultipleRows() throws SQLException { DataGenerator.addFiveThousandPeople(connectionPool); - Item item; - if (SQLTestsConstants.db == DB.ORACLE) { - item = container.getItem(new RowId(new Object[] { new BigDecimal( - 1337 + offset) })); - Assert.assertNotNull(item); - Assert.assertEquals(new BigDecimal(1337 + offset), item - .getItemProperty("ID").getValue()); - } else { - item = container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertNotNull(item); - Assert.assertEquals(1337 + offset, item.getItemProperty("ID") - .getValue()); - } - Assert.assertEquals("Person 1337", item.getItemProperty("NAME") - .getValue()); + Item item = container.getItem(getRowId(1337)); + + assertThat((Integer) item.getItemProperty(ID).getValue(), + is(equalTo(1337 + offset))); + assertThat(item.getItemProperty(NAME).getValue().toString(), + is("Person 1337")); } @Test public void getItemIds_table_returnsItemIdsWithKeys0through3() throws SQLException { Collection itemIds = container.getItemIds(); - Assert.assertEquals(4, itemIds.size()); + assertEquals(4, itemIds.size()); RowId zero = new RowId(new Object[] { 0 + offset }); RowId one = new RowId(new Object[] { 1 + offset }); RowId two = new RowId(new Object[] { 2 + offset }); @@ -259,11 +234,6 @@ public class SQLContainerTableQueryTest { } } - @Test - public void getType_tableNAMEPropertyId_returnsString() throws SQLException { - Assert.assertEquals(String.class, container.getType("NAME")); - } - @Test public void size_tableOneAddedItem_returnsFive() throws SQLException { Connection conn = connectionPool.reserveConnection(); @@ -278,17 +248,17 @@ public class SQLContainerTableQueryTest { conn.commit(); connectionPool.releaseConnection(conn); - Assert.assertEquals(5, container.size()); + assertEquals(5, container.size()); } @Test public void indexOfId_tableWithParameterThree_returnsThree() throws SQLException { if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals(3, container.indexOfId(new RowId( + assertEquals(3, container.indexOfId(new RowId( new Object[] { new BigDecimal(3 + offset) }))); } else { - Assert.assertEquals(3, + assertEquals(3, container.indexOfId(new RowId(new Object[] { 3 + offset }))); } } @@ -301,11 +271,11 @@ public class SQLContainerTableQueryTest { if (SQLTestsConstants.db == DB.ORACLE) { container.getItem(new RowId(new Object[] { new BigDecimal( 1337 + offset) })); - Assert.assertEquals(1337, container.indexOfId(new RowId( + assertEquals(1337, container.indexOfId(new RowId( new Object[] { new BigDecimal(1337 + offset) }))); } else { container.getItem(new RowId(new Object[] { 1337 + offset })); - Assert.assertEquals(1337, container.indexOfId(new RowId( + assertEquals(1337, container.indexOfId(new RowId( new Object[] { 1337 + offset }))); } } @@ -316,12 +286,10 @@ public class SQLContainerTableQueryTest { DataGenerator.addFiveThousandPeople(connectionPool); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1337 + offset }).toString(), + assertEquals(new RowId(new Object[] { 1337 + offset }).toString(), itemId.toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); + assertEquals(new RowId(new Object[] { 1337 + offset }), itemId); } } @@ -332,12 +300,10 @@ public class SQLContainerTableQueryTest { Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1337 + offset }).toString(), + assertEquals(new RowId(new Object[] { 1337 + offset }).toString(), itemId.toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 1337 + offset }), - itemId); + assertEquals(new RowId(new Object[] { 1337 + offset }), itemId); } } @@ -349,11 +315,10 @@ public class SQLContainerTableQueryTest { connectionPool, SQLTestsConstants.sqlGen)); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1338 + offset }).toString(), + assertEquals(new RowId(new Object[] { 1338 + offset }).toString(), container.nextItemId(itemId).toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 1338 + offset }), + assertEquals(new RowId(new Object[] { 1338 + offset }), container.nextItemId(itemId)); } } @@ -364,11 +329,10 @@ public class SQLContainerTableQueryTest { DataGenerator.addFiveThousandPeople(connectionPool); Object itemId = container.getIdByIndex(1337); if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 1336 + offset }).toString(), + assertEquals(new RowId(new Object[] { 1336 + offset }).toString(), container.prevItemId(itemId).toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 1336 + offset }), + assertEquals(new RowId(new Object[] { 1336 + offset }), container.prevItemId(itemId)); } } @@ -376,11 +340,10 @@ public class SQLContainerTableQueryTest { @Test public void firstItemId_table_returnsItemId0() throws SQLException { if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 0 + offset }).toString(), + assertEquals(new RowId(new Object[] { 0 + offset }).toString(), container.firstItemId().toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 0 + offset }), + assertEquals(new RowId(new Object[] { 0 + offset }), container.firstItemId()); } } @@ -391,11 +354,10 @@ public class SQLContainerTableQueryTest { DataGenerator.addFiveThousandPeople(connectionPool); if (SQLTestsConstants.db == DB.ORACLE) { - Assert.assertEquals( - new RowId(new Object[] { 4999 + offset }).toString(), + assertEquals(new RowId(new Object[] { 4999 + offset }).toString(), container.lastItemId().toString()); } else { - Assert.assertEquals(new RowId(new Object[] { 4999 + offset }), + assertEquals(new RowId(new Object[] { 4999 + offset }), container.lastItemId()); } } @@ -479,10 +441,10 @@ public class SQLContainerTableQueryTest { @Test public void refresh_table_sizeShouldUpdate() throws SQLException { - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); DataGenerator.addFiveThousandPeople(connectionPool); container.refresh(); - Assert.assertEquals(5000, container.size()); + assertEquals(5000, container.size()); } @Test @@ -492,9 +454,9 @@ public class SQLContainerTableQueryTest { // after adding lots of items unless we call refresh inbetween. This to // make sure that the refresh method actually refreshes stuff and isn't // a NOP. - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); DataGenerator.addFiveThousandPeople(connectionPool); - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); } @Test @@ -507,15 +469,15 @@ public class SQLContainerTableQueryTest { @Test public void getPageLength_table_returnsDefault100() throws SQLException { - Assert.assertEquals(100, container.getPageLength()); + assertEquals(100, container.getPageLength()); } @Test public void setPageLength_table_shouldSucceed() throws SQLException { container.setPageLength(20); - Assert.assertEquals(20, container.getPageLength()); + assertEquals(20, container.getPageLength()); container.setPageLength(200); - Assert.assertEquals(200, container.getPageLength()); + assertEquals(200, container.getPageLength()); } @Test(expected = UnsupportedOperationException.class) @@ -577,7 +539,7 @@ public class SQLContainerTableQueryTest { container.setAutoCommit(true); int originalSize = container.size(); container.addItem(); - Assert.assertEquals(originalSize + 1, container.size()); + assertEquals(originalSize + 1, container.size()); } @Test @@ -585,7 +547,7 @@ public class SQLContainerTableQueryTest { throws SQLException { int size = container.size(); container.addItem(); - Assert.assertEquals(size + 1, container.size()); + assertEquals(size + 1, container.size()); } @Test @@ -594,7 +556,7 @@ public class SQLContainerTableQueryTest { int size = container.size(); Object id1 = container.addItem(); Object id2 = container.addItem(); - Assert.assertEquals(size + 2, container.size()); + assertEquals(size + 2, container.size()); Assert.assertNotSame(id1, id2); Assert.assertFalse(id1.equals(id2)); } @@ -604,7 +566,7 @@ public class SQLContainerTableQueryTest { throws SQLException { Object lastId = container.lastItemId(); Object id = container.addItem(); - Assert.assertEquals(id, container.nextItemId(lastId)); + assertEquals(id, container.nextItemId(lastId)); } @Test @@ -612,14 +574,14 @@ public class SQLContainerTableQueryTest { throws SQLException { Object lastId = container.lastItemId(); Object id = container.addItem(); - Assert.assertEquals(id, container.lastItemId()); + assertEquals(id, container.lastItemId()); Assert.assertNotSame(lastId, container.lastItemId()); } @Test public void indexOfId_tableNewlyAddedItem_returnsFour() throws SQLException { Object id = container.addItem(); - Assert.assertEquals(4, container.indexOfId(id)); + assertEquals(4, container.indexOfId(id)); } @Test @@ -641,8 +603,8 @@ public class SQLContainerTableQueryTest { throws SQLException { Object id = container.addItem(); Item item = container.getItem(id); - item.getItemProperty("NAME").setValue("asdf"); - Assert.assertEquals("asdf", container.getContainerProperty(id, "NAME") + item.getItemProperty(NAME).setValue("asdf"); + assertEquals("asdf", container.getContainerProperty(id, NAME) .getValue()); } @@ -660,7 +622,7 @@ public class SQLContainerTableQueryTest { Object id1 = container.addItem(); Object id2 = container.addItem(); - Assert.assertEquals(id1, container.prevItemId(id2)); + assertEquals(id1, container.prevItemId(id2)); } @Test @@ -706,7 +668,7 @@ public class SQLContainerTableQueryTest { throws SQLException { Object id = container.addItem(); - Assert.assertEquals(id, container.getIdByIndex(container.size() - 1)); + assertEquals(id, container.getIdByIndex(container.size() - 1)); } @Test @@ -718,7 +680,7 @@ public class SQLContainerTableQueryTest { assertTrue(container.removeItem(id)); Assert.assertNotSame(id, container.firstItemId()); - Assert.assertEquals(originalSize - 1, container.size()); + assertEquals(originalSize - 1, container.size()); } @Test @@ -736,7 +698,7 @@ public class SQLContainerTableQueryTest { assertTrue(container.removeItem(id)); Assert.assertFalse(container.containsId(id)); - Assert.assertEquals(size - 1, container.size()); + assertEquals(size - 1, container.size()); } @Test @@ -803,7 +765,7 @@ public class SQLContainerTableQueryTest { Object third = container.getIdByIndex(2); assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); + assertEquals(third, container.nextItemId(first)); } @Test @@ -814,7 +776,7 @@ public class SQLContainerTableQueryTest { Object third = container.addItem(); assertTrue(container.removeItem(second)); - Assert.assertEquals(third, container.nextItemId(first)); + assertEquals(third, container.nextItemId(first)); } @Test @@ -825,7 +787,7 @@ public class SQLContainerTableQueryTest { Object third = container.getIdByIndex(2); assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); + assertEquals(first, container.prevItemId(third)); } @Test @@ -836,7 +798,7 @@ public class SQLContainerTableQueryTest { Object third = container.addItem(); assertTrue(container.removeItem(second)); - Assert.assertEquals(first, container.prevItemId(third)); + assertEquals(first, container.prevItemId(third)); } @Test @@ -926,7 +888,7 @@ public class SQLContainerTableQueryTest { Object id = container.getIdByIndex(2); assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); + assertEquals(-1, container.indexOfId(id)); } @Test @@ -936,7 +898,7 @@ public class SQLContainerTableQueryTest { assertTrue(container.indexOfId(id) != -1); assertTrue(container.removeItem(id)); - Assert.assertEquals(-1, container.indexOfId(id)); + assertEquals(-1, container.indexOfId(id)); } @Test @@ -962,7 +924,7 @@ public class SQLContainerTableQueryTest { @Test public void removeAllItems_table_shouldSucceed() throws SQLException { assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); + assertEquals(0, container.size()); } @Test @@ -972,7 +934,7 @@ public class SQLContainerTableQueryTest { container.addItem(); assertTrue(container.removeAllItems()); - Assert.assertEquals(0, container.size()); + assertEquals(0, container.size()); } // Set timeout to ensure there is no infinite looping (#12882) @@ -984,28 +946,28 @@ public class SQLContainerTableQueryTest { container.removeAllItems(); - Assert.assertEquals(container.size(), 0); + assertEquals(container.size(), 0); for (int i = 0; i < itemNumber; ++i) { container.addItem(); } container.commit(); - Assert.assertEquals(container.size(), itemNumber); + assertEquals(container.size(), itemNumber); assertTrue(container.removeAllItems()); container.commit(); - Assert.assertEquals(container.size(), 0); + assertEquals(container.size(), 0); } @Test public void commit_tableAddedItem_shouldBeWrittenToDB() throws SQLException { Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("New Name"); + container.getContainerProperty(id, NAME).setValue("New Name"); assertTrue(id instanceof TemporaryRowId); Assert.assertSame(id, container.lastItemId()); container.commit(); Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("New Name", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("New Name", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1014,19 +976,19 @@ public class SQLContainerTableQueryTest { throws SQLException { Object id = container.addItem(); Object id2 = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("Herbert"); - container.getContainerProperty(id2, "NAME").setValue("Larry"); + container.getContainerProperty(id, NAME).setValue("Herbert"); + container.getContainerProperty(id2, NAME).setValue("Larry"); assertTrue(id2 instanceof TemporaryRowId); Assert.assertSame(id2, container.lastItemId()); container.commit(); Object nextToLast = container.getIdByIndex(container.size() - 2); Assert.assertFalse(nextToLast instanceof TemporaryRowId); - Assert.assertEquals("Herbert", - container.getContainerProperty(nextToLast, "NAME").getValue()); + assertEquals("Herbert", container + .getContainerProperty(nextToLast, NAME).getValue()); Assert.assertFalse(container.lastItemId() instanceof TemporaryRowId); - Assert.assertEquals("Larry", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Larry", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1044,11 +1006,11 @@ public class SQLContainerTableQueryTest { public void commit_tableLastItemUpdated_shouldUpdateRowInDB() throws SQLException { Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("Donald"); + container.getContainerProperty(last, NAME).setValue("Donald"); container.commit(); - Assert.assertEquals("Donald", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Donald", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1057,23 +1019,23 @@ public class SQLContainerTableQueryTest { int size = container.size(); Object key = container.firstItemId(); Item row = container.getItem(key); - row.getItemProperty("NAME").setValue("Pekka"); + row.getItemProperty(NAME).setValue("Pekka"); assertTrue(container.removeItem(key)); container.commit(); - Assert.assertEquals(size - 1, container.size()); + assertEquals(size - 1, container.size()); } @Test public void rollback_tableItemAdded_discardsAddedItem() throws SQLException { int size = container.size(); Object id = container.addItem(); - container.getContainerProperty(id, "NAME").setValue("foo"); - Assert.assertEquals(size + 1, container.size()); + container.getContainerProperty(id, NAME).setValue("foo"); + assertEquals(size + 1, container.size()); container.rollback(); - Assert.assertEquals(size, container.size()); + assertEquals(size, container.size()); Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); + container.lastItemId(), NAME).getValue())); } @Test @@ -1082,19 +1044,19 @@ public class SQLContainerTableQueryTest { int size = container.size(); Object last = container.lastItemId(); container.removeItem(last); - Assert.assertEquals(size - 1, container.size()); + assertEquals(size - 1, container.size()); container.rollback(); - Assert.assertEquals(size, container.size()); - Assert.assertEquals(last, container.lastItemId()); + assertEquals(size, container.size()); + assertEquals(last, container.lastItemId()); } @Test public void rollback_tableItemChanged_discardsChanges() throws SQLException { Object last = container.lastItemId(); - container.getContainerProperty(last, "NAME").setValue("foo"); + container.getContainerProperty(last, NAME).setValue("foo"); container.rollback(); Assert.assertFalse("foo".equals(container.getContainerProperty( - container.lastItemId(), "NAME").getValue())); + container.lastItemId(), NAME).getValue())); } @Test @@ -1164,8 +1126,8 @@ public class SQLContainerTableQueryTest { @Test public void isModified_tableChangedItem_returnsTrue() throws SQLException { Assert.assertFalse(container.isModified()); - container.getContainerProperty(container.lastItemId(), "NAME") - .setValue("foo"); + container.getContainerProperty(container.lastItemId(), NAME).setValue( + "foo"); assertTrue(container.isModified()); } @@ -1173,10 +1135,10 @@ public class SQLContainerTableQueryTest { public void getSortableContainerPropertyIds_table_returnsAllPropertyIds() throws SQLException { Collection sortableIds = container.getSortableContainerPropertyIds(); - assertTrue(sortableIds.contains("ID")); - assertTrue(sortableIds.contains("NAME")); + assertTrue(sortableIds.contains(ID)); + assertTrue(sortableIds.contains(NAME)); assertTrue(sortableIds.contains("AGE")); - Assert.assertEquals(3, sortableIds.size()); + assertEquals(3, sortableIds.size()); if (SQLTestsConstants.db == DB.MSSQL || SQLTestsConstants.db == DB.ORACLE) { Assert.assertFalse(sortableIds.contains("rownum")); @@ -1186,20 +1148,20 @@ public class SQLContainerTableQueryTest { @Test public void addOrderBy_table_shouldReorderResults() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Ville", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); - container.addOrderBy(new OrderBy("NAME", true)); + container.addOrderBy(new OrderBy(NAME, true)); // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1211,51 +1173,51 @@ public class SQLContainerTableQueryTest { @Test public void sort_table_sortsByName() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Ville", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); - container.sort(new Object[] { "NAME" }, new boolean[] { true }); + container.sort(new Object[] { NAME }, new boolean[] { true }); // Börje, Kalle, Pelle, Ville - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @Test public void addFilter_table_filtersResults() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(4, container.size()); + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); - container.addContainerFilter(new Like("NAME", "%lle")); + container.addContainerFilter(new Like(NAME, "%lle")); // Ville, Kalle, Pelle - Assert.assertEquals(3, container.size()); - Assert.assertEquals("Pelle", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(3, container.size()); + assertEquals("Pelle", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @Test public void addContainerFilter_filtersResults() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); - container.addContainerFilter("NAME", "Vi", false, false); + container.addContainerFilter(NAME, "Vi", false, false); // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(1, container.size()); + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1263,14 +1225,14 @@ public class SQLContainerTableQueryTest { public void addContainerFilter_ignoreCase_filtersResults() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); - container.addContainerFilter("NAME", "vi", true, false); + container.addContainerFilter(NAME, "vi", true, false); // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(1, container.size()); + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1278,42 +1240,42 @@ public class SQLContainerTableQueryTest { public void removeAllContainerFilters_table_noFiltering() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); - container.addContainerFilter("NAME", "Vi", false, false); + container.addContainerFilter(NAME, "Vi", false, false); // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(1, container.size()); + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); container.removeAllContainerFilters(); - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(4, container.size()); + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @Test public void removeContainerFilters_table_noFiltering() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); + assertEquals(4, container.size()); - container.addContainerFilter("NAME", "Vi", false, false); + container.addContainerFilter(NAME, "Vi", false, false); // Ville - Assert.assertEquals(1, container.size()); - Assert.assertEquals("Ville", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(1, container.size()); + assertEquals("Ville", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); - container.removeContainerFilters("NAME"); + container.removeContainerFilters(NAME); - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(4, container.size()); + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } @@ -1321,36 +1283,32 @@ public class SQLContainerTableQueryTest { public void addFilter_tableBufferedItems_alsoFiltersBufferedItems() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals(4, container.size()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals(4, container.size()); + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Palle"); + container.getContainerProperty(id1, NAME).setValue("Palle"); Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Bengt"); + container.getContainerProperty(id2, NAME).setValue("Bengt"); - container.addContainerFilter(new Like("NAME", "%lle")); + container.addContainerFilter(new Like(NAME, "%lle")); // Ville, Kalle, Pelle, Palle - Assert.assertEquals(4, container.size()); - Assert.assertEquals( - "Ville", - container.getContainerProperty(container.getIdByIndex(0), - "NAME").getValue()); - Assert.assertEquals( - "Kalle", - container.getContainerProperty(container.getIdByIndex(1), - "NAME").getValue()); - Assert.assertEquals( - "Pelle", - container.getContainerProperty(container.getIdByIndex(2), - "NAME").getValue()); - Assert.assertEquals( - "Palle", - container.getContainerProperty(container.getIdByIndex(3), - "NAME").getValue()); + assertEquals(4, container.size()); + assertEquals("Ville", + container.getContainerProperty(container.getIdByIndex(0), NAME) + .getValue()); + assertEquals("Kalle", + container.getContainerProperty(container.getIdByIndex(1), NAME) + .getValue()); + assertEquals("Pelle", + container.getContainerProperty(container.getIdByIndex(2), NAME) + .getValue()); + assertEquals("Palle", + container.getContainerProperty(container.getIdByIndex(3), NAME) + .getValue()); try { container.getIdByIndex(4); @@ -1364,7 +1322,7 @@ public class SQLContainerTableQueryTest { Assert.assertFalse(container.getItemIds().contains(id2)); Assert.assertNull(container.getItem(id2)); - Assert.assertEquals(-1, container.indexOfId(id2)); + assertEquals(-1, container.indexOfId(id2)); Assert.assertNotSame(id2, container.lastItemId()); Assert.assertSame(id1, container.lastItemId()); @@ -1374,31 +1332,31 @@ public class SQLContainerTableQueryTest { public void sort_tableBufferedItems_sortsBufferedItemsLastInOrderAdded() throws SQLException { // Ville, Kalle, Pelle, Börje - Assert.assertEquals("Ville", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Ville", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals("Börje", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); Object id1 = container.addItem(); - container.getContainerProperty(id1, "NAME").setValue("Wilbert"); + container.getContainerProperty(id1, NAME).setValue("Wilbert"); Object id2 = container.addItem(); - container.getContainerProperty(id2, "NAME").setValue("Albert"); + container.getContainerProperty(id2, NAME).setValue("Albert"); - container.sort(new Object[] { "NAME" }, new boolean[] { true }); + container.sort(new Object[] { NAME }, new boolean[] { true }); // Börje, Kalle, Pelle, Ville, Wilbert, Albert - Assert.assertEquals("Börje", - container.getContainerProperty(container.firstItemId(), "NAME") + assertEquals("Börje", + container.getContainerProperty(container.firstItemId(), NAME) .getValue()); - Assert.assertEquals( + assertEquals( "Wilbert", container.getContainerProperty( - container.getIdByIndex(container.size() - 2), "NAME") + container.getIdByIndex(container.size() - 2), NAME) .getValue()); - Assert.assertEquals("Albert", - container.getContainerProperty(container.lastItemId(), "NAME") + assertEquals("Albert", + container.getContainerProperty(container.lastItemId(), NAME) .getValue()); } -- cgit v1.2.3 From b412ae97a659919b928fc28a3cb6500d75d514b2 Mon Sep 17 00:00:00 2001 From: Jonas Granvik Date: Mon, 12 Jan 2015 12:44:34 +0200 Subject: Add option to disable sending v-loc parameter in init request (#14460). Change-Id: Ie17e0621400c3397dc19b386974e231b6f82944c --- WebContent/VAADIN/vaadinBootstrap.js | 8 +++-- server/src/com/vaadin/server/BootstrapHandler.java | 6 ++++ server/src/com/vaadin/server/Constants.java | 1 + .../server/DefaultDeploymentConfiguration.java | 21 +++++++++++++ .../com/vaadin/server/DeploymentConfiguration.java | 9 ++++++ server/src/com/vaadin/server/Page.java | 6 ++++ .../AbstractDeploymentConfigurationTest.java | 5 +++ .../tests/util/MockDeploymentConfiguration.java | 6 ++++ .../DisableSendUrlAsParameters.java | 36 ++++++++++++++++++++++ .../DisableSendUrlAsParametersTest.java | 35 +++++++++++++++++++++ .../vaadin/tests/components/AbstractTestUI.java | 5 +++ 11 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParameters.java create mode 100644 uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParametersTest.java (limited to 'server/tests/src') diff --git a/WebContent/VAADIN/vaadinBootstrap.js b/WebContent/VAADIN/vaadinBootstrap.js index 53b213e110..89514dbcc2 100644 --- a/WebContent/VAADIN/vaadinBootstrap.js +++ b/WebContent/VAADIN/vaadinBootstrap.js @@ -136,7 +136,7 @@ params += extraParams; } - params += '&' + vaadin.getBrowserDetailsParameters(appId); + params += '&' + vaadin.getBrowserDetailsParameters(appId, getConfig('sendUrlsAsParameters')); var r; try { @@ -270,7 +270,7 @@ ws.pendingApps = null; } }, - getBrowserDetailsParameters: function(parentElementId) { + getBrowserDetailsParameters: function(parentElementId, sendUrlsAsParameters) { // Screen height and width var params = 'v-sh=' + window.screen.height; params += '&v-sw=' + window.screen.width; @@ -327,7 +327,9 @@ } // Location - params += '&v-loc=' + encodeURIComponent(location.href); + if (sendUrlsAsParameters !== false) { + params += '&v-loc=' + encodeURIComponent(location.href); + } // Window name if (window.name) { diff --git a/server/src/com/vaadin/server/BootstrapHandler.java b/server/src/com/vaadin/server/BootstrapHandler.java index e74f6d7c45..a9343a7e03 100644 --- a/server/src/com/vaadin/server/BootstrapHandler.java +++ b/server/src/com/vaadin/server/BootstrapHandler.java @@ -681,6 +681,12 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { appConfig.put(ApplicationConstants.SERVICE_URL, serviceUrl); } + boolean sendUrlsAsParameters = vaadinService + .getDeploymentConfiguration().isSendUrlsAsParameters(); + if (!sendUrlsAsParameters) { + appConfig.put("sendUrlsAsParameters", false); + } + return appConfig; } diff --git a/server/src/com/vaadin/server/Constants.java b/server/src/com/vaadin/server/Constants.java index 02a992a882..8036490333 100644 --- a/server/src/com/vaadin/server/Constants.java +++ b/server/src/com/vaadin/server/Constants.java @@ -136,6 +136,7 @@ public interface Constants { static final String SERVLET_PARAMETER_UI_PROVIDER = "UIProvider"; static final String SERVLET_PARAMETER_LEGACY_PROPERTY_TOSTRING = "legacyPropertyToString"; static final String SERVLET_PARAMETER_SYNC_ID_CHECK = "syncIdCheck"; + static final String SERVLET_PARAMETER_SENDURLSASPARAMETERS = "sendUrlsAsParameters"; // Configurable parameter names static final String PARAMETER_VAADIN_RESOURCES = "Resources"; diff --git a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java index 22d5210eaa..b26e048431 100644 --- a/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DefaultDeploymentConfiguration.java @@ -59,6 +59,8 @@ public class DefaultDeploymentConfiguration extends */ public static final boolean DEFAULT_SYNC_ID_CHECK = true; + public static final boolean DEFAULT_SEND_URLS_AS_PARAMETERS = true; + private final Properties initParameters; private boolean productionMode; private boolean xsrfProtectionEnabled; @@ -69,6 +71,7 @@ public class DefaultDeploymentConfiguration extends private final Class systemPropertyBaseClass; private LegacyProperyToStringMode legacyPropertyToStringMode; private boolean syncIdCheck; + private boolean sendUrlsAsParameters; /** * Create a new deployment configuration instance. @@ -93,6 +96,7 @@ public class DefaultDeploymentConfiguration extends checkPushMode(); checkLegacyPropertyToString(); checkSyncIdCheck(); + checkSendUrlsAsParameters(); } private void checkLegacyPropertyToString() { @@ -255,6 +259,16 @@ public class DefaultDeploymentConfiguration extends return syncIdCheck; } + /** + * {@inheritDoc} + *

+ * The default value is true. + */ + @Override + public boolean isSendUrlsAsParameters() { + return sendUrlsAsParameters; + } + /** * {@inheritDoc} *

@@ -347,6 +361,13 @@ public class DefaultDeploymentConfiguration extends Boolean.toString(DEFAULT_SYNC_ID_CHECK)).equals("true"); } + private void checkSendUrlsAsParameters() { + sendUrlsAsParameters = getApplicationOrSystemProperty( + Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS, + Boolean.toString(DEFAULT_SEND_URLS_AS_PARAMETERS)).equals( + "true"); + } + private Logger getLogger() { return Logger.getLogger(getClass().getName()); } diff --git a/server/src/com/vaadin/server/DeploymentConfiguration.java b/server/src/com/vaadin/server/DeploymentConfiguration.java index 3c20518c39..968ec7c0c3 100644 --- a/server/src/com/vaadin/server/DeploymentConfiguration.java +++ b/server/src/com/vaadin/server/DeploymentConfiguration.java @@ -110,6 +110,15 @@ public interface DeploymentConfiguration extends Serializable { */ public int getHeartbeatInterval(); + /** + * Returns whether the sending of URL's as GET and POST parameters in + * requests with content-type application/x-www-form-urlencoded + * is enabled or not. + * + * @return false if set to false or true otherwise + */ + public boolean isSendUrlsAsParameters(); + /** * Returns whether a session should be closed when all its open UIs have * been idle for longer than its configured maximum inactivity time. diff --git a/server/src/com/vaadin/server/Page.java b/server/src/com/vaadin/server/Page.java index 3ddf4862b2..74d79ade50 100644 --- a/server/src/com/vaadin/server/Page.java +++ b/server/src/com/vaadin/server/Page.java @@ -939,6 +939,12 @@ public class Page implements Serializable { * @return The browser location URI. */ public URI getLocation() { + if (location == null + && !uI.getSession().getConfiguration().isSendUrlsAsParameters()) { + throw new IllegalStateException("Location is not available as the " + + Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS + + " parameter is configured as false"); + } return location; } diff --git a/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java b/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java index 7370bd3fac..0518bea650 100644 --- a/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java +++ b/server/tests/src/com/vaadin/server/AbstractDeploymentConfigurationTest.java @@ -153,5 +153,10 @@ public class AbstractDeploymentConfigurationTest { return null; } + @Override + public boolean isSendUrlsAsParameters() { + return DefaultDeploymentConfiguration.DEFAULT_SEND_URLS_AS_PARAMETERS; + } + } } diff --git a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java index 8eceaea53f..ddee23a9ec 100644 --- a/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java +++ b/server/tests/src/com/vaadin/tests/util/MockDeploymentConfiguration.java @@ -21,6 +21,7 @@ public class MockDeploymentConfiguration extends private Map applicationOrSystemProperty = new HashMap(); private LegacyProperyToStringMode legacyPropertyToStringMode = LegacyProperyToStringMode.DISABLED; private boolean syncIdCheckEnabled = true; + private boolean sendUrlsAsParameters = true; @Override public boolean isProductionMode() { @@ -119,4 +120,9 @@ public class MockDeploymentConfiguration extends this.legacyPropertyToStringMode = legacyPropertyToStringMode; } + @Override + public boolean isSendUrlsAsParameters() { + return sendUrlsAsParameters; + } + } diff --git a/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParameters.java b/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParameters.java new file mode 100644 index 0000000000..d398ead622 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParameters.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.applicationservlet; + +import com.vaadin.launcher.CustomDeploymentConfiguration; +import com.vaadin.launcher.CustomDeploymentConfiguration.Conf; +import com.vaadin.server.Constants; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUIWithLog; + +@CustomDeploymentConfiguration({ @Conf(name = Constants.SERVLET_PARAMETER_SENDURLSASPARAMETERS, value = "false") }) +public class DisableSendUrlAsParameters extends AbstractTestUIWithLog { + + @Override + protected void setup(VaadinRequest request) { + try { + log("Init location: " + getPage().getLocation()); + } catch (IllegalStateException e) { + log("Init location exception: " + e.getMessage()); + } + } + +} diff --git a/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParametersTest.java b/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParametersTest.java new file mode 100644 index 0000000000..f10f281646 --- /dev/null +++ b/uitest/src/com/vaadin/tests/applicationservlet/DisableSendUrlAsParametersTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.applicationservlet; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class DisableSendUrlAsParametersTest extends SingleBrowserTest { + + @Test + public void testInitLocation() { + openTestURL(); + + String logRow = getLogRow(0); + + Assert.assertEquals( + "1. Init location exception: Location is not available as the sendUrlsAsParameters parameter is configured as false", + logRow); + } +} diff --git a/uitest/src/com/vaadin/tests/components/AbstractTestUI.java b/uitest/src/com/vaadin/tests/components/AbstractTestUI.java index 3a7d42e29c..dba055a65a 100644 --- a/uitest/src/com/vaadin/tests/components/AbstractTestUI.java +++ b/uitest/src/com/vaadin/tests/components/AbstractTestUI.java @@ -46,6 +46,11 @@ public abstract class AbstractTestUI extends UI { } protected void warnIfWidgetsetMaybeNotCompiled() { + // Can't check location if sendUrlAsParameters is disabled + if (!getSession().getConfiguration().isSendUrlsAsParameters()) { + return; + } + // Ignore if using debug mode String query = getPage().getLocation().getQuery(); if (query != null && query.matches(".*[&?]gwt\\.codesvr.*")) { -- cgit v1.2.3 From 8eeeb35544522b96cba07795f336a14e2737c7ea Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 13 Jan 2015 12:42:22 +0200 Subject: Fix remaining issues for NativeSelect blur/focus events (#6847) * Do not spam focus/blur events to the server * Receive focus blur events no matter which constructor is used * Run test on all browsers Change-Id: I7d548397e6df3a375f9263c695a53c801d9c5c4a --- client/src/com/vaadin/client/EventHelper.java | 55 ++++++++++++++-- client/src/com/vaadin/client/ui/VNativeSelect.java | 17 ++--- .../ui/nativeselect/NativeSelectConnector.java | 21 +++++- server/src/com/vaadin/ui/NativeSelect.java | 3 + .../tests/src/com/vaadin/ui/NativeSelectTest.java | 54 +++++++++++++++ .../NativeSelectsFocusAndBlurListenerTests.java | 76 +++++----------------- 6 files changed, 147 insertions(+), 79 deletions(-) create mode 100644 server/tests/src/com/vaadin/ui/NativeSelectTest.java (limited to 'server/tests/src') diff --git a/client/src/com/vaadin/client/EventHelper.java b/client/src/com/vaadin/client/EventHelper.java index a1cb75527d..f251215d41 100644 --- a/client/src/com/vaadin/client/EventHelper.java +++ b/client/src/com/vaadin/client/EventHelper.java @@ -25,6 +25,7 @@ import com.google.gwt.event.dom.client.FocusEvent; import com.google.gwt.event.dom.client.FocusHandler; import com.google.gwt.event.shared.EventHandler; import com.google.gwt.event.shared.HandlerRegistration; +import com.google.gwt.user.client.ui.Widget; /** * Helper class for attaching/detaching handlers for Vaadin client side @@ -61,7 +62,7 @@ public class EventHelper { * @param connector * The connector to update. Must implement focusHandler. * @param handlerRegistration - * The old registration reference or null no handler has been + * The old registration reference or null if no handler has been * registered previously * @return a new registration handler that can be used to unregister the * handler later @@ -69,7 +70,27 @@ public class EventHelper { public static HandlerRegistration updateFocusHandler( T connector, HandlerRegistration handlerRegistration) { return updateHandler(connector, FOCUS, handlerRegistration, - FocusEvent.getType()); + FocusEvent.getType(), connector.getWidget()); + } + + /** + * Adds or removes a focus handler depending on if the connector has focus + * listeners on the server side or not. + * + * @param connector + * The connector to update. Must implement focusHandler. + * @param handlerRegistration + * The old registration reference or null if no handler has been + * registered previously + * @param widget + * The widget which emits focus events + * @return a new registration handler that can be used to unregister the + * handler later + */ + public static HandlerRegistration updateFocusHandler( + T connector, HandlerRegistration handlerRegistration, Widget widget) { + return updateHandler(connector, FOCUS, handlerRegistration, + FocusEvent.getType(), widget); } /** @@ -79,7 +100,7 @@ public class EventHelper { * @param connector * The connector to update. Must implement BlurHandler. * @param handlerRegistration - * The old registration reference or null no handler has been + * The old registration reference or null if no handler has been * registered previously * @return a new registration handler that can be used to unregister the * handler later @@ -87,16 +108,36 @@ public class EventHelper { public static HandlerRegistration updateBlurHandler( T connector, HandlerRegistration handlerRegistration) { return updateHandler(connector, BLUR, handlerRegistration, - BlurEvent.getType()); + BlurEvent.getType(), connector.getWidget()); + } + + /** + * Adds or removes a blur handler depending on if the connector has blur + * listeners on the server side or not. + * + * @param connector + * The connector to update. Must implement BlurHandler. + * @param handlerRegistration + * The old registration reference or null if no handler has been + * registered previously + * @param widget + * The widget which emits blur events + * + * @return a new registration handler that can be used to unregister the + * handler later + */ + public static HandlerRegistration updateBlurHandler( + T connector, HandlerRegistration handlerRegistration, Widget widget) { + return updateHandler(connector, BLUR, handlerRegistration, + BlurEvent.getType(), widget); } private static HandlerRegistration updateHandler( ComponentConnector connector, String eventIdentifier, - HandlerRegistration handlerRegistration, Type type) { + HandlerRegistration handlerRegistration, Type type, Widget widget) { if (connector.hasEventListener(eventIdentifier)) { if (handlerRegistration == null) { - handlerRegistration = connector.getWidget().addDomHandler( - (H) connector, type); + handlerRegistration = widget.addDomHandler((H) connector, type); } } else if (handlerRegistration != null) { handlerRegistration.removeHandler(); diff --git a/client/src/com/vaadin/client/ui/VNativeSelect.java b/client/src/com/vaadin/client/ui/VNativeSelect.java index 879c54cae8..330442b550 100644 --- a/client/src/com/vaadin/client/ui/VNativeSelect.java +++ b/client/src/com/vaadin/client/ui/VNativeSelect.java @@ -19,9 +19,7 @@ package com.vaadin.client.ui; import java.util.ArrayList; import java.util.Iterator; -import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.ChangeEvent; -import com.google.gwt.event.dom.client.FocusHandler; import com.google.gwt.user.client.ui.ListBox; import com.vaadin.client.BrowserInfo; import com.vaadin.client.UIDL; @@ -119,14 +117,6 @@ public class VNativeSelect extends VOptionGroupBase implements Field { } } - public void addFocusHandler(FocusHandler handler) { - select.addFocusHandler(handler); - } - - public void addBlurHandler(BlurHandler handler) { - select.addBlurHandler(handler); - } - @Override public void setHeight(String height) { select.setHeight(height); @@ -154,4 +144,11 @@ public class VNativeSelect extends VOptionGroupBase implements Field { select.setFocus(true); } + /** + * @return the root select widget + */ + public ListBox getSelect() { + return getOptionsContainer(); + } + } diff --git a/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java b/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java index c52b762e58..938903da9a 100644 --- a/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java +++ b/client/src/com/vaadin/client/ui/nativeselect/NativeSelectConnector.java @@ -20,6 +20,9 @@ import com.google.gwt.event.dom.client.BlurEvent; import com.google.gwt.event.dom.client.BlurHandler; import com.google.gwt.event.dom.client.FocusEvent; import com.google.gwt.event.dom.client.FocusHandler; +import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.EventHelper; +import com.vaadin.client.annotations.OnStateChange; import com.vaadin.client.ui.VNativeSelect; import com.vaadin.client.ui.optiongroup.OptionGroupBaseConnector; import com.vaadin.shared.communication.FieldRpc.FocusAndBlurServerRpc; @@ -30,10 +33,19 @@ import com.vaadin.ui.NativeSelect; public class NativeSelectConnector extends OptionGroupBaseConnector implements BlurHandler, FocusHandler { + private HandlerRegistration focusHandlerRegistration = null; + private HandlerRegistration blurHandlerRegistration = null; + public NativeSelectConnector() { super(); - getWidget().addFocusHandler(this); - getWidget().addBlurHandler(this); + } + + @OnStateChange("registeredEventListeners") + private void onServerEventListenerChanged() { + focusHandlerRegistration = EventHelper.updateFocusHandler(this, + focusHandlerRegistration, getWidget().getSelect()); + blurHandlerRegistration = EventHelper.updateBlurHandler(this, + blurHandlerRegistration, getWidget().getSelect()); } @Override @@ -43,11 +55,16 @@ public class NativeSelectConnector extends OptionGroupBaseConnector implements @Override public void onFocus(FocusEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side getRpcProxy(FocusAndBlurServerRpc.class).focus(); } @Override public void onBlur(BlurEvent event) { + // EventHelper.updateFocusHandler ensures that this is called only when + // there is a listener on server side getRpcProxy(FocusAndBlurServerRpc.class).blur(); } + } diff --git a/server/src/com/vaadin/ui/NativeSelect.java b/server/src/com/vaadin/ui/NativeSelect.java index ee7bfc3e5c..137d57f677 100644 --- a/server/src/com/vaadin/ui/NativeSelect.java +++ b/server/src/com/vaadin/ui/NativeSelect.java @@ -56,14 +56,17 @@ public class NativeSelect extends AbstractSelect implements public NativeSelect(String caption, Collection options) { super(caption, options); + registerRpc(focusBlurRpc); } public NativeSelect(String caption, Container dataSource) { super(caption, dataSource); + registerRpc(focusBlurRpc); } public NativeSelect(String caption) { super(caption); + registerRpc(focusBlurRpc); } /** diff --git a/server/tests/src/com/vaadin/ui/NativeSelectTest.java b/server/tests/src/com/vaadin/ui/NativeSelectTest.java new file mode 100644 index 0000000000..7e2a04ef1c --- /dev/null +++ b/server/tests/src/com/vaadin/ui/NativeSelectTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.ui; + +import java.util.Collections; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.data.util.IndexedContainer; + +public class NativeSelectTest { + + @Test + public void rpcRegisteredConstructorNoArg() { + assertFocusRpcRegistered(new NativeSelect()); + } + + @Test + public void rpcRegisteredConstructorString() { + assertFocusRpcRegistered(new NativeSelect("foo")); + } + + @Test + public void rpcRegisteredConstructorStringCollection() { + assertFocusRpcRegistered(new NativeSelect("foo", + Collections.singleton("Hello"))); + } + + @Test + public void rpcRegisteredConstructorStringContainer() { + assertFocusRpcRegistered(new NativeSelect("foo", new IndexedContainer())); + } + + private void assertFocusRpcRegistered(NativeSelect s) { + Assert.assertNotNull( + "RPC is not correctly registered", + s.getRpcManager("com.vaadin.shared.communication.FieldRpc$FocusAndBlurServerRpc")); + } + +} diff --git a/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java index 29c8c27883..bf81ca4390 100644 --- a/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java +++ b/uitest/src/com/vaadin/tests/components/nativeselect/NativeSelectsFocusAndBlurListenerTests.java @@ -15,102 +15,58 @@ */ package com.vaadin.tests.components.nativeselect; -import java.util.Collections; -import java.util.List; - import org.junit.Assert; import org.junit.Test; import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; -import org.openqa.selenium.remote.DesiredCapabilities; +import com.vaadin.testbench.elements.NativeSelectElement; import com.vaadin.tests.tb3.MultiBrowserTest; -/** - * - * @since - * @author Vaadin Ltd - */ public class NativeSelectsFocusAndBlurListenerTests extends MultiBrowserTest { - /* - * (non-Javadoc) - * - * @see com.vaadin.tests.tb3.MultiBrowserTest#getBrowsersToTest() - */ - @Override - public List getBrowsersToTest() { - return Collections.singletonList(Browser.CHROME - .getDesiredCapabilities()); - } - @Test - public void testFocusListener() throws InterruptedException { + public void testFocusAndBlurListener() throws InterruptedException { setDebug(true); openTestURL(); - Thread.sleep(1000); + Thread.sleep(200); menu("Component"); menuSub("Listeners"); menuSub("Focus listener"); - - getDriver().findElement(By.tagName("body")).click(); - - WebElement select = getDriver().findElement(By.tagName("select")); - select.click(); - - String bodytext = getDriver().findElement(By.tagName("body")).getText(); - - Assert.assertTrue(bodytext.contains("FocusEvent")); - - } - - @Test - public void testBlurListener() throws InterruptedException { - setDebug(true); - openTestURL(); - Thread.sleep(1000); menu("Component"); menuSub("Listeners"); menuSub("Blur listener"); - getDriver().findElement(By.tagName("body")).click(); - - WebElement select = getDriver().findElement(By.tagName("select")); - select.click(); + findElement(By.tagName("body")).click(); + NativeSelectElement s = $(NativeSelectElement.class).first(); + s.selectByText("Item 3"); getDriver().findElement(By.tagName("body")).click(); - String bodytext = getDriver().findElement(By.tagName("body")).getText(); - - Assert.assertTrue(bodytext.contains("BlurEvent")); + // Somehow selectByText causes focus + blur + focus + blur on + // Chrome/PhantomJS + if (BrowserUtil.isChrome(getDesiredCapabilities()) + || BrowserUtil.isPhantomJS(getDesiredCapabilities())) { + Assert.assertEquals("4. FocusEvent", getLogRow(1)); + Assert.assertEquals("5. BlurEvent", getLogRow(0)); + } else { + Assert.assertEquals("2. FocusEvent", getLogRow(1)); + Assert.assertEquals("3. BlurEvent", getLogRow(0)); + } } - /* - * (non-Javadoc) - * - * @see com.vaadin.tests.tb3.AbstractTB3Test#getUIClass() - */ @Override protected Class getUIClass() { return NativeSelects.class; } - /** - * @since - * @param string - */ private void menuSub(String string) { getDriver().findElement(By.xpath("//span[text() = '" + string + "']")) .click(); new Actions(getDriver()).moveByOffset(100, 0).build().perform(); } - /** - * @since - * @param string - */ private void menu(String string) { getDriver().findElement(By.xpath("//span[text() = '" + string + "']")) .click(); -- cgit v1.2.3 From 7cd562df674a9855d0a757f29518405e834c8037 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Thu, 8 Jan 2015 17:59:21 +0200 Subject: Allow to override transactional property wrapper strategy (#13883). Change-Id: I38acc3c6cfbe66ac9e9ce246accbb9a0b058dddb --- .../src/com/vaadin/data/fieldgroup/FieldGroup.java | 13 +++--- .../vaadin/data/fieldgroup/FieldGroupTests.java | 47 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) (limited to 'server/tests/src') diff --git a/server/src/com/vaadin/data/fieldgroup/FieldGroup.java b/server/src/com/vaadin/data/fieldgroup/FieldGroup.java index a8cabc5887..4790d786e7 100644 --- a/server/src/com/vaadin/data/fieldgroup/FieldGroup.java +++ b/server/src/com/vaadin/data/fieldgroup/FieldGroup.java @@ -266,6 +266,14 @@ public class FieldGroup implements Serializable { configureField(field); } + /** + * Wrap property to transactional property. + */ + protected Property.Transactional wrapInTransactionalProperty( + Property itemProperty) { + return new TransactionalPropertyWrapper(itemProperty); + } + private void throwIfFieldIsNull(Field field, Object propertyId) { if (field == null) { throw new BindException( @@ -283,11 +291,6 @@ public class FieldGroup implements Serializable { } } - private Property.Transactional wrapInTransactionalProperty( - Property itemProperty) { - return new TransactionalPropertyWrapper(itemProperty); - } - /** * Gets the property with the given property id from the item. * diff --git a/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java b/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java index eb8f21c839..fc267fc7da 100644 --- a/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java +++ b/server/tests/src/com/vaadin/data/fieldgroup/FieldGroupTests.java @@ -4,10 +4,16 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.mockito.Mockito.mock; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import com.vaadin.data.Property; +import com.vaadin.data.Property.Transactional; +import com.vaadin.data.util.BeanItem; +import com.vaadin.data.util.TransactionalPropertyWrapper; import com.vaadin.ui.Field; +import com.vaadin.ui.TextField; public class FieldGroupTests { @@ -37,4 +43,45 @@ public class FieldGroupTests { public void cannotBindNullField() { sut.bind(null, "foobar"); } + + @Test + public void wrapInTransactionalProperty_provideCustomImpl_customTransactionalWrapperIsUsed() { + Bean bean = new Bean(); + FieldGroup group = new FieldGroup() { + @Override + protected Transactional wrapInTransactionalProperty( + Property itemProperty) { + return new TransactionalPropertyImpl(itemProperty); + } + }; + group.setItemDataSource(new BeanItem(bean)); + TextField field = new TextField(); + group.bind(field, "name"); + + Property propertyDataSource = field.getPropertyDataSource(); + Assert.assertTrue("Custom implementation of transactional property " + + "has not been used", + propertyDataSource instanceof TransactionalPropertyImpl); + } + + public static class TransactionalPropertyImpl extends + TransactionalPropertyWrapper { + + public TransactionalPropertyImpl(Property wrappedProperty) { + super(wrappedProperty); + } + + } + + public static class Bean { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + } } -- cgit v1.2.3