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.structure;
034:
035: import com.flexive.shared.CacheAdmin;
036: import com.flexive.shared.content.FxContent;
037: import com.flexive.shared.content.FxPK;
038: import com.flexive.shared.exceptions.FxApplicationException;
039: import com.flexive.shared.exceptions.FxLogoutFailedException;
040: import com.flexive.shared.security.ACL;
041: import com.flexive.shared.structure.*;
042: import com.flexive.shared.value.FxString;
043: import com.flexive.tests.embedded.FxTestUtils;
044: import static com.flexive.tests.embedded.FxTestUtils.login;
045: import static com.flexive.tests.embedded.FxTestUtils.logout;
046: import com.flexive.tests.embedded.TestUsers;
047: import org.apache.commons.lang.RandomStringUtils;
048: import org.testng.annotations.AfterClass;
049: import org.testng.annotations.BeforeClass;
050: import org.testng.annotations.Test;
051:
052: /**
053: * Test for relations
054: *
055: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
056: */
057: @Test(groups={"ejb","structure","relation"})
058: public class RelationTest extends StructureTestBase {
059: private final static String REL_NAME = "RELTEST_REL_"
060: + RandomStringUtils.random(16, true, true);
061: private final static String SRC_NAME = "RELTEST_SRC_"
062: + RandomStringUtils.random(16, true, true);
063: private final static String DST_NAME = "RELTEST_DST_"
064: + RandomStringUtils.random(16, true, true);
065:
066: private ACL aclStructure, aclWorkflow, aclContent;
067: private long relId, srcId[], dstId[];
068: private int src_dst_count = 6;
069:
070: private void createProperty(long typeId, String name, String XPath)
071: throws FxApplicationException {
072: ass.createProperty(typeId, FxPropertyEdit.createNew(name,
073: new FxString("Relation UnitTest property " + name),
074: new FxString("hint..."), FxMultiplicity.MULT_0_1,
075: aclStructure, FxDataType.String1024)
076: .setAutoUniquePropertyName(true), XPath);
077: }
078:
079: /**
080: * setup...
081: *
082: * @throws Exception on errors
083: */
084: @BeforeClass
085: public void beforeClass() throws Exception {
086: super .init();
087: login(TestUsers.SUPERVISOR);
088: //create the base type
089: ACL[] tmp = FxTestUtils.createACLs(
090: new String[] {
091: "STRUCTURE_"
092: + RandomStringUtils.random(16, true,
093: true),
094: "WORKFLOW_"
095: + RandomStringUtils.random(16, true,
096: true),
097: "CONTENT_"
098: + RandomStringUtils.random(16, true,
099: true)
100:
101: }, new ACL.Category[] { ACL.Category.STRUCTURE,
102: ACL.Category.WORKFLOW, ACL.Category.INSTANCE },
103: TestUsers.getTestMandator());
104: aclStructure = tmp[0];
105: aclWorkflow = tmp[1];
106: aclContent = tmp[2];
107: relId = type.save(FxTypeEdit.createNew(REL_NAME,
108: new FxString("Test data"), aclStructure, null)
109: .setMaxRelSource(0).setMaxRelDestination(0).setMode(
110: TypeMode.Relation));
111: createProperty(relId, "R", "/");
112: srcId = new long[src_dst_count];
113: dstId = new long[src_dst_count];
114: for (int i = 0; i < src_dst_count; i++) {
115: srcId[i] = type.save(FxTypeEdit.createNew(SRC_NAME + "_"
116: + (i + 1), new FxString("Test data"), aclStructure,
117: null));
118: createProperty(srcId[i], "S" + i, "/");
119: dstId[i] = type.save(FxTypeEdit.createNew(DST_NAME + "_"
120: + (i + 1), new FxString("Test data"), aclStructure,
121: null));
122: createProperty(dstId[i], "D" + i, "/");
123: }
124: }
125:
126: @Test
127: public void verifyRelationMode() {
128: assert CacheAdmin.getEnvironment().getType(relId).isRelation() : "Expected a relation but found a type";
129: }
130:
131: @Test
132: public void checkTypeRelation() throws FxApplicationException {
133: //make relation
134: FxTypeEdit rel = CacheAdmin.getEnvironment().getType(relId)
135: .asEditable();
136: rel.addRelation(FxTypeRelationEdit.createNew(CacheAdmin
137: .getEnvironment().getType(srcId[0]), CacheAdmin
138: .getEnvironment().getType(dstId[0])));
139: type.save(rel);
140:
141: rel = CacheAdmin.getEnvironment().getType(relId).asEditable();
142: assert rel.getRelations().size() == 1 : "One relation expected!";
143: assert rel.getRemovedRelations().size() == 0;
144: assert rel.getUpdatedRelations().size() == 0;
145: assert rel.getAddedRelations().size() == 0;
146:
147: String assMsg = "No changes expected";
148: rel.addRelation(buildRel(0, 0, 0, 0));
149: assert rel.getRemovedRelations().size() == 0 : assMsg;
150: assert rel.getUpdatedRelations().size() == 0 : assMsg;
151: assert rel.getAddedRelations().size() == 0 : assMsg;
152:
153: assMsg = "One update expected";
154: rel.updateRelation(buildRel(0, 0, 1, 0));
155: assert rel.getRemovedRelations().size() == 0 : assMsg;
156: assert rel.getUpdatedRelations().size() == 1 : assMsg;
157: assert rel.getAddedRelations().size() == 0 : assMsg;
158:
159: assMsg = "One update, one added expected";
160: rel.addRelation(buildRel(0, 1, 0, 10));
161: assert rel.getRemovedRelations().size() == 0 : assMsg;
162: assert rel.getUpdatedRelations().size() == 1 : assMsg;
163: assert rel.getAddedRelations().size() == 1 : assMsg;
164:
165: assMsg = "One removed, one added expected";
166: rel.removeRelation(buildRel(0, 0, 1, 0));
167: assert rel.getRemovedRelations().size() == 1 : assMsg;
168: assert rel.getUpdatedRelations().size() == 0 : assMsg;
169: assert rel.getAddedRelations().size() == 1 : assMsg;
170:
171: type.save(rel);
172: rel = CacheAdmin.getEnvironment().getType(relId).asEditable();
173: assert rel.getRelations().size() == 1;
174: FxTypeRelation check = rel.getRelations().get(0);
175: assert check.getSource().getId() == srcId[0];
176: assert !check.isSourceLimited();
177: assert check.getDestination().getId() == dstId[1];
178: assert check.isDestinationLimited();
179: assert check.getMaxDestination() == 10;
180:
181: rel.removeRelation(buildRel(0, 1, 0, 0));
182: rel.setRemoveInstancesWithRelationTypes(true);
183: type.save(rel);
184: assert CacheAdmin.getEnvironment().getType(relId)
185: .getRelations().size() == 0;
186: }
187:
188: @Test
189: public void checkRelationInstancesSimple()
190: throws FxApplicationException {
191: FxTypeEdit rel = CacheAdmin.getEnvironment().getType(relId)
192: .asEditable();
193: rel.addRelation(buildRel(0, 0, 0, 0));
194: type.save(rel);
195: rel = CacheAdmin.getEnvironment().getType(relId).asEditable();
196:
197: FxPK src = co.save(co.initialize(srcId[0]));
198: FxPK dst = co.save(co.initialize(dstId[0]));
199: FxContent r = co.initialize(relId);
200: r.setRelatedSource(src).setRelatedDestination(dst);
201: FxPK pkr = co.save(r);
202: r = co.load(pkr);
203: assert r.getRelatedSource().equals(src) : "Source does not match!";
204: assert r.getRelatedDestination().equals(dst) : "Destination does not match!";
205: rel.removeRelation(rel.getRelations().get(0));
206: rel.setRemoveInstancesWithRelationTypes(false);
207: try {
208: type.save(rel);
209: assert false : "Exception expected!";
210: } catch (FxApplicationException e) {
211: //ok
212: }
213: rel.setRemoveInstancesWithRelationTypes(true);
214: try {
215: type.save(rel);
216: } catch (FxApplicationException e) {
217: assert false : "No exception expected, relation instances should have been removed. msg: "
218: + e.getMessage();
219: }
220: rel = CacheAdmin.getEnvironment().getType(relId).asEditable();
221: assert rel.getRelations().size() == 0 : "Expected 0 relations for rel!";
222: }
223:
224: private FxTypeRelation buildRel(int src, int dst, int maxSrc,
225: int maxDst) {
226: return FxTypeRelationEdit.createNew(CacheAdmin.getEnvironment()
227: .getType(srcId[src]), CacheAdmin.getEnvironment()
228: .getType(dstId[dst]), maxSrc, maxDst);
229: }
230:
231: @AfterClass(dependsOnMethods={"tearDownStructures"})
232: public void afterClass() throws FxLogoutFailedException,
233: FxApplicationException {
234: logout();
235: }
236:
237: @AfterClass
238: public void tearDownStructures() throws Exception {
239: co.removeForType(relId);
240: type.remove(relId);
241: for (int i = 0; i < src_dst_count; i++) {
242: co.removeForType(srcId[i]);
243: type.remove(srcId[i]);
244: co.removeForType(dstId[i]);
245: type.remove(dstId[i]);
246: }
247: FxTestUtils.removeACL(aclStructure, aclWorkflow, aclContent);
248: }
249:
250: }
|