KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > listdata > persist > db > ListDatabaseFactoryTest


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: ListDatabaseFactoryTest.java,v 1.33 2007/02/01 07:26:30 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.patterns.listdata.persist.db;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Random JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32 import org.opensubsystems.core.data.BasicDataObject;
33 import org.opensubsystems.core.data.DataObject;
34 import org.opensubsystems.core.error.OSSException;
35 import org.opensubsystems.core.persist.db.DatabaseTest;
36 import org.opensubsystems.core.util.CallContext;
37 import org.opensubsystems.core.util.Config;
38 import org.opensubsystems.core.util.DateUtils;
39 import org.opensubsystems.core.util.Log;
40 import org.opensubsystems.core.util.MyTimer;
41 import org.opensubsystems.core.util.NumberUtils;
42 import org.opensubsystems.core.util.StringUtils;
43 import org.opensubsystems.patterns.listdata.data.ListDefinition;
44 import org.opensubsystems.patterns.listdata.data.ListOptions;
45 import org.opensubsystems.patterns.listdata.data.SimpleRule;
46 import org.opensubsystems.patterns.listdata.persist.ListFactory;
47
48 /**
49  * Base class for classes that test functionality of classes that implement
50  * ListDatabaseFactory interface and ListDatabaseFactoryImpl class. This class
51  * implements tests for all list related functionality provided by the mentioned
52  * interface. Once you derive your test from this class, just add test that
53  * tests the functionality you have implemented in your derived database factory
54  * class, the base class functionality will be tested automatically.
55  *
56  * @version $Id: ListDatabaseFactoryTest.java,v 1.33 2007/02/01 07:26:30 bastafidli Exp $
57  * @author Julo Legeny
58  * @code.reviewer Miro Halas
59  * @code.reviewed 1.28 2007/01/07 06:15:17 bastafidli
60  */

