From: elmot Date: Thu, 10 Nov 2016 13:30:11 +0000 (+0200) Subject: Renaming DataSource -> DataProvider to avoid collisions with javax.sql.DataSource X-Git-Tag: 8.0.0.alpha7~3 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c0d712cdbea886ea299644144bd6d7445cecec44;p=vaadin-framework.git Renaming DataSource -> DataProvider to avoid collisions with javax.sql.DataSource Change-Id: I6babcadc72f3f37feff98088eee23b153a080f11 --- diff --git a/documentation/datamodel/chapter-datamodel.asciidoc b/documentation/datamodel/chapter-datamodel.asciidoc index 5fa29a6957..6e9cf214d5 100644 --- a/documentation/datamodel/chapter-datamodel.asciidoc +++ b/documentation/datamodel/chapter-datamodel.asciidoc @@ -14,7 +14,7 @@ include::datamodel-fields.asciidoc[leveloffset=+2] include::datamodel-forms.asciidoc[leveloffset=+2] -include::datamodel-datasources.asciidoc[leveloffset=+2] +include::datamodel-providers.asciidoc[leveloffset=+2] include::datamodel-selection.asciidoc[leveloffset=+2] (((range="endofrange", startref="term.datamodel"))) diff --git a/documentation/datamodel/datamodel-datasources.asciidoc b/documentation/datamodel/datamodel-datasources.asciidoc deleted file mode 100644 index f8cf2fe6b9..0000000000 --- a/documentation/datamodel/datamodel-datasources.asciidoc +++ /dev/null @@ -1,470 +0,0 @@ ---- -title: Showing Many Items in a Listing -order: 4 -layout: page ---- - -[[datamodel.datasources]] -= Showing Many Items in a Listing - -A common pattern in applications is that the user is first presented with a list of items, from which she selects one or several items to continue working with. -These items could be inventory records to survey, messages to respond to or blog drafts to edit or publish. - -A [interfacename]#Listing# is a component that displays one or several properties from a list of item, allowing the user to inspect the data, mark items as selected and in some cases even edit the item directly through the component. -While each listing component has it's own API for configuring exactly how the data is represented and how it can be manipulated, they all share the same mechanisms for receiving data to show. - -The items are generally either loaded directly from memory or lazy loaded from some kind of backend. -Regardless of how the items are loaded, the component is configured with one or several callbacks or JavaBean property names that define how the item should be displayed. - -In the following example, a [classname]#ComboBox# that lists status items is configured to use the [classname]#Status#.[methodname]#getCaption()# method to represent each status. -There is also a [classname]#Grid#, which is configured with one column from the person's name and another column that converts the year of birth to a string for displaying. - -[source, java] ----- -ComboBox comboBox = new ComboBox<>(); -comboBox.setItemCaptionProvider(Status::getCaption); - -Grid grid = new Grid<>(); -grid.addColumn("Name", Person::getName); -grid.addColumn("Year of birth", - person -> Integer.toString(person.getYearOfBirth())); ----- - -[NOTE] -In this example, it would not even be necessary to define any item caption provider for the combo box if [classname]#Status#.[methodname]#toString()# would be implemented to return a suitable text. [classname]#ComboBox# is by default configured to use [methodname]#toString()# for finding a caption to show. - -[NOTE] -The `Year of birth` column will use [classname]#Grid#'s default [classname]#TextRenderer# which requires the column value to be a [classname]#String#. We could for instance use a [classname]#NumberRenderer# instead, and then the renderer would take care of converting the the number according to its configuration. - -After we have told the component how the data should be shown, we only need to give it some data to actually show. The easiest way of doing that is as a [interfacename]#java.util.Collection# of item instances. - -[source, java] ----- -comboBox.setItems(EnumSet.allOf(Status.class)); - -List persons = Arrays.asList( - new Person("George Washington", 1732), - new Person("John Adams", 1735), - new Person("Thomas Jefferson", 1743), - new Person("James Madison", 1751)); - -grid.setItems(persons); ----- - -Listing components that allow the user to control the display order of the items are automatically able to sort data by any property as long as the property type implements [classname]#Comparable#. - -We can also define a custom [classname]#Comparator# if we want to customize the way a specific column is sorted. The comparator can either be based on the item instances or on the values of the property that is being shown. - -[source, java] ----- -grid.addColumn("Name", Person::getName) - // Override default natural sorting - .setValueComparator( - Comparator.comparing(String::toLowerCase)); - -grid.addColumn("Year of birth", - person -> Integer.toString(person.getYearOfBirth())) - // Sort numerically instead of alphabetically by the string - .setItemComparator( - Comparator.comparing(Person::getYearOfBirth)); ----- - -With listing components that let the user filter items, we can in the same way define our own [interfacename]#BiPredicate# that is used to decide whether a specific item should be shown when the user has entered a specific text into the text field. - -[source, java] ----- -comboBox.setFilter((filterText, item) -> - item.getCaption().equalsIgnoreCase(filterText)); ----- - -Instead of directly assigning the item collection as the items that a component should be using, we can instead create a [classname]#ListDataSource# that contains the items. -The list data source can be shared between different components in the same [classname]#VaadinSession# since it is stateless. -We can also apply different sorting options for each component, without affecting how data is shown in the other components. - -[source, java] ----- -ListDataSource dataSource = - new ListDataSource<>(persons); - -ComboBox comboBox = new ComboBox<>(); -// The combo box shows the person sorted by name -comboBox.setDataSource( - dataSource.sortedBy(Person::getName)); - -Grid grid = new Grid<>(); -// The grid shows the same persons sorted by year of birth -grid.setDataSource( - dataSource.sortedBy(Person::getYearOfBirth)); ----- - -The [classname]#Listing# component cannot automatically know about changes to the list of items or to any individual item. -We must notify the data source when items are changed, added or removed so that components using the data will show the new values. - -[source, java] ----- -ListDataSource dataSource = - new ListDataSource<>(persons); - -Button addPersonButton = new Button("Add person", - clickEvent -> { - // Keep track of the index where the person will be added - int addIndex = persons.size(); - - persons.add(new Person("James Monroe", 1758)); - - dataSource.notifyAdd(addIndex); -}); - -Button modifyPersonButton = new Button("Modify person", - clickEvent -> { - Person personToChange = persons.get(0); - - personToChange.setName("Changed person"); - - dataSource.refresh(0); -}); ----- - -[TIP] -There might be situations where we cannot tell exactly how the data has changed, but only that some parts might have been modified. We can then use the [methodname]#refreshAll()# method, which will make the components reload all the data. - -== Lazy Loading Data to a Listing - -All the previous examples have shown cases with a limited amount of data that can be loaded as item instances in memory. -There are also situations where it is more efficient to only load the items that will currently be displayed. -This includes situations where all available data would use lots of memory or when it would take a long time to load all the items. - -[NOTE] -Regardless of how we make the items available to the listing component on the server, components like [classname]#Grid# will always take care of only sending the currently needed items to the browser. - -For example, if we have the following existing backend service that fetches items from a database or a REST service . - -[source, java] ----- -public interface PersonService { - List fetchPersons(int offset, int limit); - int getPersonCount(); -} ----- - -To use this service with a listing component, we need to define one callback for loading specific items and one callback for finding how many items are currently available. -Information about which items to fetch as well as some additional details are made available in a [interfacename]#Query# object that is passed to both callbacks. - -[source, java] ----- -DataSource dataSource = new BackendDataSource<>( - // First callback fetches items based on a query - query -> { - // The index of the first item to load - int offset = query.getOffset(); - - // The number of items to load - int limit = query.getLimit(); - - List persons = getPersonService().fetchPersons(offset, limit); - - return persons.stream(); - }, - // Second callback fetches the number of items for a query - query -> getPersonService().getPersonCount() -); - -Grid grid = new Grid<>(); -grid.setDataSource(dataSource); - -// Columns are configured in the same way as before -... ----- - -[NOTE] -The results of the first and second callback must be symmetric so that fetching all available items using the first callback returns the number of items indicated by the second callback. Thus if you impose any restrictions on e.g. a database query in the first callback, you must also add the same restrictions for the second callback. - -=== Sorting - -It is not practical to order items based on a [interfacename]#Comparator# when the items are loaded on demand, since it would require all items to be loaded and inspected. - -Each backend has its own way of defining how the fetched items should be ordered, but they are in general based on a list of property names and information on whether ordering should be ascending or descending. - -As an example, there could be a service interface which looks like the following. - -[source, java] ----- -public interface PersonService { - List fetchPersons( - int offset, - int limit, - List sortOrders); - - int getPersonCount(); - - PersonSort createSort( - String propertyName, - boolean descending); -} ----- - -With the above service interface, our data source can be enhanced to convert the provided sorting options into a format expected by the service. -The sorting options set through the component will be available through [interfacename]#Query#.[methodname]#getSortOrders()#. - -[source, java] ----- -DataSource dataSource = new BackEndDataSource<>( - query -> { - List sortOrders = new ArrayList<>(); - for(SortOrder queryOrder : query.getSortOrders()) { - PersonSort sort = getPersonService().createSort( - // The name of the sorted property - queryOrder.getSorted(), - // The sort direction for this property - queryOrder.getDirection() == SortDirection.DESCENDING); - sortOrders.add(sort); - } - - return getPersonService().fetchPersons( - query.getOffset(), - query.getLimit(), - sortOrders - ).stream(); - }, - // The number of persons is the same regardless of ordering - query -> persons.getPersonCount() -); ----- - -We also need to configure our grid so that it can know what property name should be included in the query when the user wants to sort by a specific column. -When a data source that does lazy loading is used, [classname]#Grid# and other similar components will only let the user sort by columns for which a sort property name is provided. - -[source, java] ----- -Grid grid = new Grid<>(); - -grid.setDataSource(dataSource); - -// Will be sortable by the user -// When sorting by this column, the query will have a SortOrder -// where getSorted() returns "name" -grid.addColumn("Name", Person::getName) - .setSortProperty("name"); - -// Will not be sortable since no sorting info is given -grid.addColumn("Year of birth", - person -> Integer.toString(person.getYearOfBirth())); ----- - -There might also be cases where a single property name is not enough for sorting. -This might be the case if the backend needs to sort by multiple properties for one column in the user interface or if the backend sort order should be inverted compared to the sort order defined by the user. -In such cases, we can define a callback that generates suitable [classname]#SortOrder# values for the given column. - -[source, java] ----- -grid.addColumn("Name", - person -> person.getFirstName() + " " + person.getLastName()) - .setSortBuilder( - // Sort according to last name, then first name - direction -> Stream.of( - new SortOrder("lastName", direction), - new SortOrder("firstName", direction) - )); ----- - -=== Filtering - -A similar approach is also needed with filtering in cases such as [classname]#ComboBox# where the user can control how items are filtered. - -The filtering of a data source query is represented as a [interfacename]#BackendFilter# instance. There are existing implementations for some common filtering cases, such as requiring a named property to not be null or a SQL `LIKE` comparison. - -[source, java] ----- -ComboBox comboBox = new ComboBox<>(); - -comboBox.setItemCaptionProvider(Person::getName); - -comboBox.setFilter( - // corresponds to this SQL: WHERE name LIKE [filterText] - filterText -> new Like("name", filterText)); ----- - -If we have a service interface that only supports some specific filtering option, the implementation might become simpler if we define our own [interfacename]#BackendFilter# instead of implementing our backend to use the generic built-in filter types. - -As an example, our service interface with support for filtering could look like this. Ordering support has been omitted in these examples to keep focus on filtering. - -[source, java] ----- -public interface PersonService { - List fetchPersons( - int offset, - int limit, - String namePrefix); - int getPersonCount(String namePrefix); -} ----- - -For the filtering needs of this service, we could define a [classname]#NamePrefixFilter# that corresponds to the only filtering option available. - -[source, java] ----- -public class NamePrefixFilter implements BackendFilter { - private final String prefix; - - public NamePrefixFilter(String prefix) { - this.prefix = prefix; - } - - public String getPrefix() { - return prefix; - } -} ----- - -In the case of [classname]#ComboBox#, we have to define what kind of [interfacename]#BackendFilter# to use when the user has entered some text that should be used for filtering the displayed items. - -[source, java] ----- -comboBox.setFilter( - filterText -> new NamePrefixFilter(filterText)); ----- - -We can then implement our data source to look for this special filter implementation and pass the name prefix to the service. -We can create a helper method for handling the filter since the same logic is needed both for fetching and counting items. - -[source, java] ----- -DataSource dataSource = new BackEndDataSource<>( - query -> { - - BackendFilter filter = query.getFilter(); - - String namePrefix = filterToNamePrefix(filter); - - return service.fetchPersons( - query.getOffset(), - query.getLimit(), - namePrefix - ).stream(); - }, - query -> persons.getPersonCount( - filterToNamePrefix(query.getFilter)) -); - -public static String filterToNamePrefix(BackendFilter filter) { - if (filter == null) { - return null; - } - - if (filter instanceof NamePrefixFilter)) { - return ((NamePrefixFilter) filter).getPrefix(); - } else { - throw new UnsupportedOperationException( - "This data source only supports NamePrefixFilter"); - } -} ----- - -[TIP] -If the amount of data in the backend is small enough, it might be better to load all the items into a list and use a [classname]#ListDataSource# instead of implementing filtering or sorting support in a custom [classname]#DataSource# class and configuring the components accordingly. - -We can also create a base data source and then use different variations for different components, similarly to the previous examples with [classname]#ListDataSource#. - -[source, java] ----- -DataSource dataSource = ... - -grid.setDataSource(dataSource - .filteredBy(new Like("name", "Ge%")) - .sortedBy(new SortOrder( - "yearOfBirth", SortDirection.ASCENDING))); - -comboBox.setDataSource(dataSource - .sortedBy(new SortOrder( - "name", SortOrder.DESCENDING))); - ----- - -=== Special Fetching Cases - -In some cases it might be necessary directly extend [classname]#BackendDataSource# instead of constructing an instance based the two simple callback methods shown above. - -One such case is if the backend loads items based on a page index and a page size so that the start index in the query always needs to be a multiple of the page size. As an example, our service interface made for paging could look like this. - -[source, java] ----- -public interface PersonService { - List fetchPersons( - int pageIndex, - int pageSize); - int getPersonCount(); -} ----- - -We can use this kind of backend service as long as we also make the data source declare that queries should always be done for whole pages. -Components using this data source will take the information into account when querying for data. - -[source, java] ----- -public class PersonDataSource - extends BackendDataSource { - - @Override - public boolean alignQueries() { - // Informs the part that fetches items that the query offset - // must be a multiple of the query limit, i.e. that only full - // pages should be requested - return true; - } - - @Override - public void fetch(Query query, - FetchResult result) { - int pageSize = query.getLimit(); - - // Caller guarantees that query.getOffset() % pageSize == 0 - int pageIndex = query.getOffset() / pageSize; - - result.setItems(getPersonService().fetchPersons(pageIndex, pageSize)); - } - - @Override - public int getCount(Query query) { - return getPersonService().getPersonCount(); - } -} ----- - -Some backends may also have limitations on how many (or few) items can be fetched at once. -While our data source implementation could deal with that limitation internally by sending multiple requests to the backend and then assembling the results together before returning the result, we can also make the data source indicate that the responsibility for splitting up the query is on the caller instead. - -[source, java] ----- -public class PersonDataSource - extends BackendDataSource { - - @Override - public int getMaxLimit() { - // Informs the part that fetches items that the maximum - // supported query limit size is 30 - return 30; - } - - @Override - public void fetch(Query query, - FetchResult result) { - List persons = getPersonService().fetchPersons( - query.getOffset(), - query.getLimit()); - result.setItems(persons); - } - - @Override - public int getCount(Query query) { - return getPersonService().getPersonCount(); - } -} ----- - -[TIP] -You can set the max limit and the min limit to the same value if you are using a backend that has a hardcoded page size. You can also combine this with aligned queries. - -Yet another case that benefits from custom querying options is backends that perform better if items are fetched relative to a previously executed query instead of by skipping items based on an absolute offset. - -To help with this, the provided query object will automatically contain a reference to the item immediately before the start of the first new item to fetch if available. -The item immediately after the end of the range to fetch might also be available in some cases if the user is scrolling through the data backwards. There are, however, no guarantees that either item will be available in all queries, so the implementation should always also support fetching by offset. diff --git a/documentation/datamodel/datamodel-providers.asciidoc b/documentation/datamodel/datamodel-providers.asciidoc new file mode 100644 index 0000000000..852c1f0c66 --- /dev/null +++ b/documentation/datamodel/datamodel-providers.asciidoc @@ -0,0 +1,470 @@ +--- +title: Showing Many Items in a Listing +order: 4 +layout: page +--- + +[[datamodel.dataproviders]] += Showing Many Items in a Listing + +A common pattern in applications is that the user is first presented with a list of items, from which she selects one or several items to continue working with. +These items could be inventory records to survey, messages to respond to or blog drafts to edit or publish. + +A [interfacename]#Listing# is a component that displays one or several properties from a list of item, allowing the user to inspect the data, mark items as selected and in some cases even edit the item directly through the component. +While each listing component has it's own API for configuring exactly how the data is represented and how it can be manipulated, they all share the same mechanisms for receiving data to show. + +The items are generally either loaded directly from memory or lazy loaded from some kind of backend. +Regardless of how the items are loaded, the component is configured with one or several callbacks or JavaBean property names that define how the item should be displayed. + +In the following example, a [classname]#ComboBox# that lists status items is configured to use the [classname]#Status#.[methodname]#getCaption()# method to represent each status. +There is also a [classname]#Grid#, which is configured with one column from the person's name and another column that converts the year of birth to a string for displaying. + +[source, java] +---- +ComboBox comboBox = new ComboBox<>(); +comboBox.setItemCaptionProvider(Status::getCaption); + +Grid grid = new Grid<>(); +grid.addColumn("Name", Person::getName); +grid.addColumn("Year of birth", + person -> Integer.toString(person.getYearOfBirth())); +---- + +[NOTE] +In this example, it would not even be necessary to define any item caption provider for the combo box if [classname]#Status#.[methodname]#toString()# would be implemented to return a suitable text. [classname]#ComboBox# is by default configured to use [methodname]#toString()# for finding a caption to show. + +[NOTE] +The `Year of birth` column will use [classname]#Grid#'s default [classname]#TextRenderer# which requires the column value to be a [classname]#String#. We could for instance use a [classname]#NumberRenderer# instead, and then the renderer would take care of converting the the number according to its configuration. + +After we have told the component how the data should be shown, we only need to give it some data to actually show. The easiest way of doing that is as a [interfacename]#java.util.Collection# of item instances. + +[source, java] +---- +comboBox.setItems(EnumSet.allOf(Status.class)); + +List persons = Arrays.asList( + new Person("George Washington", 1732), + new Person("John Adams", 1735), + new Person("Thomas Jefferson", 1743), + new Person("James Madison", 1751)); + +grid.setItems(persons); +---- + +Listing components that allow the user to control the display order of the items are automatically able to sort data by any property as long as the property type implements [classname]#Comparable#. + +We can also define a custom [classname]#Comparator# if we want to customize the way a specific column is sorted. The comparator can either be based on the item instances or on the values of the property that is being shown. + +[source, java] +---- +grid.addColumn("Name", Person::getName) + // Override default natural sorting + .setValueComparator( + Comparator.comparing(String::toLowerCase)); + +grid.addColumn("Year of birth", + person -> Integer.toString(person.getYearOfBirth())) + // Sort numerically instead of alphabetically by the string + .setItemComparator( + Comparator.comparing(Person::getYearOfBirth)); +---- + +With listing components that let the user filter items, we can in the same way define our own [interfacename]#BiPredicate# that is used to decide whether a specific item should be shown when the user has entered a specific text into the text field. + +[source, java] +---- +comboBox.setFilter((filterText, item) -> + item.getCaption().equalsIgnoreCase(filterText)); +---- + +Instead of directly assigning the item collection as the items that a component should be using, we can instead create a [classname]#ListDataProvider# that contains the items. +The list data source can be shared between different components in the same [classname]#VaadinSession# since it is stateless. +We can also apply different sorting options for each component, without affecting how data is shown in the other components. + +[source, java] +---- +ListDataProvider dataProvider = + new ListDataProvider<>(persons); + +ComboBox comboBox = new ComboBox<>(); +// The combo box shows the person sorted by name +comboBox.setDataProvider( + dataProvider.sortedBy(Person::getName)); + +Grid grid = new Grid<>(); +// The grid shows the same persons sorted by year of birth +grid.setDataProvider( + dataProvider.sortedBy(Person::getYearOfBirth)); +---- + +The [classname]#Listing# component cannot automatically know about changes to the list of items or to any individual item. +We must notify the data source when items are changed, added or removed so that components using the data will show the new values. + +[source, java] +---- +ListDataProvider dataProvider = + new ListDataProvider<>(persons); + +Button addPersonButton = new Button("Add person", + clickEvent -> { + // Keep track of the index where the person will be added + int addIndex = persons.size(); + + persons.add(new Person("James Monroe", 1758)); + + dataProvider.notifyAdd(addIndex); +}); + +Button modifyPersonButton = new Button("Modify person", + clickEvent -> { + Person personToChange = persons.get(0); + + personToChange.setName("Changed person"); + + dataProvider.refresh(0); +}); +---- + +[TIP] +There might be situations where we cannot tell exactly how the data has changed, but only that some parts might have been modified. We can then use the [methodname]#refreshAll()# method, which will make the components reload all the data. + +== Lazy Loading Data to a Listing + +All the previous examples have shown cases with a limited amount of data that can be loaded as item instances in memory. +There are also situations where it is more efficient to only load the items that will currently be displayed. +This includes situations where all available data would use lots of memory or when it would take a long time to load all the items. + +[NOTE] +Regardless of how we make the items available to the listing component on the server, components like [classname]#Grid# will always take care of only sending the currently needed items to the browser. + +For example, if we have the following existing backend service that fetches items from a database or a REST service . + +[source, java] +---- +public interface PersonService { + List fetchPersons(int offset, int limit); + int getPersonCount(); +} +---- + +To use this service with a listing component, we need to define one callback for loading specific items and one callback for finding how many items are currently available. +Information about which items to fetch as well as some additional details are made available in a [interfacename]#Query# object that is passed to both callbacks. + +[source, java] +---- +DataProvider dataProvider = new BackendDataProvider<>( + // First callback fetches items based on a query + query -> { + // The index of the first item to load + int offset = query.getOffset(); + + // The number of items to load + int limit = query.getLimit(); + + List persons = getPersonService().fetchPersons(offset, limit); + + return persons.stream(); + }, + // Second callback fetches the number of items for a query + query -> getPersonService().getPersonCount() +); + +Grid grid = new Grid<>(); +grid.setDataProvider(dataProvider); + +// Columns are configured in the same way as before +... +---- + +[NOTE] +The results of the first and second callback must be symmetric so that fetching all available items using the first callback returns the number of items indicated by the second callback. Thus if you impose any restrictions on e.g. a database query in the first callback, you must also add the same restrictions for the second callback. + +=== Sorting + +It is not practical to order items based on a [interfacename]#Comparator# when the items are loaded on demand, since it would require all items to be loaded and inspected. + +Each backend has its own way of defining how the fetched items should be ordered, but they are in general based on a list of property names and information on whether ordering should be ascending or descending. + +As an example, there could be a service interface which looks like the following. + +[source, java] +---- +public interface PersonService { + List fetchPersons( + int offset, + int limit, + List sortOrders); + + int getPersonCount(); + + PersonSort createSort( + String propertyName, + boolean descending); +} +---- + +With the above service interface, our data source can be enhanced to convert the provided sorting options into a format expected by the service. +The sorting options set through the component will be available through [interfacename]#Query#.[methodname]#getSortOrders()#. + +[source, java] +---- +DataProvider dataProvider = new BackEndDataProvider<>( + query -> { + List sortOrders = new ArrayList<>(); + for(SortOrder queryOrder : query.getSortOrders()) { + PersonSort sort = getPersonService().createSort( + // The name of the sorted property + queryOrder.getSorted(), + // The sort direction for this property + queryOrder.getDirection() == SortDirection.DESCENDING); + sortOrders.add(sort); + } + + return getPersonService().fetchPersons( + query.getOffset(), + query.getLimit(), + sortOrders + ).stream(); + }, + // The number of persons is the same regardless of ordering + query -> persons.getPersonCount() +); +---- + +We also need to configure our grid so that it can know what property name should be included in the query when the user wants to sort by a specific column. +When a data source that does lazy loading is used, [classname]#Grid# and other similar components will only let the user sort by columns for which a sort property name is provided. + +[source, java] +---- +Grid grid = new Grid<>(); + +grid.setDataProvider(dataProvider); + +// Will be sortable by the user +// When sorting by this column, the query will have a SortOrder +// where getSorted() returns "name" +grid.addColumn("Name", Person::getName) + .setSortProperty("name"); + +// Will not be sortable since no sorting info is given +grid.addColumn("Year of birth", + person -> Integer.toString(person.getYearOfBirth())); +---- + +There might also be cases where a single property name is not enough for sorting. +This might be the case if the backend needs to sort by multiple properties for one column in the user interface or if the backend sort order should be inverted compared to the sort order defined by the user. +In such cases, we can define a callback that generates suitable [classname]#SortOrder# values for the given column. + +[source, java] +---- +grid.addColumn("Name", + person -> person.getFirstName() + " " + person.getLastName()) + .setSortBuilder( + // Sort according to last name, then first name + direction -> Stream.of( + new SortOrder("lastName", direction), + new SortOrder("firstName", direction) + )); +---- + +=== Filtering + +A similar approach is also needed with filtering in cases such as [classname]#ComboBox# where the user can control how items are filtered. + +The filtering of a data source query is represented as a [interfacename]#BackendFilter# instance. There are existing implementations for some common filtering cases, such as requiring a named property to not be null or a SQL `LIKE` comparison. + +[source, java] +---- +ComboBox comboBox = new ComboBox<>(); + +comboBox.setItemCaptionProvider(Person::getName); + +comboBox.setFilter( + // corresponds to this SQL: WHERE name LIKE [filterText] + filterText -> new Like("name", filterText)); +---- + +If we have a service interface that only supports some specific filtering option, the implementation might become simpler if we define our own [interfacename]#BackendFilter# instead of implementing our backend to use the generic built-in filter types. + +As an example, our service interface with support for filtering could look like this. Ordering support has been omitted in these examples to keep focus on filtering. + +[source, java] +---- +public interface PersonService { + List fetchPersons( + int offset, + int limit, + String namePrefix); + int getPersonCount(String namePrefix); +} +---- + +For the filtering needs of this service, we could define a [classname]#NamePrefixFilter# that corresponds to the only filtering option available. + +[source, java] +---- +public class NamePrefixFilter implements BackendFilter { + private final String prefix; + + public NamePrefixFilter(String prefix) { + this.prefix = prefix; + } + + public String getPrefix() { + return prefix; + } +} +---- + +In the case of [classname]#ComboBox#, we have to define what kind of [interfacename]#BackendFilter# to use when the user has entered some text that should be used for filtering the displayed items. + +[source, java] +---- +comboBox.setFilter( + filterText -> new NamePrefixFilter(filterText)); +---- + +We can then implement our data source to look for this special filter implementation and pass the name prefix to the service. +We can create a helper method for handling the filter since the same logic is needed both for fetching and counting items. + +[source, java] +---- +DataProvider dataProvider = new BackEndDataProvider<>( + query -> { + + BackendFilter filter = query.getFilter(); + + String namePrefix = filterToNamePrefix(filter); + + return service.fetchPersons( + query.getOffset(), + query.getLimit(), + namePrefix + ).stream(); + }, + query -> persons.getPersonCount( + filterToNamePrefix(query.getFilter)) +); + +public static String filterToNamePrefix(BackendFilter filter) { + if (filter == null) { + return null; + } + + if (filter instanceof NamePrefixFilter)) { + return ((NamePrefixFilter) filter).getPrefix(); + } else { + throw new UnsupportedOperationException( + "This data source only supports NamePrefixFilter"); + } +} +---- + +[TIP] +If the amount of data in the backend is small enough, it might be better to load all the items into a list and use a [classname]#ListDataProvider# instead of implementing filtering or sorting support in a custom [classname]#DataProvider# class and configuring the components accordingly. + +We can also create a base data source and then use different variations for different components, similarly to the previous examples with [classname]#ListDataProvider#. + +[source, java] +---- +DataProvider dataProvider = ... + +grid.setDataProvider(dataProvider + .filteredBy(new Like("name", "Ge%")) + .sortedBy(new SortOrder( + "yearOfBirth", SortDirection.ASCENDING))); + +comboBox.setDataProvider(dataProvider + .sortedBy(new SortOrder( + "name", SortOrder.DESCENDING))); + +---- + +=== Special Fetching Cases + +In some cases it might be necessary directly extend [classname]#BackendDataProvider# instead of constructing an instance based the two simple callback methods shown above. + +One such case is if the backend loads items based on a page index and a page size so that the start index in the query always needs to be a multiple of the page size. As an example, our service interface made for paging could look like this. + +[source, java] +---- +public interface PersonService { + List fetchPersons( + int pageIndex, + int pageSize); + int getPersonCount(); +} +---- + +We can use this kind of backend service as long as we also make the data source declare that queries should always be done for whole pages. +Components using this data source will take the information into account when querying for data. + +[source, java] +---- +public class PersonDataProvider + extends BackendDataProvider { + + @Override + public boolean alignQueries() { + // Informs the part that fetches items that the query offset + // must be a multiple of the query limit, i.e. that only full + // pages should be requested + return true; + } + + @Override + public void fetch(Query query, + FetchResult result) { + int pageSize = query.getLimit(); + + // Caller guarantees that query.getOffset() % pageSize == 0 + int pageIndex = query.getOffset() / pageSize; + + result.setItems(getPersonService().fetchPersons(pageIndex, pageSize)); + } + + @Override + public int getCount(Query query) { + return getPersonService().getPersonCount(); + } +} +---- + +Some backends may also have limitations on how many (or few) items can be fetched at once. +While our data source implementation could deal with that limitation internally by sending multiple requests to the backend and then assembling the results together before returning the result, we can also make the data source indicate that the responsibility for splitting up the query is on the caller instead. + +[source, java] +---- +public class PersonDataProvider + extends BackendDataProvider { + + @Override + public int getMaxLimit() { + // Informs the part that fetches items that the maximum + // supported query limit size is 30 + return 30; + } + + @Override + public void fetch(Query query, + FetchResult result) { + List persons = getPersonService().fetchPersons( + query.getOffset(), + query.getLimit()); + result.setItems(persons); + } + + @Override + public int getCount(Query query) { + return getPersonService().getPersonCount(); + } +} +---- + +[TIP] +You can set the max limit and the min limit to the same value if you are using a backend that has a hardcoded page size. You can also combine this with aligned queries. + +Yet another case that benefits from custom querying options is backends that perform better if items are fetched relative to a previously executed query instead of by skipping items based on an absolute offset. + +To help with this, the provided query object will automatically contain a reference to the item immediately before the start of the first new item to fetch if available. +The item immediately after the end of the range to fetch might also be available in some cases if the user is scrolling through the data backwards. There are, however, no guarantees that either item will be available in all queries, so the implementation should always also support fetching by offset. diff --git a/server/src/main/java/com/vaadin/data/Listing.java b/server/src/main/java/com/vaadin/data/Listing.java index 422b3a1128..ec48be8875 100644 --- a/server/src/main/java/com/vaadin/data/Listing.java +++ b/server/src/main/java/com/vaadin/data/Listing.java @@ -18,7 +18,7 @@ package com.vaadin.data; import java.io.Serializable; import java.util.Collection; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; /** * A generic interface for components that show a list of data. @@ -34,18 +34,18 @@ public interface Listing extends Serializable { /** * Returns the source of data items used by this listing. * - * @return the data source, not null + * @return the data provider, not null */ - DataSource getDataSource(); + DataProvider getDataProvider(); /** - * Sets the source of data items used by this listing. The data source is + * Sets the source of data items used by this listing. The data provider is * queried for displayed items as needed. * - * @param dataSource - * the data source, not null + * @param dataProvider + * the data provider, not null */ - void setDataSource(DataSource dataSource); + void setDataProvider(DataProvider dataProvider); /** * Sets the collection of data items of this listing. @@ -55,7 +55,7 @@ public interface Listing extends Serializable { * */ default void setItems(Collection items) { - setDataSource(DataSource.create(items)); + setDataProvider(DataProvider.create(items)); } /** @@ -65,7 +65,7 @@ public interface Listing extends Serializable { * the data items to display */ default void setItems(@SuppressWarnings("unchecked") T... items) { - setDataSource(DataSource.create(items)); + setDataProvider(DataProvider.create(items)); } } diff --git a/server/src/main/java/com/vaadin/server/data/AbstractDataProvider.java b/server/src/main/java/com/vaadin/server/data/AbstractDataProvider.java new file mode 100644 index 0000000000..531d77e44c --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/AbstractDataProvider.java @@ -0,0 +1,102 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.lang.reflect.Method; +import java.util.EventObject; + +import com.vaadin.event.EventRouter; +import com.vaadin.shared.Registration; + +/** + * Abstract data provider implementation which takes care of refreshing data from + * the underlying data provider. + * + * @author Vaadin Ltd + * @since 8.0 + * + */ +public abstract class AbstractDataProvider implements DataProvider { + + private EventRouter eventRouter; + + @Override + public Registration addDataProviderListener(DataProviderListener listener) { + addListener(DataChangeEvent.class, listener, + DataProviderListener.class.getMethods()[0]); + return () -> removeListener(DataChangeEvent.class, listener); + } + + @Override + public void refreshAll() { + fireEvent(new DataChangeEvent(this)); + } + + /** + * Registers a new listener with the specified activation method to listen + * events generated by this component. If the activation method does not + * have any arguments the event object will not be passed to it when it's + * called. + * + * @param eventType + * the type of the listened event. Events of this type or its + * subclasses activate the listener. + * @param listener + * the object instance who owns the activation method. + * @param method + * the activation method. + * + */ + protected void addListener(Class eventType, DataProviderListener listener, + Method method) { + if (eventRouter == null) { + eventRouter = new EventRouter(); + } + eventRouter.addListener(eventType, listener, method); + } + + /** + * Removes all registered listeners matching the given parameters. Since + * this method receives the event type and the listener object as + * parameters, it will unregister all object's methods that are + * registered to listen to events of type eventType generated + * by this component. + * + * @param eventType + * the exact event type the object listens to. + * @param listener + * the target object that has registered to listen to events of + * type eventType with one or more methods. + */ + protected void removeListener(Class eventType, + DataProviderListener listener) { + if (eventRouter != null) { + eventRouter.removeListener(eventType, listener); + } + } + + /** + * Sends the event to all listeners. + * + * @param event + * the Event to be sent to all listeners. + */ + protected void fireEvent(EventObject event) { + if (eventRouter != null) { + eventRouter.fireEvent(event); + } + } +} diff --git a/server/src/main/java/com/vaadin/server/data/AbstractDataSource.java b/server/src/main/java/com/vaadin/server/data/AbstractDataSource.java deleted file mode 100644 index f54056e1b0..0000000000 --- a/server/src/main/java/com/vaadin/server/data/AbstractDataSource.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.lang.reflect.Method; -import java.util.EventObject; -import java.util.Objects; - -import com.vaadin.event.EventRouter; -import com.vaadin.shared.Registration; - -/** - * Abstract data source implementation which takes care of refreshing data from - * the underlying data provider. - * - * @author Vaadin Ltd - * @since 8.0 - * - */ -public abstract class AbstractDataSource implements DataSource { - - private EventRouter eventRouter; - - @Override - public Registration addDataSourceListener(DataSourceListener listener) { - addListener(DataChangeEvent.class, listener, - DataSourceListener.class.getMethods()[0]); - return () -> removeListener(DataChangeEvent.class, listener); - } - - @Override - public void refreshAll() { - fireEvent(new DataChangeEvent(this)); - } - - /** - * Registers a new listener with the specified activation method to listen - * events generated by this component. If the activation method does not - * have any arguments the event object will not be passed to it when it's - * called. - * - * @param eventType - * the type of the listened event. Events of this type or its - * subclasses activate the listener. - * @param listener - * the object instance who owns the activation method. - * @param method - * the activation method. - * - */ - protected void addListener(Class eventType, DataSourceListener listener, - Method method) { - if (eventRouter == null) { - eventRouter = new EventRouter(); - } - eventRouter.addListener(eventType, listener, method); - } - - /** - * Removes all registered listeners matching the given parameters. Since - * this method receives the event type and the listener object as - * parameters, it will unregister all object's methods that are - * registered to listen to events of type eventType generated - * by this component. - * - * @param eventType - * the exact event type the object listens to. - * @param listener - * the target object that has registered to listen to events of - * type eventType with one or more methods. - */ - protected void removeListener(Class eventType, - DataSourceListener listener) { - if (eventRouter != null) { - eventRouter.removeListener(eventType, listener); - } - } - - /** - * Sends the event to all listeners. - * - * @param event - * the Event to be sent to all listeners. - */ - protected void fireEvent(EventObject event) { - if (eventRouter != null) { - eventRouter.fireEvent(event); - } - } -} diff --git a/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java b/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java new file mode 100644 index 0000000000..89a780248f --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/BackEndDataProvider.java @@ -0,0 +1,86 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; + +import com.vaadin.server.SerializableFunction; + +/** + * A {@link DataProvider} for any back end. + * + * @param + * data provider data type + */ +public class BackEndDataProvider extends AbstractDataProvider { + + private final SerializableFunction> request; + private final SerializableFunction sizeCallback; + + /** + * Constructs a new DataProvider to request data from an arbitrary back end + * request function. + * + * @param request + * function that requests data from back end based on query + * @param sizeCallback + * function that return the amount of data in back end for query + */ + public BackEndDataProvider(SerializableFunction> request, + SerializableFunction sizeCallback) { + Objects.requireNonNull(request, "Request function can't be null"); + Objects.requireNonNull(sizeCallback, "Size callback can't be null"); + this.request = request; + this.sizeCallback = sizeCallback; + } + + @Override + public Stream fetch(Query query) { + return request.apply(query); + } + + @Override + public int size(Query query) { + return sizeCallback.apply(query); + } + + /** + * Sets a default sorting order to the data provider. + * + * @param sortOrders + * a list of sorting information containing field ids and + * directions + * @return new data provider with modified sorting + */ + public BackEndDataProvider sortingBy(List> sortOrders) { + return new BackEndDataProvider<>(query -> { + List> queryOrder = new ArrayList<>( + query.getSortOrders()); + queryOrder.addAll(sortOrders); + return request.apply(new Query(query.getLimit(), query.getOffset(), + queryOrder, query.getFilters())); + }, sizeCallback); + } + + @Override + public boolean isInMemory() { + return false; + } + +} diff --git a/server/src/main/java/com/vaadin/server/data/BackEndDataSource.java b/server/src/main/java/com/vaadin/server/data/BackEndDataSource.java deleted file mode 100644 index 3ca31de15a..0000000000 --- a/server/src/main/java/com/vaadin/server/data/BackEndDataSource.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.stream.Stream; - -import com.vaadin.server.SerializableFunction; - -/** - * A {@link DataSource} for any back end. - * - * @param - * data source data type - */ -public class BackEndDataSource extends AbstractDataSource { - - private final SerializableFunction> request; - private final SerializableFunction sizeCallback; - - /** - * Constructs a new DataSource to request data from an arbitrary back end - * request function. - * - * @param request - * function that requests data from back end based on query - * @param sizeCallback - * function that return the amount of data in back end for query - */ - public BackEndDataSource(SerializableFunction> request, - SerializableFunction sizeCallback) { - Objects.requireNonNull(request, "Request function can't be null"); - Objects.requireNonNull(sizeCallback, "Size callback can't be null"); - this.request = request; - this.sizeCallback = sizeCallback; - } - - @Override - public Stream fetch(Query query) { - return request.apply(query); - } - - @Override - public int size(Query query) { - return sizeCallback.apply(query); - } - - /** - * Sets a default sorting order to the data source. - * - * @param sortOrders - * a list of sorting information containing field ids and - * directions - * @return new data source with modified sorting - */ - public BackEndDataSource sortingBy(List> sortOrders) { - return new BackEndDataSource<>(query -> { - List> queryOrder = new ArrayList<>( - query.getSortOrders()); - queryOrder.addAll(sortOrders); - return request.apply(new Query(query.getLimit(), query.getOffset(), - queryOrder, query.getFilters())); - }, sizeCallback); - } - - @Override - public boolean isInMemory() { - return false; - } - -} diff --git a/server/src/main/java/com/vaadin/server/data/DataChangeEvent.java b/server/src/main/java/com/vaadin/server/data/DataChangeEvent.java index cf2e9621de..94d28fc9d3 100644 --- a/server/src/main/java/com/vaadin/server/data/DataChangeEvent.java +++ b/server/src/main/java/com/vaadin/server/data/DataChangeEvent.java @@ -18,10 +18,10 @@ package com.vaadin.server.data; import java.util.EventObject; /** - * An event fired when the data of a {@code DataSource} changes. + * An event fired when the data of a {@code DataProvider} changes. * * - * @see DataSourceListener + * @see DataProviderListener * * @author Vaadin Ltd * @since 8.0 @@ -31,18 +31,18 @@ public class DataChangeEvent extends EventObject { /** * Creates a new {@code DataChangeEvent} event originating from the given - * data source. + * data provider. * * @param source - * the data source, not null + * the data provider, not null */ - public DataChangeEvent(DataSource source) { + public DataChangeEvent(DataProvider source) { super(source); } @Override - public DataSource getSource() { - return (DataSource) super.getSource(); + public DataProvider getSource() { + return (DataProvider) super.getSource(); } } diff --git a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java index 24bb464722..46b8fa079a 100644 --- a/server/src/main/java/com/vaadin/server/data/DataCommunicator.java +++ b/server/src/main/java/com/vaadin/server/data/DataCommunicator.java @@ -52,7 +52,7 @@ import elemental.json.JsonObject; */ public class DataCommunicator extends AbstractExtension { - private Registration dataSourceUpdateRegistration; + private Registration dataProviderUpdateRegistration; /** * Simple implementation of collection data provider communication. All data @@ -180,7 +180,7 @@ public class DataCommunicator extends AbstractExtension { private final Collection> generators = new LinkedHashSet<>(); private final ActiveDataHandler handler = new ActiveDataHandler(); - private DataSource dataSource = DataSource.create(); + private DataProvider dataProvider = DataProvider.create(); private final DataKeyMapper keyMapper; private boolean reset = false; @@ -202,13 +202,13 @@ public class DataCommunicator extends AbstractExtension { @Override public void attach() { super.attach(); - attachDataSourceListener(); + attachDataProviderListener(); } @Override public void detach() { super.detach(); - detachDataSourceListener(); + detachDataProviderListener(); } /** @@ -219,7 +219,7 @@ public class DataCommunicator extends AbstractExtension { public void beforeClientResponse(boolean initial) { super.beforeClientResponse(initial); - if (getDataSource() == null) { + if (getDataProvider() == null) { return; } @@ -227,14 +227,14 @@ public class DataCommunicator extends AbstractExtension { Set filters = Collections.emptySet(); if (initial || reset) { - int dataSourceSize; - if (getDataSource().isInMemory() && inMemoryFilter != null) { - dataSourceSize = (int) getDataSource().fetch(new Query()) + int dataProviderSize; + if (getDataProvider().isInMemory() && inMemoryFilter != null) { + dataProviderSize = (int) getDataProvider().fetch(new Query()) .filter(inMemoryFilter).count(); } else { - dataSourceSize = getDataSource().size(new Query(filters)); + dataProviderSize = getDataProvider().size(new Query(filters)); } - rpc.reset(dataSourceSize); + rpc.reset(dataProviderSize); } if (!pushRows.isEmpty()) { @@ -243,9 +243,9 @@ public class DataCommunicator extends AbstractExtension { Stream rowsToPush; - if (getDataSource().isInMemory()) { + if (getDataProvider().isInMemory()) { // We can safely request all the data when in memory - rowsToPush = getDataSource().fetch(new Query()); + rowsToPush = getDataProvider().fetch(new Query()); if (inMemoryFilter != null) { rowsToPush = rowsToPush.filter(inMemoryFilter); } @@ -255,7 +255,7 @@ public class DataCommunicator extends AbstractExtension { rowsToPush = rowsToPush.skip(offset).limit(limit); } else { Query query = new Query(offset, limit, backEndSorting, filters); - rowsToPush = getDataSource().fetch(query); + rowsToPush = getDataProvider().fetch(query); } pushData(offset, rowsToPush); } @@ -458,39 +458,39 @@ public class DataCommunicator extends AbstractExtension { } /** - * Gets the current data source from this DataCommunicator. + * Gets the current data provider from this DataCommunicator. * - * @return the data source + * @return the data provider */ - public DataSource getDataSource() { - return dataSource; + public DataProvider getDataProvider() { + return dataProvider; } /** - * Sets the current data source for this DataCommunicator. + * Sets the current data provider for this DataCommunicator. * - * @param dataSource - * the data source to set, not null + * @param dataProvider + * the data provider to set, not null */ - public void setDataSource(DataSource dataSource) { - Objects.requireNonNull(dataSource, "data source cannot be null"); - this.dataSource = dataSource; - detachDataSourceListener(); + public void setDataProvider(DataProvider dataProvider) { + Objects.requireNonNull(dataProvider, "data provider cannot be null"); + this.dataProvider = dataProvider; + detachDataProviderListener(); if (isAttached()) { - attachDataSourceListener(); + attachDataProviderListener(); } reset(); } - private void attachDataSourceListener() { - dataSourceUpdateRegistration = getDataSource() - .addDataSourceListener(event -> reset()); + private void attachDataProviderListener() { + dataProviderUpdateRegistration = getDataProvider() + .addDataProviderListener(event -> reset()); } - private void detachDataSourceListener() { - if (dataSourceUpdateRegistration != null) { - dataSourceUpdateRegistration.remove(); - dataSourceUpdateRegistration = null; + private void detachDataProviderListener() { + if (dataProviderUpdateRegistration != null) { + dataProviderUpdateRegistration.remove(); + dataProviderUpdateRegistration = null; } } } diff --git a/server/src/main/java/com/vaadin/server/data/DataGenerator.java b/server/src/main/java/com/vaadin/server/data/DataGenerator.java index ff4c82888c..f062e9b80a 100644 --- a/server/src/main/java/com/vaadin/server/data/DataGenerator.java +++ b/server/src/main/java/com/vaadin/server/data/DataGenerator.java @@ -35,7 +35,7 @@ public interface DataGenerator extends Serializable { /** * Adds custom data for the given item to its serialized {@code JsonObject} - * representation. This JSON object will be sent to client-side DataSource. + * representation. This JSON object will be sent to client-side DataProvider. * * @param item * the data item being serialized diff --git a/server/src/main/java/com/vaadin/server/data/DataProvider.java b/server/src/main/java/com/vaadin/server/data/DataProvider.java new file mode 100644 index 0000000000..b99cbe5d75 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/DataProvider.java @@ -0,0 +1,119 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collection; +import java.util.stream.Stream; + +import com.vaadin.shared.Registration; + +/** + * Minimal DataProvider API for communication between the DataProvider and a back + * end service. + * + * @author Vaadin Ltd. + * + * @param + * data type + * + * @see ListDataProvider + * @see BackEndDataProvider + * + * @since 8.0 + */ +public interface DataProvider extends Serializable { + + /** + * Gets whether the DataProvider content all available in memory or does it + * use some external backend. + * + * @return {@code true} if all data is in memory; {@code false} if not + */ + boolean isInMemory(); + + /** + * Gets the amount of data in this DataProvider. + * + * @param t + * query with sorting and filtering + * @return the size of the data provider + */ + int size(Query t); + + /** + * Fetches data from this DataProvider using given {@code query}. + * + * @param query + * given query to request data + * @return the result of the query request: a stream of data objects, not + * {@code null} + */ + Stream fetch(Query query); + + /** + * Refreshes all data based on currently available data in the underlying + * provider. + */ + void refreshAll(); + + /** + * Adds a data provider listener. The listener is called when some piece of + * data is updated. + *

