Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When setting the dataprovider in the Grid constructor do not rely on … #12423

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/src/main/java/com/vaadin/ui/Grid.java
Original file line number Diff line number Diff line change
Expand Up @@ -5061,7 +5061,7 @@ protected SerializableComparator<T> createSortingComparator() {

@Override
protected void internalSetDataProvider(DataProvider<T, ?> dataProvider) {
boolean newProvider = getDataProvider() != dataProvider;
boolean newProvider = internalGetDataProvider() != dataProvider;
super.internalSetDataProvider(dataProvider);
if (newProvider) {
Set<T> oldVisibleDetails = new HashSet<>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.vaadin.tests.components.grid;

import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.ui.Grid;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class GridOverrideDataProviderGetterTest
{
private ListDataProvider<MyBean> listDataProvider;

@Before
public void setUp()
{
MyBean myBean1 = new MyBean("name1");
MyBean myBean2 = new MyBean("name2");
listDataProvider = new ListDataProvider<>(Arrays.asList(myBean1,myBean2));
}

/**
* Originally calling the constructor of MyGrid will throw:
* <pre>
* java.lang.ClassCastException: class com.vaadin.data.provider.CallbackDataProvider cannot be cast to class com.vaadin.data.provider.ListDataProvider (com.vaadin.data.provider.CallbackDataProvider and com.vaadin.data.provider.ListDataProvider are in unnamed module of loader 'app')
* </pre>
* Don't call an overridable method in your constructor is the moral of the story
*/
@Test
public void testCalling()
{
try {
Grid<MyBean> grid = new MyGrid<>(listDataProvider);
assertTrue(grid.getDataProvider() instanceof ListDataProvider);
} catch(ClassCastException e) {
fail("Should not throw a ClassCastException error:" + e);
}
}

private static class MyGrid<T> extends Grid<T>
{
public MyGrid(ListDataProvider<T> myDataProvider)
{
super(myDataProvider);
}

/**
* I'm overriding this because I can
*/
@Override
public ListDataProvider<T> getDataProvider()
{
return (ListDataProvider<T>) super.getDataProvider();
}
}

private static class MyBean
{
private final String name;

MyBean(String name)
{
this.name = name;
}

public String getName()
{
return name;
}
}
}