61 public abstract class ListDatabaseFactoryTest extends DatabaseTest
62 {
63    // Configuration settings ///////////////////////////////////////////////////
64

65    /**
66     * Configuration setting specifying the total number of items to use for list
67     * performance testing. This many randomly generated data items will be loaded
68     * into the database and then list operations and searches will be performed
69     * with this data set.
70     */

71    private static final String JavaDoc LOADTEST_LIST_SIZE = "oss.loadtest.list.size";
72
73    /**
74     * Configuration setting specifying the page size to use for list performance
75     * testing. When testing the list performance, only the specified number of
76     * items will be accessed at the same time.
77     */

78    private static final String JavaDoc LOADTEST_PAGE_SIZE = "oss.loadtest.page.size";
79
80    /**
81     * Configuration setting specifying the time threshold in milliseconds to
82     * still consider list operation performance acceptable. If any list operation
83     * exceeds this value then the performance will be considered too bad and the
84     * test will fail.
85     */

86    private static final String JavaDoc LOADTEST_LOADTIME = "oss.loadtest.loadtime";
87
88    // Constants ////////////////////////////////////////////////////////////////
89

90    /**
91     * How many items to use for load testing. This is the maximal amount of
92     * items to exist in single domain. When running multi domain tests the total
93     * amount of data is usually larger. By default make this small by default
94     * for casual use.
95     */

96    public static final int DEFAULT_LOADTEST_LIST_SIZE = 100;
97    
98    /**
99     * What page size should be used for load testing. This number specifies
100     * how many items to retrieve at once.
101     */

102    public static final int DEFAULT_LOADTEST_PAGE_SIZE = 20;
103    
104    /**
105     * What time to retrieve page should be considered too slow and will cause
106     * given test to fail? 4 sec to load one page of list is considered slow.
107     */

108    public static final int DEFAULT_LOADTEST_LOADTIME = 4000;
109    
110    // Attributes ///////////////////////////////////////////////////////////////
111

112    /**
113     * Utility class to implement helper methods for list functionality.
114     */

115    protected ListDatabaseTestUtils m_listTestUtils;
116
117    // Cached values ////////////////////////////////////////////////////////////
118

119    /**
120     * Logger for this class
121     */

122    private static Logger JavaDoc s_logger = Log.getInstance(ListDatabaseFactoryTest.class);
123
124    /**
125     * Value telling us list size for loadtests
126     */

127    protected static int s_iLoadtestListSize;
128
129    /**
130     * Value telling us page size for loadtests
131     */

132    protected static int s_iLoadtestPageSize;
133
134    /**
135     * Value telling us predetermined time the data in the list should be retrieved in
136     */

137    protected static int s_iLoadtestLoadTime;
138
139    // Constructors /////////////////////////////////////////////////////////////
140

141    /**
142     * Static initializer
143     */

144    static
145    {
146       Properties JavaDoc prpSettings;
147       
148       prpSettings = Config.getInstance().getPropertiesSafely();
149       s_iLoadtestListSize = Config.getIntPropertyInRange(
150                                       prpSettings,
151                                       LOADTEST_LIST_SIZE,
152                                       DEFAULT_LOADTEST_LIST_SIZE,
153                                       "Total number of items to use for list"
154                                       + " performance testing",
155                                       1,
156                                       Integer.MAX_VALUE);
157       
158
159       s_iLoadtestPageSize = Config.getIntPropertyInRange(
160                                       prpSettings,
161                                       LOADTEST_PAGE_SIZE,
162                                       DEFAULT_LOADTEST_PAGE_SIZE,
163                                       "Page size to use for list performance testing",
164                                       1, Integer.MAX_VALUE);
165       
166       s_iLoadtestLoadTime = Config.getIntPropertyInRange(
167                                       prpSettings,
168                                       LOADTEST_LOADTIME,
169                                       DEFAULT_LOADTEST_LOADTIME,
170                                       "Time threshold to still consider performance"
171                                       + " acceptable",
172                                       1,
173                                       Integer.MAX_VALUE);
174    }
175
176    /**
177     * Constructor with data object specific parameters
178     *
179     * @param strTestName - name of the test
180     * @param listTestUtils - utilities used during test
181     */

182    public ListDatabaseFactoryTest(
183       String JavaDoc strTestName,
184       ListDatabaseTestUtils listTestUtils
185    )
186    {
187       super(strTestName);
188       
189       m_listTestUtils = listTestUtils;
190    }
191
192    // Test setup ///////////////////////////////////////////////////////////////
193

194    /**
195     * Set up environment for the test case.
196     *
197     * @throws Exception - an error has occured during setting up test
198     */

199    protected void setUp(
200    ) throws Exception JavaDoc
201    {
202       super.setUp();
203    }
204
205    /**
206     * Restore original environment after the test case.
207     *
208     * @throws Exception - an error has occured during tearing down up test
209     */

210
211    protected void tearDown() throws Exception JavaDoc
212    {
213       super.tearDown();
214    }
215
216    // Tests ////////////////////////////////////////////////////////////////////
217

218    /**
219     * Some controllers needs to know the code for the ID column so they can
220     * operate properly. For example getRightsForCurrentUser method in
221     * AuthorizationControllerImpl class in security package has hardcoded
222     * assumption that the code for the ID column is the value for datatype + 1.
223     * This test will test that assumption This test should be really in
224     * ListDatabaseSchemaTest class but that would require developer to derive
225     * two classes instead of one and therefore we will bundle it with tests for
226     * factory.
227     *
228     * @throws Exception - an error has occured
229     */

230    public void testGetColumnsUsingId(
231    ) throws Exception JavaDoc
232    {
233       ListDatabaseSchema listSchema;
234       int iDataType;
235       
236       listSchema = m_listTestUtils.getListDatabaseSchema();
237       iDataType = m_listTestUtils.getListFactory().getDataType();
238       
239       // Now generate benchmark SQL for the columns from default filter
240
String JavaDoc strSQL;
241
242       // Here we expect that if we ask for column with code 1 greater that datatype
243
// we get an ID column
244
strSQL = listSchema.getColumns(false, new int[] {iDataType + 1}, null,
245                                      null, null).toString();
246       
247       assertEquals("The column name for the code which should identify ID column "
248                    + " is not ID", "ID", strSQL.toUpperCase());
249    }
250
251    /**
252     * Test implementation of the getColumns method in the database schema. This
253     * test should be really in ListDatabaseSchemaTest class but that would
254     * require developer to derive two classes instead of one and therefore we
255     * will bundle it with tests for factory.
256     *
257     * @throws Exception - an error has occured
258     */

259    public void testGetColumns(
260    ) throws Exception JavaDoc
261    {
262       // First test the columns in the default list definition to help us narrow
263
// down a problem if there is any
264
doTestGetColumns(m_listTestUtils.getListFactory()
265                         .getDefaultListDefinition().getShowColumnCodes());
266       // Now test all columns, this will be superset of the above columns
267
doTestGetColumns(m_listTestUtils.getAllColumnCodes());
268    }
269    
270    /**
271     * Test of get(ListOptions) method of the ListFactory accessing data in
272     * database.
273     *
274     * This test use test util to do various tests
275     * @throws Exception - an error has occured during test
276     */

277    public void testGet(
278    ) throws Exception JavaDoc
279    {
280       Object JavaDoc[] data = null;
281       
282       try
283       {
284          Object JavaDoc parent;
285          int[] columns = new int[] {m_listTestUtils.getColumnOneCode(),
286                                      m_listTestUtils.getColumnTwoCode(),
287                                     };
288                                        
289          data = m_listTestUtils.insertTestData(m_transaction);
290          parent = data[0];
291          
292          // Test retrieval of various portions of the list of elements we have
293
// created and inserted
294

295          // First try it just with the two columns we need
296
doTestGet(parent, 5, 2, columns);
297          doTestGet(parent, 10, 12, columns);
298          doTestGet(parent, 30, 22, columns);
299
300          // Now try it with all the columns
301
doTestGet(parent, 5, 2, m_listTestUtils.getAllColumnCodes());
302          doTestGet(parent, 10, 12, m_listTestUtils.getAllColumnCodes());
303          doTestGet(parent, 30, 22, m_listTestUtils.getAllColumnCodes());
304       }
305       finally
306       {
307          m_listTestUtils.deleteTestData(m_transaction, data);
308       }
309    }
310
311    /**
312     * Test loading pages by browsing through the whole list page by page from
313     * the first to the last therefore retrieving every page of the list in
314     * sequential order.
315     *
316     * @throws Exception - an error has occured
317     */

318    public void testLoadPagesFromFirstToLast(
319    ) throws Exception JavaDoc
320    {
321       Object JavaDoc[] data = null;
322       
323       try
324       {
325          data = m_listTestUtils.insertLoadTestData(m_transaction,
326                                                    s_iLoadtestListSize);
327          doTestLoadPagesFromFirstToLast(data[0], "(single domain)");
328       }
329       finally
330       {
331          m_listTestUtils.deleteLoadTestData(m_transaction, data);
332       }
333    }
334
335    /**
336     * Test loading pages by browsing through the whole list page by page from
337     * the last to the first therefore retrieving every page of the list in
338     * sequential order and testing the retrieved data to make sure that we have
339     * retrieved the correct data.
340     *
341     * @throws Exception - an error has occured
342     */

343    public void testLoadPagesFromLastToFirst(
344    ) throws Exception JavaDoc
345    {
346       Object JavaDoc[] data = null;
347       
348       try
349       {
350          boolean bOrderDirectionAsc = true;
351          
352          data = m_listTestUtils.insertLoadTestData(m_transaction,
353                                                    s_iLoadtestListSize);
354          
355          // By specifying true we will be testing the actual retrieved values
356
// to make sure we have retrieved correct data
357
doTestLoadPagesFromLastToFirst(data[0],
358                                         "(single domain)",
359                                         m_listTestUtils.getColumnOneCode(),
360                                         bOrderDirectionAsc,
361                                         true);
362       }
363       finally
364       {
365          m_listTestUtils.deleteLoadTestData(m_transaction, data);
366       }
367    }
368
369    /**
370     * Test loading pages by browsing through the whole list page by page from
371     * the last to the first therefore retrieving every page of the list in
372     * sequential order. This tests just ensures that the database can use the
373     * specified columns for sorting and doesnt actually tests if the retrieved
374     * data are the correct data.
375     *
376     * @throws Exception - an error has occured
377     */

378    public void testLoadPagesFromLastToFirstMoreSortableColumns(
379    ) throws Exception JavaDoc
380    {
381       Object JavaDoc[] data = null;
382       
383       try
384       {
385          int iIndex;
386          boolean bOrderDirectionAsc;
387          ListFactory factory = m_listTestUtils.getListFactory();
388          ListDatabaseSchema schema = m_listTestUtils.getListDatabaseSchema();
389          int iDataType = factory.getDataType();
390          Map JavaDoc mapSortableColumns = schema.getSortableColumns();
391          // Get all sortable columns that can be used for this data type
392
int[] arrSortableColumns = (int[])mapSortableColumns.get(
393                                                new Integer JavaDoc(iDataType));
394          
395          data = m_listTestUtils.insertLoadTestData(m_transaction,
396                                                    s_iLoadtestListSize);
397          
398          // Test browsing through data for each sortable column to make sure
399
// that the data can be actually sorted using that column
400
for (iIndex = 0; iIndex < arrSortableColumns.length; iIndex++)
401          {
402             if (iIndex % 2 != 0)
403             {
404                bOrderDirectionAsc = false;
405             }
406             else
407             {
408                bOrderDirectionAsc = true;
409             }
410             doTestLoadPagesFromLastToFirst(data[0],
411                                            "(single domain)",
412                                            arrSortableColumns[iIndex],
413                                            bOrderDirectionAsc,
414                                            false);
415          }
416       }
417       finally
418       {
419          m_listTestUtils.deleteLoadTestData(m_transaction, data);
420       }
421    }
422
423    /**
424     * Test loading pages from the list by randomly selecting a page and then
425     * retrieving it. Every page in the data set will be retrieved in random
426     * order and performance of the data retrieval will be monitored.
427     *
428     * @throws Exception - an error has occured
429     */

430    public void testLoadPagesRandom(
431    ) throws Exception JavaDoc
432    {
433       Object JavaDoc[] data = null;
434       
435       try
436       {
437          data = m_listTestUtils.insertLoadTestData(m_transaction,
438                                                       s_iLoadtestListSize);
439          
440          doTestLoadPagesRandom(data[0], "(single domain)");
441       }
442       finally
443       {
444          m_listTestUtils.deleteLoadTestData(m_transaction, data);
445       }
446    }
447
448    // Helper methods ///////////////////////////////////////////////////////////
449

450    /**
451     * Helper method to test getColumns method of ListDatabaseSchema.
452     *
453     * @param columns - columns to test
454     * @throws OSSException - an error has occured
455     */

456    protected void doTestGetColumns(
457       int[] columns
458    ) throws OSSException
459    {
460       ListDatabaseSchema listSchema;
461       String JavaDoc strDefaultSQL;
462       String JavaDoc strSpecificSQL;
463       String JavaDoc strFixedSQL;
464       int iIndex;
465       int columnLength = columns.length;
466       String JavaDoc[] prefixes = new String JavaDoc[columnLength];
467       String JavaDoc[] postfixes = new String JavaDoc[columnLength];
468       Iterator JavaDoc def;
469       Iterator JavaDoc specific;
470       Iterator JavaDoc fixed;
471       String JavaDoc strColumn;
472       String JavaDoc strDefault;
473       String JavaDoc strSpecific;
474       String JavaDoc strFixed;
475       Collection JavaDoc colDefault;
476       Collection JavaDoc colSpecific;
477       Collection JavaDoc colFixed;
478       
479       listSchema = m_listTestUtils.getListDatabaseSchema();
480       
481       for (iIndex = 0; iIndex < columns.length; iIndex++)
482       {
483          strColumn = listSchema.getColumns(false, new int[] {columns[iIndex]},
484                                            null, null, null).toString();
485          assertTrue("getColumns didnt return column name for column code "
486                     + columns[iIndex], strColumn.trim().length() > 0);
487       }
488       
489       // Now generate benchmark SQL for the specified columns
490
strDefaultSQL = listSchema.getColumns(false, columns, null,
491                                             null, null).toString();
492       // Now try to generate specific SQL (it should have table name) in front
493
// of each column
494
strSpecificSQL = listSchema.getColumns(true, columns, null,
495                                              null, null).toString();
496       
497       assertFalse("getColumns in the " + listSchema.getClass().getName() +
498                   " is not implemented correctly since the specific flag has no effect"
499                   + " nonspecific=" + strDefaultSQL + " specific=" + strSpecificSQL,
500                   strDefaultSQL.equals(strSpecificSQL));
501       // And now verify that the columns are correctly pre and postfixed
502
colDefault = StringUtils.parseStringToCollection(
503                                   strDefaultSQL, ",", true,
504                                   StringUtils.CASE_ORIGINAL, null);
505       colSpecific = StringUtils.parseStringToCollection(
506                                    strSpecificSQL, ",", true,
507                                    StringUtils.CASE_ORIGINAL, null);
508       
509       assertEquals("Default SQL doesn't have the correct number of elements",
510                    columnLength, colSpecific.size());
511       assertEquals("Default SQL and specific SQL don't have the same" +
512                    " number of elements", colDefault.size(), colSpecific.size());
513       
514       // Now go through every element and verify that it is correctly specified
515
for (specific = colSpecific.iterator(), def = colDefault.iterator(),
516            iIndex = 0; specific.hasNext() && def.hasNext(); iIndex++)
517       {
518          strSpecific = (String JavaDoc)specific.next();
519          strDefault = (String JavaDoc)def.next();
520          assertEquals("Column was incorrectly specified",
521                       strSpecific.substring(strSpecific.lastIndexOf('.') + 1),
522                       strDefault);
523       }
524       
525       // Now try to generate specific and prefix and postfix
526
for (iIndex = 0; iIndex < columnLength; iIndex++)
527       {
528          prefixes[iIndex] = "prefix" + iIndex;
529          postfixes[iIndex] = "postfix" + iIndex;
530       }
531       
532       // Now generate the prefix/postfix SQL
533
strFixedSQL = listSchema.getColumns(true, columns, prefixes,
534                                                    postfixes, null).toString();
535       // And now verify that the columns are correctly pre and postfixed
536
colFixed = StringUtils.parseStringToCollection(
537                                 strFixedSQL, ",", true,
538                                 StringUtils.CASE_ORIGINAL, null);
539       
540       assertEquals("Specific SQL and prefixes/postfixed SQL don't have the same" +
541                    " number of elements", colSpecific.size(), colFixed.size());
542       assertEquals("Specific SQL and prefixes/postfixed SQL don't have the correct" +
543                    " number of elements", columnLength, colFixed.size());
544       
545       // Now go through every element and verify that it is correctly prefixed
546
// and postfixed
547
for (specific = colSpecific.iterator(), fixed = colFixed.iterator(),
548            iIndex = 0; specific.hasNext() && fixed.hasNext(); iIndex++)
549       {
550          strSpecific = (String JavaDoc)specific.next();
551          strFixed = (String JavaDoc)fixed.next();
552          assertEquals("Column was incorrectly prefixed and postfixed",
553                       prefixes[iIndex] + strSpecific + postfixes[iIndex], strFixed);
554       }
555    }
556    
557    /**
558     * Helper method to test get(ListOptions) method of List using the modified
559     * default list options.
560     *
561     * @param parent - parent object to which the items in the list belongs to
562     * it can be null if those items do not have any parent
563     * @param pageSize - how many items to retrieve at once
564     * @param beginPos - what should be the first parameter retrieved
565     * @param columns - columns to retrieve
566     * @throws Exception - an error has occured
567     */

568    protected void doTestGet(
569       Object JavaDoc parent,
570       int pageSize,
571       int beginPos,
572       int[] columns
573    ) throws Exception JavaDoc
574    {
575       ListFactory factory;
576       ListOptions options;
577       List JavaDoc lstTestData;
578       String JavaDoc[] arrExpectedData;
579       int helpCounter;
580       int assertCounter;
581       
582       factory = m_listTestUtils.getListFactory();
583       options = m_listTestUtils.getDefaultListOptions(parent);
584       arrExpectedData = m_listTestUtils.getExpectedResultListColumnOne();
585       
586       options.setPageSize(pageSize);
587       options.setActualListSize(30);
588       options.setBeginPosition(beginPos);
589       options.setEndPosition(9);
590       options.setActualPage(13);
591       options.setShowColumnCodes(new int[] {m_listTestUtils.getColumnOneCode(),
592                                             m_listTestUtils.getColumnTwoCode(),
593                                            });
594       options.setSecurityFilter(SimpleRule.ALL_DATA);
595       
596       lstTestData = factory.get(options);
597       //printDebug(lstTestData);
598
//printDebug(options);
599
assertNotNull("Selected data object list is null", lstTestData);
600       if ((arrExpectedData.length - options.getBeginPosition() + 1) < pageSize)
601       {
602          assertEquals("Number of selected items is incorrect",
603                       (arrExpectedData.length - options.getBeginPosition() + 1),
604                       lstTestData.size());
605       }
606       else
607       {
608          assertEquals("Number of selected items is incorrect",
609                       pageSize, lstTestData.size());
610       }
611       assertEquals("PageSize should have not changed", pageSize,
612                    options.getPageSize());
613       assertEquals("ShowColumnCodes should have not changed", 2,
614                    options.getShowColumnCodes().length);
615       assertEquals("ShowColumnCodes should have not changed",
616                    m_listTestUtils.getColumnOneCode(),
617                    options.getShowColumnCodes()[0]);
618       assertEquals("ShowColumnCodes should have not changed",
619                    m_listTestUtils.getColumnTwoCode(),
620                    options.getShowColumnCodes()[1]);
621       assertEquals("ActualListSize is not set correctly",
622                    arrExpectedData.length, options.getActualListSize());
623       assertTrue("BeginPosition is not set correctly",
624                  (beginPos == options.getBeginPosition()
625                  || (beginPos > options.getBeginPosition())));
626       if (pageSize >= arrExpectedData.length)
627       {
628          assertTrue("BeginPosition is not set correctly",
629                    options.getBeginPosition() == 1);
630       }
631       assertTrue("EndPosition is not set correctly",
632                 ((options.getBeginPosition() + pageSize - 1)
633                  == options.getEndPosition()
634                 || (options.getEndPosition() == arrExpectedData.length)));
635       assertEquals("ActualPage is not set correctly",
636                    ((options.getBeginPosition() + pageSize - 2) / pageSize) + 1,
637                    options.getActualPage());
638                 
639       for (assertCounter = (options.getBeginPosition() - 1), helpCounter = 0;
640            assertCounter < options.getEndPosition();
641            assertCounter++, helpCounter++)
642       {
643          // Compare the data we are expecting with those that were retrieved
644
assertEquals("The retrieved element #" + helpCounter +
645                       " is not correct",
646                       arrExpectedData[assertCounter],
647                       m_listTestUtils.getColumnOneValue(
648                          (DataObject)lstTestData.get(helpCounter)
649                       ));
650       }
651    }
652    
653    /**
654     * Helper method to test get method of ListFactory. This is used for load
655     * tests that test different retrieval strategies of the list pages.
656     *
657     * @param parent - parent object, to which items in the list should belong to
658     * @param iListSize - the total list size
659     * @param iPageSize - the page size to retrieve
660     * @param iBeginPos - where to retrieve the page
661     * @param bCountLastPageSize - flag specifying if the retrieved page is the
662     * last and its size needs to be computed
663     * @param arrColumns - columns to retrieve
664     * @param iOrderColumn - column code that will be used for sorting (in clause
665     * order by). Use -1 if no sorting should be done.
666     * @param bOrderDirectionAsc - true, if the list should be sorted in
667     * ascending order
668     * - false, if the list should be sorted in
669     * descending order
670     * @param bTestDataValues - true, if the test will actually test the
671     * retrieved values (1st and last record)
672     * - false, if the test will not test the
673     * retrieved values and just retrieve the data
674     * @return long - time duration how long it took to load the page
675     * @throws Exception - an error has occured
676     */

677    protected long doLoadTestGet(
678       Object JavaDoc parent,
679       int iListSize,
680       int iPageSize,
681       int iBeginPos,
682       boolean bCountLastPageSize,
683       int[] arrColumns,
684       int iOrderColumn,
685       boolean bOrderDirectionAsc,
686       boolean bTestDataValues
687    ) throws Exception JavaDoc
688    {
689       MyTimer timer = new MyTimer();
690       long lDataSelectDuration;
691       String JavaDoc strDataSelectDuration = "";
692       int iActualPage = 0;
693       String JavaDoc strTestValue = "";
694       int iCurrentPageSize = iPageSize;
695       ListFactory factory = m_listTestUtils.getListFactory();
696       ListOptions options = m_listTestUtils.getDefaultListOptions(parent);
697
698       // Get number of items in the current page
699
if (bCountLastPageSize)
700       {
701          iCurrentPageSize = iListSize % iPageSize;
702       }
703       
704       // If we were asked to sort the list using different column then the
705
// default one, we have to change it within the list options togdther with
706
// the order direction (asc/desc).
707
if (iOrderColumn != -1)
708       {
709          options.setOrdering(new int[] {iOrderColumn},
710             bOrderDirectionAsc ? ListDefinition.ORDER_ASCENDING_ARRAY
711                                  : ListDefinition.ORDER_DESCENDING_ARRAY);
712       }
713       options.setPageSize(iPageSize);
714       options.setActualListSize(iListSize);
715       options.setBeginPosition(iBeginPos);
716       options.setEndPosition(iBeginPos + iPageSize);
717       // Set up actual page
718
if ((iBeginPos % iPageSize) == 0)
719       {
720          iActualPage = iBeginPos / iPageSize;
721       }
722       else
723       {
724          iActualPage = (iBeginPos / iPageSize) + 1;
725       }
726       options.setActualPage(iActualPage);
727       options.setShowColumnCodes(new int[] {m_listTestUtils.getColumnOneCode(),
728                                             m_listTestUtils.getColumnTwoCode(),
729                                            });
730       options.setSecurityFilter(SimpleRule.ALL_DATA);
731       
732       try
733       {
734          timer.reset();
735          List JavaDoc lstTestData = factory.get(options);
736          lDataSelectDuration = timer.getDuration();
737          strDataSelectDuration = timer.toString();
738          // prepare statistic
739
/*
740          log.info("Total duration for selecting " + lstTestData.size()
741                   + " items at position " + wListOptions.getBeginPosition() +
742                   " from " + wListOptions.getActualListSize()
743                   + " items is " + strDataSelectDuration);
744          s_logger.info("-----------------------------------------------");
745          */

746          //printDebug(lstTestData);
747
//printDebug(options);
748

749          assertNotNull("Selected data object list is null", lstTestData);
750          assertEquals("Number of selected data objects is incorrect",
751                       iCurrentPageSize, lstTestData.size());
752          assertEquals("Page size should have not changed", iPageSize,
753                       options.getPageSize());
754          assertTrue("ShowColumnCodes list should have not changed",
755                      m_listTestUtils.getColumnOneCode() == options.getShowColumnCodes()[0]
756                      && m_listTestUtils.getColumnTwoCode() == options.getShowColumnCodes()[1]);
757          assertEquals("ActualListSize is incorrectly set",
758                       iListSize, options.getActualListSize());
759          assertTrue("BeginPosition is incorrectly set",
760                     (iBeginPos == options.getBeginPosition()
761                     || (iBeginPos > options.getBeginPosition())));
762          assertTrue("EndPosition is incorrectly set",
763                     ((options.getBeginPosition() + iPageSize - 1)
764                        == options.getEndPosition()
765                     || (options.getEndPosition() == iCurrentPageSize)));
766          assertEquals("ActualPage is incorrectly set",
767                       ((options.getBeginPosition() + iPageSize - 2)
768                          / iPageSize) + 1,
769                       options.getActualPage());
770
771          if (bTestDataValues)
772          {
773             if (m_listTestUtils.getListDatabaseSchema().isInDomain())
774             {
775                assertEquals("The domain id of item in Data Object List is not correct",
776                             CallContext.getInstance().getCurrentDomainId(),
777                             ((BasicDataObject)lstTestData.get(0)).getDomainId());
778             }
779             
780             // set tested value
781
if (bOrderDirectionAsc)
782             {
783                strTestValue = "test_1_" + NumberUtils.getDigitNumberString(
784                               options.getBeginPosition(), 8);
785             }
786             else
787             {
788                // for descending order there will be retrieved another values
789
strTestValue = "test_1_" + NumberUtils.getDigitNumberString(
790                               (s_iLoadtestListSize - options.getBeginPosition()) + 1, 8);
791             }
792             
793             // For descending order there will be not processed equalsing of
794
// values, because some order columns have values that are not
795
// appropriate for sorting
796
if (bOrderDirectionAsc)
797             {
798                // test first retrieved record value from the list
799
assertEquals("The 1st value in Data Object List is not correct",
800                                strTestValue,
801                                m_listTestUtils.getColumnOneValue(
802                                   (DataObject)lstTestData.get(0)
803                                ));
804             }
805          }
806
807          assertTrue("A poor perforance of data loading was acountered by" +
808                     " loading page with " + iCurrentPageSize +
809                     " items from list with " + iListSize +
810                     " items at position " + iBeginPos + " (total" +
811                     " duration of the SQL select is " + strDataSelectDuration +
812                     ")",
813                     lDataSelectDuration <= s_iLoadtestLoadTime);
814       }
815       catch (Throwable JavaDoc thr)
816       {
817          throw new Exception JavaDoc(thr);
818       }
819       
820       return lDataSelectDuration;
821    }
822
823    /**
824     * Load test for data performance. Browse through the whole list page by page
825     * from the first to the last therefore retrieving every page of the list in
826     * sequential order.
827     *
828     * @param parent - parent object containing all data using which to execute
829     * the test
830     * @param strExtraText - text to use in reporting of performance
831     * @throws Exception - an error has occured
832     */

833    protected void doTestLoadPagesFromFirstToLast(
834       Object JavaDoc parent,
835       String JavaDoc strExtraText
836    ) throws Exception JavaDoc
837    {
838       int iIndex;
839       int iTotalPages = 0;
840       boolean isLastPage = false;
841       boolean bCountLastPageSize = false;
842       long lSumDataSelectDurations = 0;
843       long lActualDataSelectDuration;
844       long[][] lMaxDataSelectDuration = {{0, 0}};
845       long[][] lMinDataSelectDuration = {{Long.MAX_VALUE, 0}};
846       int[] arrAllColumnCodes = m_listTestUtils.getAllColumnCodes();
847
848       // Determine the total number of pages that we will iterate through
849
if ((s_iLoadtestListSize % s_iLoadtestPageSize) > 0)
850       {
851          iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize) + 1;
852          isLastPage = true;
853       }
854       else
855       {
856          iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize);
857       }
858
859       // -----------------------------------------------------------------------
860
// Load each page in the loop from first to last page
861
// -----------------------------------------------------------------------
862
for (iIndex = 0; iIndex < iTotalPages; iIndex++)
863       {
864          if ((iIndex == iTotalPages - 1) && (isLastPage))
865          {
866             bCountLastPageSize = true;
867          }
868          
869          // Now try load one specified page with all columns
870
lActualDataSelectDuration = doLoadTestGet(
871                                         parent,
872                                         s_iLoadtestListSize,
873                                         s_iLoadtestPageSize,
874                                        (iIndex * s_iLoadtestPageSize) + 1,
875                                         bCountLastPageSize,
876                                         arrAllColumnCodes,
877                                         // there will be not used all possible sorted columns
878
-1,
879                                         true,
880                                         true);
881          
882          lSumDataSelectDurations += lActualDataSelectDuration;
883
884          // If this was the fastest retrieved page
885
if (lActualDataSelectDuration < lMinDataSelectDuration[0][0])
886          {
887             // set minimal time duration
888
lMinDataSelectDuration[0][0] = lActualDataSelectDuration;
889             // set page number of minimal time
890
lMinDataSelectDuration[0][1] = iIndex + 1;
891          }
892
893          // If this was the slowest retrieved page
894
if (lActualDataSelectDuration > lMaxDataSelectDuration[0][0])
895          {
896             // set maximal time duration
897
lMaxDataSelectDuration[0][0] = lActualDataSelectDuration;
898             // set page number of maximal time
899
lMaxDataSelectDuration[0][1] = iIndex + 1;
900          }
901       }
902       
903       // Compute the average duration to retrieve page
904
lActualDataSelectDuration = lSumDataSelectDurations / iTotalPages;
905
906       s_logger.info("Total retrieved pages from first to last " + strExtraText +
907                     " : " + iTotalPages +
908                     ", number of items for page: " + s_iLoadtestPageSize + "\n" +
909                     "The best performance has page "
910                     + lMinDataSelectDuration[0][1] +
911                     " with duration "
912                     + DateUtils.getStringTime(lMinDataSelectDuration[0][0]) +
913                     ". \n" +
914                     "The worst performance has page "
915                     + lMaxDataSelectDuration[0][1] +
916                     " with duration "
917                     + DateUtils.getStringTime(lMaxDataSelectDuration[0][0]) +
918                     ". \n" +
919                     "Average duration for page is " +
920                     DateUtils.getStringTime(lActualDataSelectDuration));
921    }
922
923    /**
924     * Load test for data performance. Browse through the whole list from the last
925     * to the first therefore retrieving every page of the list in sequential
926     * order but in reverse.
927     *
928     * This method will also providing test for all possible sorting columns
929     * (columns used in the clause ORDER BY). Direction will be changed for each
930     * 2nd sortable column.
931     *
932     * @param parent - parent object containing all data using which to execute
933     * the test
934     * @param strExtraText - text to use in reporting of performance
935     * @param iOrderColumn - column code that will be used for sorting (in clause
936     * order by)
937     * @param bOrderDirectionAsc - true, if there will be used ASC direction
938     * - false, if there will be used DESC direction
939     * @param bTestDataValues - true, if there will be tested retrieved values
940     * (1st and last record)
941     * - false, if there will be not tested retrieved values
942     * @throws Exception - an error has occured during test
943     */

