001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
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;
034:
035: import com.flexive.shared.EJBLookup;
036: import com.flexive.shared.exceptions.FxApplicationException;
037: import com.flexive.shared.exceptions.FxLogoutFailedException;
038: import com.flexive.shared.interfaces.BriefcaseEngine;
039: import com.flexive.shared.search.Briefcase;
040: import com.flexive.shared.search.FxResultRow;
041: import com.flexive.shared.search.FxResultSet;
042: import com.flexive.shared.search.query.PropertyValueComparator;
043: import com.flexive.shared.search.query.SqlQueryBuilder;
044: import static com.flexive.tests.embedded.FxTestUtils.login;
045: import static com.flexive.tests.embedded.FxTestUtils.logout;
046: import org.apache.commons.lang.ArrayUtils;
047: import org.testng.Assert;
048: import org.testng.annotations.AfterClass;
049: import org.testng.annotations.BeforeClass;
050: import org.testng.annotations.Test;
051:
052: import java.util.ArrayList;
053: import java.util.List;
054:
055: /**
056: * Briefcase engine tests.
057: *
058: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
059: * @version $Rev: 181 $
060: */
061: @Test(groups={"ejb","search"})
062: public class BriefcaseTest {
063: @BeforeClass
064: public void beforeClass() throws Exception {
065: login(TestUsers.SUPERVISOR);
066: }
067:
068: @AfterClass
069: public void afterClass() throws FxLogoutFailedException {
070: logout();
071: }
072:
073: @Test
074: public void createDeleteBriefcase() throws FxApplicationException {
075: final BriefcaseEngine be = EJBLookup.getBriefcaseEngine();
076: final long briefcaseId = be.create("test briefcase",
077: "test description", null);
078: try {
079: final Briefcase briefcase = be.load(briefcaseId);
080: Assert.assertEquals(briefcase.getName(), "test briefcase");
081: Assert.assertEquals(briefcase.getDescription(),
082: "test description");
083: Assert.assertEquals(be.getItems(briefcaseId).length, 0,
084: "Briefcase should be empty");
085: } finally {
086: be.remove(briefcaseId);
087: }
088: }
089:
090: @Test
091: public void addBriefcaseItems() throws FxApplicationException {
092: final BriefcaseEngine be = EJBLookup.getBriefcaseEngine();
093: final long briefcaseId = be.create("test briefcase",
094: "test description", null);
095: try {
096: // get some objects
097: final FxResultSet result = new SqlQueryBuilder().select(
098: "@pk").filterType("FOLDER").condition("id",
099: PropertyValueComparator.NE, 0).getResult();
100: final List<Long> ids = new ArrayList<Long>(result
101: .getRowCount());
102: for (FxResultRow row : result.getResultRows()) {
103: ids.add(row.getPk(1).getId());
104: }
105: assert !ids.isEmpty() : "No objects found for testing the briefcase engine";
106: be.addItems(briefcaseId, ArrayUtils.toPrimitive(ids
107: .toArray(new Long[ids.size()])));
108: Assert.assertEquals(be.getItems(briefcaseId).length, ids
109: .size());
110: // add them again - shouldn't change briefcase
111: be.addItems(briefcaseId, ArrayUtils.toPrimitive(ids
112: .toArray(new Long[ids.size()])));
113: Assert.assertEquals(be.getItems(briefcaseId).length, ids
114: .size());
115:
116: // query briefcase
117: final FxResultSet briefcaseResult = new SqlQueryBuilder()
118: .filterBriefcase(briefcaseId).select("@pk")
119: .getResult();
120: Assert
121: .assertEquals(briefcaseResult.getRowCount(), ids
122: .size(),
123: "Briefcase does not contain all items added previously");
124: Assert
125: .assertEquals(be.getItems(briefcaseId).length, ids
126: .size(),
127: "Briefcase does not contain all items added previously");
128: // query briefcase with a condition
129: final FxResultSet briefcaseResult2 = new SqlQueryBuilder()
130: .filterBriefcase(briefcaseId).select("@pk")
131: .condition("id", PropertyValueComparator.NE, 0)
132: .getResult();
133: Assert
134: .assertEquals(briefcaseResult2.getRowCount(), ids
135: .size(),
136: "Briefcase does not contain all items added previously");
137:
138: // replace briefcase content with first row only
139: final long addId = result.getResultRow(0).getPk(1).getId();
140: be.setItems(briefcaseId, new long[] { addId });
141: testSingleItemBriefcase(be, briefcaseId, addId);
142:
143: // remove first row item, add second row item
144: final long secondAddId = result.getResultRow(1).getPk(1)
145: .getId();
146: be.updateItems(briefcaseId, new long[] { secondAddId },
147: new long[] { addId });
148: testSingleItemBriefcase(be, briefcaseId, secondAddId);
149:
150: // clear briefcase
151: be.clear(briefcaseId);
152: Assert.assertEquals(new SqlQueryBuilder().filterBriefcase(
153: briefcaseId).getResult().getRowCount(), 0);
154: } finally {
155: be.remove(briefcaseId);
156: }
157: }
158:
159: private void testSingleItemBriefcase(BriefcaseEngine be,
160: long briefcaseId, long itemId)
161: throws FxApplicationException {
162: final FxResultSet oneResult = new SqlQueryBuilder()
163: .filterBriefcase(briefcaseId).select("@pk").getResult();
164: Assert.assertEquals(oneResult.getRowCount(), 1,
165: "Briefcase should contain only one item");
166: Assert.assertEquals(be.getItems(briefcaseId).length, 1,
167: "Briefcase should contain only one item");
168: Assert.assertEquals(be.getItems(briefcaseId)[0], itemId,
169: "Briefcase should contain item " + itemId);
170: Assert.assertEquals(oneResult.getResultRow(0).getPk(1).getId(),
171: itemId, "Briefcase item ID should be " + itemId);
172: }
173: }
|