1 package org.apache.torque.util; 2 3 18 19 import org.apache.torque.BaseRuntimeTestCase; 20 import org.apache.torque.TorqueException; 21 import org.apache.torque.test.Author; 22 import org.apache.torque.test.AuthorPeer; 23 24 import java.util.List ; 25 26 32 public class LargeSelectTest extends BaseRuntimeTestCase 33 { 34 private static final int TEST_PAGE_SIZE = 9; 35 private static final int TEST_PAGES = 9; 36 private static final int TEST_ROWS = TEST_PAGE_SIZE * TEST_PAGES; 37 private static final String LARGE_SELECT_AUTHOR = "LargeSelectAuthor"; 38 private int firstAuthorId = -1; 39 40 private Criteria criteria; 41 42 45 public LargeSelectTest(String name) 46 { 47 super(name); 48 } 49 50 public void setUp() 51 { 52 super.setUp(); 53 54 tearDown(); 56 57 for (int i = 0; i < TEST_ROWS; i++) 59 { 60 Author author = new Author(); 61 author.setName(LARGE_SELECT_AUTHOR); 62 try 63 { 64 author.save(); 65 } 66 catch (Exception e) 67 { 68 fail("Cannot create test data for LargeSelectTest."); 69 } 70 if (-1 == firstAuthorId) 71 { 72 firstAuthorId = author.getAuthorId(); 73 } 74 } 75 criteria = new Criteria(); 77 criteria.add(AuthorPeer.NAME, LARGE_SELECT_AUTHOR); 78 } 79 80 public void tearDown() 81 { 82 criteria = new Criteria(); 84 criteria.add(AuthorPeer.NAME, LARGE_SELECT_AUTHOR); 85 try 86 { 87 AuthorPeer.doDelete(criteria); 88 } 89 catch (TorqueException e) 90 { 91 fail("Cannot delete test data for LargeSelectTest."); 92 } 93 criteria = null; 94 } 95 96 99 public void testCriteria() throws TorqueException 100 { 101 List result = null; 102 result = AuthorPeer.doSelect(criteria); 103 assertEquals("Selected rows", TEST_ROWS, result.size()); 104 } 105 106 109 public void testBadCriteria11() throws TorqueException 110 { 111 criteria.setLimit(1); 112 try 113 { 114 new LargeSelect(criteria, TEST_PAGE_SIZE, 115 "org.apache.torque.test.AuthorPeer"); 116 } 117 catch (IllegalArgumentException success) 118 { 119 } 121 } 122 123 126 public void testBadCriteria12() throws TorqueException 127 { 128 criteria.setOffset(1); 129 try 130 { 131 new LargeSelect(criteria, TEST_PAGE_SIZE, 132 "org.apache.torque.test.AuthorPeer"); 133 } 134 catch (IllegalArgumentException success) 135 { 136 } 138 } 139 140 143 public void testBadPageSize() throws TorqueException 144 { 145 try 146 { 147 new LargeSelect(criteria, 0, "org.apache.torque.test.AuthorPeer"); 148 } 149 catch (IllegalArgumentException success) 150 { 151 } 153 } 154 155 158 public void testBadMemoryLimit() throws TorqueException 159 { 160 try 161 { 162 new LargeSelect(criteria, TEST_PAGE_SIZE, 0, 163 "org.apache.torque.test.AuthorPeer"); 164 } 165 catch (IllegalArgumentException success) 166 { 167 } 169 } 170 171 174 public void testBadClass() 175 { 176 try 177 { 178 new LargeSelect(criteria, TEST_PAGE_SIZE, 179 "org.apache.torque.test.Author"); 180 } 181 catch (IllegalArgumentException success) 182 { 183 } 184 } 185 186 189 public void testStaticMethods() throws TorqueException 190 { 191 assertEquals("Memory page limit", 5, LargeSelect.getMemoryPageLimit()); 192 LargeSelect.setMemoryPageLimit(10); 193 assertEquals("Memory page limit", 10, LargeSelect.getMemoryPageLimit()); 194 LargeSelect.setMemoryPageLimit(LargeSelect.DEFAULT_MEMORY_LIMIT_PAGES); 195 assertEquals("Memory page limit", 5, LargeSelect.getMemoryPageLimit()); 196 197 assertEquals("More indicator", ">", LargeSelect.getMoreIndicator()); 198 String newMoreIndicator = "more than"; 199 LargeSelect.setMoreIndicator(newMoreIndicator); 200 assertEquals("More indicator", newMoreIndicator, 201 LargeSelect.getMoreIndicator()); 202 LargeSelect.setMoreIndicator(LargeSelect.DEFAULT_MORE_INDICATOR); 203 assertEquals("More indicator", ">", LargeSelect.getMoreIndicator()); 204 } 205 206 209 public void testLargeSelect() throws TorqueException 210 { 211 LargeSelect ls = new LargeSelect(criteria, TEST_PAGE_SIZE, 212 "org.apache.torque.test.AuthorPeer"); 213 214 assertEquals("Page size", TEST_PAGE_SIZE, ls.getPageSize()); 215 assertTrue("Paginated", ls.getPaginated()); 216 217 218 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); 220 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 221 assertTrue("Next results available", ls.getNextResultsAvailable()); 222 assertEquals("Current page size", 0, ls.getCurrentPageSize()); 223 assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); 224 assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); 225 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 226 assertEquals("Total pages", 0, ls.getTotalPages()); 227 assertEquals("Total records", 0, ls.getTotalRecords()); 228 assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); 229 assertEquals("Record progress text", "0 - 0 of > 0", ls.getRecordProgressText()); 230 231 List results = ls.getNextResults(); 232 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 234 assertEquals("Current page number", 1, ls.getCurrentPageNumber()); 235 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 236 assertTrue("Next results available", ls.getNextResultsAvailable()); 237 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 238 assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); 239 assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); 240 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 241 assertEquals("Total pages", 5, ls.getTotalPages()); 242 assertEquals("Total records", 45, ls.getTotalRecords()); 243 assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); 244 assertEquals("Record progress text", "1 - 9 of > 45", ls.getRecordProgressText()); 245 246 results = ls.getPage(5); 247 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 249 assertEquals("Current page number", 5, ls.getCurrentPageNumber()); 250 assertTrue("Previous results available", ls.getPreviousResultsAvailable()); 251 assertTrue("Next results available", ls.getNextResultsAvailable()); 252 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 253 assertEquals("First record for page", 37, ls.getFirstRecordNoForPage()); 254 assertEquals("Last record for page", 45, ls.getLastRecordNoForPage()); 255 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 256 assertEquals("Total pages", 5, ls.getTotalPages()); 257 assertEquals("Total records", 45, ls.getTotalRecords()); 258 assertEquals("Page progress text", "5 of > 5", ls.getPageProgressText()); 259 assertEquals("Record progress text", "37 - 45 of > 45", ls.getRecordProgressText()); 260 261 results = ls.getNextResults(); 262 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 264 assertEquals("Current page number", 6, ls.getCurrentPageNumber()); 265 assertTrue("Previous results available", ls.getPreviousResultsAvailable()); 266 assertTrue("Next results available", ls.getNextResultsAvailable()); 267 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 268 assertEquals("First record for page", 46, ls.getFirstRecordNoForPage()); 269 assertEquals("Last record for page", 54, ls.getLastRecordNoForPage()); 270 assertTrue("Totals finalised", ls.getTotalsFinalized()); 271 assertEquals("Total pages", TEST_PAGES, ls.getTotalPages()); 272 assertEquals("Total records", TEST_ROWS, ls.getTotalRecords()); 273 assertEquals("Page progress text", "6 of 9", ls.getPageProgressText()); 274 assertEquals("Record progress text", "46 - 54 of 81", ls.getRecordProgressText()); 275 276 results = ls.getNextResults(); 277 results = ls.getNextResults(); 279 results = ls.getNextResults(); 281 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 283 assertEquals("Current page number", 9, ls.getCurrentPageNumber()); 284 assertTrue("Previous results available", ls.getPreviousResultsAvailable()); 285 assertEquals("Next results available", false, ls.getNextResultsAvailable()); 286 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 287 assertEquals("First record for page", 73, ls.getFirstRecordNoForPage()); 288 assertEquals("Last record for page", 81, ls.getLastRecordNoForPage()); 289 assertTrue("Totals finalised", ls.getTotalsFinalized()); 290 assertEquals("Total pages", TEST_PAGES, ls.getTotalPages()); 291 assertEquals("Total records", TEST_ROWS, ls.getTotalRecords()); 292 assertEquals("Page progress text", "9 of 9", ls.getPageProgressText()); 293 assertEquals("Record progress text", "73 - 81 of 81", ls.getRecordProgressText()); 294 295 results = ls.getPage(2); 296 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 298 assertEquals("Current page number", 2, ls.getCurrentPageNumber()); 299 assertTrue("Previous results available", ls.getPreviousResultsAvailable()); 300 assertTrue("Next results available", ls.getNextResultsAvailable()); 301 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 302 assertEquals("First record for page", 10, ls.getFirstRecordNoForPage()); 303 assertEquals("Last record for page", 18, ls.getLastRecordNoForPage()); 304 assertTrue("Totals finalised", ls.getTotalsFinalized()); 305 assertEquals("Total pages", 9, ls.getTotalPages()); 306 assertEquals("Total records", 81, ls.getTotalRecords()); 307 assertEquals("Page progress text", "2 of 9", ls.getPageProgressText()); 308 assertEquals("Record progress text", "10 - 18 of 81", ls.getRecordProgressText()); 309 310 List sameResults = ls.getCurrentPageResults(); 311 assertSame("Same results", results, sameResults); 313 314 results = ls.getPreviousResults(); 315 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 317 assertEquals("Current page number", 1, ls.getCurrentPageNumber()); 318 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 319 assertTrue("Next results available", ls.getNextResultsAvailable()); 320 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 321 assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); 322 assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); 323 assertTrue("Totals finalised", ls.getTotalsFinalized()); 324 assertEquals("Total pages", 9, ls.getTotalPages()); 325 assertEquals("Total records", 81, ls.getTotalRecords()); 326 assertEquals("Page progress text", "1 of 9", ls.getPageProgressText()); 327 assertEquals("Record progress text", "1 - 9 of 81", ls.getRecordProgressText()); 328 } 329 330 333 public void testLargeSelectOneRow() throws Exception 334 { 335 criteria.add(AuthorPeer.AUTHOR_ID, firstAuthorId); 337 338 LargeSelect ls = new LargeSelect(criteria, TEST_PAGE_SIZE, 339 "org.apache.torque.test.AuthorPeer"); 340 341 List results = ls.getNextResults(); 343 assertTrue("Totals finalised", ls.getTotalsFinalized()); 344 assertEquals("Paginated", false, ls.getPaginated()); 345 assertEquals("results.size()", 1, results.size()); 346 assertEquals("Current page number", 1, ls.getCurrentPageNumber()); 347 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 348 assertEquals("Next results available", false, ls.getNextResultsAvailable()); 349 assertEquals("Current page size", 1, ls.getCurrentPageSize()); 350 assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); 351 assertEquals("Last record for page", 1, ls.getLastRecordNoForPage()); 352 assertEquals("Total pages", 1, ls.getTotalPages()); 353 assertEquals("Total records", 1, ls.getTotalRecords()); 354 assertEquals("Page progress text", "1 of 1", ls.getPageProgressText()); 355 assertEquals("Record progress text", "1 - 1 of 1", ls.getRecordProgressText()); 356 assertTrue("Results available", ls.hasResultsAvailable()); 357 } 358 359 362 public void testInvalidateResult() throws Exception 363 { 364 LargeSelect ls = new LargeSelect(criteria, TEST_PAGE_SIZE, 365 "org.apache.torque.test.AuthorPeer"); 366 367 assertEquals("Page size", TEST_PAGE_SIZE, ls.getPageSize()); 368 assertTrue("Paginated", ls.getPaginated()); 369 370 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); 372 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 373 assertTrue("Next results available", ls.getNextResultsAvailable()); 374 assertEquals("Current page size", 0, ls.getCurrentPageSize()); 375 assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); 376 assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); 377 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 378 assertEquals("Total pages", 0, ls.getTotalPages()); 379 assertEquals("Total records", 0, ls.getTotalRecords()); 380 assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); 381 assertEquals("Record progress text", "0 - 0 of > 0", ls.getRecordProgressText()); 382 383 List results = ls.getNextResults(); 384 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 386 assertEquals("Current page number", 1, ls.getCurrentPageNumber()); 387 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 388 assertTrue("Next results available", ls.getNextResultsAvailable()); 389 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 390 assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); 391 assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); 392 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 393 assertEquals("Total pages", 5, ls.getTotalPages()); 394 assertEquals("Total records", 45, ls.getTotalRecords()); 395 assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); 396 assertEquals("Record progress text", "1 - 9 of > 45", ls.getRecordProgressText()); 397 398 ls.invalidateResult(); 399 400 assertEquals("Page size", TEST_PAGE_SIZE, ls.getPageSize()); 401 assertTrue("Paginated", ls.getPaginated()); 402 403 assertEquals("Current page number", 0, ls.getCurrentPageNumber()); 405 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 406 assertTrue("Next results available", ls.getNextResultsAvailable()); 407 assertEquals("Current page size", 0, ls.getCurrentPageSize()); 408 assertEquals("First record for page", 0, ls.getFirstRecordNoForPage()); 409 assertEquals("Last record for page", 0, ls.getLastRecordNoForPage()); 410 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 411 assertEquals("Total pages", 0, ls.getTotalPages()); 412 assertEquals("Total records", 0, ls.getTotalRecords()); 413 assertEquals("Page progress text", "0 of > 0", ls.getPageProgressText()); 414 assertEquals("Record progress text", "0 - 0 of > 0", ls.getRecordProgressText()); 415 416 results = ls.getNextResults(); 417 assertEquals("results.size()", TEST_PAGE_SIZE, results.size()); 419 assertEquals("Current page number", 1, ls.getCurrentPageNumber()); 420 assertEquals("Previous results available", false, ls.getPreviousResultsAvailable()); 421 assertTrue("Next results available", ls.getNextResultsAvailable()); 422 assertEquals("Current page size", TEST_PAGE_SIZE, ls.getCurrentPageSize()); 423 assertEquals("First record for page", 1, ls.getFirstRecordNoForPage()); 424 assertEquals("Last record for page", 9, ls.getLastRecordNoForPage()); 425 assertEquals("Totals finalised", false, ls.getTotalsFinalized()); 426 assertEquals("Total pages", 5, ls.getTotalPages()); 427 assertEquals("Total records", 45, ls.getTotalRecords()); 428 assertEquals("Page progress text", "1 of > 5", ls.getPageProgressText()); 429 assertEquals("Record progress text", "1 - 9 of > 45", ls.getRecordProgressText()); 430 } 431 432 434 } 436 | Popular Tags |