944    protected void doTestLoadPagesFromLastToFirst(
945       Object JavaDoc parent,
946       String JavaDoc strExtraText,
947       int iOrderColumn,
948       boolean bOrderDirectionAsc,
949       boolean bTestDataValues
950    ) throws Exception JavaDoc
951    {
952       StringBuffer JavaDoc sbLogInfo = new StringBuffer JavaDoc();
953       int iIndex;
954       int iTotalPages = 0;
955       boolean isLastPage = false;
956       boolean bCountLastPageSize = false;
957       long lSumDataSelectDurations = 0;
958       long lActualDataSelectDuration;
959       long[][] lMaxDataSelectDuration = {{0, 0}};
960       long[][] lMinDataSelectDuration = {{Long.MAX_VALUE, 0}};
961       int[] arrAllColumnCodes = m_listTestUtils.getAllColumnCodes();
962       
963       sbLogInfo.append(strExtraText);
964       if (iOrderColumn != -1)
965       {
966          sbLogInfo.append("(for column ");
967          sbLogInfo.append(iOrderColumn);
968          sbLogInfo.append(" , order direction ");
969          sbLogInfo.append(bOrderDirectionAsc ? "ASC)" : "DESC)");
970       }
971
972       // set number of total pages that we will iterate for
973
if ((s_iLoadtestListSize % s_iLoadtestPageSize) > 0)
974       {
975          iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize) + 1;
976          isLastPage = true;
977       }
978       else
979       {
980          iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize);
981       }
982
983 // TODO: For Julo: We shouldn't analyze in the middle of test, this is only
984
// needed when we create data, I haved disabled this line for now
985
// m_listTestUtils.doAnalyze(m_connection);
986

