]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix NullPointerException when changing Grid container (#16559)
authorJohannes Dahlström <johannesd@vaadin.com>
Tue, 10 Feb 2015 12:53:11 +0000 (14:53 +0200)
committerVaadin Code Review <review@vaadin.com>
Wed, 11 Feb 2015 11:40:34 +0000 (11:40 +0000)
Change-Id: I6e8ca4af7baf0b560e9f28dfd6b7dd437c1a4254

server/src/com/vaadin/data/RpcDataProviderExtension.java
server/tests/src/com/vaadin/tests/server/component/grid/GridContainerTest.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/component/grid/TestGrid.java [new file with mode: 0644]
server/tests/src/com/vaadin/tests/server/renderer/RendererTest.java

index 924bd3de841ea249dcf7191e052a035c5b7564f8..354bfe336c62088bafbdd5b53e073476dfda9fa3 100644 (file)
@@ -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 (file)
index 0000000..cbd4486
--- /dev/null
@@ -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 (file)
index 0000000..63fa569
--- /dev/null
@@ -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);
+    }
+}
index 05a67b3f02eaef15722e64365e78949fd1d2cb2d..8bcbc6a5a99eea463c3424f194c645d99a6975a2 100644 (file)
@@ -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");