KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > util > LargeSelectTest


1 package org.apache.torque.util;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

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 JavaDoc;
25
26 /**
27  * Test code for LargeSelect.
28  *
29  * @author <a HREF="mailto:seade@backstagetech.com.au">Scott Eade</a>
30  * @version $Id: LargeSelectTest.java,v 1.6 2005/03/19 06:25:27 tfischer Exp $
31  */

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 JavaDoc LARGE_SELECT_AUTHOR = "LargeSelectAuthor";
38     private int firstAuthorId = -1;
39
40     private Criteria criteria;
41
42     /**
43      * Creates a new instance.
44      */

45     public LargeSelectTest(String JavaDoc name)
46     {
47         super(name);
48     }
49
50     public void setUp()
51     {
52         super.setUp();
53
54         // Clean up any previous failures
55
tearDown();
56
57         // Create some test data
58
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 JavaDoc e)
67             {
68                 fail("Cannot create test data for LargeSelectTest.");
69             }
70             if (-1 == firstAuthorId)
71             {
72                 firstAuthorId = author.getAuthorId();
73             }
74         }
75         // Set up the standard criteria for the test.
76
criteria = new Criteria();
77         criteria.add(AuthorPeer.NAME, LARGE_SELECT_AUTHOR);
78     }
79
80     public void tearDown()
81     {
82         // Delete the test data
83
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     /**
97      * Test the criteria provides the correct number of rows.
98      */

99     public void testCriteria() throws TorqueException
100     {
101         List JavaDoc result = null;
102         result = AuthorPeer.doSelect(criteria);
103         assertEquals("Selected rows", TEST_ROWS, result.size());
104     }
105
106     /**
107      * Test an invalid criteria - includes a limit.
108      */

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 JavaDoc success)
118         {
119             // Do nothing
120
}
121     }
122
123     /**
124      * Test an invalid criteria - includes an offset.
125      */

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 JavaDoc success)
135         {
136             // Do nothing
137
}
138     }
139
140     /**
141      * Test an invalid page size.
142      */

143     public void testBadPageSize() throws TorqueException
144     {
145         try
146         {
147             new LargeSelect(criteria, 0, "org.apache.torque.test.AuthorPeer");
148         }
149         catch (IllegalArgumentException JavaDoc success)
150         {
151             // Do nothing
152
}
153     }
154
155     /**
156      * Test an invalid memory limit.
157      */

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 JavaDoc success)
166         {
167             // Do nothing
168
}
169     }
170
171     /**
172      * Test an invalid builder class (doesn't provide necessary methods).
173      */

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 JavaDoc success)
182         {
183         }
184     }
185
186     /**
187      * Test a couple of static methods.
188      */

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", "&gt;", LargeSelect.getMoreIndicator());
198         String JavaDoc 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", "&gt;", LargeSelect.getMoreIndicator());
204     }
205
206     /**
207      * Test a bunch of different methods when everything is set up correctly.
208      */

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         // Page 0
219
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 &gt; 0", ls.getPageProgressText());
229         assertEquals("Record progress text", "0 - 0 of &gt; 0", ls.getRecordProgressText());
230
231         List JavaDoc results = ls.getNextResults();
232         // Page 1
233
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 &gt; 5", ls.getPageProgressText());
244         assertEquals("Record progress text", "1 - 9 of &gt; 45", ls.getRecordProgressText());
245
246         results = ls.getPage(5);
247         // Page 5
248
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 &gt; 5", ls.getPageProgressText());
259         assertEquals("Record progress text", "37 - 45 of &gt; 45", ls.getRecordProgressText());
260
261         results = ls.getNextResults();
262         // Page 6
263
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         // Page 7
278
results = ls.getNextResults();
279         // Page 8
280
results = ls.getNextResults();
281         // Page 9
282
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         // Page 2
297
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 JavaDoc sameResults = ls.getCurrentPageResults();
311         // Page 2
312
assertSame("Same results", results, sameResults);
313
314         results = ls.getPreviousResults();
315         // Page 1
316
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     /**
331      * Test what happens when only one row is returned.
332      */

333     public void testLargeSelectOneRow() throws Exception JavaDoc
334     {
335         // Alter criteria to retrieve only one row
336
criteria.add(AuthorPeer.AUTHOR_ID, firstAuthorId);
337
338         LargeSelect ls = new LargeSelect(criteria, TEST_PAGE_SIZE,
339                 "org.apache.torque.test.AuthorPeer");
340
341         // Page 1
342
List JavaDoc 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     /**
360      * Test invalidateResult()
361      */

362     public void testInvalidateResult() throws Exception JavaDoc
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         // Page 0
371
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 &gt; 0", ls.getPageProgressText());
381         assertEquals("Record progress text", "0 - 0 of &gt; 0", ls.getRecordProgressText());
382
383         List JavaDoc results = ls.getNextResults();
384         // Page 1
385
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 &gt; 5", ls.getPageProgressText());
396         assertEquals("Record progress text", "1 - 9 of &gt; 45", ls.getRecordProgressText());
397
398         ls.invalidateResult();
399
400         assertEquals("Page size", TEST_PAGE_SIZE, ls.getPageSize());
401         assertTrue("Paginated", ls.getPaginated());
402
403         // Page 0
404
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 &gt; 0", ls.getPageProgressText());
414         assertEquals("Record progress text", "0 - 0 of &gt; 0", ls.getRecordProgressText());
415
416         results = ls.getNextResults();
417         // Page 1
418
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 &gt; 5", ls.getPageProgressText());
429         assertEquals("Record progress text", "1 - 9 of &gt; 45", ls.getRecordProgressText());
430     }
431
432     // todo Add a test for getPaginated() - was previously returning false when 6 results and pageSize 5
433

434     // todo Add test for parameter storage
435
}
436
Popular Tags