From f1c94667627a34a1ba6a93a4724a194a9873f323 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Dahlstr=C3=B6m?= Date: Tue, 10 Feb 2015 14:53:11 +0200 Subject: [PATCH] Fix NullPointerException when changing Grid container (#16559) Change-Id: I6e8ca4af7baf0b560e9f28dfd6b7dd437c1a4254 --- .../vaadin/data/RpcDataProviderExtension.java | 4 +- .../component/grid/GridContainerTest.java | 46 ++++++++++++++ .../tests/server/component/grid/TestGrid.java | 62 +++++++++++++++++++ .../tests/server/renderer/RendererTest.java | 12 +--- 4 files changed, 112 insertions(+), 12 deletions(-) create mode 100644 server/tests/src/com/vaadin/tests/server/component/grid/GridContainerTest.java create mode 100644 server/tests/src/com/vaadin/tests/server/component/grid/TestGrid.java diff --git a/server/src/com/vaadin/data/RpcDataProviderExtension.java b/server/src/com/vaadin/data/RpcDataProviderExtension.java index 924bd3de84..354bfe336c 100644 --- a/server/src/com/vaadin/data/RpcDataProviderExtension.java +++ b/server/src/com/vaadin/data/RpcDataProviderExtension.java @@ -984,9 +984,8 @@ public class RpcDataProviderExtension extends AbstractExtension { @Override public void setParent(ClientConnector parent) { - super.setParent(parent); if (parent == null) { - // We're detached, release various listeners + // We're being detached, release various listeners activeRowHandler .removeValueChangeListeners(activeRowHandler.activeRange); @@ -1004,6 +1003,7 @@ public class RpcDataProviderExtension extends AbstractExtension { throw new IllegalStateException( "Grid is the only accepted parent type"); } + super.setParent(parent); } /** diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/GridContainerTest.java b/server/tests/src/com/vaadin/tests/server/component/grid/GridContainerTest.java new file mode 100644 index 0000000000..cbd448618b --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/GridContainerTest.java @@ -0,0 +1,46 @@ +/* + * 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.server.component.grid; + +import org.junit.Test; + +import com.vaadin.data.util.IndexedContainer; + +public class GridContainerTest { + + @Test + public void testSetContainerTwice() throws Exception { + + TestGrid grid = new TestGrid(); + + grid.setContainerDataSource(createContainer()); + + // Simulate initial response to ensure "lazy" state changes are done + // before resetting the datasource + grid.beforeClientResponse(true); + grid.getDataProvider().beforeClientResponse(true); + + grid.setContainerDataSource(createContainer()); + } + + @SuppressWarnings("unchecked") + private IndexedContainer createContainer() { + IndexedContainer container = new IndexedContainer(); + container.addContainerProperty("x", String.class, null); + container.addItem(0).getItemProperty("x").setValue("y"); + return container; + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/grid/TestGrid.java b/server/tests/src/com/vaadin/tests/server/component/grid/TestGrid.java new file mode 100644 index 0000000000..63fa569ab4 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/grid/TestGrid.java @@ -0,0 +1,62 @@ +/* + * 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.server.component.grid; + +import java.lang.reflect.Field; + +import org.easymock.EasyMock; + +import com.vaadin.data.RpcDataProviderExtension; +import com.vaadin.data.util.IndexedContainer; +import com.vaadin.ui.ConnectorTracker; +import com.vaadin.ui.Grid; +import com.vaadin.ui.UI; + +/** + * A Grid attached to a mock UI with a mock ConnectorTracker. + * + * @since 7.4 + * @author Vaadin Ltd + */ +public class TestGrid extends Grid { + + public TestGrid() { + super(); + init(); + } + + public TestGrid(IndexedContainer c) { + super(c); + init(); + } + + public RpcDataProviderExtension getDataProvider() throws Exception { + Field dseField = Grid.class.getDeclaredField("datasourceExtension"); + dseField.setAccessible(true); + return (RpcDataProviderExtension) dseField.get(this); + } + + private void init() { + UI mockUI = EasyMock.createNiceMock(UI.class); + ConnectorTracker mockCT = EasyMock + .createNiceMock(ConnectorTracker.class); + EasyMock.expect(mockUI.getConnectorTracker()).andReturn(mockCT) + .anyTimes(); + EasyMock.replay(mockUI, mockCT); + + setParent(mockUI); + } +} diff --git a/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java index 05a67b3f02..8bcbc6a5a9 100644 --- a/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java +++ b/server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java @@ -21,7 +21,6 @@ import static org.junit.Assert.assertSame; import java.util.Locale; -import org.easymock.EasyMock; import org.junit.Before; import org.junit.Test; @@ -31,11 +30,10 @@ import com.vaadin.data.util.IndexedContainer; import com.vaadin.data.util.converter.Converter; import com.vaadin.data.util.converter.StringToIntegerConverter; import com.vaadin.server.VaadinSession; +import com.vaadin.tests.server.component.grid.TestGrid; import com.vaadin.tests.util.AlwaysLockedVaadinSession; -import com.vaadin.ui.ConnectorTracker; import com.vaadin.ui.Grid; import com.vaadin.ui.Grid.Column; -import com.vaadin.ui.UI; import com.vaadin.ui.renderers.TextRenderer; import elemental.json.JsonValue; @@ -119,13 +117,7 @@ public class RendererTest { item.getItemProperty("baz").setValue(new TestBean()); item.getItemProperty("bah").setValue(new ExtendedBean()); - UI ui = EasyMock.createNiceMock(UI.class); - ConnectorTracker ct = EasyMock.createNiceMock(ConnectorTracker.class); - EasyMock.expect(ui.getConnectorTracker()).andReturn(ct).anyTimes(); - EasyMock.replay(ui, ct); - - grid = new Grid(c); - grid.setParent(ui); + grid = new TestGrid(c); foo = grid.getColumn("foo"); bar = grid.getColumn("bar"); -- 2.39.5