001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.tests.embedded.jsf;
034:
035: import com.flexive.faces.model.FxResultSetDataModel;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.exceptions.FxAccountInUseException;
038: import com.flexive.shared.exceptions.FxApplicationException;
039: import com.flexive.shared.exceptions.FxLoginFailedException;
040: import com.flexive.shared.exceptions.FxLogoutFailedException;
041: import com.flexive.shared.search.FxResultSet;
042: import com.flexive.shared.search.query.SqlQueryBuilder;
043: import org.testng.annotations.AfterClass;
044: import org.testng.annotations.BeforeClass;
045: import org.testng.annotations.Test;
046:
047: /**
048: * Tests for the FxResultSetDataModel class, including basic SQL searches.
049: *
050: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
051: */
052: @Test(groups="jsf")
053: public class FxResultSetDataModelTest extends AbstractSqlQueryTest {
054:
055: @Override
056: @BeforeClass
057: public void beforeClass() throws FxLoginFailedException,
058: FxAccountInUseException, FxApplicationException {
059: super .beforeClass();
060: }
061:
062: @Override
063: @AfterClass
064: public void afterClass() throws FxLogoutFailedException,
065: FxApplicationException {
066: super .afterClass();
067: }
068:
069: /**
070: * Checks if the test content can be found as expected.
071: * @throws FxApplicationException
072: */
073: @Test
074: public void findTestData() throws FxApplicationException {
075: FxResultSet rs = EJBLookup.getSearchEngine().search(SELECT_ALL,
076: 1, null, null);
077: assert rs.getRowCount() == TOTALROWS : "Unexpected number of results: "
078: + rs.getRowCount();
079: assertTotalRowCount(rs);
080: }
081:
082: /**
083: * Tests a partial result set model with start index 0.
084: * @throws FxApplicationException
085: */
086: @Test
087: public void preloadedPartialResult() throws FxApplicationException {
088: testPreloadedPartialResult(0);
089: testPreloadedPartialResult(10);
090: testPreloadedPartialResult(TOTALROWS - 11);
091: }
092:
093: /**
094: * Tests partial results retrieved using a query builder.
095: * @throws FxApplicationException
096: *
097: */
098: @Test
099: public void lazyPartialResult() throws FxApplicationException {
100: SqlQueryBuilder builder = getSelectAllBuilder();
101:
102: testLazyPartialResult(builder, 0, 10);
103: testLazyPartialResult(builder, 10, 10);
104: testLazyPartialResult(builder, 0, 1);
105: testLazyPartialResult(builder, 10, 1);
106: }
107:
108: private void testLazyPartialResult(SqlQueryBuilder builder,
109: final int startIndex, final int maxRows)
110: throws FxApplicationException {
111: FxResultSetDataModel model = new FxResultSetDataModel(EJBLookup
112: .getSearchEngine(), builder, startIndex, maxRows);
113: FxResultSet refResult = getRows(startIndex, maxRows);
114: // trigger load
115: // model.setRowIndex(startIndex - 1);
116: // assert startIndex == 0 || model.isRowAvailable() : "Row before first row should be available";
117: model.setRowIndex(startIndex);
118: assert model.isRowAvailable() : "First row should be available";
119: model.setRowIndex(startIndex + maxRows - 1);
120: assert model.isRowAvailable() : "Last row should be available";
121: model.setRowIndex(startIndex + maxRows);
122: assert !model.isRowAvailable() : "Row after last row should not be available";
123: assert model.getWrappedData() == model.getResult() : "Result set should be wrapped data";
124: assert model.getRowCount() == TOTALROWS : "Total row count should be "
125: + TOTALROWS + ", got: " + model.getRowCount();
126: assert model.getFetchRows() == maxRows : "FetchRows should be "
127: + maxRows + ", got: " + model.getFetchRows();
128: assertEqualResults(model, refResult, startIndex, maxRows);
129: }
130:
131: /**
132: * Tests a query that returns only the last row.
133: * @throws FxApplicationException
134: */
135: @Test
136: public void oneRowResult() throws FxApplicationException {
137: FxResultSetDataModel model = new FxResultSetDataModel(getRows(
138: TOTALROWS - 1, 1));
139: model.setRowIndex(TOTALROWS - 1);
140: assert model.isRowAvailable() : "Row should be available";
141: model.getRowData();
142: }
143:
144: /**
145: * Sets a custom result as data source.
146: */
147: @Test
148: public void testWrappedData() throws FxApplicationException {
149: FxResultSet refResult = getRows(0, 10);
150: FxResultSetDataModel model = new FxResultSetDataModel(refResult);
151: assert model.getWrappedData() == refResult : "Original result set should not be wrapped.";
152: assertEqualResults(model, refResult, 0, 10);
153: model.setWrappedData(getRows(10, 10));
154: assertEqualResults(model, (FxResultSet) model.getWrappedData(),
155: 10, 10);
156: }
157:
158: private void testPreloadedPartialResult(int startRow)
159: throws FxApplicationException {
160: FxResultSetDataModel model = new FxResultSetDataModel(getRows(
161: startRow, 10));
162: model.setRowIndex(startRow - 1);
163: assert !model.isRowAvailable() : "No rows before startRow should be available.";
164: for (int i = 0; i < 10; i++) {
165: model.setRowIndex(startRow + i);
166: assert model.isRowAvailable() : "Expected more rows, current row: "
167: + (startRow + i + 1);
168: model.getRowData();
169: }
170: model.setRowIndex(startRow + 10);
171: assert !model.isRowAvailable() : "No more rows should be available";
172: }
173: }
|