987       // -----------------------------------------------------------------------
988
// Load each page in the loop from last to first page
989
// -----------------------------------------------------------------------
990
for (iIndex = iTotalPages - 1; iIndex >= 0; iIndex--)
991       {
992          bCountLastPageSize = false;
993          if ((iIndex == iTotalPages - 1) && (isLastPage))
994          {
995             bCountLastPageSize = true;
996          }
997          
998          // Now try load one specified page with all columns
999
lActualDataSelectDuration = doLoadTestGet(
1000                                        parent,
1001                                        s_iLoadtestListSize,
1002                                        s_iLoadtestPageSize,
1003                                       (iIndex * s_iLoadtestPageSize) + 1,
1004                                        bCountLastPageSize,
1005                                        arrAllColumnCodes,
1006                                        iOrderColumn,
1007                                        bOrderDirectionAsc,
1008                                        bTestDataValues);
1009         
1010         lSumDataSelectDurations += lActualDataSelectDuration;
1011
1012         // If this was the fastest retrieved page
1013
if (lActualDataSelectDuration < lMinDataSelectDuration[0][0])
1014         {
1015            // set minimal time duration
1016
lMinDataSelectDuration[0][0] = lActualDataSelectDuration;
1017            // set page number of minimal time
1018
lMinDataSelectDuration[0][1] = iIndex + 1;
1019         }
1020
1021         // If this was the fastest retrieved page
1022
if (lActualDataSelectDuration > lMaxDataSelectDuration[0][0])
1023         {
1024            // set maximal time duration
1025
lMaxDataSelectDuration[0][0] = lActualDataSelectDuration;
1026            // set page number of maximal time
1027
lMaxDataSelectDuration[0][1] = iIndex + 1;
1028         }
1029      }
1030      
1031      // Compute the average duration to retrieve page
1032
lActualDataSelectDuration = lSumDataSelectDurations / iTotalPages;
1033
1034      s_logger.info("Total retrieved pages from last to first " + sbLogInfo +
1035                    " : " + iTotalPages +
1036                    ", number of items for page: " + s_iLoadtestPageSize + "\n" +
1037                    "The best performance has page "
1038                    + lMinDataSelectDuration[0][1] +
1039                    " with duration "
1040                    + DateUtils.getStringTime(lMinDataSelectDuration[0][0]) +
1041                    ". \n" +
1042                    "The worst performance has page "
1043                    + lMaxDataSelectDuration[0][1] +
1044                    " with duration "
1045                    + DateUtils.getStringTime(lMaxDataSelectDuration[0][0]) +
1046                    ". \n" +
1047                    "Average duration for page is " +
1048                    DateUtils.getStringTime(lActualDataSelectDuration));
1049   }
1050
1051   /**
1052    * Load test for data performance. Retrieve the pages from the list randomly,
1053    * generate page number or starting position somewhere in the list and then
1054    * retrieve the page.
1055    *
1056    * @param parent - parent object containing all data using which to execute
1057    * the test
1058    * @param strExtraText - text to use in reporting of performance
1059    * @throws Exception - an error has occured
1060    */

