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.CacheAdmin;
037: import com.flexive.shared.EJBLookup;
038: import com.flexive.shared.content.FxContent;
039: import com.flexive.shared.content.FxPK;
040: import com.flexive.shared.exceptions.*;
041: import com.flexive.shared.interfaces.AssignmentEngine;
042: import com.flexive.shared.interfaces.ContentEngine;
043: import com.flexive.shared.interfaces.TypeEngine;
044: import com.flexive.shared.search.FxResultSet;
045: import com.flexive.shared.search.query.PropertyValueComparator;
046: import com.flexive.shared.search.query.QueryOperatorNode;
047: import com.flexive.shared.search.query.SqlQueryBuilder;
048: import com.flexive.shared.security.ACL;
049: import com.flexive.shared.security.Mandator;
050: import com.flexive.shared.structure.*;
051: import com.flexive.shared.value.FxString;
052: import com.flexive.tests.embedded.FxTestUtils;
053: import static com.flexive.tests.embedded.FxTestUtils.login;
054: import static com.flexive.tests.embedded.FxTestUtils.logout;
055: import com.flexive.tests.embedded.TestUsers;
056: import org.testng.annotations.AfterClass;
057: import org.testng.annotations.BeforeClass;
058:
059: /**
060: * Base class for SQL query tests, including test data generation.
061: *
062: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
063: * @version $Rev: 18 $
064: */
065: public class AbstractSqlQueryTest {
066: protected static final int TOTALROWS = 25;
067: protected static final String TEST_TYPE = "FXRESULTSET_TYPE";
068: protected final static String TEST_PROPERTY = "FXRESULTSET_TESTPROPERTY";
069: protected static final String SELECT_ALL = "SELECT co.@PK FROM content co WHERE co."
070: + TEST_PROPERTY + " like 'testValue%'";
071:
072: protected ACL structureAcl;
073: protected ACL contentAcl;
074: protected long typeId = -1;
075: protected long assignmentId = -1;
076:
077: @BeforeClass
078: public void beforeClass() throws FxLoginFailedException,
079: FxAccountInUseException, FxApplicationException {
080: final AssignmentEngine assignmentEngine = EJBLookup
081: .getAssignmentEngine();
082: final TypeEngine typeEngine = EJBLookup.getTypeEngine();
083: final ContentEngine contentEngine = EJBLookup
084: .getContentEngine();
085:
086: login(TestUsers.SUPERVISOR);
087:
088: // create structure
089: tearDownStructures();
090: this .structureAcl = FxTestUtils.createACL("Test:"
091: + FxResultSetDataModelTest.class,
092: ACL.Category.STRUCTURE, Mandator.MANDATOR_FLEXIVE);
093: this .contentAcl = FxTestUtils.createACL("Test_Content:"
094: + FxResultSetDataModelTest.class,
095: ACL.Category.INSTANCE, Mandator.MANDATOR_FLEXIVE);
096: FxPropertyEdit pe = FxPropertyEdit.createNew(TEST_PROPERTY,
097: new FxString(""), new FxString(""),
098: FxMultiplicity.MULT_0_1, structureAcl,
099: FxDataType.String1024);
100: pe.setAutoUniquePropertyName(true);
101: typeId = typeEngine.save(FxTypeEdit.createNew(TEST_TYPE,
102: new FxString(""), structureAcl, null));
103: try {
104: assignmentId = assignmentEngine.createProperty(typeId, pe,
105: "/");
106: } catch (FxEntryExistsException e) {
107: // ignore
108: }
109:
110: // create some test data
111: for (int i = 0; i < TOTALROWS; i++) {
112: FxContent co = contentEngine.initialize(typeId,
113: Mandator.MANDATOR_FLEXIVE, contentAcl.getId(), -1,
114: -1);
115: co.setValue("/" + TEST_PROPERTY, new FxString(false,
116: "testValue" + i));
117: contentEngine.save(co);
118: }
119: }
120:
121: @AfterClass
122: public void afterClass() throws FxLogoutFailedException,
123: FxApplicationException {
124: tearDownStructures();
125: EJBLookup.getACLEngine().remove(structureAcl.getId());
126: EJBLookup.getACLEngine().remove(contentAcl.getId());
127: logout();
128: }
129:
130: private void tearDownStructures() {
131: final TypeEngine typeEngine = EJBLookup.getTypeEngine();
132: // remove contents
133: try {
134: if (typeId != -1) {
135: EJBLookup.getContentEngine().removeForType(typeId);
136: }
137: } catch (Exception e) {
138: // ignore
139: }
140:
141: // remove type
142: try {
143: FxType type = CacheAdmin.getEnvironment()
144: .getType(TEST_TYPE);
145: typeEngine.remove(type.getId());
146: } catch (Exception e) {
147: // ignore
148: }
149: }
150:
151: protected FxResultSet getRows(int startRow, int maxRows)
152: throws FxApplicationException {
153: FxResultSet rs = EJBLookup.getSearchEngine().search(SELECT_ALL,
154: startRow, maxRows, null);
155: assert rs.getRowCount() == maxRows : "Unexpected number of results: "
156: + rs.getRowCount() + ", expected: " + maxRows;
157: assertTotalRowCount(rs);
158: return rs;
159: }
160:
161: protected void assertEqualResults(FxResultSetDataModel model,
162: FxResultSet refResult, int startIndex, int maxRows) {
163: for (int i = startIndex; i < startIndex + maxRows; i++) {
164: model.setRowIndex(i);
165: final Object[] refRow = refResult.getRows().get(
166: i - startIndex);
167: final FxPK refPk = (FxPK) refRow[0];
168: final FxPK pk = (FxPK) ((Object[]) model.getRowData())[0];
169: assert refPk.equals(pk) : "Rows should be equal:\n"
170: + "Lazy-load: " + pk + "\nPreload: " + refPk;
171: assert model.getRowIndex() == i : "Expected row index: "
172: + i + ", got: " + model.getRowIndex();
173: }
174: }
175:
176: protected void assertTotalRowCount(FxResultSet rs) {
177: assert rs.getTotalRowCount() == TOTALROWS : "Unexpected total rows: "
178: + rs.getTotalRowCount();
179: }
180:
181: protected SqlQueryBuilder getSelectAllBuilder() {
182: SqlQueryBuilder builder = new SqlQueryBuilder();
183: builder.enterSub(QueryOperatorNode.Operator.AND);
184: builder.condition(CacheAdmin.getEnvironment().getAssignment(
185: assignmentId), PropertyValueComparator.LIKE,
186: new FxString("testValue%"));
187: builder.closeSub();
188: return builder;
189: }
190: }
|