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.CacheAdmin;
036: import com.flexive.shared.EJBLookup;
037: import com.flexive.shared.FxLanguage;
038: import com.flexive.shared.content.*;
039: import com.flexive.shared.exceptions.*;
040: import com.flexive.shared.interfaces.ACLEngine;
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.security.ACL;
045: import com.flexive.shared.security.Mandator;
046: import com.flexive.shared.stream.FxStreamUtils;
047: import com.flexive.shared.structure.*;
048: import com.flexive.shared.value.FxFloat;
049: import com.flexive.shared.value.FxNoAccess;
050: import com.flexive.shared.value.FxNumber;
051: import com.flexive.shared.value.FxString;
052: import static com.flexive.tests.embedded.FxTestUtils.*;
053: import org.apache.commons.lang.RandomStringUtils;
054: import org.testng.Assert;
055: import org.testng.annotations.AfterClass;
056: import org.testng.annotations.BeforeClass;
057: import org.testng.annotations.Test;
058:
059: import java.io.File;
060: import java.io.FileInputStream;
061: import java.util.List;
062:
063: /**
064: * Tests for the ContentEngine
065: *
066: * @author Markus Plesser (markus.plesser@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
067: */
068: @Test(groups={"ejb","content"})
069: public class ContentEngineTest {
070:
071: private ContentEngine co;
072: private ACLEngine acl;
073: private TypeEngine type;
074: private AssignmentEngine ass;
075: public static final String TEST_EN = "Test value in english";
076: public static final String TEST_DE = "Test datensatz in deutsch mit \u00E4ml\u00E5ut te\u00DFt";
077: public static final String TEST_FR = "My french is bad but testing special characters: ?`?$\u010E1\u00C6~\u0119\\/";
078: public static final String TEST_IT = "If i knew italian this would be a test value in italian ;)";
079:
080: public static final String TEST_TYPE = "TEST_TYPE_"
081: + RandomStringUtils.random(16, true, true);
082: public static final String TEST_GROUP = "TEST_GROUP_"
083: + RandomStringUtils.random(16, true, true);
084: private static final String TYPE_ARTICLE = "__ArticleTest";
085:
086: /**
087: * setup...
088: *
089: * @throws Exception on errors
090: */
091: @BeforeClass
092: public void beforeClass() throws Exception {
093: co = EJBLookup.getContentEngine();
094: acl = EJBLookup.getACLEngine();
095: type = EJBLookup.getTypeEngine();
096: ass = EJBLookup.getAssignmentEngine();
097: login(TestUsers.SUPERVISOR);
098: }
099:
100: @AfterClass(dependsOnMethods={"tearDownStructures"})
101: public void afterClass() throws FxLogoutFailedException {
102: logout();
103: }
104:
105: @AfterClass
106: public void tearDownStructures() throws Exception {
107: long typeId = CacheAdmin.getEnvironment().getType(TEST_TYPE)
108: .getId();
109: co.removeForType(typeId);
110: type.remove(typeId);
111: typeId = CacheAdmin.getEnvironment().getType(TYPE_ARTICLE)
112: .getId();
113: co.removeForType(typeId);
114: type.remove(typeId);
115: //remove the test group
116: ass.removeAssignment(CacheAdmin.getEnvironment().getAssignment(
117: "ROOT/" + TEST_GROUP).getId(), true, false);
118: }
119:
120: /**
121: * Setup testing structure.
122: * <p/>
123: * Hierarchy looks like this:
124: * * TestProperty1 (String 255)[0..1]
125: * * TestProperty2 (String 1024) [1..1]
126: * * TestProperty3 (String 255) [0..5]
127: * * TestProperty4 (String 255) [1..N]
128: * $ TestGroup1 [0..2]
129: * $ * TestProperty1_1 (String 255) [0..1]
130: * $ * TestProperty1_2 (String 255) [1..1]
131: * $ * TestProperty1_3 (String 255) [1..N]
132: * $ $ TestGroup1_1 [1..1]
133: * $ $ * TestProperty1_1_1 (String 255) [0..1]
134: * $ $ TestGroup1_2 [0..N]
135: * $ $ * TestProperty1_2_1 (String 255) [1..1]
136: * $ $ * TestProperty1_2_2 (String 255) [0..5]
137: * $ $ $ TestGroup1_2_1 [1..2]
138: * $ $ $ * TestProperty1_2_1_1 (String 255) [0..2]
139: * $ $ TestGroup1_3 [1..2]
140: * $ $ * TestProperty1_3_1 (String 255) [0..1]
141: * * TestNumber (FxNumber) [0..2]
142: * * TestNumberSL (FxNumber) [0..2] (Single language only)
143: * * TestFloat (FxFloat) [0..2]
144: *
145: * @throws Exception on errors
146: */
147: @BeforeClass(dependsOnMethods={"setupACL"})
148: public void setupStructures() throws Exception {
149: try {
150: if (CacheAdmin.getEnvironment().getType(TEST_TYPE) != null)
151: return;
152: } catch (FxRuntimeException e) {
153: //ignore and create
154: }
155: ACL structACL = CacheAdmin.getEnvironment().getACL(
156: "Test ACL Structure 1");
157: boolean createRootStuff = true;
158: try {
159: createRootStuff = CacheAdmin.getEnvironment().getGroup(
160: TEST_TYPE) == null;
161: } catch (FxRuntimeException e) {
162: //ignore and create
163: }
164: if (createRootStuff) {
165: FxString desc = new FxString("Test data structure");
166: desc.setTranslation(FxLanguage.GERMAN,
167: "Testdaten Strukturen");
168: FxString hint = new FxString("Hint text ...");
169: FxGroupEdit ge = FxGroupEdit.createNew(TEST_GROUP, desc,
170: hint, true, new FxMultiplicity(0, 1));
171: ass.createGroup(ge, "/");
172: FxPropertyEdit pe = FxPropertyEdit.createNew(
173: "TestProperty1", desc, hint, true,
174: new FxMultiplicity(0, 1), true, structACL,
175: FxDataType.String1024,
176: new FxString(FxString.EMPTY), true, null, null,
177: null).setMultiLang(true).setOverrideMultiLang(true);
178: pe.setAutoUniquePropertyName(true);
179: ass.createProperty(pe, "/" + TEST_GROUP);
180: pe.setName("TestProperty2");
181: pe.setDataType(FxDataType.String1024);
182: pe.setMultiplicity(new FxMultiplicity(1, 1));
183: ass.createProperty(pe, "/" + TEST_GROUP);
184: pe.setName("TestProperty3");
185: pe.setDataType(FxDataType.String1024);
186: pe.setMultiplicity(new FxMultiplicity(0, 5));
187: ass.createProperty(pe, "/" + TEST_GROUP);
188: pe.setName("TestProperty4");
189: pe.setMultiplicity(new FxMultiplicity(1, FxMultiplicity.N));
190: ass.createProperty(pe, "/" + TEST_GROUP);
191: ge.setName("TestGroup1");
192: ge.setMultiplicity(new FxMultiplicity(0, 2));
193: ass.createGroup(ge, "/" + TEST_GROUP);
194: pe.setName("TestProperty1_1");
195: pe.setMultiplicity(new FxMultiplicity(0, 1));
196: ass.createProperty(pe, "/" + TEST_GROUP + "/TestGroup1");
197: pe.setName("TestProperty1_2");
198: pe.setMultiplicity(new FxMultiplicity(1, 1));
199: ass.createProperty(pe, "/" + TEST_GROUP + "/TestGroup1");
200: pe.setName("TestProperty1_3");
201: pe.setMultiplicity(new FxMultiplicity(1, FxMultiplicity.N));
202: ass.createProperty(pe, "/" + TEST_GROUP + "/TestGroup1");
203: ge.setName("TestGroup1_1");
204: ge.setMultiplicity(new FxMultiplicity(1, 1));
205: ass.createGroup(ge, "/" + TEST_GROUP + "/TestGroup1");
206: pe.setName("TestProperty1_1_1");
207: pe.setMultiplicity(new FxMultiplicity(0, 1));
208: ass.createProperty(pe, "/" + TEST_GROUP
209: + "/TestGroup1/TestGroup1_1");
210: ge.setName("TestGroup1_2");
211: ge.setMultiplicity(new FxMultiplicity(0, FxMultiplicity.N));
212: ass.createGroup(ge, "/" + TEST_GROUP + "/TestGroup1");
213: pe.setName("TestProperty1_2_1");
214: pe.setMultiplicity(new FxMultiplicity(1, 1));
215: ass.createProperty(pe, "/" + TEST_GROUP
216: + "/TestGroup1/TestGroup1_2");
217: pe.setName("TestProperty1_2_2");
218: pe.setMultiplicity(new FxMultiplicity(0, 5));
219: ass.createProperty(pe, "/" + TEST_GROUP
220: + "/TestGroup1/TestGroup1_2");
221: ge.setName("TestGroup1_2_1");
222: ge.setMultiplicity(new FxMultiplicity(1, 2));
223: ass.createGroup(ge, "/" + TEST_GROUP
224: + "/TestGroup1/TestGroup1_2");
225: pe.setName("TestProperty1_2_1_1");
226: pe.setMultiplicity(new FxMultiplicity(0, 2));
227: ass.createProperty(pe, "/" + TEST_GROUP
228: + "/TestGroup1/TestGroup1_2/TestGroup1_2_1");
229: ge.setName("TestGroup1_3");
230: ge.setMultiplicity(new FxMultiplicity(1, 2));
231: ass.createGroup(ge, "/" + TEST_GROUP + "/TestGroup1");
232: pe.setName("TestProperty1_3_1");
233: pe.setMultiplicity(new FxMultiplicity(0, 1));
234: ass.createProperty(pe, "/" + TEST_GROUP
235: + "/TestGroup1/TestGroup1_3");
236: pe.setName("TestNumber");
237: pe.setDataType(FxDataType.Number);
238: pe.setMultiplicity(new FxMultiplicity(0, 2));
239: ass.createProperty(pe, "/" + TEST_GROUP);
240: pe.setName("TestNumberSL");
241: pe.setDataType(FxDataType.Number);
242: pe.setMultiLang(false);
243: ass.createProperty(pe, "/" + TEST_GROUP);
244: pe.setMultiLang(true);
245: pe.setName("TestFloat");
246: pe.setDataType(FxDataType.Float);
247: pe.setMultiplicity(new FxMultiplicity(0, 2));
248: ass.createProperty(pe, "/" + TEST_GROUP);
249: }
250: //create article type
251: FxPropertyEdit pe = FxPropertyEdit.createNew("MyTitle",
252: new FxString("Description"), new FxString("Hint"),
253: true, new FxMultiplicity(0, 1), true, structACL,
254: FxDataType.String1024, new FxString(FxString.EMPTY),
255: true, null, null, null).setAutoUniquePropertyName(true)
256: .setMultiLang(true).setOverrideMultiLang(true);
257: long articleId = type.save(FxTypeEdit.createNew(TYPE_ARTICLE,
258: new FxString("Article test type"), CacheAdmin
259: .getEnvironment().getACLs(
260: ACL.Category.STRUCTURE).get(0), null));
261: ass.createProperty(articleId, pe, "/");
262: pe.setName("Text");
263: pe.setDataType(FxDataType.Text);
264: pe.setMultiplicity(new FxMultiplicity(0, 2));
265: ass.createProperty(articleId, pe, "/");
266:
267: long testDataId = type.save(FxTypeEdit.createNew(TEST_TYPE,
268: new FxString("Test data"), CacheAdmin.getEnvironment()
269: .getACLs(ACL.Category.STRUCTURE).get(0), null));
270: FxGroupAssignment ga = (FxGroupAssignment) CacheAdmin
271: .getEnvironment().getAssignment("ROOT/" + TEST_GROUP);
272: FxGroupAssignmentEdit gae = FxGroupAssignmentEdit.createNew(ga,
273: CacheAdmin.getEnvironment().getType(TEST_TYPE), null,
274: "/");
275: ass.save(gae, true);
276:
277: try {
278: if (ScriptingTest.loadScript != null) {
279: //only install scripts if scripting is being tested as well
280: EJBLookup.getScriptingEngine()
281: .createTypeScriptMapping(
282: ScriptingTest.loadScript.getId(), 0,
283: true, true);
284: EJBLookup.getScriptingEngine().createTypeScriptMapping(
285: ScriptingTest.removeScript.getId(), 0, true,
286: true);
287: EJBLookup.getScriptingEngine().createTypeScriptMapping(
288: ScriptingTest.loadScript.getId(), testDataId,
289: true, true);
290: EJBLookup.getScriptingEngine().createTypeScriptMapping(
291: ScriptingTest.removeScript.getId(), testDataId,
292: true, true);
293: }
294: } catch (Exception ex) {
295: //ignore since scripting might not be enabled for this test run
296: ex.printStackTrace();
297: }
298: }
299:
300: /**
301: * Setup ACL's needed for testing
302: *
303: * @throws Exception on errors
304: */
305: @BeforeClass(dependsOnMethods={"beforeClass"})
306: public void setupACL() throws Exception {
307: try {
308: CacheAdmin.getEnvironment().getACL("Test ACL Content 1");
309: } catch (FxRuntimeException e) {
310: acl.create("Test ACL Content 1", new FxString(
311: "Test ACL Content 1"), Mandator.MANDATOR_FLEXIVE,
312: "#00CCDD", "", ACL.Category.INSTANCE);
313: }
314: try {
315: CacheAdmin.getEnvironment().getACL("Test ACL Structure 1");
316: } catch (FxRuntimeException e) {
317: acl.create("Test ACL Structure 1", new FxString(
318: "Test ACL Structure 1"), Mandator.MANDATOR_FLEXIVE,
319: "#BBCCDD", "", ACL.Category.STRUCTURE);
320: }
321: try {
322: CacheAdmin.getEnvironment().getACL("Test ACL Workflow 1");
323: } catch (FxRuntimeException e) {
324: acl.create("Test ACL Workflow 1", new FxString(
325: "Test ACL Workflow 1"), Mandator.MANDATOR_FLEXIVE,
326: "#BB00DD", "", ACL.Category.WORKFLOW);
327: }
328: try {
329: CacheAdmin.getEnvironment().getACL("Article ACL");
330: } catch (FxRuntimeException e) {
331: acl.create("Article ACL", new FxString("ACL for articles"),
332: Mandator.MANDATOR_FLEXIVE, "#00CC00", "",
333: ACL.Category.INSTANCE);
334: }
335: }
336:
337: @Test
338: public void removeAddData() throws Exception {
339: FxType testType = CacheAdmin.getEnvironment()
340: .getType(TEST_TYPE);
341: assert testType != null;
342: FxContent test = co.initialize(testType.getId());
343: assert test != null;
344: test.setAclId(CacheAdmin.getEnvironment().getACL(
345: "Test ACL Content 1").getId());
346:
347: assert test.getRootGroup().getCreateableChildren(true).size() == 6;
348: assert !test.getRootGroup().isRemoveable();
349: assert test.getGroupData("/TestGroup1").isRemoveable();
350: assert !test.getGroupData("/TestGroup1/TestGroup1_1")
351: .isRemoveable();
352: assert test.getPropertyData("/TestProperty1").isRemoveable();
353: assert !test.getPropertyData("/TestProperty2").isRemoveable();
354: assert test.getPropertyData("/TestProperty3").isRemoveable();
355: assert !test.getPropertyData("/TestProperty4").isRemoveable();
356: test.remove("/TestGroup1/TestGroup1_2");
357: try {
358: test.getGroupData("/TestGroup1/TestGroup1_2");
359: assert false : "/TestGroup1/TestGroup1_2 should no longer exist!";
360: } catch (FxNotFoundException e) {
361: //expected
362: }
363: List<String> cr = test.getGroupData("/TestGroup1")
364: .getCreateableChildren(true);
365: assert cr.size() == 3;
366: assert cr.get(0).equals("/TESTGROUP1[1]/TESTPROPERTY1_3[2]");
367: assert cr.get(1).equals("/TESTGROUP1[1]/TESTGROUP1_2[1]");
368: assert cr.get(2).equals("/TESTGROUP1[1]/TESTGROUP1_3[2]");
369: cr = test.getGroupData("/TestGroup1").getCreateableChildren(
370: false);
371: // for(String xp: cr)
372: // System.out.println("==cr=> "+xp);
373: assert cr.size() == 1;
374: assert cr.get(0).equals("/TESTGROUP1[1]/TESTGROUP1_2[1]");
375:
376: test.getGroupData("/TestGroup1").explode(false);
377: assert test.getGroupData("/TestGroup1").getChildren().size() == 6;
378: assert test.getGroupData("/TESTGROUP1[1]/TESTGROUP1_2[1]")
379: .getChildren().size() == 3;
380: test.remove("/TESTGROUP1[1]/TESTGROUP1_2[1]");
381: assert test.getGroupData("/TestGroup1").getCreateableChildren(
382: false).size() == 1;
383:
384: test.getGroupData("/TestGroup1").addEmptyChild(
385: "/TESTGROUP1[1]/TESTGROUP1_2[1]",
386: FxData.POSITION_BOTTOM);
387: test.getGroupData("/TestGroup1").addEmptyChild(
388: "/TESTGROUP1[1]/TESTGROUP1_2[2]",
389: FxData.POSITION_BOTTOM);
390: test.getGroupData("/TestGroup1").addEmptyChild(
391: "/TESTGROUP1[1]/TESTGROUP1_2[4]",
392: FxData.POSITION_BOTTOM);
393: try {
394: test.getRootGroup().addEmptyChild("/TESTPROPERTY1[2]",
395: FxData.POSITION_BOTTOM);
396: assert false : "FxCreateException expected! max. multiplicity reached";
397: } catch (FxInvalidParameterException e) {
398: //expected
399: }
400: test.remove("/TestGroup1");
401: try {
402: test.getGroupData("/TestGroup1");
403: assert false : "/TestGroup1 should no longer exist!";
404: } catch (FxNotFoundException e) {
405: //expected
406: }
407: test.remove("/TestNumber");
408: try {
409: test.getPropertyData("/TestNumber");
410: assert false : "/TestNumber should no longer exist!";
411: } catch (FxNotFoundException e) {
412: //expected
413: }
414: }
415:
416: @Test
417: public void contentComplex() throws Exception {
418: FxType testType = CacheAdmin.getEnvironment()
419: .getType(TEST_TYPE);
420: assert testType != null;
421: //initialize tests start
422: FxContent test = co.initialize(testType.getId());
423: test.setAclId(CacheAdmin.getEnvironment().getACL(
424: "Test ACL Content 1").getId());
425: assert test != null;
426: int rootSize = 8 + CacheAdmin.getEnvironment()
427: .getSystemInternalRootPropertyAssignments().size();
428: assert rootSize == test.getData("/").size() : "Root size expected "
429: + rootSize + ", was " + test.getData("/").size();
430: // FxGroupData groot = test.getData("/").get(0).getParent();
431: //basic sanity checks
432: // assert groot.isEmpty(); TODO: isEmpty means no sys internal properties!
433: assert test.getData("/TestProperty1").get(0)
434: .getAssignmentMultiplicity().toString().equals("0..1");
435: assert 1 == test.getData("/TestProperty1").get(0).getIndex();
436: assert !(test.getData("/TestProperty1").get(0).mayCreateMore()); //should be at multiplicity 1
437: assert !(test.getPropertyData("/TestProperty2").mayCreateMore()); //should be at multiplicity 1
438: assert test.getPropertyData("/TestProperty3").mayCreateMore(); //max of 5
439: assert test.getGroupData("/TestGroup1").mayCreateMore(); //max of 2
440: assert 1 == test.getGroupData("/TestGroup1")
441: .getCreateableElements(); //1 left to create
442: assert 1 == test.getGroupData("/TestGroup1")
443: .getRemoveableElements(); //1 left to remove
444: assert 6 == test.getData("/TestGroup1").size();
445: assert 0 == test.getPropertyData(
446: "/TestGroup1/TestGroup1_1/TestProperty1_1_1")
447: .getCreateableElements(); //0 left
448: assert 1 == test.getPropertyData(
449: "/TestGroup1/TestGroup1_1/TestProperty1_1_1")
450: .getRemoveableElements(); //0 left
451: assert !(test
452: .getPropertyData("/TestGroup1/TestGroup1_1/TestProperty1_1_1")
453: .mayCreateMore()); //0 left
454: assert 1 == test.getGroupData("/TestGroup1/TestGroup1_2")
455: .getIndex();
456: FxPropertyData p = test
457: .getPropertyData("/TestGroup1/TestGroup1_2/TestGroup1_2_1/TestProperty1_2_1_1");
458: assert 1 == p.getIndex();
459: assert p.isEmpty();
460: assert p.isProperty();
461: assert "TESTPROPERTY1_2_1_1".equals(p.getAlias());
462: assert "TESTGROUP1_2_1".equals(p.getParent().getAlias());
463: assert p.getAssignmentMultiplicity().equals(
464: new FxMultiplicity(0, 2));
465: assert 4 == p.getIndices().length;
466: //check required with empty values
467: try {
468: test.checkValidity();
469: assert false : "checkValidity() succeeded when it should not!";
470: } catch (FxInvalidParameterException e) {
471: //ok
472: assert "ex.content.required.missing".equals(e
473: .getExceptionMessage().getKey());
474: }
475: //fill all required properties
476: FxString testValue = new FxString(FxLanguage.ENGLISH, TEST_EN);
477: testValue.setTranslation(FxLanguage.GERMAN, TEST_DE);
478: testValue.setTranslation(FxLanguage.FRENCH, TEST_FR);
479: testValue.setTranslation(FxLanguage.ITALIAN, TEST_IT);
480: test.setValue("/TestProperty2", testValue);
481: test.setValue("/TestProperty4", testValue);
482: //check required with empty groups
483: try {
484: test.checkValidity();
485: } catch (FxInvalidParameterException e) {
486: assert false : "checkValidity() did not succeed when it should!";
487: }
488: test.setValue("/TestGroup1[1]/TestProperty1_2", testValue);
489: try {
490: test.checkValidity();
491: assert false : "checkValidity() succeeded but /TestGroup1/TestProperty1_3 is missing!";
492: } catch (FxInvalidParameterException e) {
493: //ok
494: assert "ex.content.required.missing".equals(e
495: .getExceptionMessage().getKey());
496: }
497: test.setValue("/TestGroup1[1]/TestProperty1_3", testValue);
498: try {
499: test.checkValidity();
500: } catch (FxInvalidParameterException e) {
501: assert false : "checkValidity() did not succeed when it should!";
502: }
503: test.setValue("/TestGroup1/TestProperty1_2", testValue);
504: try {
505: test.checkValidity();
506: } catch (FxInvalidParameterException e) {
507: assert false : "checkValidity() did not succeed when it should!";
508: }
509: FxPK pk = co.save(test);
510: co.remove(pk);
511: //test /TestGroup1[2]...
512: FxGroupData gd = test.getGroupData("/TestGroup1");
513: assert gd.mayCreateMore();
514: assert 1 == gd.getCreateableElements(); //1 more should be createable
515: gd.createNew(FxData.POSITION_BOTTOM);
516: assert test.getGroupData("/TestGroup1[2]").isEmpty();
517: //should still be valid since [2] is empty
518: try {
519: test.checkValidity();
520: } catch (FxInvalidParameterException e) {
521: assert false : "checkValidity() did not succeed when it should!";
522: }
523: test.setValue("/TestGroup1[2]/TestProperty1_2", testValue);
524: test.setValue("/TestGroup1[2]/TestProperty1_3", testValue);
525: pk = co.save(test);
526: FxContent testLoad = co.load(pk);
527: assert TEST_IT.equals(((FxString) testLoad.getPropertyData(
528: "/TestGroup1[2]/TestProperty1_3").getValue())
529: .getTranslation(FxLanguage.ITALIAN));
530: co.remove(pk);
531: pk = co.save(test);
532: FxContent testLoad2 = co.load(pk);
533: FxNumber number = new FxNumber(true, FxLanguage.GERMAN, 42);
534: number.setTranslation(FxLanguage.ENGLISH, 43);
535: testLoad2.setValue("/TestNumber", number);
536: FxNumber numberSL = new FxNumber(false, FxLanguage.GERMAN, 12);
537: assert numberSL.getDefaultLanguage() == FxLanguage.SYSTEM_ID;
538: assert 12 == numberSL.getTranslation(FxLanguage.FRENCH);
539: numberSL.setTranslation(FxLanguage.ITALIAN, 13);
540: assert 13 == numberSL.getTranslation(FxLanguage.FRENCH);
541: testLoad2.setValue("/TestNumberSL", numberSL);
542: FxFloat fxFloat = new FxFloat(true, FxLanguage.GERMAN, 42.42f);
543: fxFloat.setTranslation(FxLanguage.ENGLISH, 43.43f);
544: testLoad2.setValue("/TestFloat", fxFloat);
545: assert 42 == ((FxNumber) testLoad2.getPropertyData(
546: "/TestNumber").getValue()).getDefaultTranslation() : "Default translation invalid (should be 42 for german, before save)";
547: assert 43 == ((FxNumber) testLoad2.getPropertyData(
548: "/TestNumber").getValue())
549: .getTranslation(FxLanguage.ENGLISH) : "English translation invalid (should be 43, before save)";
550: assert testLoad2.getPropertyData("/TestNumber").getValue()
551: .hasDefaultLanguage();
552: assert 42.42f == ((FxFloat) testLoad2.getPropertyData(
553: "/TestFloat").getValue()).getDefaultTranslation() : "Default translation invalid (should be 42.42f for german, before save)";
554: assert 43.43f == ((FxFloat) testLoad2.getPropertyData(
555: "/TestFloat").getValue())
556: .getTranslation(FxLanguage.ENGLISH) : "English translation invalid (should be 43.43f, before save)";
557: assert testLoad2.getPropertyData("/TestFloat").getValue()
558: .hasDefaultLanguage();
559: FxPK saved = co.save(testLoad2);
560: FxContent testLoad3 = co.load(saved);
561: assert 42 == ((FxNumber) testLoad3.getPropertyData(
562: "/TestNumber").getValue()).getDefaultTranslation() : "Default translation invalid (should be 42 for german, after load)";
563: assert 43 == ((FxNumber) testLoad3.getPropertyData(
564: "/TestNumber").getValue())
565: .getTranslation(FxLanguage.ENGLISH) : "English translation invalid (should be 43, after load)";
566: assert 13 == ((FxNumber) testLoad3.getPropertyData(
567: "/TestNumberSL").getValue())
568: .getTranslation(FxLanguage.ENGLISH) : "English translation invalid (should be 13, after load)";
569: assert !testLoad3.getPropertyData("/TestNumberSL").getValue()
570: .isMultiLanguage() : "Single language value expected";
571: assert testLoad3.getPropertyData("/TestNumber").getValue()
572: .hasDefaultLanguage() : "Missing default language after load";
573: assert 42.42f == ((FxFloat) testLoad3.getPropertyData(
574: "/TestFloat").getValue()).getDefaultTranslation() : "Default translation invalid (should be 42.42f for german, before save)";
575: assert 43.43f == ((FxFloat) testLoad3.getPropertyData(
576: "/TestFloat").getValue())
577: .getTranslation(FxLanguage.ENGLISH) : "English translation invalid (should be 43.43f, before save)";
578: assert testLoad3.getPropertyData("/TestFloat").getValue()
579: .hasDefaultLanguage() : "Missing default language after load";
580: assert TEST_IT.equals(((FxString) testLoad3.getPropertyData(
581: "/TestGroup1[2]/TestProperty1_3").getValue())
582: .getTranslation(FxLanguage.ITALIAN));
583: assert 1 == co.removeForType(testType.getId()) : "Only one instance should be removed!";
584: assert 0 == co.removeForType(testType.getId()) : "No instance should be left to remove!";
585:
586: co.initialize(testType.getId()).randomize();
587: }
588:
589: @Test
590: public void contentInitialize() throws Exception {
591: try {
592: FxType article = CacheAdmin.getEnvironment().getType(
593: TYPE_ARTICLE);
594: FxContent test = co.initialize(article.getId());
595: test.setAclId(CacheAdmin.getEnvironment().getACL(
596: "Article ACL").getId());
597: test.getData("/");
598: test.getData("/MYTITLE");
599: test.getData("/TEXT");
600: //test if shared message loading works
601: FxNoAccess noAccess = new FxNoAccess(getUserTicket(), null);
602: if (getUserTicket().getLanguage().getId() == FxLanguage.ENGLISH)
603: assert "Access denied!".equals(noAccess
604: .getDefaultTranslation()) : "Shared message loading failed! Expected [Access denied!] got: ["
605: + noAccess.getDefaultTranslation() + "]";
606: else if (getUserTicket().getLanguage().getId() == FxLanguage.GERMAN)
607: assert "Zugriff verweigert!".equals(noAccess
608: .getDefaultTranslation()) : "Shared message loading failed!";
609: } catch (FxApplicationException e) {
610: assert false : e.getMessage();
611: }
612: }
613:
614: @Test
615: public void defaultMultiplicity() throws Exception {
616: try {
617: FxType article = CacheAdmin.getEnvironment().getType(
618: TYPE_ARTICLE);
619:
620: FxPropertyAssignmentEdit pe = new FxPropertyAssignmentEdit(
621: (FxPropertyAssignment) article
622: .getAssignment("/TEXT"));
623: pe.setDefaultMultiplicity(2);
624: assert 2 == pe.getDefaultMultiplicity() : "Wrong default multiplicity";
625: try {
626: pe.setDefaultMultiplicity(-3);
627: assert false : "Wrong default multiplicities must not be settable!";
628: } catch (Exception e) {
629: //ok
630: }
631: //assert 0 == pe.getDefaultMultiplicity() : "Wrong default multiplicity";
632: try {
633: pe.setDefaultMultiplicity(3);
634: assert false : "Wrong default multiplicites must not be settable!";
635: } catch (Exception e) {
636: //ok
637: }
638: //assert 2 == pe.getDefaultMultiplicity() : "Wrong default multiplicity";
639: ass.save(pe, false);
640:
641: FxContent test = co.initialize(article.getId());
642: test.getData("/TEXT[1]");
643: test.getData("/TEXT[2]");
644: pe.setDefaultMultiplicity(1);
645: ass.save(pe, false);
646: test = co.initialize(article.getId());
647: test.getData("/TEXT[1]");
648: try {
649: test.getData("/TEXT[2]");
650: assert false : "No /TEXT[2] should exist!";
651: } catch (Exception e) {
652: //ok
653: }
654: } catch (FxApplicationException e) {
655: assert false : e.getMessage();
656: }
657: }
658:
659: @Test
660: public void contentCreate() throws Exception {
661: FxType article = CacheAdmin.getEnvironment().getType(
662: TYPE_ARTICLE);
663: FxContent test = co.initialize(article.getId());
664: test.setAclId(CacheAdmin.getEnvironment().getACL("Article ACL")
665: .getId());
666: FxString title = new FxString(FxLanguage.ENGLISH,
667: "Title english");
668: title.setTranslation(FxLanguage.GERMAN, "Titel deutsch");
669: FxString text = new FxString(FxLanguage.ENGLISH,
670: "Text english1");
671: text.setTranslation(FxLanguage.GERMAN, "Text deutsch1");
672: test.setValue("/MYTITLE", title);
673: test.setValue("/TEXT", text);
674: int titlePos = test.getPropertyData("/MYTITLE").getPos();
675: test.move("/MYTITLE", 1); //move title 1 position down
676: FxPropertyData pText = test.getPropertyData("/TEXT");
677: assert "Text english1".equals(((FxString) test.getPropertyData(
678: "/TEXT[1]").getValue())
679: .getTranslation(FxLanguage.ENGLISH));
680: if (pText.mayCreateMore()) {
681: FxPropertyData pText2 = (FxPropertyData) pText
682: .createNew(FxData.POSITION_BOTTOM);
683: pText2.setValue(new FxString(FxLanguage.ENGLISH,
684: "Text english2"));
685: assert "Text english2".equals(((FxString) test
686: .getPropertyData("/TEXT[2]").getValue())
687: .getTranslation(FxLanguage.ENGLISH));
688: }
689: // for( int i=0; i<100;i++)
690: FxPK pk = co.save(test);
691: FxContent comp = co.load(pk);
692: assert comp != null;
693: assert comp.getPk().getId() == pk.getId() : "Id failed";
694: assert comp.getPk().getId() == comp.getId() : "Id of content not equal the Id of contents pk";
695: assert comp.matchesPk(pk) : "matchesPk failed";
696: assert comp.matchesPk(new FxPK(pk.getId(), FxPK.MAX)) : "matchesPk for max version failed";
697: assert 1 == comp.getPk().getVersion() : "Version is not 1";
698: assert comp.getStepId() == test.getStepId() : "Step failed";
699: assert comp.getAclId() == test.getAclId() : "ACL failed";
700: assert comp.isMaxVersion() : "MaxVersion failed";
701: assert comp.isLiveVersion() == article.getWorkflow().getSteps()
702: .get(0).isLiveStep() : "LiveVersion failed. Expected:"
703: + article.getWorkflow().getSteps().get(0).isLiveStep()
704: + " Got:" + comp.isLiveVersion();
705: assert comp.getMainLanguage() == FxLanguage.ENGLISH : "MainLang failed";
706: assert comp.getLifeCycleInfo().getCreatorId() == getUserTicket()
707: .getUserId() : "CreatedBy failed";
708: assert "Text english1".equals(((FxString) comp.getPropertyData(
709: "/TEXT[1]").getValue())
710: .getTranslation(FxLanguage.ENGLISH)) : "Expected 'Text english1', got '"
711: + ((FxString) comp.getPropertyData("/TEXT[1]")
712: .getValue()).getTranslation(FxLanguage.ENGLISH)
713: + "'";
714: //test result of move
715: assert titlePos == comp.getPropertyData("/TEXT").getPos() : "Text[1] position should be "
716: + (titlePos)
717: + " but is "
718: + comp.getPropertyData("/TEXT").getPos();
719: assert titlePos + 1 == comp.getPropertyData("/MYTITLE[1]")
720: .getPos();
721: assert titlePos + 2 == comp.getPropertyData("/TEXT[2]")
722: .getPos();
723: FxPK pk2 = co.createNewVersion(comp);
724: assert 2 == pk2.getVersion();
725: FxPK pk3 = co.createNewVersion(comp);
726: assert 3 == pk3.getVersion();
727: FxContentVersionInfo cvi = co.getContentVersionInfo(pk3);
728: assert 3 == cvi.getLastModifiedVersion();
729: assert 3 == cvi.getLiveVersion();
730: assert 1 == cvi.getMinVersion();
731: co.removeVersion(new FxPK(pk.getId(), 1));
732: cvi = co.getContentVersionInfo(pk3);
733: System.out.println("After rm1: " + cvi);
734: assert 2 == cvi.getMinVersion();
735: assert 3 == cvi.getMaxVersion();
736: co.removeVersion(new FxPK(pk.getId(), 3));
737: cvi = co.getContentVersionInfo(pk3);
738: System.out.println("After rm3: " + cvi);
739: assert 2 == cvi.getMinVersion();
740: assert 2 == cvi.getMaxVersion();
741: assert !cvi.hasLiveVersion();
742: co.removeVersion(new FxPK(pk.getId(), 2));
743: try {
744: co.getContentVersionInfo(new FxPK(pk.getId()));
745: assert false : "VersionInfo available for a removed instance!";
746: } catch (FxApplicationException e) {
747: //ok
748: }
749: }
750:
751: @Test
752: public void binaryUploadTest() throws Exception {
753: // File testFile = new File("/home/mplesser/install/java/testng-5.1.zip");
754: File testFile = new File("test.file");
755: if (!testFile.exists())
756: testFile = new File("build/ui/flexive.war");
757: if (!testFile.exists())
758: return;
759: FileInputStream fis = new FileInputStream(testFile);
760: String handle = FxStreamUtils.uploadBinary(testFile.length(),
761: fis).getHandle();
762: System.out
763: .println("==Client done== Handle received: " + handle);
764: fis.close();
765: }
766:
767: @Test
768: public void typeValidityTest() throws Exception {
769: FxType t = CacheAdmin.getEnvironment().getType(TEST_TYPE);
770: assert t.isXPathValid("/", false) : "Root group should be valid for groups";
771: assert !t.isXPathValid("/", true) : "Root group should be invalid for properties";
772: assert t.isXPathValid("/TestProperty1", true);
773: assert !t.isXPathValid("/TestProperty1", false);
774: assert t.isXPathValid(TEST_TYPE + "/TestProperty1", true);
775: assert !t.isXPathValid(TEST_TYPE + "123/TestProperty1", true);
776: assert !t.isXPathValid("WrongType/TestProperty1", true);
777: assert !t
778: .isXPathValid(TEST_TYPE + "/TestProperty1/Dummy", true);
779: assert t.isXPathValid("/TestProperty1[1]", true);
780: assert !t.isXPathValid("/TestProperty1[2]", true);
781: assert t.isXPathValid("/TestGroup1[2]", false);
782: assert t.isXPathValid("/TestGroup1[1]/TestProperty1_3[4711]",
783: true);
784: assert !t.isXPathValid("/TestGroup1[1]/TestProperty1_3[4711]",
785: false);
786: assert t.isXPathValid(
787: "/TestGroup1[1]/TestGroup1_2[42]/TestProperty1_2_2[5]",
788: true);
789: assert !t.isXPathValid(
790: "/TestGroup1[1]/TestGroup1_2[42]/TestProperty1_2_2[5]",
791: false);
792: assert !t.isXPathValid(
793: "/TestGroup1[1]/TestGroup1_2[42]/TestProperty1_2_2[6]",
794: true);
795: }
796:
797: @Test
798: public void setValueTest() throws Exception {
799: FxType testType = CacheAdmin.getEnvironment()
800: .getType(TEST_TYPE);
801: FxContent test = co.initialize(testType.getId());
802: FxString testValue = new FxString("Hello world");
803: test.setValue(
804: "/TestGroup1[2]/TestGroup1_2[3]/TestProperty1_2_2[4]",
805: testValue);
806: assert test.getValue(
807: "/TestGroup1[2]/TestGroup1_2[3]/TestProperty1_2_2[4]")
808: .equals(testValue);
809: }
810:
811: @Test
812: public void deltaTest() throws Exception {
813: FxType testType = CacheAdmin.getEnvironment()
814: .getType(TEST_TYPE);
815: FxContent org = co.initialize(testType.getId());
816: FxString testValue1 = new FxString("Hello world1");
817: FxString testValue2 = new FxString("Hello world2");
818: //set required properties to allow saving ..
819: org.setValue("/TestProperty2[1]", testValue1);
820: org.setValue("/TestProperty4[1]", testValue1);
821:
822: //test properties
823: org.setValue("/TestProperty3[1]", testValue1);
824: org.setValue("/TestProperty3[2]", testValue2);
825: org.setValue("/TestProperty3[3]", testValue1);
826:
827: FxPK pk = co.save(org);
828: try {
829: org = co.load(pk);
830: FxContent test = co.load(pk);
831: FxDelta d = FxDelta.processDelta(org, test);
832: System.out.println(d.dump());
833: assert d.getAdds().size() == 0 : "Expected no adds, but got "
834: + d.getAdds().size();
835: assert d.getRemoves().size() == 0 : "Expected no deletes, but got "
836: + d.getRemoves().size();
837: assert d.getUpdates().size() == 0 : "Expected no updates, but got "
838: + d.getUpdates().size();
839:
840: test.remove("/TestProperty3[2]");
841: d = FxDelta.processDelta(org, test);
842: System.out.println(d.dump());
843: assert d.getAdds().size() == 0 : "Expected no adds, but got "
844: + d.getAdds().size();
845: assert d.getRemoves().size() == 1 : "Expected 1 deletes, but got "
846: + d.getRemoves().size();
847: assert d.getUpdates().size() == 1 : "Expected 1 updates, but got "
848: + d.getUpdates().size();
849: assert d.getRemoves().get(0).getXPath().equals(
850: "/TESTPROPERTY3[3]") : "Expected /TESTPROPERTY3[3] but got: "
851: + d.getRemoves().get(0).getXPath();
852: assert d.getUpdates().get(0).getXPath().equals(
853: "/TESTPROPERTY3[2]") : "Expected /TESTPROPERTY3[2] but got: "
854: + d.getUpdates().get(0).getXPath();
855:
856: test = co.load(pk);
857: test.setValue("/TestGroup1/TestProperty1_2", testValue1);
858: test.setValue("/TestGroup1/TestProperty1_3", testValue1);
859: test.getGroupData("/TestGroup1").removeEmptyEntries();
860: test.getGroupData("/TestGroup1").compactPositions(true);
861: d = FxDelta.processDelta(org, test);
862: System.out.println(d.dump());
863: assert d.changes() : "Expected some changes";
864: assert d.getAdds().size() == 3 : "Expected 3 (group + 2 properties) adds but got "
865: + d.getAdds().size();
866: assert d.getRemoves().size() == 0 : "Expected 0 deletes but got "
867: + d.getRemoves().size();
868: assert d.getUpdates().size() == 0 : "Expected 0 updates but got "
869: + d.getUpdates().size();
870: } finally {
871: co.remove(pk);
872: }
873: }
874:
875: /**
876: * Test compacting and removing empty entries
877: *
878: * @throws Exception on errors
879: */
880: @Test
881: public void compactTest() throws Exception {
882: FxContent org = co.initialize(TEST_TYPE);
883: FxString testValue1 = new FxString("Hello world1");
884: FxString testValue2 = new FxString("Hello world2");
885: FxString testValue3 = new FxString("Hello world3");
886: org.setValue("/TestProperty2[1]", testValue3);
887: org.setValue("/TestProperty4[1]", testValue1);
888: org.setValue("/TestProperty4[2]", testValue2);
889: org.getValue("/TestProperty4[1]").setEmpty();
890: org.setValue("/TestGroup1/TestProperty1_3[4]", testValue3);
891: org.getRootGroup().removeEmptyEntries();
892: org.getRootGroup().compact();
893: Assert.assertEquals(org.getValue("/TestProperty4[1]"),
894: testValue2);
895: Assert.assertFalse(org.containsValue("/TestProperty4[2]"));
896: Assert
897: .assertEquals(org
898: .getValue("/TestGroup1/TestProperty1_3[1]"),
899: testValue3);
900: Assert.assertFalse(org
901: .containsValue("/TestGroup1/TestProperty1_3[4]"));
902: }
903: }
|