1061   protected void doTestLoadPagesRandom(
1062      Object JavaDoc parent,
1063      String JavaDoc strExtraText
1064   ) throws Exception JavaDoc
1065   {
1066      int iActualPage = 1;
1067      int iIndex;
1068      int iTotalPages = 0;
1069      boolean isLastPage = false;
1070      boolean bCountLastPageSize = false;
1071      int[] arrPageNumbers;
1072      long lSumDataSelectDurations = 0;
1073      long lActualDataSelectDuration;
1074      long[][] lMaxDataSelectDuration = {{0, 0}};
1075      long[][] lMinDataSelectDuration = {{Long.MAX_VALUE, 0}};
1076      int[] arrAllColumnCodes = m_listTestUtils.getAllColumnCodes();
1077      Random JavaDoc generator = new Random JavaDoc();
1078
1079      // Determine the total number of pages that we will iterate through
1080
if ((s_iLoadtestListSize % s_iLoadtestPageSize) > 0)
1081      {
1082         iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize) + 1;
1083         isLastPage = true;
1084      }
1085      else
1086      {
1087         iTotalPages = (s_iLoadtestListSize / s_iLoadtestPageSize);
1088      }
1089
1090      // Precompute the order in which the pages will be retrieved
1091
arrPageNumbers = new int[iTotalPages];
1092      for (iIndex = 0; iIndex < iTotalPages; iIndex++)
1093      {
1094         arrPageNumbers[iIndex] = generator.nextInt(iTotalPages - 1) + 1;
1095      }
1096
1097// TODO: For Julo: We shouldn't analyze in the middle of test, this is only
1098
// needed when we create data, I haved disabled this line for now
1099
// m_listTestUtils.doAnalyze(m_connection);
1100