+ * The {@link #refreshAll()} method fires {@link DataChangeEvent} each time + * when it's called. It allows to update UI components when user changes + * something in the underlying data. + * + * @see #refreshAll() + * @param listener + * the data change listener, not null + * @return a registration for the listener + */ + Registration addDataProviderListener(DataProviderListener listener); + + /** + * This method creates a new {@link ListDataProvider} from a given Collection. + * The ListDataProvider creates a protective List copy of all the contents in + * the Collection. + * + * @param + * the data item type + * @param items + * the collection of data, not null + * @return a new list data provider + */ + public static ListDataProvider create(Collection items) { + return new ListDataProvider<>(items); + } + + /** + * This method creates a new {@link ListDataProvider} from given objects.The + * ListDataProvider creates a protective List copy of all the contents in the + * array. + * + * @param + * the data item type + * @param items + * the data items + * @return a new list data provider + */ + @SafeVarargs + public static ListDataProvider create(T... items) { + return new ListDataProvider<>(Arrays.asList(items)); + } +} diff --git a/server/src/main/java/com/vaadin/server/data/DataProviderListener.java b/server/src/main/java/com/vaadin/server/data/DataProviderListener.java new file mode 100644 index 0000000000..54ec2d1877 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/DataProviderListener.java @@ -0,0 +1,42 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.io.Serializable; + +/** + * Interface for listening for a data change events fired by a + * {@link DataProvider}. + * + * @author Vaadin Ltd + * @since 8.0 + */ +public interface DataProviderListener extends Serializable { + + /** + * Invoked when this listener receives a data change event from a data + * source to which it has been added. + *

+ * This event is fired when something has changed in the underlying data. It + * doesn't allow to distinguish different kind of events + * (add/remove/update). It means that the method implementation normally + * just reloads the whole data to refresh. + * + * @param event + * the received event, not null + */ + void onDataChange(DataChangeEvent event); +} diff --git a/server/src/main/java/com/vaadin/server/data/DataSource.java b/server/src/main/java/com/vaadin/server/data/DataSource.java deleted file mode 100644 index c4042ecc11..0000000000 --- a/server/src/main/java/com/vaadin/server/data/DataSource.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.io.Serializable; -import java.util.Arrays; -import java.util.Collection; -import java.util.stream.Stream; - -import com.vaadin.shared.Registration; - -/** - * Minimal DataSource API for communication between the DataProvider and a back - * end service. - * - * @author Vaadin Ltd. - * - * @param - * data type - * - * @see ListDataSource - * @see BackEndDataSource - * - * @since 8.0 - */ -public interface DataSource extends Serializable { - - /** - * Gets whether the DataSource content all available in memory or does it - * use some external backend. - * - * @return {@code true} if all data is in memory; {@code false} if not - */ - boolean isInMemory(); - - /** - * Gets the amount of data in this DataSource. - * - * @param t - * query with sorting and filtering - * @return the size of the data source - */ - int size(Query t); - - /** - * Fetches data from this DataSource using given {@code query}. - * - * @param query - * given query to request data - * @return the result of the query request: a stream of data objects, not - * {@code null} - */ - Stream fetch(Query query); - - /** - * Refreshes all data based on currently available data in the underlying - * provider. - */ - void refreshAll(); - - /** - * Adds a data source listener. The listener is called when some piece of - * data is updated. - *

- * The {@link #refreshAll()} method fires {@link DataChangeEvent} each time - * when it's called. It allows to update UI components when user changes - * something in the underlying data. - * - * @see #refreshAll() - * @param listener - * the data change listener, not null - * @return a registration for the listener - */ - Registration addDataSourceListener(DataSourceListener listener); - - /** - * This method creates a new {@link ListDataSource} from a given Collection. - * The ListDataSource creates a protective List copy of all the contents in - * the Collection. - * - * @param - * the data item type - * @param items - * the collection of data, not null - * @return a new list data source - */ - public static ListDataSource create(Collection items) { - return new ListDataSource<>(items); - } - - /** - * This method creates a new {@link ListDataSource} from given objects.The - * ListDataSource creates a protective List copy of all the contents in the - * array. - * - * @param - * the data item type - * @param items - * the data items - * @return a new list data source - */ - @SafeVarargs - public static ListDataSource create(T... items) { - return new ListDataSource<>(Arrays.asList(items)); - } -} diff --git a/server/src/main/java/com/vaadin/server/data/DataSourceListener.java b/server/src/main/java/com/vaadin/server/data/DataSourceListener.java deleted file mode 100644 index c9c486cfd1..0000000000 --- a/server/src/main/java/com/vaadin/server/data/DataSourceListener.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.io.Serializable; - -/** - * Interface for listening for a data change events fired by a - * {@link DataSource}. - * - * @author Vaadin Ltd - * @since 8.0 - */ -public interface DataSourceListener extends Serializable { - - /** - * Invoked when this listener receives a data change event from a data - * source to which it has been added. - *

- * This event is fired when something has changed in the underlying data. It - * doesn't allow to distinguish different kind of events - * (add/remove/update). It means that the method implementation normally - * just reloads the whole data to refresh. - * - * @param event - * the received event, not null - */ - void onDataChange(DataChangeEvent event); -} diff --git a/server/src/main/java/com/vaadin/server/data/ListDataProvider.java b/server/src/main/java/com/vaadin/server/data/ListDataProvider.java new file mode 100644 index 0000000000..14527749a5 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/data/ListDataProvider.java @@ -0,0 +1,124 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.util.Collection; +import java.util.Comparator; +import java.util.Objects; +import java.util.function.Function; +import java.util.stream.Stream; + +/** + * {@link DataProvider} wrapper for {@link Collection}s. This class does not + * actually handle the {@link Query} parameters. + * + * @param + * data type + */ +public class ListDataProvider extends AbstractDataProvider { + + private Comparator sortOrder; + private final Collection backend; + + /** + * Constructs a new ListDataProvider. This method makes a protective copy of + * the contents of the Collection. + * + * @param items + * the initial data, not null + */ + public ListDataProvider(Collection items) { + Objects.requireNonNull(items, "items cannot be null"); + backend = items; + sortOrder = null; + } + + /** + * Chaining constructor for making modified {@link ListDataProvider}s. This + * Constructor is used internally for making sorted and filtered variants of + * a base data provider with actual data. + * + * @param items + * the backend data from the original list data provider + * @param sortOrder + * a {@link Comparator} providing the needed sorting order + * + */ + protected ListDataProvider(Collection items, Comparator sortOrder) { + this(items); + this.sortOrder = sortOrder; + } + + @Override + public Stream fetch(Query query) { + Stream stream = backend.stream(); + if (sortOrder != null) { + stream = stream.sorted(sortOrder); + } + return stream; + } + + /** + * Creates a new list data provider based on this list data provider with the + * given sort order. + *

+ * NOTE: this data provider is not modified in any way. + * + * @param sortOrder + * a {@link Comparator} providing the needed sorting order + * @return new data provider with modified sorting + */ + public ListDataProvider sortingBy(Comparator sortOrder) { + return new ListDataProvider<>(backend, sortOrder); + } + + /** + * Creates a new list data provider based on this list data provider with the + * given sort order. + *

+ * NOTE: this data provider is not modified in any way. + *

+ * This method is a short-hand for + * {@code sortingBy(Comparator.comparing(sortOrder))}. + * + * @param sortOrder + * function to sort by + * @param + * the type of the Comparable sort key + * @return new data provider with modified sorting + */ + public > ListDataProvider sortingBy( + Function sortOrder) { + return sortingBy(Comparator.comparing(sortOrder)); + } + + @Override + public boolean isInMemory() { + return true; + } + + /** + * {@inheritDoc} + *

+ * For in-memory data provider the query is not handled, and it will always + * return the full size. + */ + @Override + public int size(Query query) { + return backend.size(); + } + +} diff --git a/server/src/main/java/com/vaadin/server/data/ListDataSource.java b/server/src/main/java/com/vaadin/server/data/ListDataSource.java deleted file mode 100644 index 1e2724dc5b..0000000000 --- a/server/src/main/java/com/vaadin/server/data/ListDataSource.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.util.Collection; -import java.util.Comparator; -import java.util.Objects; -import java.util.function.Function; -import java.util.stream.Stream; - -/** - * {@link DataSource} wrapper for {@link Collection}s. This class does not - * actually handle the {@link Query} parameters. - * - * @param - * data type - */ -public class ListDataSource extends AbstractDataSource { - - private Comparator sortOrder; - private final Collection backend; - - /** - * Constructs a new ListDataSource. This method makes a protective copy of - * the contents of the Collection. - * - * @param items - * the initial data, not null - */ - public ListDataSource(Collection items) { - Objects.requireNonNull(items, "items cannot be null"); - backend = items; - sortOrder = null; - } - - /** - * Chaining constructor for making modified {@link ListDataSource}s. This - * Constructor is used internally for making sorted and filtered variants of - * a base data source with actual data. - * - * @param items - * the backend data from the original list data source - * @param sortOrder - * a {@link Comparator} providing the needed sorting order - * - */ - protected ListDataSource(Collection items, Comparator sortOrder) { - this(items); - this.sortOrder = sortOrder; - } - - @Override - public Stream fetch(Query query) { - Stream stream = backend.stream(); - if (sortOrder != null) { - stream = stream.sorted(sortOrder); - } - return stream; - } - - /** - * Creates a new list data source based on this list data source with the - * given sort order. - *

- * NOTE: this data source is not modified in any way. - * - * @param sortOrder - * a {@link Comparator} providing the needed sorting order - * @return new data source with modified sorting - */ - public ListDataSource sortingBy(Comparator sortOrder) { - return new ListDataSource<>(backend, sortOrder); - } - - /** - * Creates a new list data source based on this list data source with the - * given sort order. - *

- * NOTE: this data source is not modified in any way. - *

- * This method is a short-hand for - * {@code sortingBy(Comparator.comparing(sortOrder))}. - * - * @param sortOrder - * function to sort by - * @param - * the type of the Comparable sort key - * @return new data source with modified sorting - */ - public > ListDataSource sortingBy( - Function sortOrder) { - return sortingBy(Comparator.comparing(sortOrder)); - } - - @Override - public boolean isInMemory() { - return true; - } - - /** - * {@inheritDoc} - *

- * For in-memory data source the query is not handled, and it will always - * return the full size. - */ - @Override - public int size(Query query) { - return backend.size(); - } - -} diff --git a/server/src/main/java/com/vaadin/ui/AbstractListing.java b/server/src/main/java/com/vaadin/ui/AbstractListing.java index 40d3001ca6..99dd6e5eb1 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractListing.java +++ b/server/src/main/java/com/vaadin/ui/AbstractListing.java @@ -22,7 +22,7 @@ import com.vaadin.data.SelectionModel; import com.vaadin.server.AbstractExtension; import com.vaadin.server.data.DataCommunicator; import com.vaadin.server.data.DataGenerator; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; /** * A base class for listing components. Provides common handling for fetching @@ -133,13 +133,13 @@ public abstract class AbstractListing extends AbstractComponent } @Override - public void setDataSource(DataSource dataSource) { - getDataCommunicator().setDataSource(dataSource); + public void setDataProvider(DataProvider dataProvider) { + getDataCommunicator().setDataProvider(dataProvider); } @Override - public DataSource getDataSource() { - return getDataCommunicator().getDataSource(); + public DataProvider getDataProvider() { + return getDataCommunicator().getDataProvider(); } /** diff --git a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java index fc6ce4f0a4..25173d4646 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractMultiSelect.java @@ -138,7 +138,7 @@ public abstract class AbstractMultiSelect extends AbstractListing private SerializablePredicate itemEnabledProvider = item -> true; /** - * Creates a new multi select with an empty data source. + * Creates a new multi select with an empty data provider. */ protected AbstractMultiSelect() { registerRpc(new MultiSelectServerRpcImpl()); diff --git a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java index 2ffcedde15..e04e871cb4 100644 --- a/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java +++ b/server/src/main/java/com/vaadin/ui/AbstractSingleSelect.java @@ -246,7 +246,7 @@ public abstract class AbstractSingleSelect extends AbstractListing * the item to select or {@code null} to clear selection */ protected void setSelectedFromServer(T item) { - // TODO creates a key if item not in data source + // TODO creates a key if item not in data provider String key = itemToKey(item); if (isKeySelected(key) || isSelected(item)) { @@ -282,7 +282,7 @@ public abstract class AbstractSingleSelect extends AbstractListing if (item == null) { return null; } else { - // TODO creates a key if item not in data source + // TODO creates a key if item not in data provider return getDataCommunicator().getKeyMapper().key(item); } } diff --git a/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java b/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java index 9c25b3e75b..a3f99e7b27 100644 --- a/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java +++ b/server/src/main/java/com/vaadin/ui/CheckBoxGroup.java @@ -27,13 +27,13 @@ import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.event.FieldEvents.FocusNotifier; import com.vaadin.server.SerializablePredicate; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.optiongroup.CheckBoxGroupState; /** * A group of Checkboxes. Individual checkboxes are made from items supplied by - * a {@link DataSource}. Checkboxes may have captions and icons. + * a {@link DataProvider}. Checkboxes may have captions and icons. * * @param * item type @@ -48,7 +48,7 @@ public class CheckBoxGroup extends AbstractMultiSelect * * @param caption * caption text - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public CheckBoxGroup(String caption) { this(); @@ -56,37 +56,37 @@ public class CheckBoxGroup extends AbstractMultiSelect } /** - * Constructs a new CheckBoxGroup with caption and DataSource. + * Constructs a new CheckBoxGroup with caption and DataProvider. * * @param caption * the caption text - * @param dataSource - * the data source, not null - * @see Listing#setDataSource(DataSource) + * @param dataProvider + * the data provider, not null + * @see Listing#setDataProvider(DataProvider) */ - public CheckBoxGroup(String caption, DataSource dataSource) { + public CheckBoxGroup(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } /** - * Constructs a new CheckBoxGroup with caption and DataSource containing + * Constructs a new CheckBoxGroup with caption and DataProvider containing * given items. * * @param caption * the caption text * @param items * the data items to use, not null - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public CheckBoxGroup(String caption, Collection items) { - this(caption, DataSource.create(items)); + this(caption, DataProvider.create(items)); } /** * Constructs a new CheckBoxGroup. * - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public CheckBoxGroup() { registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent)); diff --git a/server/src/main/java/com/vaadin/ui/ComboBox.java b/server/src/main/java/com/vaadin/ui/ComboBox.java index d4df2236f7..60a229ba55 100644 --- a/server/src/main/java/com/vaadin/ui/ComboBox.java +++ b/server/src/main/java/com/vaadin/ui/ComboBox.java @@ -34,7 +34,7 @@ import com.vaadin.server.Resource; import com.vaadin.server.ResourceReference; import com.vaadin.server.data.DataCommunicator; import com.vaadin.server.data.DataKeyMapper; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.ui.combobox.ComboBoxConstants; @@ -124,7 +124,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, /** * Constructs an empty combo box without a caption. The content of the combo - * box can be set with {@link #setDataSource(DataSource)} or + * box can be set with {@link #setDataProvider(DataProvider)} or * {@link #setItems(Collection)} */ public ComboBox() { @@ -146,7 +146,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, /** * Constructs an empty combo box, whose content can be set with - * {@link #setDataSource(DataSource)} or {@link #setItems(Collection)}. + * {@link #setDataProvider(DataProvider)} or {@link #setItems(Collection)}. * * @param caption * the caption to show in the containing layout, null for no @@ -158,7 +158,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, } /** - * Constructs a combo box with a static in-memory data source with the given + * Constructs a combo box with a static in-memory data provider with the given * options. * * @param caption @@ -168,21 +168,21 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, * collection of options, not null */ public ComboBox(String caption, Collection options) { - this(caption, DataSource.create(options)); + this(caption, DataProvider.create(options)); } /** - * Constructs a combo box with the given data source. + * Constructs a combo box with the given data provider. * * @param caption * the caption to show in the containing layout, null for no * caption - * @param dataSource - * the data source to use, not null + * @param dataProvider + * the data provider to use, not null */ - public ComboBox(String caption, DataSource dataSource) { + public ComboBox(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } /** @@ -489,7 +489,7 @@ public class ComboBox extends AbstractSingleSelect implements HasValue, /** * Returns the handler called when the user enters a new item (not present - * in the data source). + * in the data provider). * * @return new item handler or null if none specified */ diff --git a/server/src/main/java/com/vaadin/ui/ListSelect.java b/server/src/main/java/com/vaadin/ui/ListSelect.java index 7bbcbb2e8b..e616fa2af3 100644 --- a/server/src/main/java/com/vaadin/ui/ListSelect.java +++ b/server/src/main/java/com/vaadin/ui/ListSelect.java @@ -17,7 +17,7 @@ package com.vaadin.ui; import java.util.Collection; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.ui.listselect.ListSelectState; /** @@ -54,16 +54,16 @@ public class ListSelect extends AbstractMultiSelect { } /** - * Constructs a new ListSelect with caption and data source for options. + * Constructs a new ListSelect with caption and data provider for options. * * @param caption * the caption to set, can be {@code null} - * @param dataSource - * the data source, not {@code null} + * @param dataProvider + * the data provider, not {@code null} */ - public ListSelect(String caption, DataSource dataSource) { + public ListSelect(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } /** @@ -75,7 +75,7 @@ public class ListSelect extends AbstractMultiSelect { * the options, cannot be {@code null} */ public ListSelect(String caption, Collection options) { - this(caption, DataSource.create(options)); + this(caption, DataProvider.create(options)); } /** diff --git a/server/src/main/java/com/vaadin/ui/NativeSelect.java b/server/src/main/java/com/vaadin/ui/NativeSelect.java index a1ec0dca40..2a696c75d4 100644 --- a/server/src/main/java/com/vaadin/ui/NativeSelect.java +++ b/server/src/main/java/com/vaadin/ui/NativeSelect.java @@ -25,7 +25,7 @@ import com.vaadin.event.FieldEvents.FocusAndBlurServerRpcDecorator; import com.vaadin.event.FieldEvents.FocusEvent; import com.vaadin.event.FieldEvents.FocusListener; import com.vaadin.event.FieldEvents.FocusNotifier; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.shared.ui.nativeselect.NativeSelectState; @@ -81,16 +81,16 @@ public class NativeSelect extends AbstractSingleSelect /** * Creates a new {@code NativeSelect} with the given caption, using the - * given {@code DataSource} as the source of data items. + * given {@code DataProvider} as the source of data items. * * @param caption * the component caption to set, null for no caption - * @param dataSource + * @param dataProvider * the source of data items to use, not null */ - public NativeSelect(String caption, DataSource dataSource) { + public NativeSelect(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } @Override diff --git a/server/src/main/java/com/vaadin/ui/RadioButtonGroup.java b/server/src/main/java/com/vaadin/ui/RadioButtonGroup.java index 4c5b814f22..8949493d6f 100644 --- a/server/src/main/java/com/vaadin/ui/RadioButtonGroup.java +++ b/server/src/main/java/com/vaadin/ui/RadioButtonGroup.java @@ -31,7 +31,7 @@ import com.vaadin.server.Resource; import com.vaadin.server.ResourceReference; import com.vaadin.server.SerializablePredicate; import com.vaadin.server.data.DataGenerator; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.Registration; import com.vaadin.shared.ui.ListingJsonConstants; import com.vaadin.shared.ui.optiongroup.RadioButtonGroupState; @@ -40,7 +40,7 @@ import elemental.json.JsonObject; /** * A group of RadioButtons. Individual radiobuttons are made from items supplied - * by a {@link DataSource}. RadioButtons may have captions and icons. + * by a {@link DataProvider}. RadioButtons may have captions and icons. * * @param * item type @@ -61,7 +61,7 @@ public class RadioButtonGroup extends AbstractSingleSelect * * @param caption * caption text - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public RadioButtonGroup(String caption) { this(); @@ -69,37 +69,37 @@ public class RadioButtonGroup extends AbstractSingleSelect } /** - * Constructs a new RadioButtonGroup with caption and DataSource. + * Constructs a new RadioButtonGroup with caption and DataProvider. * * @param caption * the caption text - * @param dataSource - * the data source, not null - * @see Listing#setDataSource(DataSource) + * @param dataProvider + * the data provider, not null + * @see Listing#setDataProvider(DataProvider) */ - public RadioButtonGroup(String caption, DataSource dataSource) { + public RadioButtonGroup(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } /** - * Constructs a new RadioButtonGroup with caption and DataSource containing + * Constructs a new RadioButtonGroup with caption and DataProvider containing * given items. * * @param caption * the caption text * @param items * the data items to use, not null - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public RadioButtonGroup(String caption, Collection items) { - this(caption, DataSource.create(items)); + this(caption, DataProvider.create(items)); } /** * Constructs a new RadioButtonGroup. * - * @see Listing#setDataSource(DataSource) + * @see Listing#setDataProvider(DataProvider) */ public RadioButtonGroup() { registerRpc(new FocusAndBlurServerRpcDecorator(this, this::fireEvent)); diff --git a/server/src/main/java/com/vaadin/ui/TwinColSelect.java b/server/src/main/java/com/vaadin/ui/TwinColSelect.java index 4b321559a3..0858a76830 100644 --- a/server/src/main/java/com/vaadin/ui/TwinColSelect.java +++ b/server/src/main/java/com/vaadin/ui/TwinColSelect.java @@ -18,7 +18,7 @@ package com.vaadin.ui; import java.util.Collection; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.ui.twincolselect.TwinColSelectState; /** @@ -50,16 +50,16 @@ public class TwinColSelect extends AbstractMultiSelect { } /** - * Constructs a new TwinColSelect with caption and data source for options. + * Constructs a new TwinColSelect with caption and data provider for options. * * @param caption * the caption to set, can be {@code null} - * @param dataSource - * the data source, not {@code null} + * @param dataProvider + * the data provider, not {@code null} */ - public TwinColSelect(String caption, DataSource dataSource) { + public TwinColSelect(String caption, DataProvider dataProvider) { this(caption); - setDataSource(dataSource); + setDataProvider(dataProvider); } /** @@ -71,7 +71,7 @@ public class TwinColSelect extends AbstractMultiSelect { * the options, cannot be {@code null} */ public TwinColSelect(String caption, Collection options) { - this(caption, DataSource.create(options)); + this(caption, DataProvider.create(options)); } /** diff --git a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModel.java b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModel.java index bf2f55648e..6829cfc0e0 100644 --- a/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModel.java +++ b/server/src/main/java/com/vaadin/ui/components/grid/SingleSelectionModel.java @@ -187,7 +187,7 @@ public class SingleSelectionModel extends AbstractGridExtension * the item to select or {@code null} to clear selection */ protected void setSelectedFromServer(T item) { - // TODO creates a key if item not in data source + // TODO creates a key if item not in data provider String key = itemToKey(item); if (isSelected(item) || isKeySelected(key)) { @@ -210,7 +210,7 @@ public class SingleSelectionModel extends AbstractGridExtension if (item == null) { return null; } else { - // TODO creates a key if item not in data source + // TODO creates a key if item not in data provider return grid.getDataCommunicator().getKeyMapper().key(item); } } @@ -296,4 +296,4 @@ public class SingleSelectionModel extends AbstractGridExtension } }; } -} \ No newline at end of file +} diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java b/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java index 761c7edd04..b72edc4c1a 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignContext.java @@ -753,7 +753,7 @@ public class DesignContext implements Serializable { /** * Determines whether the container data of a component should be written * out by delegating to a {@link ShouldWriteDataDelegate}. The default - * delegate assumes that all component data is provided by a data source + * delegate assumes that all component data is provided by a data provider * connected to a back end system and that the data should thus not be * written. * diff --git a/server/src/main/java/com/vaadin/ui/declarative/ShouldWriteDataDelegate.java b/server/src/main/java/com/vaadin/ui/declarative/ShouldWriteDataDelegate.java index 5ad97fb4ef..ed686f0f2b 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/ShouldWriteDataDelegate.java +++ b/server/src/main/java/com/vaadin/ui/declarative/ShouldWriteDataDelegate.java @@ -32,7 +32,7 @@ public interface ShouldWriteDataDelegate extends Serializable { /** * The default delegate implementation that assumes that all component data - * is provided by a data source connected to a back end system and that the + * is provided by a data provider connected to a back end system and that the * data should thus not be written. */ public static final ShouldWriteDataDelegate DEFAULT = ( diff --git a/server/src/test/java/com/vaadin/server/data/AbstractDataProviderTest.java b/server/src/test/java/com/vaadin/server/data/AbstractDataProviderTest.java new file mode 100644 index 0000000000..b1260b8097 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/AbstractDataProviderTest.java @@ -0,0 +1,72 @@ +/* + * Copyright 2000-2016 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.server.data; + +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Stream; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.shared.Registration; + +/** + * @author Vaadin Ltd + * + */ +public class AbstractDataProviderTest { + + private static class TestDataProvider extends AbstractDataProvider { + @Override + public Stream fetch(Query t) { + return null; + } + + @Override + public int size(Query t) { + return 0; + } + + @Override + public boolean isInMemory() { + return false; + } + } + + @Test + public void refreshAll_notifyListeners() { + AbstractDataProvider dataProvider = new TestDataProvider(); + AtomicReference event = new AtomicReference<>(); + dataProvider.addDataProviderListener(ev -> { + Assert.assertNull(event.get()); + event.set(ev); + }); + dataProvider.refreshAll(); + Assert.assertNotNull(event.get()); + Assert.assertEquals(dataProvider, event.get().getSource()); + } + + @Test + public void removeListener_listenerIsNotNotified() { + AbstractDataProvider dataProvider = new TestDataProvider(); + AtomicReference event = new AtomicReference<>(); + Registration registration = dataProvider + .addDataProviderListener(ev -> event.set(ev)); + registration.remove(); + dataProvider.refreshAll(); + Assert.assertNull(event.get()); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/AbstractDataSourceTest.java b/server/src/test/java/com/vaadin/server/data/AbstractDataSourceTest.java deleted file mode 100644 index 187f8e0d77..0000000000 --- a/server/src/test/java/com/vaadin/server/data/AbstractDataSourceTest.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data; - -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Stream; - -import org.junit.Assert; -import org.junit.Test; - -import com.vaadin.shared.Registration; - -/** - * @author Vaadin Ltd - * - */ -public class AbstractDataSourceTest { - - private static class TestDataSource extends AbstractDataSource { - @Override - public Stream fetch(Query t) { - return null; - } - - @Override - public int size(Query t) { - return 0; - } - - @Override - public boolean isInMemory() { - return false; - } - } - - @Test - public void refreshAll_notifyListeners() { - AbstractDataSource dataSource = new TestDataSource(); - AtomicReference event = new AtomicReference<>(); - dataSource.addDataSourceListener(ev -> { - Assert.assertNull(event.get()); - event.set(ev); - }); - dataSource.refreshAll(); - Assert.assertNotNull(event.get()); - Assert.assertEquals(dataSource, event.get().getSource()); - } - - @Test - public void removeListener_listenerIsNotNotified() { - AbstractDataSource dataSource = new TestDataSource(); - AtomicReference event = new AtomicReference<>(); - Registration registration = dataSource - .addDataSourceListener(ev -> event.set(ev)); - registration.remove(); - dataSource.refreshAll(); - Assert.assertNull(event.get()); - } -} diff --git a/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java b/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java index c9a30fa517..4c40c1a654 100644 --- a/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java +++ b/server/src/test/java/com/vaadin/server/data/DataCommunicatorTest.java @@ -52,18 +52,18 @@ public class DataCommunicatorTest { } } - private static class TestDataSource extends ListDataSource + private static class TestDataProvider extends ListDataProvider implements Registration { private Registration registration; - public TestDataSource() { + public TestDataProvider() { super(Collections.singleton(new Object())); } @Override - public Registration addDataSourceListener(DataSourceListener listener) { - registration = super.addDataSourceListener(listener); + public Registration addDataProviderListener(DataProviderListener listener) { + registration = super.addDataProviderListener(listener); return this; } @@ -89,41 +89,41 @@ public class DataCommunicatorTest { Mockito.mock(VaadinService.class)); @Test - public void attach_dataSourceListenerIsNotAddedBeforeAttachAndAddedAfter() { + public void attach_dataProviderListenerIsNotAddedBeforeAttachAndAddedAfter() { session.lock(); UI ui = new TestUI(session); TestDataCommunicator communicator = new TestDataCommunicator(); - TestDataSource dataSource = new TestDataSource(); - communicator.setDataSource(dataSource); + TestDataProvider dataProvider = new TestDataProvider(); + communicator.setDataProvider(dataProvider); - Assert.assertFalse(dataSource.isListenerAdded()); + Assert.assertFalse(dataProvider.isListenerAdded()); communicator.extend(ui); - Assert.assertTrue(dataSource.isListenerAdded()); + Assert.assertTrue(dataProvider.isListenerAdded()); } @Test - public void detach_dataSourceListenerIsRemovedAfterDetach() { + public void detach_dataProviderListenerIsRemovedAfterDetach() { session.lock(); UI ui = new TestUI(session); TestDataCommunicator communicator = new TestDataCommunicator(); - TestDataSource dataSource = new TestDataSource(); - communicator.setDataSource(dataSource); + TestDataProvider dataProvider = new TestDataProvider(); + communicator.setDataProvider(dataProvider); communicator.extend(ui); - Assert.assertTrue(dataSource.isListenerAdded()); + Assert.assertTrue(dataProvider.isListenerAdded()); communicator.detach(); - Assert.assertFalse(dataSource.isListenerAdded()); + Assert.assertFalse(dataProvider.isListenerAdded()); } } diff --git a/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java b/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java deleted file mode 100644 index a428858a42..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/ListDataSourceTest.java +++ /dev/null @@ -1,195 +0,0 @@ -package com.vaadin.server.data.datasource; - -import static org.junit.Assert.assertTrue; - -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; -import java.util.stream.Collectors; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.server.data.DataSource; -import com.vaadin.server.data.ListDataSource; -import com.vaadin.server.data.Query; - -public class ListDataSourceTest { - - private ListDataSource dataSource; - private List data; - - @Before - public void setUp() { - data = StrBean.generateRandomBeans(100); - dataSource = DataSource.create(data); - } - - @Test - public void testListContainsAllData() { - List list = new LinkedList<>(data); - dataSource.fetch(new Query()) - .forEach(str -> assertTrue( - "Data source contained values not in original data", - list.remove(str))); - assertTrue("Not all values from original data were in data source", - list.isEmpty()); - } - - @Test - public void testSortByComparatorListsDiffer() { - Comparator comp = Comparator.comparing(StrBean::getValue) - .thenComparing(StrBean::getRandomNumber) - .thenComparing(StrBean::getId); - List list = dataSource.sortingBy(comp).fetch(new Query()) - .collect(Collectors.toList()); - - // First value in data is { Xyz, 10, 100 } which should be last in list - Assert.assertNotEquals("First value should not match", data.get(0), - list.get(0)); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - data.sort(comp); - for (int i = 0; i < data.size(); ++i) { - Assert.assertEquals("Sorting result differed", data.get(i), - list.get(i)); - } - } - - @Test - public void testDefatulSortWithSpecifiedPostSort() { - Comparator comp = Comparator.comparing(StrBean::getValue) - .thenComparing(Comparator.comparing(StrBean::getId).reversed()); - List list = dataSource.sortingBy(comp).fetch(new Query()) - // The sort here should come e.g from a Component - .sorted(Comparator.comparing(StrBean::getRandomNumber)) - .collect(Collectors.toList()); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - for (int i = 1; i < list.size(); ++i) { - StrBean prev = list.get(i - 1); - StrBean cur = list.get(i); - // Test specific sort - Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber()); - - if (prev.getRandomNumber() == cur.getRandomNumber()) { - // Test default sort - Assert.assertTrue( - prev.getValue().compareTo(cur.getValue()) <= 0); - if (prev.getValue().equals(cur.getValue())) { - Assert.assertTrue(prev.getId() > cur.getId()); - } - } - } - } - - @Test - public void testDefatulSortWithFunction() { - List list = dataSource.sortingBy(StrBean::getValue) - .fetch(new Query()).collect(Collectors.toList()); - - Assert.assertEquals("Sorted data and original data sizes don't match", - data.size(), list.size()); - - for (int i = 1; i < list.size(); ++i) { - StrBean prev = list.get(i - 1); - StrBean cur = list.get(i); - - // Test default sort - Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); - } - } - - @Test - public void refreshAll_changeBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - Query query = new Query(); - int size = dataSource.size(query); - - data.set(0, bean); - dataSource.refreshAll(); - - List list = dataSource.fetch(query) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size, dataSource.size(query)); - } - - @Test - public void refreshAll_updateBean() { - Query query = new Query(); - int size = dataSource.size(query); - - StrBean bean = data.get(0); - bean.setValue("foo"); - dataSource.refreshAll(); - - List list = dataSource.fetch(query) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals("foo", first.getValue()); - - Assert.assertEquals(size, dataSource.size(query)); - } - - @Test - public void refreshAll_sortingBy_changeBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - Query query = new Query(); - int size = dataSource.size(query); - - data.set(0, bean); - - ListDataSource dSource = dataSource - .sortingBy(Comparator.comparing(StrBean::getId)); - dSource.refreshAll(); - - List list = dSource.fetch(query).collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size, dataSource.size(query)); - } - - @Test - public void refreshAll_addBeanInstance() { - StrBean bean = new StrBean("foo", -1, hashCode()); - - Query query = new Query(); - int size = dataSource.size(query); - - data.add(0, bean); - dataSource.refreshAll(); - - List list = dataSource.fetch(query) - .collect(Collectors.toList()); - StrBean first = list.get(0); - Assert.assertEquals(bean.getValue(), first.getValue()); - Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); - Assert.assertEquals(bean.getId(), first.getId()); - - Assert.assertEquals(size + 1, dataSource.size(query)); - } - - @Test - public void refreshAll_removeBeanInstance() { - Query query = new Query(); - int size = dataSource.size(query); - - data.remove(0); - dataSource.refreshAll(); - - Assert.assertEquals(size - 1, dataSource.size(query)); - } -} diff --git a/server/src/test/java/com/vaadin/server/data/datasource/StrBean.java b/server/src/test/java/com/vaadin/server/data/datasource/StrBean.java deleted file mode 100644 index 44650d63bb..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/StrBean.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.vaadin.server.data.datasource; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; - -class StrBean implements Serializable { - - private static final String[] values = new String[] { "Foo", "Bar", "Baz" }; - - private String value; - private final int id; - private final int randomNumber; - - public StrBean(String value, int id, int randomNumber) { - this.value = value; - this.id = id; - this.randomNumber = randomNumber; - } - - public String getValue() { - return value; - } - - public int getId() { - return id; - } - - public int getRandomNumber() { - return randomNumber; - } - - public void setValue(String value) { - this.value = value; - } - - public static List generateRandomBeans(int max) { - List data = new ArrayList<>(); - Random r = new Random(13337); - data.add(new StrBean("Xyz", 10, max)); - for (int i = 0; i < max - 1; ++i) { - data.add(new StrBean(values[r.nextInt(values.length)], i, - r.nextInt(10))); - } - return data; - } - - @Override - public String toString() { - return "{ " + value + ", " + randomNumber + ", " + id + " }"; - } -} \ No newline at end of file diff --git a/server/src/test/java/com/vaadin/server/data/datasource/bov/DataSourceBoVTest.java b/server/src/test/java/com/vaadin/server/data/datasource/bov/DataSourceBoVTest.java deleted file mode 100644 index 58f0c32401..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/bov/DataSourceBoVTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data.datasource.bov; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.junit.Before; -import org.junit.Test; - -import com.vaadin.server.data.BackEndDataSource; -import com.vaadin.server.data.DataSource; -import com.vaadin.server.data.SortOrder; -import com.vaadin.shared.data.sort.SortDirection; - -/** - * Vaadin 8 Example from Book of Vaadin - * - * @author Vaadin Ltd - */ -public class DataSourceBoVTest { - - private PersonServiceImpl personService; - - public static class PersonServiceImpl implements PersonService { - final Person[] persons; - - public PersonServiceImpl(Person... persons) { - this.persons = persons; - } - - @Override - public List fetchPersons(int offset, int limit) { - return Arrays.stream(persons).skip(offset).limit(limit) - .collect(Collectors.toList()); - } - - @Override - public List fetchPersons(int offset, int limit, - Collection personSorts) { - Stream personStream = Arrays.stream(persons).skip(offset) - .limit(limit); - if (personSorts != null) { - for (PersonSort personSort : personSorts) { - personStream = personStream.sorted(personSort); - } - } - return personStream.collect(Collectors.toList()); - } - - @Override - public int getPersonCount() { - return persons.length; - } - - @Override - public PersonSort createSort(String propertyName, boolean descending) { - PersonSort result; - switch (propertyName) { - case "name": - result = (person1, person2) -> String.CASE_INSENSITIVE_ORDER - .compare(person1.getName(), person2.getName()); - break; - case "born": - result = (person1, person2) -> person2.getBorn() - - person1.getBorn(); - break; - default: - throw new IllegalArgumentException( - "wrong field name " + propertyName); - } - if (descending) { - return (person1, person2) -> result.compare(person2, person1); - } else { - return result; - } - } - } - - @Test - public void testPersons() { - DataSource dataSource = createUnsortedDatasource(); - // TODO test if the datasource contains all defined Persons in - // correct(unchanged) order - } - - private DataSource createUnsortedDatasource() { - DataSource dataSource = new BackEndDataSource<>( - // First callback fetches items based on a query - query -> { - // The index of the first item to load - int offset = query.getOffset(); - - // The number of items to load - int limit = query.getLimit(); - - List persons = getPersonService() - .fetchPersons(offset, limit); - - return persons.stream(); - }, - // Second callback fetches the number of items for a query - query -> getPersonService().getPersonCount()); - return dataSource; - } - - @Test - public void testSortedPersons() { - - DataSource dataSource = createSortedDataSource(); - // TODO test if datasource contains all defined Persons in correct order - // TODO test Query.sortOrders correctness - } - - private DataSource createSortedDataSource() { - DataSource dataSource = new BackEndDataSource<>( - // First callback fetches items based on a query - query -> { - List sortOrders = new ArrayList<>(); - for (SortOrder queryOrder : query.getSortOrders()) { - PersonService.PersonSort sort = personService - .createSort( - // The name of the sorted property - queryOrder.getSorted(), - // The sort direction for this property - queryOrder - .getDirection() == SortDirection.DESCENDING); - sortOrders.add(sort); - } - return getPersonService().fetchPersons(query.getOffset(), - query.getLimit(), sortOrders).stream(); - }, - // Second callback fetches the number of items for a query - query -> getPersonService().getPersonCount()); - return dataSource; - } - - public PersonServiceImpl getPersonService() { - return personService; - } - - @Before - public void buildService() { - personService = new PersonServiceImpl( - new Person("George Washington", 1732), - new Person("John Adams", 1735), - new Person("Thomas Jefferson", 1743), - new Person("James Madison", 1751)); - } -} diff --git a/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java b/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java deleted file mode 100644 index f551e5be98..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/bov/Person.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data.datasource.bov; - -import java.io.Serializable; - -/** - * POJO - * - * @author Vaadin Ltd - */ -public class Person implements Serializable { - private final String name; - private final int born; - - public Person(String name, int born) { - this.name = name; - this.born = born; - } - - public String getName() { - return name; - } - - public int getBorn() { - return born; - } - - @Override - public String toString() { - return name + "(" + born + ")"; - } -} diff --git a/server/src/test/java/com/vaadin/server/data/datasource/bov/PersonService.java b/server/src/test/java/com/vaadin/server/data/datasource/bov/PersonService.java deleted file mode 100644 index 18e6132313..0000000000 --- a/server/src/test/java/com/vaadin/server/data/datasource/bov/PersonService.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2000-2016 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.server.data.datasource.bov; - -import java.io.Serializable; -import java.util.Collection; -import java.util.Comparator; -import java.util.List; - -/** - * Data access service example. - * - * @author Vaadin Ltd - * @see Person - */ -public interface PersonService extends Serializable { - List fetchPersons(int offset, int limit); - - List fetchPersons(int offset, int limit, - Collection personSorts); - - int getPersonCount(); - - public interface PersonSort extends Comparator, Serializable { - } - - PersonSort createSort(String propertyName, boolean descending); -} diff --git a/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java new file mode 100644 index 0000000000..37b4214a45 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/ListDataProviderTest.java @@ -0,0 +1,195 @@ +package com.vaadin.server.data.provider; + +import static org.junit.Assert.assertTrue; + +import java.util.Comparator; +import java.util.LinkedList; +import java.util.List; +import java.util.stream.Collectors; + +import com.vaadin.server.data.DataProvider; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.data.ListDataProvider; +import com.vaadin.server.data.Query; + +public class ListDataProviderTest { + + private ListDataProvider dataProvider; + private List data; + + @Before + public void setUp() { + data = StrBean.generateRandomBeans(100); + dataProvider = DataProvider.create(data); + } + + @Test + public void testListContainsAllData() { + List list = new LinkedList<>(data); + dataProvider.fetch(new Query()) + .forEach(str -> assertTrue( + "Data provider contained values not in original data", + list.remove(str))); + assertTrue("Not all values from original data were in data provider", + list.isEmpty()); + } + + @Test + public void testSortByComparatorListsDiffer() { + Comparator comp = Comparator.comparing(StrBean::getValue) + .thenComparing(StrBean::getRandomNumber) + .thenComparing(StrBean::getId); + List list = dataProvider.sortingBy(comp).fetch(new Query()) + .collect(Collectors.toList()); + + // First value in data is { Xyz, 10, 100 } which should be last in list + Assert.assertNotEquals("First value should not match", data.get(0), + list.get(0)); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + data.sort(comp); + for (int i = 0; i < data.size(); ++i) { + Assert.assertEquals("Sorting result differed", data.get(i), + list.get(i)); + } + } + + @Test + public void testDefatulSortWithSpecifiedPostSort() { + Comparator comp = Comparator.comparing(StrBean::getValue) + .thenComparing(Comparator.comparing(StrBean::getId).reversed()); + List list = dataProvider.sortingBy(comp).fetch(new Query()) + // The sort here should come e.g from a Component + .sorted(Comparator.comparing(StrBean::getRandomNumber)) + .collect(Collectors.toList()); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + for (int i = 1; i < list.size(); ++i) { + StrBean prev = list.get(i - 1); + StrBean cur = list.get(i); + // Test specific sort + Assert.assertTrue(prev.getRandomNumber() <= cur.getRandomNumber()); + + if (prev.getRandomNumber() == cur.getRandomNumber()) { + // Test default sort + Assert.assertTrue( + prev.getValue().compareTo(cur.getValue()) <= 0); + if (prev.getValue().equals(cur.getValue())) { + Assert.assertTrue(prev.getId() > cur.getId()); + } + } + } + } + + @Test + public void testDefatulSortWithFunction() { + List list = dataProvider.sortingBy(StrBean::getValue) + .fetch(new Query()).collect(Collectors.toList()); + + Assert.assertEquals("Sorted data and original data sizes don't match", + data.size(), list.size()); + + for (int i = 1; i < list.size(); ++i) { + StrBean prev = list.get(i - 1); + StrBean cur = list.get(i); + + // Test default sort + Assert.assertTrue(prev.getValue().compareTo(cur.getValue()) <= 0); + } + } + + @Test + public void refreshAll_changeBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + Query query = new Query(); + int size = dataProvider.size(query); + + data.set(0, bean); + dataProvider.refreshAll(); + + List list = dataProvider.fetch(query) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size, dataProvider.size(query)); + } + + @Test + public void refreshAll_updateBean() { + Query query = new Query(); + int size = dataProvider.size(query); + + StrBean bean = data.get(0); + bean.setValue("foo"); + dataProvider.refreshAll(); + + List list = dataProvider.fetch(query) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals("foo", first.getValue()); + + Assert.assertEquals(size, dataProvider.size(query)); + } + + @Test + public void refreshAll_sortingBy_changeBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + Query query = new Query(); + int size = dataProvider.size(query); + + data.set(0, bean); + + ListDataProvider dSource = dataProvider + .sortingBy(Comparator.comparing(StrBean::getId)); + dSource.refreshAll(); + + List list = dSource.fetch(query).collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size, dataProvider.size(query)); + } + + @Test + public void refreshAll_addBeanInstance() { + StrBean bean = new StrBean("foo", -1, hashCode()); + + Query query = new Query(); + int size = dataProvider.size(query); + + data.add(0, bean); + dataProvider.refreshAll(); + + List list = dataProvider.fetch(query) + .collect(Collectors.toList()); + StrBean first = list.get(0); + Assert.assertEquals(bean.getValue(), first.getValue()); + Assert.assertEquals(bean.getRandomNumber(), first.getRandomNumber()); + Assert.assertEquals(bean.getId(), first.getId()); + + Assert.assertEquals(size + 1, dataProvider.size(query)); + } + + @Test + public void refreshAll_removeBeanInstance() { + Query query = new Query(); + int size = dataProvider.size(query); + + data.remove(0); + dataProvider.refreshAll(); + + Assert.assertEquals(size - 1, dataProvider.size(query)); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/StrBean.java b/server/src/test/java/com/vaadin/server/data/provider/StrBean.java new file mode 100644 index 0000000000..b0a346d9c5 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/StrBean.java @@ -0,0 +1,53 @@ +package com.vaadin.server.data.provider; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + +class StrBean implements Serializable { + + private static final String[] values = new String[] { "Foo", "Bar", "Baz" }; + + private String value; + private final int id; + private final int randomNumber; + + public StrBean(String value, int id, int randomNumber) { + this.value = value; + this.id = id; + this.randomNumber = randomNumber; + } + + public String getValue() { + return value; + } + + public int getId() { + return id; + } + + public int getRandomNumber() { + return randomNumber; + } + + public void setValue(String value) { + this.value = value; + } + + public static List generateRandomBeans(int max) { + List data = new ArrayList<>(); + Random r = new Random(13337); + data.add(new StrBean("Xyz", 10, max)); + for (int i = 0; i < max - 1; ++i) { + data.add(new StrBean(values[r.nextInt(values.length)], i, + r.nextInt(10))); + } + return data; + } + + @Override + public String toString() { + return "{ " + value + ", " + randomNumber + ", " + id + " }"; + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/bov/DataProviderBoVTest.java b/server/src/test/java/com/vaadin/server/data/provider/bov/DataProviderBoVTest.java new file mode 100644 index 0000000000..2ad7fc4178 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/bov/DataProviderBoVTest.java @@ -0,0 +1,167 @@ +/* + * Copyright 2000-2016 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.server.data.provider.bov; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import com.vaadin.server.data.BackEndDataProvider; +import com.vaadin.server.data.DataProvider; +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.server.data.SortOrder; +import com.vaadin.shared.data.sort.SortDirection; + +/** + * Vaadin 8 Example from Book of Vaadin + * + * @author Vaadin Ltd + */ +public class DataProviderBoVTest { + + private PersonServiceImpl personService; + + public static class PersonServiceImpl implements PersonService { + final Person[] persons; + + public PersonServiceImpl(Person... persons) { + this.persons = persons; + } + + @Override + public List fetchPersons(int offset, int limit) { + return Arrays.stream(persons).skip(offset).limit(limit) + .collect(Collectors.toList()); + } + + @Override + public List fetchPersons(int offset, int limit, + Collection personSorts) { + Stream personStream = Arrays.stream(persons).skip(offset) + .limit(limit); + if (personSorts != null) { + for (PersonSort personSort : personSorts) { + personStream = personStream.sorted(personSort); + } + } + return personStream.collect(Collectors.toList()); + } + + @Override + public int getPersonCount() { + return persons.length; + } + + @Override + public PersonSort createSort(String propertyName, boolean descending) { + PersonSort result; + switch (propertyName) { + case "name": + result = (person1, person2) -> String.CASE_INSENSITIVE_ORDER + .compare(person1.getName(), person2.getName()); + break; + case "born": + result = (person1, person2) -> person2.getBorn() + - person1.getBorn(); + break; + default: + throw new IllegalArgumentException( + "wrong field name " + propertyName); + } + if (descending) { + return (person1, person2) -> result.compare(person2, person1); + } else { + return result; + } + } + } + + @Test + public void testPersons() { + DataProvider dataProvider = createUnsortedDataProvider(); + // TODO test if the provider contains all defined Persons in + // correct(unchanged) order + } + + private DataProvider createUnsortedDataProvider() { + DataProvider dataProvider = new BackEndDataProvider<>( + // First callback fetches items based on a query + query -> { + // The index of the first item to load + int offset = query.getOffset(); + + // The number of items to load + int limit = query.getLimit(); + + List persons = getPersonService() + .fetchPersons(offset, limit); + + return persons.stream(); + }, + // Second callback fetches the number of items for a query + query -> getPersonService().getPersonCount()); + return dataProvider; + } + + @Test + public void testSortedPersons() { + + DataProvider dataProvider = createSortedDataProvider(); + // TODO test if provider contains all defined Persons in correct order + // TODO test Query.sortOrders correctness + } + + private DataProvider createSortedDataProvider() { + DataProvider dataProvider = new BackEndDataProvider<>( + // First callback fetches items based on a query + query -> { + List sortOrders = new ArrayList<>(); + for (SortOrder queryOrder : query.getSortOrders()) { + PersonService.PersonSort sort = personService + .createSort( + // The name of the sorted property + queryOrder.getSorted(), + // The sort direction for this property + queryOrder + .getDirection() == SortDirection.DESCENDING); + sortOrders.add(sort); + } + return getPersonService().fetchPersons(query.getOffset(), + query.getLimit(), sortOrders).stream(); + }, + // Second callback fetches the number of items for a query + query -> getPersonService().getPersonCount()); + return dataProvider; + } + + public PersonServiceImpl getPersonService() { + return personService; + } + + @Before + public void buildService() { + personService = new PersonServiceImpl( + new Person("George Washington", 1732), + new Person("John Adams", 1735), + new Person("Thomas Jefferson", 1743), + new Person("James Madison", 1751)); + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/bov/Person.java b/server/src/test/java/com/vaadin/server/data/provider/bov/Person.java new file mode 100644 index 0000000000..8a60510949 --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/bov/Person.java @@ -0,0 +1,46 @@ +/* + * Copyright 2000-2016 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.server.data.provider.bov; + +import java.io.Serializable; + +/** + * POJO + * + * @author Vaadin Ltd + */ +public class Person implements Serializable { + private final String name; + private final int born; + + public Person(String name, int born) { + this.name = name; + this.born = born; + } + + public String getName() { + return name; + } + + public int getBorn() { + return born; + } + + @Override + public String toString() { + return name + "(" + born + ")"; + } +} diff --git a/server/src/test/java/com/vaadin/server/data/provider/bov/PersonService.java b/server/src/test/java/com/vaadin/server/data/provider/bov/PersonService.java new file mode 100644 index 0000000000..70c355720e --- /dev/null +++ b/server/src/test/java/com/vaadin/server/data/provider/bov/PersonService.java @@ -0,0 +1,41 @@ +/* + * Copyright 2000-2016 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.server.data.provider.bov; + +import java.io.Serializable; +import java.util.Collection; +import java.util.Comparator; +import java.util.List; + +/** + * Data access service example. + * + * @author Vaadin Ltd + * @see Person + */ +public interface PersonService extends Serializable { + List fetchPersons(int offset, int limit); + + List fetchPersons(int offset, int limit, + Collection personSorts); + + int getPersonCount(); + + public interface PersonSort extends Comparator, Serializable { + } + + PersonSort createSort(String propertyName, boolean descending); +} diff --git a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java index 9746e2eef1..67b35c5208 100644 --- a/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java +++ b/server/src/test/java/com/vaadin/tests/components/grid/GridSingleSelectionModelTest.java @@ -18,7 +18,7 @@ import org.mockito.Mockito; import com.vaadin.data.HasValue.ValueChangeEvent; import com.vaadin.event.selection.SingleSelectionEvent; import com.vaadin.event.selection.SingleSelectionListener; -import com.vaadin.server.data.datasource.bov.Person; +import com.vaadin.server.data.provider.bov.Person; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorClientRpc; import com.vaadin.ui.Grid; diff --git a/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java b/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java index 70b7b0b77c..b6392f0910 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/StateGetDoesNotMarkDirtyTest.java @@ -23,7 +23,7 @@ public class StateGetDoesNotMarkDirtyTest { @Before public void setUp() { - excludedMethods.add(Label.class.getName() + "getDataSourceValue"); + excludedMethods.add(Label.class.getName() + "getDataProviderValue"); excludedMethods.add("getConnectorId"); } diff --git a/server/src/test/java/com/vaadin/ui/AbstractListingTest.java b/server/src/test/java/com/vaadin/ui/AbstractListingTest.java index 494fc2e1c5..eca5eed854 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractListingTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractListingTest.java @@ -6,13 +6,13 @@ import java.util.LinkedList; import java.util.List; import java.util.stream.Stream; +import com.vaadin.server.data.BackEndDataProvider; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import com.vaadin.server.data.BackEndDataSource; -import com.vaadin.server.data.DataSource; -import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.DataProvider; +import com.vaadin.server.data.ListDataProvider; import com.vaadin.server.data.Query; import com.vaadin.ui.AbstractListing.AbstractListingExtension; @@ -66,38 +66,38 @@ public class AbstractListingTest { public void testSetItemsWithCollection() { listing.setItems(items); List list = new LinkedList<>(items); - listing.getDataSource().fetch(new Query()).forEach( - str -> Assert.assertTrue("Unexpected item in data source", + listing.getDataProvider().fetch(new Query()).forEach( + str -> Assert.assertTrue("Unexpected item in data provider", list.remove(str))); - Assert.assertTrue("Not all items from list were in data source", + Assert.assertTrue("Not all items from list were in data provider", list.isEmpty()); } @Test public void testSetItemsWithVarargs() { listing.setItems(ITEM_ARRAY); - listing.getDataSource().fetch(new Query()).forEach( - str -> Assert.assertTrue("Unexpected item in data source", + listing.getDataProvider().fetch(new Query()).forEach( + str -> Assert.assertTrue("Unexpected item in data provider", items.remove(str))); - Assert.assertTrue("Not all items from list were in data source", + Assert.assertTrue("Not all items from list were in data provider", items.isEmpty()); } @Test - public void testSetDataSource() { - ListDataSource dataSource = DataSource.create(items); - listing.setDataSource(dataSource); - Assert.assertEquals("setDataSource did not set data source", dataSource, - listing.getDataSource()); - listing.setDataSource(new BackEndDataSource<>(q -> Stream.of(ITEM_ARRAY) + public void testSetDataProvider() { + ListDataProvider dataProvider = DataProvider.create(items); + listing.setDataProvider(dataProvider); + Assert.assertEquals("setDataProvider did not set data provider", dataProvider, + listing.getDataProvider()); + listing.setDataProvider(new BackEndDataProvider<>(q -> Stream.of(ITEM_ARRAY) .skip(q.getOffset()).limit(q.getLimit()), q -> ITEM_ARRAY.length)); - Assert.assertNotEquals("setDataSource did not replace data source", - dataSource, listing.getDataSource()); + Assert.assertNotEquals("setDataProvider did not replace data provider", + dataProvider, listing.getDataProvider()); } @Test - public void testAddDataGeneratorBeforeDataSource() { + public void testAddDataGeneratorBeforeDataProvider() { CountGenerator generator = new CountGenerator(); generator.extend(listing); listing.setItems("Foo"); @@ -107,7 +107,7 @@ public class AbstractListingTest { } @Test - public void testAddDataGeneratorAfterDataSource() { + public void testAddDataGeneratorAfterDataProvider() { CountGenerator generator = new CountGenerator(); listing.setItems("Foo"); generator.extend(listing); diff --git a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java index 92c288fc34..e31e6e28db 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractMultiSelectTest.java @@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; import java.util.stream.Stream; +import com.vaadin.server.data.DataProvider; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -39,7 +40,6 @@ import org.mockito.Mockito; import com.vaadin.data.HasValue.ValueChangeEvent; import com.vaadin.event.selection.MultiSelectionEvent; import com.vaadin.event.selection.MultiSelectionListener; -import com.vaadin.server.data.DataSource; import com.vaadin.shared.Registration; import com.vaadin.shared.data.selection.MultiSelectServerRpc; @@ -63,8 +63,8 @@ public class AbstractMultiSelectTest { public void setUp() { selectToTest.deselectAll(); // Intentional deviation from upcoming selection order - selectToTest.setDataSource( - DataSource.create("3", "2", "1", "5", "8", "7", "4", "6")); + selectToTest.setDataProvider( + DataProvider.create("3", "2", "1", "5", "8", "7", "4", "6")); rpc = ComponentTest.getRpcProxy(selectToTest, MultiSelectServerRpc.class); } diff --git a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java index b429afb600..83cfbd1ee1 100644 --- a/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java +++ b/server/src/test/java/com/vaadin/ui/AbstractSingleSelectTest.java @@ -33,7 +33,7 @@ import org.mockito.Mockito; import com.vaadin.data.HasValue.ValueChangeEvent; import com.vaadin.event.selection.SingleSelectionEvent; import com.vaadin.event.selection.SingleSelectionListener; -import com.vaadin.server.data.datasource.bov.Person; +import com.vaadin.server.data.provider.bov.Person; import com.vaadin.shared.Registration; import com.vaadin.shared.data.DataCommunicatorClientRpc; diff --git a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java index ec48a74799..22b9206760 100644 --- a/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java +++ b/server/src/test/java/com/vaadin/ui/RadioButtonGroupTest.java @@ -24,7 +24,7 @@ import org.junit.Before; import org.junit.Test; import com.vaadin.data.SelectionModel.Multi; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.shared.data.selection.SelectionServerRpc; public class RadioButtonGroupTest { @@ -35,7 +35,7 @@ public class RadioButtonGroupTest { radioButtonGroup = new RadioButtonGroup<>(); // Intentional deviation from upcoming selection order radioButtonGroup - .setDataSource(DataSource.create("Third", "Second", "First")); + .setDataProvider(DataProvider.create("Third", "Second", "First")); } @Test diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractListingTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractListingTestUI.java index 126eb4bf33..9d2ca4f6ea 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractListingTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractListingTestUI.java @@ -28,7 +28,7 @@ public abstract class AbstractListingTestUI> options.put("10000", 10000); options.put("100000", 100000); - createSelectAction("Items", "Data source", options, "20", + createSelectAction("Items", "Data provider", options, "20", (c, number, data) -> { c.setItems(createItems(number)); }); diff --git a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java index 89f9f9e0ea..0037f0f2d1 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/abstractlisting/AbstractMultiSelectTestUI.java @@ -32,7 +32,7 @@ public abstract class AbstractMultiSelectTestUI { abstractMultiSelect .setItemCaptionGenerator(captionGenerator); - abstractMultiSelect.getDataSource().refreshAll(); + abstractMultiSelect.getDataProvider().refreshAll(); }, true); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java index 95d2c016b1..56d45995cb 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/checkbox/CheckBoxGroupTestUI.java @@ -64,7 +64,7 @@ public class CheckBoxGroupTestUI } else { group.setItemIconGenerator(DEFAULT_ICON_GENERATOR); } - group.getDataSource().refreshAll(); + group.getDataProvider().refreshAll(); } private int getIndex(Object item) { diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java index f6de3d22ba..d6d0c78ea6 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxClickIcon.java @@ -17,7 +17,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.FontAwesome; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractReindeerTestUI; import com.vaadin.ui.ComboBox; @@ -31,7 +31,7 @@ public class ComboBoxClickIcon extends AbstractReindeerTestUI { @Override protected void setup(VaadinRequest request) { final ComboBox combo = new ComboBox<>(null, - DataSource.create("A", "B", "C")); + DataProvider.create("A", "B", "C")); combo.setItemIconGenerator(item -> FontAwesome.ALIGN_CENTER); combo.setTextInputAllowed(false); addComponent(combo); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyItemsKeyboardNavigation.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyItemsKeyboardNavigation.java index 8d3936aecd..90d6ad74a1 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyItemsKeyboardNavigation.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEmptyItemsKeyboardNavigation.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractReindeerTestUI; import com.vaadin.ui.ComboBox; @@ -9,7 +9,7 @@ public class ComboBoxEmptyItemsKeyboardNavigation extends AbstractReindeerTestUI @Override protected void setup(VaadinRequest request) { ComboBox comboBox = new ComboBox<>(null, - DataSource.create("foo", "bar")); + DataProvider.create("foo", "bar")); addComponent(comboBox); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEnablesComboBox.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEnablesComboBox.java index 3917ad2ebd..c6ff7496af 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEnablesComboBox.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxEnablesComboBox.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.tests.components.TestBase; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; public class ComboBoxEnablesComboBox extends TestBase { @@ -11,10 +11,10 @@ public class ComboBoxEnablesComboBox extends TestBase { @Override protected void setup() { ComboBox cb = new ComboBox<>("Always enabled", - new ItemDataSource(10)); + new ItemDataProvider(10)); cb.addValueChangeListener(event -> cb2.setEnabled(true)); cb2 = new ComboBox("Initially disabled", - new ItemDataSource(10)); + new ItemDataProvider(10)); cb2.setEnabled(false); addComponent(cb); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInPopup.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInPopup.java index f34259e750..fdb97f3402 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInPopup.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInPopup.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.event.ShortcutAction.KeyCode; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; import com.vaadin.ui.Button.ClickEvent; @@ -36,7 +36,7 @@ public class ComboBoxInPopup extends TestBase { private Component createComboBox() { return new ComboBox("A combo box", - DataSource.create("Yes", "No", "Maybe")); + DataProvider.create("Yes", "No", "Maybe")); } @Override diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInvalidNullSelection.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInvalidNullSelection.java index a569801cbe..d01565e7c7 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInvalidNullSelection.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxInvalidNullSelection.java @@ -1,6 +1,6 @@ package com.vaadin.tests.components.combobox; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.TestBase; import com.vaadin.tests.util.Log; import com.vaadin.ui.Button; @@ -9,28 +9,28 @@ import com.vaadin.ui.ComboBox; public class ComboBoxInvalidNullSelection extends TestBase { private static final Object CAPTION = "C"; - private DataSource ds1; - private DataSource ds2; + private DataProvider ds1; + private DataProvider ds2; private ComboBox combo; private Log log = new Log(5); @Override protected void setup() { - createDataSources(); + createDataProviders(); - Button b = new Button("Swap data source"); + Button b = new Button("Swap data provider"); b.addClickListener(event -> { - if (combo.getDataSource() == ds1) { - combo.setDataSource(ds2); + if (combo.getDataProvider() == ds1) { + combo.setDataProvider(ds2); } else { - combo.setDataSource(ds1); + combo.setDataProvider(ds1); } combo.setValue("Item 3"); }); combo = new ComboBox<>(); - combo.setDataSource(ds1); + combo.setDataProvider(ds1); combo.addValueChangeListener( event -> log.log("Value is now: " + combo.getValue())); addComponent(log); @@ -39,16 +39,16 @@ public class ComboBoxInvalidNullSelection extends TestBase { addComponent(new Button("Dummy for TestBench")); } - private void createDataSources() { - ds1 = DataSource.create("Item 1", "Item 2", "Item 3", "Item 4"); + private void createDataProviders() { + ds1 = DataProvider.create("Item 1", "Item 2", "Item 3", "Item 4"); - ds2 = DataSource.create("Item 3"); + ds2 = DataProvider.create("Item 3"); } @Override protected String getDescription() { - return "Select \"Item 3\" in the ComboBox, change the data source, focus and blur the ComboBox. The value should temporarily change to null when changing data source but not when focusing and blurring the ComboBox"; + return "Select \"Item 3\" in the ComboBox, change the data provider, focus and blur the ComboBox. The value should temporarily change to null when changing data provider but not when focusing and blurring the ComboBox"; } @Override diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java index c262b5f01a..1909a90c92 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxLargeIcons.java @@ -3,7 +3,7 @@ package com.vaadin.tests.components.combobox; import java.util.Date; import com.vaadin.server.ThemeResource; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.ComboBox; @@ -22,7 +22,7 @@ public class ComboBoxLargeIcons extends TestBase { @Override protected void setup() { ComboBox cb = new ComboBox(null, - DataSource.create("folder-add", "folder-delete", "arrow-down", + DataProvider.create("folder-add", "folder-delete", "arrow-down", "arrow-left", "arrow-right", "arrow-up", "document-add", "document-delete", "document-doc", "document-edit", "document-image", "document-pdf", "document-ppt", diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxMousewheel.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxMousewheel.java index 8479733267..f77de3650c 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxMousewheel.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxMousewheel.java @@ -17,7 +17,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractReindeerTestUI; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; /** @@ -37,7 +37,7 @@ public class ComboBoxMousewheel extends AbstractReindeerTestUI { } private ComboBox createComboBox(String caption) { - ComboBox cb = new ComboBox<>(caption, new ItemDataSource(100)); + ComboBox cb = new ComboBox<>(caption, new ItemDataProvider(100)); cb.setId(caption); return cb; } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxNavigation.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxNavigation.java index b4b12bcb5b..bc5144a7bf 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxNavigation.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxNavigation.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.tests.components.TestBase; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; public class ComboBoxNavigation extends TestBase { @@ -18,7 +18,7 @@ public class ComboBoxNavigation extends TestBase { @Override protected void setup() { - ComboBox cb = new ComboBox<>(null, new ItemDataSource(100)); + ComboBox cb = new ComboBox<>(null, new ItemDataProvider(100)); addComponent(cb); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxPopupWhenBodyScrolls.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxPopupWhenBodyScrolls.java index b42e66e531..04636a3f4f 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxPopupWhenBodyScrolls.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxPopupWhenBodyScrolls.java @@ -17,7 +17,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.VaadinRequest; import com.vaadin.tests.components.AbstractReindeerTestUI; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; import com.vaadin.ui.Label; @@ -29,7 +29,7 @@ public class ComboBoxPopupWhenBodyScrolls extends AbstractReindeerTestUI { .add("body.v-generated-body { overflow: auto;height:auto;}"); getPage().getStyles().add( "body.v-generated-body .v-ui.v-scrollable{ overflow: visible;height:auto !important;}"); - ComboBox cb = new ComboBox<>(null, new ItemDataSource(10)); + ComboBox cb = new ComboBox<>(null, new ItemDataProvider(10)); Label spacer = new Label("foo"); spacer.setHeight("2000px"); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelectingWithNewItemsAllowed.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelectingWithNewItemsAllowed.java index dc87ee59e5..a3e48f3509 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelectingWithNewItemsAllowed.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxSelectingWithNewItemsAllowed.java @@ -36,7 +36,7 @@ public class ComboBoxSelectingWithNewItemsAllowed extends ComboBoxSelecting { }); comboBox.addValueChangeListener(event -> label.setValue( - String.valueOf(comboBox.getDataSource().size(new Query())))); + String.valueOf(comboBox.getDataProvider().size(new Query())))); addComponent(label); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java index d1f8c16562..10ec271745 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboBoxUndefinedWidthAndIcon.java @@ -2,13 +2,13 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.ThemeResource; import com.vaadin.tests.components.TestBase; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; public class ComboBoxUndefinedWidthAndIcon extends TestBase { @Override protected void setup() { - ComboBox cb = new ComboBox<>(null, new ItemDataSource(200)); + ComboBox cb = new ComboBox<>(null, new ItemDataProvider(200)); cb.setItemIconGenerator( item -> new ThemeResource("../runo/icons/16/users.png")); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboFocusBlurEvents.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboFocusBlurEvents.java index 5513c9eba9..9ab060e702 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboFocusBlurEvents.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboFocusBlurEvents.java @@ -1,7 +1,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.tests.components.TestBase; -import com.vaadin.tests.util.ItemDataSource; +import com.vaadin.tests.util.ItemDataProvider; import com.vaadin.ui.ComboBox; import com.vaadin.ui.TextField; import com.vaadin.v7.data.util.ObjectProperty; @@ -16,7 +16,7 @@ public class ComboFocusBlurEvents extends TestBase { protected void setup() { ComboBox cb = new ComboBox<>("Combobox", - new ItemDataSource(100)); + new ItemDataProvider(100)); cb.setPlaceholder("Enter text"); cb.setDescription("Some Combobox"); addComponent(cb); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxMenuBarAutoopen.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxMenuBarAutoopen.java index a0793aef47..3d5a2ad3c9 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxMenuBarAutoopen.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxMenuBarAutoopen.java @@ -17,7 +17,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.Page; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractReindeerTestUI; import com.vaadin.ui.ComboBox; import com.vaadin.ui.HorizontalLayout; @@ -38,7 +38,7 @@ public class ComboboxMenuBarAutoopen extends AbstractReindeerTestUI { HorizontalLayout layout = new HorizontalLayout(); layout.setSpacing(true); ComboBox combo = new ComboBox<>(null, - DataSource.create("1", "2", "3")); + DataProvider.create("1", "2", "3")); layout.addComponent(combo); MenuBar menubar = getMenubar(); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxPrimaryStyleNames.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxPrimaryStyleNames.java index 8072db9de4..7e78036d56 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxPrimaryStyleNames.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxPrimaryStyleNames.java @@ -1,6 +1,6 @@ package com.vaadin.tests.components.combobox; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.TestBase; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; @@ -10,7 +10,7 @@ public class ComboboxPrimaryStyleNames extends TestBase { @Override protected void setup() { final ComboBox box = new ComboBox(null, - DataSource.create("Value 1", "Value 2", "Value 3", "Value 4")); + DataProvider.create("Value 1", "Value 2", "Value 3", "Value 4")); box.setPrimaryStyleName("my-combobox"); addComponent(box); diff --git a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxStyleChangeWidth.java b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxStyleChangeWidth.java index 3faef53ed7..6d5f02be48 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxStyleChangeWidth.java +++ b/uitest/src/main/java/com/vaadin/tests/components/combobox/ComboboxStyleChangeWidth.java @@ -16,7 +16,7 @@ package com.vaadin.tests.components.combobox; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.ui.Button; import com.vaadin.ui.ComboBox; @@ -30,7 +30,7 @@ public class ComboboxStyleChangeWidth extends AbstractTestUIWithLog { @Override protected void setup(VaadinRequest request) { - final ComboBox cbFoo = new ComboBox<>(null, DataSource.create( + final ComboBox cbFoo = new ComboBox<>(null, DataProvider.create( "A really long string that causes an inline width to be set")); cbFoo.setSizeUndefined(); diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java b/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java index b452965765..b1672fbbed 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/GridApplyFilterWhenScrolledDown.java @@ -5,7 +5,7 @@ import java.util.List; import com.vaadin.server.SerializableFunction; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; +import com.vaadin.server.data.DataProvider; import com.vaadin.tests.components.AbstractTestUI; import com.vaadin.ui.Button; import com.vaadin.ui.Grid; @@ -28,15 +28,15 @@ public class GridApplyFilterWhenScrolledDown extends AbstractTestUI { addComponent(grid); Button button = new Button("Filter Test item", - event -> filter(grid.getDataSource(), data)); + event -> filter(grid.getDataProvider(), data)); addComponent(button); } - private void filter(DataSource dataSource, List data) { + private void filter(DataProvider dataProvider, List data) { String last = data.get(data.size() - 1); data.clear(); data.add(last); - dataSource.refreshAll(); + dataProvider.refreshAll(); } -} \ No newline at end of file +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataProvider.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataProvider.java new file mode 100644 index 0000000000..288bc190f3 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataProvider.java @@ -0,0 +1,79 @@ +/* + * Copyright 2000-2016 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.components.grid.basics; + +import java.util.List; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.server.data.DataProvider; +import com.vaadin.server.data.ListDataProvider; +import com.vaadin.tests.components.AbstractReindeerTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; + +/** + * @author Vaadin Ltd + * + */ +public class RefreshDataProvider extends AbstractReindeerTestUI { + + @Override + protected void setup(VaadinRequest request) { + Grid grid = new Grid<>(); + List data = DataObject.generateObjects(); + + ListDataProvider dataProvider = DataProvider.create(data); + grid.setDataProvider(dataProvider); + + grid.setDataProvider(dataProvider); + grid.addColumn("Coordinates", DataObject::getCoordinates); + addComponent(grid); + + Button update = new Button("Update data", + event -> updateData(dataProvider, data)); + update.setId("update"); + addComponent(update); + + Button add = new Button("Add data", event -> addData(dataProvider, data)); + add.setId("add"); + addComponent(add); + + Button remove = new Button("Remove data", + event -> removeData(dataProvider, data)); + remove.setId("remove"); + addComponent(remove); + } + + private void updateData(DataProvider dataProvider, + List data) { + data.get(0).setCoordinates("Updated coordinates"); + dataProvider.refreshAll(); + } + + private void addData(DataProvider dataProvider, + List data) { + DataObject dataObject = new DataObject(); + dataObject.setCoordinates("Added"); + data.add(0, dataObject); + dataProvider.refreshAll(); + } + + private void removeData(DataProvider dataProvider, + List data) { + data.remove(0); + dataProvider.refreshAll(); + } +} diff --git a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataSource.java b/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataSource.java deleted file mode 100644 index fec3e4aee3..0000000000 --- a/uitest/src/main/java/com/vaadin/tests/components/grid/basics/RefreshDataSource.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright 2000-2016 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.components.grid.basics; - -import java.util.List; - -import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.DataSource; -import com.vaadin.server.data.ListDataSource; -import com.vaadin.tests.components.AbstractReindeerTestUI; -import com.vaadin.ui.Button; -import com.vaadin.ui.Grid; - -/** - * @author Vaadin Ltd - * - */ -public class RefreshDataSource extends AbstractReindeerTestUI { - - @Override - protected void setup(VaadinRequest request) { - Grid grid = new Grid<>(); - List data = DataObject.generateObjects(); - - ListDataSource dataSource = DataSource.create(data); - grid.setDataSource(dataSource); - - grid.setDataSource(dataSource); - grid.addColumn("Coordinates", DataObject::getCoordinates); - addComponent(grid); - - Button update = new Button("Update data", - event -> updateData(dataSource, data)); - update.setId("update"); - addComponent(update); - - Button add = new Button("Add data", event -> addData(dataSource, data)); - add.setId("add"); - addComponent(add); - - Button remove = new Button("Remove data", - event -> removeData(dataSource, data)); - remove.setId("remove"); - addComponent(remove); - } - - private void updateData(DataSource dataSource, - List data) { - data.get(0).setCoordinates("Updated coordinates"); - dataSource.refreshAll(); - } - - private void addData(DataSource dataSource, - List data) { - DataObject dataObject = new DataObject(); - dataObject.setCoordinates("Added"); - data.add(0, dataObject); - dataSource.refreshAll(); - } - - private void removeData(DataSource dataSource, - List data) { - data.remove(0); - dataSource.refreshAll(); - } -} diff --git a/uitest/src/main/java/com/vaadin/tests/components/listselect/ListSelectAddRemoveItems.java b/uitest/src/main/java/com/vaadin/tests/components/listselect/ListSelectAddRemoveItems.java index d78025bf4c..0ee679e891 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/listselect/ListSelectAddRemoveItems.java +++ b/uitest/src/main/java/com/vaadin/tests/components/listselect/ListSelectAddRemoveItems.java @@ -21,21 +21,21 @@ import java.util.List; import java.util.stream.Collectors; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.ListDataProvider; import com.vaadin.tests.components.AbstractTestUIWithLog; import com.vaadin.ui.Button; import com.vaadin.ui.ListSelect; -// FIXME this test should be updated once the datasource supports CRUD operations #77 +// FIXME this test should be updated once the provider supports CRUD operations #77 public class ListSelectAddRemoveItems extends AbstractTestUIWithLog { - private ListDataSource dataSource = new ListDataSource<>( + private ListDataProvider dataProvider = new ListDataProvider<>( Collections.emptyList()); private ListSelect listSelect; @Override protected void setup(VaadinRequest request) { - listSelect = new ListSelect<>("ListSelect", dataSource); + listSelect = new ListSelect<>("ListSelect", dataProvider); listSelect.setWidth("100px"); listSelect.setRows(10); @@ -50,69 +50,69 @@ public class ListSelectAddRemoveItems extends AbstractTestUIWithLog { })); addComponent(new Button("Add first", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.add(0, "first"); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Add middle", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.add(list.size() / 2, "middle"); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Add last", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.add("last"); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Swap", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); Collections.swap(list, 0, list.size() - 1); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Remove first", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.remove(0); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Remove middle", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.remove(list.size() / 2); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); addComponent(new Button("Remove last", event -> { - List list = dataSource.fetch(null) + List list = dataProvider.fetch(null) .collect(Collectors.toList()); list.remove(list.size() - 1); - dataSource = new ListDataSource<>(list); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(list); + listSelect.setDataProvider(dataProvider); logContainer(); })); @@ -121,7 +121,7 @@ public class ListSelectAddRemoveItems extends AbstractTestUIWithLog { private void logContainer() { StringBuilder b = new StringBuilder(); - List list = dataSource.fetch(null).collect(Collectors.toList()); + List list = dataProvider.fetch(null).collect(Collectors.toList()); for (int i = 0; i < list.size(); i++) { Object id = list.get(i); if (i != 0) { @@ -134,8 +134,8 @@ public class ListSelectAddRemoveItems extends AbstractTestUIWithLog { } public void resetContainer() { - dataSource = new ListDataSource<>(Arrays.asList("a", "b", "c")); - listSelect.setDataSource(dataSource); + dataProvider = new ListDataProvider<>(Arrays.asList("a", "b", "c")); + listSelect.setDataProvider(dataProvider); } @Override diff --git a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java index 246441598b..07e5b2c4d0 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java +++ b/uitest/src/main/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTestUI.java @@ -75,7 +75,7 @@ public class RadioButtonGroupTestUI } else { group.setItemIconGenerator(item -> null); } - group.getDataSource().refreshAll(); + group.getDataProvider().refreshAll(); } private void createItemCaptionGeneratorMenu() { @@ -88,7 +88,7 @@ public class RadioButtonGroupTestUI createSelectAction("Item Caption Generator", "Item Caption Generator", options, "None", (radioButtonGroup, captionGenerator, data) -> { radioButtonGroup.setItemCaptionGenerator(captionGenerator); - radioButtonGroup.getDataSource().refreshAll(); + radioButtonGroup.getDataProvider().refreshAll(); }, true); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/slider/SliderValueFromDataSource.java b/uitest/src/main/java/com/vaadin/tests/components/slider/SliderValueFromDataSource.java index 9d147eb3b5..6f9bd6b4ef 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/slider/SliderValueFromDataSource.java +++ b/uitest/src/main/java/com/vaadin/tests/components/slider/SliderValueFromDataSource.java @@ -47,7 +47,7 @@ public class SliderValueFromDataSource extends AbstractReindeerTestUI { @Override protected String getTestDescription() { - return "Slider and ProgressBar do not properly pass a value from data source to the client"; + return "Slider and ProgressBar do not properly pass a value from data provider to the client"; } @Override diff --git a/uitest/src/main/java/com/vaadin/tests/data/DummyData.java b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java index cc1e60e7b0..fdf8a710d0 100644 --- a/uitest/src/main/java/com/vaadin/tests/data/DummyData.java +++ b/uitest/src/main/java/com/vaadin/tests/data/DummyData.java @@ -9,7 +9,7 @@ import java.util.stream.Stream; import com.vaadin.annotations.Widgetset; import com.vaadin.server.VaadinRequest; -import com.vaadin.server.data.ListDataSource; +import com.vaadin.server.data.ListDataProvider; import com.vaadin.server.data.Query; import com.vaadin.shared.data.DataCommunicatorConstants; import com.vaadin.tests.components.AbstractTestUIWithLog; @@ -22,12 +22,12 @@ import com.vaadin.ui.HorizontalLayout; public class DummyData extends AbstractTestUIWithLog { /** - * DataSource that keeps track on how often the data is requested. + * DataProvider that keeps track on how often the data is requested. */ - private class LoggingDataSource extends ListDataSource { + private class LoggingDataProvider extends ListDataProvider { private int count = 0; - private LoggingDataSource(Collection collection) { + private LoggingDataProvider(Collection collection) { super(collection); } @@ -80,7 +80,7 @@ public class DummyData extends AbstractTestUIWithLog { for (int i = 0; i < 300; ++i) { items.add("Foo " + i); } - dummy.setDataSource(new LoggingDataSource(items)); + dummy.setDataProvider(new LoggingDataProvider(items)); dummy.setValue("Foo 200"); HorizontalLayout controls = new HorizontalLayout(); @@ -88,11 +88,11 @@ public class DummyData extends AbstractTestUIWithLog { controls.addComponent(new Button("Select Foo 20", e -> { dummy.setValue("Foo " + 20); })); - controls.addComponent(new Button("Reset data source", e -> { - dummy.setDataSource(new LoggingDataSource(items)); + controls.addComponent(new Button("Reset data provider", e -> { + dummy.setDataProvider(new LoggingDataProvider(items)); })); controls.addComponent(new Button("Remove all data", e -> { - dummy.setDataSource(new LoggingDataSource(Collections.emptyList())); + dummy.setDataProvider(new LoggingDataProvider(Collections.emptyList())); })); addComponent(dummy); } diff --git a/uitest/src/main/java/com/vaadin/tests/layouts/MovingComponentsWhileOldParentInvisible.java b/uitest/src/main/java/com/vaadin/tests/layouts/MovingComponentsWhileOldParentInvisible.java index 910265696f..16e131da76 100644 --- a/uitest/src/main/java/com/vaadin/tests/layouts/MovingComponentsWhileOldParentInvisible.java +++ b/uitest/src/main/java/com/vaadin/tests/layouts/MovingComponentsWhileOldParentInvisible.java @@ -53,7 +53,7 @@ public class MovingComponentsWhileOldParentInvisible extends TestBase { }); componentContainerSelect.setValue(componentContainerSelect - .getDataSource().fetch(new Query()).iterator().next()); + .getDataProvider().fetch(new Query()).iterator().next()); Button but1 = new Button("Move in and out of component container", new Button.ClickListener() { diff --git a/uitest/src/main/java/com/vaadin/tests/performance/AbstractBeansMemoryTest.java b/uitest/src/main/java/com/vaadin/tests/performance/AbstractBeansMemoryTest.java index 526b758e22..8f352e23f3 100644 --- a/uitest/src/main/java/com/vaadin/tests/performance/AbstractBeansMemoryTest.java +++ b/uitest/src/main/java/com/vaadin/tests/performance/AbstractBeansMemoryTest.java @@ -175,7 +175,7 @@ public abstract class AbstractBeansMemoryTest private Component createMenu(T component) { MenuBar menu = new MenuBar(); createContainerSizeMenu(menu.addItem("Size", null), component); - createContainerMenu(menu.addItem("Data source", null), component); + createContainerMenu(menu.addItem("Data provider", null), component); menu.addItem("Create only data", item -> toggleDataOnly(item, component)).setCheckable(true); @@ -208,7 +208,7 @@ public abstract class AbstractBeansMemoryTest private MenuItem addContainerSizeMenu(int size, MenuItem menu, T component) { - MenuItem item = menu.addItem("Set data source size to " + size, + MenuItem item = menu.addItem("Set data provider size to " + size, itm -> setData(itm, size, component, isInMemory)); item.setCheckable(true); return item; diff --git a/uitest/src/main/java/com/vaadin/tests/util/ItemDataProvider.java b/uitest/src/main/java/com/vaadin/tests/util/ItemDataProvider.java new file mode 100644 index 0000000000..5d207720d0 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/util/ItemDataProvider.java @@ -0,0 +1,21 @@ +package com.vaadin.tests.util; + +import java.util.stream.IntStream; + +import com.vaadin.server.data.BackEndDataProvider; + +/** + * A data provider for tests that creates "Item n" strings on the fly. + * + * @author Vaadin Ltd + */ +public class ItemDataProvider extends BackEndDataProvider { + + public ItemDataProvider(int size) { + super(q -> IntStream + .range(q.getOffset(), + Math.max(q.getOffset() + q.getLimit() + 1, size)) + .mapToObj(i -> "Item " + i), q -> size); + } + +} diff --git a/uitest/src/main/java/com/vaadin/tests/util/ItemDataSource.java b/uitest/src/main/java/com/vaadin/tests/util/ItemDataSource.java deleted file mode 100644 index e7a184f6da..0000000000 --- a/uitest/src/main/java/com/vaadin/tests/util/ItemDataSource.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.vaadin.tests.util; - -import java.util.stream.IntStream; - -import com.vaadin.server.data.BackEndDataSource; - -/** - * A data source for tests that creates "Item n" strings on the fly. - * - * @author Vaadin Ltd - */ -public class ItemDataSource extends BackEndDataSource { - - public ItemDataSource(int size) { - super(q -> IntStream - .range(q.getOffset(), - Math.max(q.getOffset() + q.getLimit() + 1, size)) - .mapToObj(i -> "Item " + i), q -> size); - } - -} diff --git a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java index b4e462a983..65c12e2687 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/checkboxgroup/CheckBoxGroupTest.java @@ -55,13 +55,13 @@ public class CheckBoxGroupTest extends MultiBrowserTest { @Test public void initialItems_reduceItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); } @Test public void initialItems_increaseItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "100"); + selectMenuPath("Component", "Data provider", "Items", "100"); assertItems(100); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataProviderTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataProviderTest.java new file mode 100644 index 0000000000..7c60bb9572 --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataProviderTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2000-2016 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.components.grid.basics; + +import org.junit.Assert; +import org.junit.Test; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import com.vaadin.tests.tb3.MultiBrowserTest; + +/** + * @author Vaadin Ltd + * + */ +public class RefreshDataProviderTest extends MultiBrowserTest { + + @Test + public void updateFirstRow() { + openTestURL(); + + findElement(By.id("update")).click(); + WebElement first = findElement(By.tagName("td")); + Assert.assertEquals( + "UI component is not refreshed after update in data", + "Updated coordinates", first.getText()); + } + + @Test + public void addFirstRow() { + openTestURL(); + + findElement(By.id("add")).click(); + WebElement first = findElement(By.tagName("td")); + + Assert.assertEquals("UI component is not refreshed after add new data", + "Added", first.getText()); + } + + @Test + public void removeFirstRow() { + openTestURL(); + + WebElement first = findElement(By.tagName("td")); + String old = first.getText(); + first = findElement(By.id("remove")); + Assert.assertNotEquals("UI component is not refreshed after removal", + old, first.getText()); + } + +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataSourceTest.java b/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataSourceTest.java deleted file mode 100644 index e73bbd602c..0000000000 --- a/uitest/src/test/java/com/vaadin/tests/components/grid/basics/RefreshDataSourceTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2000-2016 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.components.grid.basics; - -import org.junit.Assert; -import org.junit.Test; -import org.openqa.selenium.By; -import org.openqa.selenium.WebElement; - -import com.vaadin.tests.tb3.MultiBrowserTest; - -/** - * @author Vaadin Ltd - * - */ -public class RefreshDataSourceTest extends MultiBrowserTest { - - @Test - public void updateFirstRow() { - openTestURL(); - - findElement(By.id("update")).click(); - WebElement first = findElement(By.tagName("td")); - Assert.assertEquals( - "UI component is not refreshed after update in data", - "Updated coordinates", first.getText()); - } - - @Test - public void addFirstRow() { - openTestURL(); - - findElement(By.id("add")).click(); - WebElement first = findElement(By.tagName("td")); - - Assert.assertEquals("UI component is not refreshed after add new data", - "Added", first.getText()); - } - - @Test - public void removeFirstRow() { - openTestURL(); - - WebElement first = findElement(By.tagName("td")); - String old = first.getText(); - first = findElement(By.id("remove")); - Assert.assertNotEquals("UI component is not refreshed after removal", - old, first.getText()); - } - -} \ No newline at end of file diff --git a/uitest/src/test/java/com/vaadin/tests/components/listselect/ListSelectTest.java b/uitest/src/test/java/com/vaadin/tests/components/listselect/ListSelectTest.java index 68011d7813..d934fa448f 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/listselect/ListSelectTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/listselect/ListSelectTest.java @@ -32,13 +32,13 @@ public class ListSelectTest extends SingleBrowserTestPhantomJS2 { @Test public void initialItems_reduceItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); } @Test public void initialItems_increaseItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "100"); + selectMenuPath("Component", "Data provider", "Items", "100"); assertItems(100); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectTest.java b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectTest.java index 364e53aa75..abb7acb428 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/nativeselect/NativeSelectTest.java @@ -39,13 +39,13 @@ public class NativeSelectTest extends MultiBrowserTest { @Test public void initialItems_reduceItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); } @Test public void initialItems_increaseItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "100"); + selectMenuPath("Component", "Data provider", "Items", "100"); assertItems(100); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java b/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java index b2546347dd..bff6e85b98 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/radiobutton/RadioButtonGroupTest.java @@ -50,13 +50,13 @@ public class RadioButtonGroupTest extends MultiBrowserTest { @Test public void initialItems_reduceItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); } @Test public void initialItems_increaseItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "100"); + selectMenuPath("Component", "Data provider", "Items", "100"); assertItems(100); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java b/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java index ad2752112f..cc819a2744 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/twincolselect/TwinColSelectTest.java @@ -32,19 +32,19 @@ public class TwinColSelectTest extends MultiBrowserTest { @Test public void initialItems_reduceItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); } @Test public void initialItems_increaseItemCount_containsCorrectItems() { - selectMenuPath("Component", "Data source", "Items", "100"); + selectMenuPath("Component", "Data provider", "Items", "100"); assertItems(100); } @Test public void itemsMovedFromLeftToRight() { - selectMenuPath("Component", "Data source", "Items", "5"); + selectMenuPath("Component", "Data provider", "Items", "5"); assertItems(5); selectItems("Item 1", "Item 2", "Item 4"); diff --git a/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java b/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java index 1e9f0f9ade..b05c64e713 100644 --- a/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java +++ b/uitest/src/test/java/com/vaadin/tests/data/DummyDataTest.java @@ -57,10 +57,10 @@ public class DummyDataTest extends SingleBrowserTest { } @Test - public void testDataSourceChangeOnlyOneRequest() { - // Change to a new logging data source + public void testDataProviderChangeOnlyOneRequest() { + // Change to a new logging data provider $(ButtonElement.class).get(1).click(); - assertEquals("DataSource change should only cause 1 request", + assertEquals("DataProvider change should only cause 1 request", "3. Backend request #0", getLogRow(0)); } @@ -69,12 +69,12 @@ public class DummyDataTest extends SingleBrowserTest { assertEquals("Unexpected amount of content on init.", 300, $(DummyElement.class).first() .findElements(By.className("v-label")).size()); - // Change to an empty data source + // Change to an empty data provider $(ButtonElement.class).get(2).click(); - assertEquals("Empty data source did not work as expected.", 0, + assertEquals("Empty data provider did not work as expected.", 0, $(DummyElement.class).first() .findElements(By.className("v-label")).size()); - // Change back to logging data source + // Change back to logging data provider $(ButtonElement.class).get(1).click(); assertEquals("Data was not correctly restored.", 300, $(DummyElement.class).first()