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.FxCreateException;
040: import com.flexive.shared.exceptions.FxLogoutFailedException;
041: import com.flexive.shared.security.ACL;
042: import com.flexive.shared.structure.*;
043: import com.flexive.shared.value.FxString;
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: * Tests for the unique mode
054: *
055: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
056: */
057: @Test(groups={"ejb","structure"})
058: public class UniqueModeTest extends StructureTestBase {
059:
060: // TYPE_A > TYPE_B > TYPE_C
061: private final static String TYPE_A = "UNIQUEMODETEST_A_"
062: + RandomStringUtils.random(16, true, true);
063: private final static String TYPE_B = "UNIQUEMODETEST_B_"
064: + RandomStringUtils.random(16, true, true);
065: private final static String TYPE_C = "UNIQUEMODETEST_C_"
066: + RandomStringUtils.random(16, true, true);
067: private final static String TYPE_D = "UNIQUEMODETEST_D_"
068: + RandomStringUtils.random(16, true, true);
069:
070: private static final String PROP_NAME = "UniqueProperty";
071: private static final String XPATH = "/" + PROP_NAME;
072: private static final FxString V1A = new FxString(false, "Abc123");
073: private static final FxString V1B = new FxString(false, "AbC123");
074: private static final FxString V2 = new FxString(false, "def456");
075: private static final FxString V3 = new FxString(false, "ghi789");
076:
077: final int A = 0;
078: final int B = 1;
079: final int C = 2;
080: final int D = 3;
081:
082: /**
083: * setup...
084: *
085: * @throws Exception on errors
086: */
087: @BeforeClass
088: public void beforeClass() throws Exception {
089: super .init();
090: login(TestUsers.SUPERVISOR);
091: }
092:
093: @AfterClass
094: public void afterClass() throws FxLogoutFailedException,
095: FxApplicationException {
096: logout();
097: }
098:
099: private void createProperty(long typeId, ACL acl, String name,
100: String XPath, UniqueMode mode)
101: throws FxApplicationException {
102: ass.createProperty(typeId, FxPropertyEdit.createNew(
103: name,
104: new FxString("UniqueMode." + mode
105: + " UnitTest property " + name),
106: new FxString("hint..."), new FxMultiplicity(1, 2), acl,
107: FxDataType.String1024).setUniqueMode(mode), XPath);
108: }
109:
110: private long[] createTypes(UniqueMode mode)
111: throws FxApplicationException {
112: ACL structACL = CacheAdmin.getEnvironment().getACL(
113: ACL.Category.STRUCTURE.getDefaultId());
114: long typeA = type.save(FxTypeEdit.createNew(TYPE_A,
115: new FxString("Test type " + TYPE_A), structACL, null));
116: createProperty(typeA, structACL, PROP_NAME, "/", mode);
117: long typeB = type.save(FxTypeEdit.createNew(TYPE_B,
118: new FxString("Test type " + TYPE_B), structACL,
119: CacheAdmin.getEnvironment().getType(typeA)));
120: long typeC = type.save(FxTypeEdit.createNew(TYPE_C,
121: new FxString("Test type " + TYPE_C), structACL,
122: CacheAdmin.getEnvironment().getType(typeB)));
123: long typeD = type.save(FxTypeEdit.createNew(TYPE_D,
124: new FxString("Test type " + TYPE_D), structACL, null));
125: ass.save(FxPropertyAssignmentEdit.createNew(
126: (FxPropertyAssignment) CacheAdmin.getEnvironment()
127: .getAssignment(TYPE_A + "/" + PROP_NAME),
128: CacheAdmin.getEnvironment().getType(TYPE_D), PROP_NAME,
129: "/").setEnabled(true), false);
130: return new long[] { typeA, typeB, typeC, typeD };
131: }
132:
133: private void dropTypes() throws FxApplicationException {
134: try {
135: long id = CacheAdmin.getEnvironment().getType(TYPE_D)
136: .getId();
137: co.removeForType(id);
138: type.remove(id);
139: } catch (FxApplicationException e) {
140: //ignore
141: }
142: try {
143: long id = CacheAdmin.getEnvironment().getType(TYPE_C)
144: .getId();
145: co.removeForType(id);
146: type.remove(id);
147: } catch (FxApplicationException e) {
148: //ignore
149: }
150: try {
151: long id = CacheAdmin.getEnvironment().getType(TYPE_B)
152: .getId();
153: co.removeForType(id);
154: type.remove(id);
155: } catch (FxApplicationException e) {
156: //ignore
157: }
158: try {
159: long id = CacheAdmin.getEnvironment().getType(TYPE_A)
160: .getId();
161: co.removeForType(id);
162: type.remove(id);
163: } catch (FxApplicationException e) {
164: //ignore
165: }
166: }
167:
168: @Test
169: public void uniqueGlobal() throws FxApplicationException {
170: long[] types = createTypes(UniqueMode.Global);
171: try {
172: FxContent c = co.initialize(types[A]);
173: c.setValue(XPATH, V1A);
174: FxPK pk = co.save(c);
175: c = co.load(pk);
176: co.createNewVersion(c); //new version has to work!
177: c = co.initialize(types[B]);
178: c.setValue(XPATH, V2);
179: co.save(c);
180: c = co.initialize(types[C]);
181: c.setValue(XPATH, V1B);
182: try {
183: //save on C violates A
184: co.save(c);
185: assert false : "Constraint violation C on A expected!";
186: } catch (FxCreateException e) {
187: //ok
188: }
189: c = co.initialize(types[A]);
190: c.setValue(XPATH, V1A);
191: try {
192: //save on A violates A
193: co.save(c);
194: assert false : "Constraint violation A on A expected!";
195: } catch (FxCreateException e) {
196: //ok
197: }
198: c = co.initialize(types[D]);
199: c.setValue(XPATH, V1A);
200: try {
201: //save on D violates A
202: co.save(c);
203: assert false : "Constraint violation D on A expected!";
204: } catch (FxCreateException e) {
205: //ok
206: }
207: c = co.initialize(types[D]);
208: c.setValue(XPATH, V3);
209: co.save(c); //new value in D => ok
210: } finally {
211: dropTypes();
212: }
213: }
214:
215: @Test
216: public void uniqueType() throws FxApplicationException {
217: long[] types = createTypes(UniqueMode.Type);
218: try {
219: FxContent c = co.initialize(types[A]);
220: c.setValue(XPATH, V1A);
221: FxPK pk = co.save(c);
222: c = co.load(pk);
223: co.createNewVersion(c); //new version has to work!
224: c = co.initialize(types[B]);
225: c.setValue(XPATH, V2);
226: co.save(c);
227: c = co.initialize(types[C]);
228: c.setValue(XPATH, V1A);
229: co.save(c); //C and A same => ok
230: c = co.initialize(types[D]);
231: c.setValue(XPATH, V1A);
232: co.save(c); //D, C and A same => ok
233: c = co.initialize(types[A]);
234: c.setValue(XPATH, V1B);
235: try {
236: //save on A violates A
237: co.save(c);
238: assert false : "Constraint violation expected!";
239: } catch (FxCreateException e) {
240: //ok
241: }
242: } finally {
243: dropTypes();
244: }
245: uniqueWithinInstance(UniqueMode.Type);
246: }
247:
248: @Test
249: public void uniqueDerivedTypes() throws FxApplicationException {
250: long[] types = createTypes(UniqueMode.DerivedTypes);
251: try {
252: FxContent c = co.initialize(types[B]);
253: c.setValue(XPATH, V2);
254: FxPK pk = co.save(c);
255: c = co.load(pk);
256: co.createNewVersion(c); //new version has to work!
257: c = co.initialize(types[C]);
258: c.setValue(XPATH, V2);
259: try {
260: //save on C violates B
261: co.save(c);
262: assert false : "Constraint violation C on B expected!";
263: } catch (FxCreateException e) {
264: //ok
265: }
266: c = co.initialize(types[A]);
267: c.setValue(XPATH, V2);
268: try {
269: //save on C violates A
270: co.save(c);
271: assert false : "Constraint violation C on A expected!";
272: } catch (FxCreateException e) {
273: //ok
274: }
275: c = co.initialize(types[D]);
276: c.setValue(XPATH, V2);
277: co.save(c); //D is not derived or parent => ok
278: } finally {
279: dropTypes();
280: }
281: uniqueWithinInstance(UniqueMode.DerivedTypes);
282: }
283:
284: @Test
285: public void uniqueInstance() throws FxApplicationException {
286: uniqueWithinInstance(UniqueMode.Instance);
287: }
288:
289: private void uniqueWithinInstance(UniqueMode mode)
290: throws FxApplicationException {
291: long[] types = createTypes(mode);
292: try {
293: FxContent c = co.initialize(types[A]);
294: c.setValue(XPATH, V2);
295: FxPK pk = co.save(c);
296: c = co.load(pk);
297: co.createNewVersion(c); //new version has to work!
298: c = co.load(pk);
299: c.setValue(XPATH + "[2]", V3);
300: co.save(c); //different value at [2] => ok
301: c = co.load(pk);
302: c.setValue(XPATH + "[2]", V2);
303: try {
304: //save on [2] violates [1]
305: co.save(c);
306: assert false : "Constraint violation [2] on [1] expected!";
307: } catch (FxCreateException e) {
308: //ok
309: }
310: c = co.initialize(types[D]);
311: c.setValue(XPATH, V2);
312: co.save(c); //different type => ok
313: } finally {
314: dropTypes();
315: }
316: }
317:
318: }
|