1 16 17 package org.springframework.beans.support; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.Map ; 25 26 import junit.framework.TestCase; 27 28 import org.springframework.beans.BeanWrapper; 29 import org.springframework.beans.BeanWrapperImpl; 30 import org.springframework.beans.TestBean; 31 32 37 public class PagedListHolderTests extends TestCase { 38 39 public void testPagedListHolder() { 40 TestBean tb1 = new TestBean(); 41 tb1.setName("eva"); 42 tb1.setAge(25); 43 TestBean tb2 = new TestBean(); 44 tb2.setName("juergen"); 45 tb2.setAge(99); 46 TestBean tb3 = new TestBean(); 47 tb3.setName("Rod"); 48 tb3.setAge(32); 49 List tbs = new ArrayList (); 50 tbs.add(tb1); 51 tbs.add(tb2); 52 tbs.add(tb3); 53 54 PagedListHolder holder = new PagedListHolder(tbs); 55 assertTrue("Correct source", holder.getSource() == tbs); 56 assertTrue("Correct number of elements", holder.getNrOfElements() == 3); 57 assertTrue("Correct number of pages", holder.getPageCount() == 1); 58 assertTrue("Correct page size", holder.getPageSize() == PagedListHolder.DEFAULT_PAGE_SIZE); 59 assertTrue("Correct page number", holder.getPage() == 0); 60 assertTrue("First page", holder.isFirstPage()); 61 assertTrue("Last page", holder.isLastPage()); 62 assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); 63 assertTrue("Correct first element", holder.getLastElementOnPage() == 2); 64 assertTrue("Correct page list size", holder.getPageList().size() == 3); 65 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); 66 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2); 67 assertTrue("Correct page list contents", holder.getPageList().get(2) == tb3); 68 69 holder.setPageSize(2); 70 assertTrue("Correct number of pages", holder.getPageCount() == 2); 71 assertTrue("Correct page size", holder.getPageSize() == 2); 72 assertTrue("Correct page number", holder.getPage() == 0); 73 assertTrue("First page", holder.isFirstPage()); 74 assertFalse("Last page", holder.isLastPage()); 75 assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); 76 assertTrue("Correct last element", holder.getLastElementOnPage() == 1); 77 assertTrue("Correct page list size", holder.getPageList().size() == 2); 78 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); 79 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb2); 80 81 holder.setPage(1); 82 assertTrue("Correct page number", holder.getPage() == 1); 83 assertFalse("First page", holder.isFirstPage()); 84 assertTrue("Last page", holder.isLastPage()); 85 assertTrue("Correct first element", holder.getFirstElementOnPage() == 2); 86 assertTrue("Correct last element", holder.getLastElementOnPage() == 2); 87 assertTrue("Correct page list size", holder.getPageList().size() == 1); 88 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); 89 90 holder.setPageSize(3); 91 assertTrue("Correct number of pages", holder.getPageCount() == 1); 92 assertTrue("Correct page size", holder.getPageSize() == 3); 93 assertTrue("Correct page number", holder.getPage() == 0); 94 assertTrue("First page", holder.isFirstPage()); 95 assertTrue("Last page", holder.isLastPage()); 96 assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); 97 assertTrue("Correct last element", holder.getLastElementOnPage() == 2); 98 99 holder.setPage(1); 100 holder.setPageSize(2); 101 assertTrue("Correct number of pages", holder.getPageCount() == 2); 102 assertTrue("Correct page size", holder.getPageSize() == 2); 103 assertTrue("Correct page number", holder.getPage() == 1); 104 assertFalse("First page", holder.isFirstPage()); 105 assertTrue("Last page", holder.isLastPage()); 106 assertTrue("Correct first element", holder.getFirstElementOnPage() == 2); 107 assertTrue("Correct last element", holder.getLastElementOnPage() == 2); 108 109 holder.setPageSize(2); 110 holder.setPage(1); 111 ((MutableSortDefinition) holder.getSort()).setProperty("name"); 112 ((MutableSortDefinition) holder.getSort()).setIgnoreCase(false); 113 holder.resort(); 114 assertTrue("Correct source", holder.getSource() == tbs); 115 assertTrue("Correct number of elements", holder.getNrOfElements() == 3); 116 assertTrue("Correct number of pages", holder.getPageCount() == 2); 117 assertTrue("Correct page size", holder.getPageSize() == 2); 118 assertTrue("Correct page number", holder.getPage() == 0); 119 assertTrue("First page", holder.isFirstPage()); 120 assertFalse("Last page", holder.isLastPage()); 121 assertTrue("Correct first element", holder.getFirstElementOnPage() == 0); 122 assertTrue("Correct last element", holder.getLastElementOnPage() == 1); 123 assertTrue("Correct page list size", holder.getPageList().size() == 2); 124 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); 125 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); 126 127 ((MutableSortDefinition) holder.getSort()).setProperty("name"); 128 holder.resort(); 129 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2); 130 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); 131 132 ((MutableSortDefinition) holder.getSort()).setProperty("name"); 133 holder.resort(); 134 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb3); 135 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb1); 136 137 holder.setPage(1); 138 assertTrue("Correct page list size", holder.getPageList().size() == 1); 139 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb2); 140 141 ((MutableSortDefinition) holder.getSort()).setProperty("age"); 142 holder.resort(); 143 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); 144 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3); 145 146 ((MutableSortDefinition) holder.getSort()).setIgnoreCase(true); 147 holder.resort(); 148 assertTrue("Correct page list contents", holder.getPageList().get(0) == tb1); 149 assertTrue("Correct page list contents", holder.getPageList().get(1) == tb3); 150 151 holder.nextPage(); 152 assertEquals(1, holder.getPage()); 153 holder.previousPage(); 154 assertEquals(0, holder.getPage()); 155 holder.nextPage(); 156 assertEquals(1, holder.getPage()); 157 holder.nextPage(); 158 assertEquals(1, holder.getPage()); 159 holder.previousPage(); 160 assertEquals(0, holder.getPage()); 161 holder.previousPage(); 162 assertEquals(0, holder.getPage()); 163 } 164 165 public void testRefreshablePagedListHolder() { 166 String s; 167 int n; 168 MockSourceProvider provider = new MockSourceProvider(); 169 RefreshablePagedListHolder holder = new RefreshablePagedListHolder(provider); 170 holder.setFilter(new MockFilter()); 171 BeanWrapper bw = new BeanWrapperImpl(holder); 172 173 holder.refresh(false); 174 n = holder.getNrOfElements(); 175 assertTrue("A:number of elements: " + n, n == 12); 176 n = holder.getPageCount(); 177 assertTrue("A:number of pages" + n, n == 2); 178 n = holder.getPageSize(); 179 assertTrue("A:page size: " + n, n == 10); 180 n = holder.getPage(); 181 assertTrue("A:page number: " + n, n == 0); 182 n = holder.getFirstElementOnPage(); 183 assertTrue("A:first element: " + n, n == 0); 184 n = holder.getLastElementOnPage(); 185 assertTrue("A:last element: " + n, n == 9); 186 n = holder.getPageList().size(); 187 assertTrue("A:page list size: " + n, n == 10); 188 assertTrue("A:correct filter", holder.getFilter() instanceof MockFilter); 189 n = holder.getMaxLinkedPages(); 190 assertTrue("A:maxLinkedPages: " + n, n == RefreshablePagedListHolder.DEFAULT_MAX_LINKED_PAGES); 191 n = holder.getFirstLinkedPage(); 192 assertTrue("A:first display page: " + n, n == 0); 193 n = holder.getLastLinkedPage(); 194 assertTrue("A:last display page: " + n, n == 1); 195 assertTrue("A:sort must be not null", holder.getSort().getProperty() != null); 196 n = holder.getSort().getProperty().length(); 197 assertTrue("A:sort: " + n, n == 0); 198 s = ((TestBean)holder.getPageList().get(0)).getName(); 199 assertTrue("A:page list contents0: " + s, s.equals("Calogero")); 200 s = ((TestBean)holder.getPageList().get(4)).getName(); 201 assertTrue("A:page list contents4: " + s, s.equals("Jesus")); 202 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 203 assertTrue("Correct number of times called: " + n, n == 1); 204 205 Map cmd = new HashMap (); 207 cmd.put("page", "1"); 208 bw.setPropertyValues(cmd); 209 holder.refresh(false); 210 n = holder.getPage(); 211 assertTrue("B:page number with array map: " + n, n == 1); 212 n = holder.getPageList().size(); 213 assertTrue("B:page list size: " + n, n == 2); 214 n = holder.getFirstElementOnPage(); 215 assertTrue("B:first element: " + n, n == 10); 216 n = holder.getLastElementOnPage(); 217 assertTrue("B:last element: " + n, n == 11); 218 s = ((TestBean)holder.getPageList().get(0)).getName(); 219 assertTrue("B:page list contents0: " + s, s.equals("Toma")); 220 s = ((TestBean)holder.getPageList().get(1)).getName(); 221 assertTrue("B:page list contents1: " + s, s.equals("Sergio")); 222 223 cmd.clear(); 225 cmd.put("sort.property", "name"); 226 cmd.put("pageSize", "2"); 227 bw.setPropertyValues(cmd); 228 holder.refresh(false); 229 n = holder.getPageCount(); 230 assertTrue("C:number of pages: " + n, n == 6); 231 n = holder.getPageSize(); 232 assertTrue("C:page size: " + n, n == 2); 233 n = holder.getPage(); 234 assertTrue("C:page number: " + n, n == 0); 235 n = holder.getFirstLinkedPage(); 236 assertTrue("C:first display page: " + n, n == 0); 237 n = holder.getLastLinkedPage(); 238 assertTrue("C:last display page: " + n, n == 5); 239 n = holder.getFirstElementOnPage(); 240 assertTrue("C:first element: " + n, n == 0); 241 n = holder.getLastElementOnPage(); 242 assertTrue("C:last element: " + n, n == 1); 243 n = holder.getPageList().size(); 244 assertTrue("C:page list size: " + n, n == 2); 245 s = ((TestBean)holder.getPageList().get(0)).getName(); 246 assertTrue("C:page list contents0: " + s, s.equals("amid")); 247 s = ((TestBean)holder.getPageList().get(1)).getName(); 248 assertTrue("C:page list contents1: " + s, s.equals("Aziz")); 249 assertTrue("C:sort must be not null", holder.getSort().getProperty() != null); 250 s = holder.getSort().getProperty(); 251 assertTrue("C:sort: " + s, s.equals("name")); 252 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 253 assertTrue("Correct number of times called: " + n, n == 1); 254 255 cmd.clear(); 257 cmd.put("sort.ignoreCase", "false"); 258 cmd.put("maxLinkedPages", "3"); 259 bw.setPropertyValues(cmd); 260 holder.refresh(false); 261 s = ((TestBean)holder.getPageList().get(0)).getName(); 262 assertTrue("D:page list contents0: " + s, s.equals("Aziz")); 263 s = ((TestBean)holder.getPageList().get(1)).getName(); 264 assertTrue("D:page list contents1: " + s, s.equals("Calogero")); 265 n = holder.getFirstLinkedPage(); 266 assertTrue("D:first display page: " + n, n == 0); 267 n = holder.getLastLinkedPage(); 268 assertTrue("D:last display page: " + n, n == 2); 269 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 270 assertTrue("Correct number of times called: " + n, n == 1); 271 272 holder.setPage(3); 274 s = ((TestBean)holder.getPageList().get(0)).getName(); 275 assertTrue("E:page list contents0: " + s, s.equals("Giuseppe")); 276 s = ((TestBean)holder.getPageList().get(1)).getName(); 277 assertTrue("E:page list contents1: " + s, s.equals("Jesus")); 278 n = holder.getFirstLinkedPage(); 279 assertTrue("E:first display page: " + n, n == 2); 280 n = holder.getLastLinkedPage(); 281 assertTrue("E:last display page: " + n, n == 4); 282 283 cmd.clear(); 285 cmd.put("filter.name", "G"); 286 bw.setPropertyValues(cmd); 287 holder.refresh(false); 288 n = holder.getPageCount(); 289 assertTrue("F:number of pages: " + n, n == 1); 290 n = holder.getPageSize(); 291 assertTrue("F:page size: " + n, n == 2); 292 n = holder.getPage(); 293 assertTrue("F:page number: " + n, n == 0); 294 n = holder.getFirstLinkedPage(); 295 assertTrue("F:first display page: " + n, n == 0); 296 n = holder.getLastLinkedPage(); 297 assertTrue("F:last display page: " + n, n == 0); 298 n = holder.getFirstElementOnPage(); 299 assertTrue("F:first element: " + n, n == 0); 300 n = holder.getLastElementOnPage(); 301 assertTrue("F:last element: " + n, n == 1); 302 n = holder.getPageList().size(); 303 assertTrue("F:page list size: " + n, n == 2); 304 s = ((TestBean)holder.getPageList().get(0)).getName(); 305 assertTrue("F:page list contents0: " + s, s.equals("Garcia")); 306 s = ((TestBean)holder.getPageList().get(1)).getName(); 307 assertTrue("F:page list contents1: " + s, s.equals("Giuseppe")); 308 s = ((MockFilter) holder.getFilter()).getName(); 309 assertTrue("F:filter name: " + s, s.equals("G")); 310 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 311 assertTrue("Correct number of times called: " + n, n == 2); 312 313 cmd.clear(); 315 cmd.put("sort.property", "name"); 316 bw.setPropertyValues(cmd); 317 holder.refresh(false); 318 s = ((TestBean)holder.getPageList().get(0)).getName(); 319 assertTrue("G:page list contents0: " + s, s.equals("Giuseppe")); 320 s = ((TestBean)holder.getPageList().get(1)).getName(); 321 assertTrue("G:page list contents1: " + s, s.equals("Garcia")); 322 323 cmd.clear(); 325 cmd.put("filter.age", "34"); 326 bw.setPropertyValues(cmd); 327 holder.refresh(false); 328 n = holder.getLastElementOnPage(); 329 assertTrue("H:last element: " + n, n == 0); 330 n = holder.getPageList().size(); 331 assertTrue("H:page list size: " + n, n == 1); 332 s = ((TestBean)holder.getPageList().get(0)).getName(); 333 assertTrue("H:page list contents0: " + s, s.equals("Garcia")); 334 s = ((MockFilter) holder.getFilter()).getAge(); 335 assertTrue("H:filter age: " + s, s.equals("34")); 336 s = ((MockFilter) holder.getFilter()).getName(); 337 assertTrue("H:filter name: " + s, s.equals("G")); 338 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 339 assertTrue("Correct number of times called: " + n, n == 3); 340 341 cmd.clear(); 344 cmd.put("filter.name", ""); 345 bw.setPropertyValues(cmd); 346 holder.refresh(false); 347 n = holder.getPageList().size(); 348 assertTrue("I:page list size: " + n, n == 2); 349 s = ((MockFilter) holder.getFilter()).getName(); 350 assertTrue("H:filter name: " + s, s.equals("")); 351 s = ((TestBean)holder.getPageList().get(0)).getName(); 352 assertTrue("I:page list contents0: " + s, s.equals("Garcia")); 353 s = ((TestBean)holder.getPageList().get(1)).getName(); 354 assertTrue("I:page list contents1: " + s, s.equals("Djamel")); 355 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 356 assertTrue("Correct number of times called: " + n, n == 4); 357 358 cmd.clear(); 362 cmd.put("filter.extendedInfo", "F"); 363 bw.setPropertyValues(cmd); 364 n = holder.getPageList().size(); 365 assertTrue("I:page list size: " + n, n == 2); 366 s = ((TestBean)holder.getPageList().get(0)).getName(); 367 assertTrue("I:page list contents0: " + s, s.equals("Garcia")); 368 s = ((TestBean)holder.getPageList().get(1)).getName(); 369 assertTrue("I:page list contents1: " + s, s.equals("Djamel")); 370 371 holder.refresh(true); 374 n = holder.getPageList().size(); 375 assertTrue("J:page list size: " + n, n == 1); 376 s = ((TestBean)holder.getPageList().get(0)).getName(); 377 assertTrue("J:page list contents0: " + s, s.equals("Catherine")); 378 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 379 assertTrue("Correct number of times called: " + n, n == 5); 380 381 cmd.clear(); 384 cmd.put("locale", "en_US"); 385 bw.setPropertyValues(cmd); 386 holder.refresh(false); 387 n = holder.getPageList().size(); 388 assertTrue("K:page list size: " + n, n == 0); 389 n = holder.getPageCount(); 390 assertTrue("K:number of pages: " + n, n == 1); 391 n = holder.getFirstElementOnPage(); 392 assertTrue("K:first element: " + n, n == 0); 393 n = holder.getLastElementOnPage(); 394 assertTrue("K:last element: " + n, n == -1); 395 n = holder.getPage(); 396 assertTrue("K:page number: " + n, n == 0); 397 n = holder.getFirstLinkedPage(); 398 assertTrue("K:first display page: " + n, n == 0); 399 n = holder.getLastLinkedPage(); 400 assertTrue("K:last display page: " + n, n == 0); 401 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 402 assertTrue("Correct number of times called: " + n, n == 6); 403 404 cmd.clear(); 406 cmd.put("locale", "en_US"); 407 n = ((MockSourceProvider) holder.getSourceProvider()).getNrOfCalls(); 408 assertTrue("Correct number of times called: " + n, n == 6); 409 410 } 411 412 413 public static class MockFilter { 414 415 private String name = ""; 416 private String age = ""; 417 private String extendedInfo = ""; 418 419 public String getName() { 420 return name; 421 } 422 423 public void setName(String name) { 424 this.name = name; 425 } 426 427 public String getAge() { 428 return age; 429 } 430 431 public void setAge(String age) { 432 this.age = age; 433 } 434 435 public String getExtendedInfo() { 436 return extendedInfo; 437 } 438 439 public void setExtendedInfo(String extendedInfo) { 440 this.extendedInfo = extendedInfo; 441 } 442 443 public boolean equals(Object o) { 444 if (this == o) return true; 445 if (!(o instanceof MockFilter)) return false; 446 447 final MockFilter mockFilter = (MockFilter) o; 448 449 if (!age.equals(mockFilter.age)) return false; 450 if (!extendedInfo.equals(mockFilter.extendedInfo)) return false; 451 if (!name.equals(mockFilter.name)) return false; 452 453 return true; 454 } 455 456 public int hashCode() { 457 int result; 458 result = name.hashCode(); 459 result = 29 * result + age.hashCode(); 460 result = 29 * result + extendedInfo.hashCode(); 461 return result; 462 } 463 } 464 465 471 private static class MockSourceProvider implements PagedListSourceProvider { 472 473 private List internalList; 474 private List altList; 475 private int nrOfCalls; 476 477 public MockSourceProvider() { 478 internalList = new ArrayList (); 479 internalList.add(new TestBean("Calogero",33)); 480 internalList.add(new TestBean("Giuseppe",38)); 481 internalList.add(new TestBean("Francesco",41)); 482 internalList.add(new TestBean("Djamel",34)); 483 internalList.add(new TestBean("Jesus",48)); 484 internalList.add(new TestBean("Cedric",30)); 485 internalList.add(new TestBean("amid",35)); 486 internalList.add(new TestBean("John",54)); 487 internalList.add(new TestBean("Aziz",26)); 488 internalList.add(new TestBean("Garcia",34)); 489 internalList.add(new TestBean("Toma",38)); 490 internalList.add(new TestBean("Sergio",50)); 491 altList = new ArrayList (); 492 altList.add(new TestBean("Cecile",36)); 493 altList.add(new TestBean("Elizabeth",49)); 494 altList.add(new TestBean("Denise",28)); 495 altList.add(new TestBean("Catherine",34)); 496 } 497 498 public int getNrOfCalls() { 499 return nrOfCalls; 500 } 501 502 public List loadList(Locale locale, Object filter) { 503 nrOfCalls++; 504 MockFilter mf = (MockFilter) filter; 505 List baseList = internalList; 506 if (null != mf.getExtendedInfo() && "F".equals(mf.getExtendedInfo())) { 507 baseList = altList; 508 } 509 if ("".equals(mf.getName()) && "".equals(mf.getAge())) { 510 return internalList; 511 } 512 List list = new ArrayList (); 513 Iterator it = baseList.iterator(); 514 while (it.hasNext()) { 515 TestBean b = (TestBean)it.next(); 516 String fName = mf.getName(); 517 String tmp = mf.getAge(); 518 int fAge = -1; 519 if (!"".equals(tmp)) { 520 fAge = Integer.parseInt(tmp); 521 } 522 if ( 523 ("".equals(fName) || b.getName().startsWith(fName)) && 524 (fAge < 0 || b.getAge() == fAge) && 525 locale == null 526 ) { 527 list.add(b); 528 } 529 } 530 return list; 531 } 532 } 533 534 } 535 | Popular Tags |