1101      // -----------------------------------------------------------------------
1102
// Load random page in the loop
1103
// -----------------------------------------------------------------------
1104
for (iIndex = 0; iIndex < iTotalPages; iIndex++)
1105      {
1106         iActualPage = arrPageNumbers[iIndex];
1107         bCountLastPageSize = false;
1108         if ((iActualPage == iTotalPages) && isLastPage)
1109         {
1110            bCountLastPageSize = true;
1111         }
1112         // Now try load one specified page with all columns
1113
lActualDataSelectDuration = doLoadTestGet(
1114                                        parent,
1115                                        s_iLoadtestListSize,
1116                                        s_iLoadtestPageSize,
1117                                        // count begin position
1118
((iActualPage - 1) * s_iLoadtestPageSize) + 1,
1119                                        bCountLastPageSize,
1120                                        arrAllColumnCodes,
1121                                        // there will be not used all possible sorted columns
1122
-1,
1123                                        true,
1124                                        true);
1125         
1126         lSumDataSelectDurations += lActualDataSelectDuration;
1127
1128         // If this was the fastest retrieved page
1129
if (lActualDataSelectDuration < lMinDataSelectDuration[0][0])
1130         {
1131            // set minimal time duration
1132
lMinDataSelectDuration[0][0] = lActualDataSelectDuration;
1133            // set page number of minimal time
1134
lMinDataSelectDuration[0][1] = iActualPage;
1135         }
1136
1137         // If this was the fastest retrieved page
1138
if (lActualDataSelectDuration > lMaxDataSelectDuration[0][0])
1139         {
1140            // set maximal time duration
1141
lMaxDataSelectDuration[0][0] = lActualDataSelectDuration;
1142            // set page number of maximal time
1143
lMaxDataSelectDuration[0][1] = iActualPage;
1144         }
1145      }
1146      
1147      // Compute the average duration to retrieve page
1148
lActualDataSelectDuration = lSumDataSelectDurations / iTotalPages;
1149
1150      s_logger.info("Total randomly retrieved pages " + strExtraText + ": " +
1151                    iTotalPages + ", number of items for page: "
1152                    + s_iLoadtestPageSize +
1153                    "\n" +
1154                    "The best performance has page "
1155                    + lMinDataSelectDuration[0][1] +
1156                    " with duration "
1157                    + DateUtils.getStringTime(lMinDataSelectDuration[0][0]) +
1158                    ". \n" +
1159                    "The worst performance has page "
1160                    + lMaxDataSelectDuration[0][1] +
1161                    " with duration "
1162                    + DateUtils.getStringTime(lMaxDataSelectDuration[0][0]) +
1163                    ". \n" +
1164                    "Average duration for page is " +
1165                    DateUtils.getStringTime(lActualDataSelectDuration));
1166   }
1167}
1168
Popular Tags