String s;
int n;
MockSourceProvider provider = new MockSourceProvider();
RefreshablePagedListHolder holder = new RefreshablePagedListHolder(provider);
holder.setFilter(new MockFilter());
BeanWrapper bw = new BeanWrapperImpl(holder);
holder.refresh(false);
n = holder.getNrOfElements();
assertTrue("A:number of elements: " + n, n == 12);
n = holder.getPageCount();
assertTrue("A:number of pages" + n, n == 2);
n = holder.getPageSize();
assertTrue("A:page size: " + n, n == 10);
n = holder.getPage();
assertTrue("A:page number: " + n, n == 0);
n = holder.getFirstElementOnPage();
assertTrue("A:first element: " + n, n == 0);
n = holder.getLastElementOnPage();
assertTrue("A:last element: " + n, n == 9);
n = holder.getPageList().size();
assertTrue("A:page list size: " + n, n == 10);
assertTrue("A:correct filter", holder.getFilter() instanceof MockFilter);
n = holder.getMaxLinkedPages();
assertTrue("A:maxLinkedPages: " + n, n == RefreshablePagedListHolder.DEFAULT_MAX_LINKED_PAGES);
n = holder.getFirstLinkedPage();
assertTrue("A:first display page: " + n, n == 0);
n = holder.getLastLinkedPage();
assertTrue("A:last display page: " + n, n == 1);
assertTrue("A:sort must be not null", holder.getSort().getProperty() != null);
n = holder.getSort().getProperty().length();
assertTrue("A:sort: " + n, n == 0);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("A:page list contents0: " + s, s.equals("Calogero"));
s = ((TestBean)holder.getPageList().get(4)).getName();
assertTrue("A:page list contents4: " + s, s.equals("Jesus"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 1);
// Go to the second page
Map cmd = new HashMap();
cmd.put("page", "1");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getPage();
assertTrue("B:page number with array map: " + n, n == 1);
n = holder.getPageList().size();
assertTrue("B:page list size: " + n, n == 2);
n = holder.getFirstElementOnPage();
assertTrue("B:first element: " + n, n == 10);
n = holder.getLastElementOnPage();
assertTrue("B:last element: " + n, n == 11);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("B:page list contents0: " + s, s.equals("Toma"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("B:page list contents1: " + s, s.equals("Sergio"));
// Return to the first page and sort by name setting the page size to 5
cmd.clear();
cmd.put("sort.property", "name");
cmd.put("pageSize", "2");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getPageCount();
assertTrue("C:number of pages: " + n, n == 6);
n = holder.getPageSize();
assertTrue("C:page size: " + n, n == 2);
n = holder.getPage();
assertTrue("C:page number: " + n, n == 0);
n = holder.getFirstLinkedPage();
assertTrue("C:first display page: " + n, n == 0);
n = holder.getLastLinkedPage();
assertTrue("C:last display page: " + n, n == 5);
n = holder.getFirstElementOnPage();
assertTrue("C:first element: " + n, n == 0);
n = holder.getLastElementOnPage();
assertTrue("C:last element: " + n, n == 1);
n = holder.getPageList().size();
assertTrue("C:page list size: " + n, n == 2);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("C:page list contents0: " + s, s.equals("amid"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("C:page list contents1: " + s, s.equals("Aziz"));
assertTrue("C:sort must be not null", holder.getSort().getProperty() != null);
s = holder.getSort().getProperty();
assertTrue("C:sort: " + s, s.equals("name"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 1);
// Ignore case in sort, maxLinkedPages set to 3
cmd.clear();
cmd.put("sort.ignoreCase", "false");
cmd.put("maxLinkedPages", "3");
bw.setPropertyValues(cmd);
holder.refresh(false);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("D:page list contents0: " + s, s.equals("Aziz"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("D:page list contents1: " + s, s.equals("Calogero"));
n = holder.getFirstLinkedPage();
assertTrue("D:first display page: " + n, n == 0);
n = holder.getLastLinkedPage();
assertTrue("D:last display page: " + n, n == 2);
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 1);
// Go to the fourth page
holder.setPage(3);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("E:page list contents0: " + s, s.equals("Giuseppe"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("E:page list contents1: " + s, s.equals("Jesus"));
n = holder.getFirstLinkedPage();
assertTrue("E:first display page: " + n, n == 2);
n = holder.getLastLinkedPage();
assertTrue("E:last display page: " + n, n == 4);
// Filter the name
cmd.clear();
cmd.put("filter.name", "G");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getPageCount();
assertTrue("F:number of pages: " + n, n == 1);
n = holder.getPageSize();
assertTrue("F:page size: " + n, n == 2);
n = holder.getPage();
assertTrue("F:page number: " + n, n == 0);
n = holder.getFirstLinkedPage();
assertTrue("F:first display page: " + n, n == 0);
n = holder.getLastLinkedPage();
assertTrue("F:last display page: " + n, n == 0);
n = holder.getFirstElementOnPage();
assertTrue("F:first element: " + n, n == 0);
n = holder.getLastElementOnPage();
assertTrue("F:last element: " + n, n == 1);
n = holder.getPageList().size();
assertTrue("F:page list size: " + n, n == 2);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("F:page list contents0: " + s, s.equals("Garcia"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("F:page list contents1: " + s, s.equals("Giuseppe"));
s = ((MockFilter) holder.getFilter()).getName();
assertTrue("F:filter name: " + s, s.equals("G"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 2);
// Request sort on name will reverse the sort
cmd.clear();
cmd.put("sort.property", "name");
bw.setPropertyValues(cmd);
holder.refresh(false);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("G:page list contents0: " + s, s.equals("Giuseppe"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("G:page list contents1: " + s, s.equals("Garcia"));
// Filter the age
cmd.clear();
cmd.put("filter.age", "34");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getLastElementOnPage();
assertTrue("H:last element: " + n, n == 0);
n = holder.getPageList().size();
assertTrue("H:page list size: " + n, n == 1);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("H:page list contents0: " + s, s.equals("Garcia"));
s = ((MockFilter) holder.getFilter()).getAge();
assertTrue("H:filter age: " + s, s.equals("34"));
s = ((MockFilter) holder.getFilter()).getName();
assertTrue("H:filter name: " + s, s.equals("G"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 3);
// Remove the name filter
// The sort is always name reversed
cmd.clear();
cmd.put("filter.name", "");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getPageList().size();
assertTrue("I:page list size: " + n, n == 2);
s = ((MockFilter) holder.getFilter()).getName();
assertTrue("H:filter name: " + s, s.equals(""));
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("I:page list contents0: " + s, s.equals("Garcia"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("I:page list contents1: " + s, s.equals("Djamel"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 4);
// Go to the alternative list
// The extendedInfo is provided to eventually pass a contextual data on the
// source provider. BUT, BY ITSELF, IT DOESN'T IMPLY THE REFRESH OF THE LIST.
cmd.clear();
cmd.put("filter.extendedInfo", "F");
bw.setPropertyValues(cmd);
n = holder.getPageList().size();
assertTrue("I:page list size: " + n, n == 2);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("I:page list contents0: " + s, s.equals("Garcia"));
s = ((TestBean)holder.getPageList().get(1)).getName();
assertTrue("I:page list contents1: " + s, s.equals("Djamel"));
// When the list is reloaded for any reason, the source provider
// can use the extendedInfo.
holder.refresh(true);
n = holder.getPageList().size();
assertTrue("J:page list size: " + n, n == 1);
s = ((TestBean)holder.getPageList().get(0)).getName();
assertTrue("J:page list contents0: " + s, s.equals("Catherine"));
n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls();
assertTrue("Correct number of times called: " + n, n == 5);
// The locale change imply a refresh of the list.
// In this test the list will be empty if a locale is provided
cmd.clear();
cmd.put("locale", "en_US");
bw.setPropertyValues(cmd);
holder.refresh(false);
n = holder.getPageList().size();
assertTrue("K:page list size: " + n, n == 0);
n = holder.getPageCount();
assertTrue("K:number of pages: " + n, n == 1);