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.configuration.SystemParameters;
037: import com.flexive.shared.exceptions.FxApplicationException;
038: import com.flexive.shared.exceptions.FxLogoutFailedException;
039: import com.flexive.shared.interfaces.TreeEngine;
040: import com.flexive.shared.tree.FxTreeMode;
041: import com.flexive.shared.tree.FxTreeNode;
042: import com.flexive.shared.tree.FxTreeNodeEdit;
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 org.testng.Assert;
047: import org.testng.annotations.AfterClass;
048: import org.testng.annotations.BeforeClass;
049: import org.testng.annotations.Test;
050:
051: /**
052: * Tree engine tests.
053: *
054: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
055: */
056: @Test(groups={"ejb","tree"})
057: public class FxTreeTest {
058: TreeEngine tree;
059:
060: @BeforeClass
061: public void beforeClass() throws Exception {
062: login(TestUsers.SUPERVISOR);
063: tree = EJBLookup.getTreeEngine();
064: EJBLookup.getDivisionConfigurationEngine().put(
065: SystemParameters.TREE_CHECKS_ENABLED, true);
066: }
067:
068: @AfterClass
069: public void afterClass() throws FxLogoutFailedException {
070: try {
071: tree.clear(FxTreeMode.Live);
072: tree.clear(FxTreeMode.Edit);
073: EJBLookup.getDivisionConfigurationEngine().put(
074: SystemParameters.TREE_CHECKS_ENABLED, false);
075: } catch (FxApplicationException e) {
076: //ignore
077: }
078: logout();
079: }
080:
081: /**
082: * Helper to build a node name
083: *
084: * @param number running number
085: * @return generated node name
086: */
087: private static String getNodeName(int number) {
088: return "Node" + number;
089: }
090:
091: /**
092: * Helper to build a node label
093: *
094: * @param number running number
095: * @return generated node label
096: */
097: private static FxString getNodeLabel(int number) {
098: return new FxString(true, "Node" + number + "Description");
099: }
100:
101: /**
102: * Edit tests
103: *
104: * @throws FxApplicationException on errors
105: */
106: @Test
107: public void treeTestEdit() throws FxApplicationException {
108: treeCRUD(FxTreeMode.Edit);
109: createPath(FxTreeMode.Edit);
110: }
111:
112: /**
113: * Live tests
114: *
115: * @throws FxApplicationException on errors
116: */
117: @Test
118: public void treeTestLive() throws FxApplicationException {
119: treeCRUD(FxTreeMode.Live);
120: createPath(FxTreeMode.Live);
121: }
122:
123: /**
124: * Test path creation
125: *
126: * @param mode FxTreeMode
127: * @throws FxApplicationException on errors
128: */
129: private void createPath(FxTreeMode mode)
130: throws FxApplicationException {
131: final long[] nodes = tree.createNodes(mode,
132: FxTreeNode.ROOT_NODE, 0, "my/virtual/directory");
133: Assert.assertEquals(nodes.length, 3,
134: "Should have created 3 nodes");
135: Assert.assertEquals(tree.getNode(mode, nodes[0]).getName(),
136: "my");
137: Assert.assertEquals(tree.getNode(mode, nodes[1]).getName(),
138: "virtual");
139: Assert.assertEquals(tree.getNode(mode, nodes[2]).getName(),
140: "directory");
141:
142: // create subfolder, reuse two folders
143: final long[] tmpNodes = tree.createNodes(mode,
144: FxTreeNode.ROOT_NODE, 0, "my/virtual/tmp");
145: Assert.assertEquals(tmpNodes.length, 3,
146: "Should have returned 3 nodes");
147: Assert.assertEquals(tree.getNode(mode, tmpNodes[0]).getName(),
148: "my");
149: Assert.assertEquals(tree.getNode(mode, tmpNodes[1]).getName(),
150: "virtual");
151: Assert.assertEquals(tree.getNode(mode, tmpNodes[2]).getName(),
152: "tmp");
153: Assert.assertEquals(nodes[0], tmpNodes[0]);
154: Assert.assertEquals(nodes[1], tmpNodes[1]);
155:
156: // use /my/virtual as root node
157: final long[] nestedNodes = tree.createNodes(mode, nodes[1], 0,
158: "/directory/tmp2");
159: Assert.assertEquals(nestedNodes.length, 2);
160: Assert.assertEquals(tree.getNode(mode, nestedNodes[0])
161: .getName(), "directory");
162: Assert.assertEquals(tree.getNode(mode, nestedNodes[1])
163: .getName(), "tmp2");
164: Assert.assertEquals(nestedNodes[0], nodes[2]); // check "directory" node
165: }
166:
167: /**
168: * Test most methods of the simplified API
169: *
170: * @param mode FxTreeMode to test
171: * @throws FxApplicationException on errors
172: */
173: private void treeCRUD(FxTreeMode mode)
174: throws FxApplicationException {
175: //clear the tree
176: tree.clear(mode);
177: assert tree.getNode(mode, FxTreeNode.ROOT_NODE)
178: .getTotalChildCount() == 0 : "Expected to have 0 children, got: ["
179: + tree.getNode(mode, FxTreeNode.ROOT_NODE)
180: .getTotalChildCount() + "]";
181: //create new node
182: FxTreeNodeEdit node1 = FxTreeNodeEdit.createNew(getNodeName(1));
183: node1.setLabel(getNodeLabel(1));
184: node1.setMode(mode);
185: long id1 = tree.save(node1);
186: assert tree.exist(mode, id1);
187: assert tree.getNode(mode, FxTreeNode.ROOT_NODE)
188: .getTotalChildCount() == 1 : "Expected to have 1 child, got: ["
189: + tree.getNode(mode, FxTreeNode.ROOT_NODE)
190: .getTotalChildCount() + "]";
191: //load and check if all well
192: FxTreeNode node1_loaded = tree.getNode(mode, id1);
193: assert node1_loaded.getName().equals(node1.getName());
194: Assert.assertEquals(node1_loaded.getLabel(), node1.getLabel());
195: //rename name
196: tree.save(new FxTreeNodeEdit(node1_loaded).setName("abcd"));
197: node1_loaded = tree.getNode(mode, id1);
198: assert node1_loaded.getName().equals("abcd") : "Expected [abcd] - got ["
199: + node1_loaded.getName() + "]";
200: Assert.assertEquals(node1_loaded.getLabel(), node1.getLabel());
201: //rename label
202: tree.save(new FxTreeNodeEdit(node1_loaded)
203: .setLabel(getNodeLabel(42)));
204: node1_loaded = tree.getNode(mode, id1);
205: assert node1_loaded.getName().equals("abcd");
206: assert node1_loaded.getLabel().equals(getNodeLabel(42));
207: //create child
208: FxTreeNodeEdit node1_1 = FxTreeNodeEdit.createNewChildNode(
209: node1_loaded).setName("1").setLabel(getNodeLabel(1))
210: .setMode(mode);
211: long id1_1 = tree.save(node1_1);
212: FxTreeNode node1_1_loaded = tree.getNode(mode, id1_1);
213: assert node1_1_loaded.getParentNodeId() == node1_loaded.getId();
214: //verify path
215: assert node1_1_loaded.getPath().equals("/abcd/1");
216: //verify label
217: Assert.assertEquals(tree
218: .getLabels(mode, node1_1_loaded.getId()).get(0), "/"
219: + getNodeLabel(42).getBestTranslation() + "/"
220: + getNodeLabel(1).getBestTranslation());
221: //create 2 other children for positioning tests
222: FxTreeNodeEdit node1_2 = FxTreeNodeEdit.createNewChildNode(
223: node1_loaded).setName("2").setLabel(getNodeLabel(2))
224: .setMode(mode);
225: long id1_2 = tree.save(node1_2);
226: FxTreeNode node1_2_loaded = tree.getNode(mode, id1_2);
227: //verify path
228: assert node1_2_loaded.getPath().equals("/abcd/2") : "Expected [/abcd/2] got: ["
229: + node1_2_loaded.getPath() + "]";
230: //verify label
231: assert ("/" + getNodeLabel(42).getBestTranslation() + "/" + getNodeLabel(
232: 2).getBestTranslation()).equals(tree.getLabels(mode,
233: node1_2_loaded.getId()).get(0)) : "Expected [/"
234: + getNodeLabel(42).getBestTranslation() + "/"
235: + getNodeLabel(2).getBestTranslation() + "] got: ["
236: + tree.getLabels(mode, node1_2_loaded.getId()).get(0)
237: + "]";
238: FxTreeNodeEdit node1_3 = FxTreeNodeEdit.createNewChildNode(
239: node1_loaded).setName("3").setLabel(getNodeLabel(3))
240: .setMode(mode);
241: long id1_3 = tree.save(node1_3);
242: FxTreeNode node1_3_loaded = tree.getNode(mode, id1_3);
243: assert tree.getNode(mode, FxTreeNode.ROOT_NODE)
244: .getTotalChildCount() == 4 : "Expected to have 4 children, got: ["
245: + tree.getNode(mode, FxTreeNode.ROOT_NODE)
246: .getTotalChildCount() + "]";
247: //verify positions - should be 1-2-3
248: assert node1_1_loaded.getPosition() == 0 : "Expected [0] got: ["
249: + node1_1_loaded.getPosition() + "]";
250: assert node1_2_loaded.getPosition() == 1 : "Expected [1] got: ["
251: + node1_2_loaded.getPosition() + "]";
252: assert node1_3_loaded.getPosition() == 2 : "Expected [2] got: ["
253: + node1_3_loaded.getPosition() + "]";
254:
255: //swap positions of 1 and 3 to net 3-2-1
256: tree.save(new FxTreeNodeEdit(node1_3_loaded).setPosition(-1));
257: tree.save(new FxTreeNodeEdit(node1_1_loaded).setPosition(100));
258: node1_1_loaded = tree.getNode(mode, id1_1);
259: node1_2_loaded = tree.getNode(mode, id1_2);
260: node1_3_loaded = tree.getNode(mode, id1_3);
261: assert node1_1_loaded.getPosition() == 2 : "Expected [2] got: ["
262: + node1_1_loaded.getPosition() + "]";
263: assert node1_2_loaded.getPosition() == 1 : "Expected [1] got: ["
264: + node1_2_loaded.getPosition() + "]";
265: assert node1_3_loaded.getPosition() == 0 : "Expected [0] got: ["
266: + node1_3_loaded.getPosition() + "]";
267: //3-2-1 => 3-1-2
268: tree.save(new FxTreeNodeEdit(node1_1_loaded).setPosition(1));
269: node1_1_loaded = tree.getNode(mode, id1_1);
270: node1_2_loaded = tree.getNode(mode, id1_2);
271: node1_3_loaded = tree.getNode(mode, id1_3);
272: assert node1_1_loaded.getPosition() == 1 : "Expected [1] got: ["
273: + node1_1_loaded.getPosition() + "]";
274: assert node1_2_loaded.getPosition() == 2 : "Expected [2] got: ["
275: + node1_2_loaded.getPosition() + "]";
276: assert node1_3_loaded.getPosition() == 0 : "Expected [0] got: ["
277: + node1_3_loaded.getPosition() + "]";
278: //3-1-2 => 1-2-3
279: tree.save(new FxTreeNodeEdit(node1_3_loaded).setPosition(4));
280: node1_1_loaded = tree.getNode(mode, id1_1);
281: node1_2_loaded = tree.getNode(mode, id1_2);
282: node1_3_loaded = tree.getNode(mode, id1_3);
283: assert node1_1_loaded.getPosition() == 0 : "Expected [0] got: ["
284: + node1_1_loaded.getPosition() + "]";
285: assert node1_2_loaded.getPosition() == 1 : "Expected [1] got: ["
286: + node1_2_loaded.getPosition() + "]";
287: assert node1_3_loaded.getPosition() == 2 : "Expected [2] got: ["
288: + node1_3_loaded.getPosition() + "]";
289: //delete 1_2 and check positions
290: tree.remove(new FxTreeNodeEdit(node1_2_loaded), true, false);
291: assert !tree.exist(mode, id1_2);
292: node1_1_loaded = tree.getNode(mode, id1_1);
293: node1_3_loaded = tree.getNode(mode, id1_3);
294: assert node1_1_loaded.getPosition() == 0 : "Expected [0] got: ["
295: + node1_1_loaded.getPosition() + "]";
296: assert node1_3_loaded.getPosition() == 1 : "Expected [1] got: ["
297: + node1_3_loaded.getPosition() + "]";
298: assert tree.getNode(mode, FxTreeNode.ROOT_NODE)
299: .getTotalChildCount() == 3 : "Expected to have 3 children, got: ["
300: + tree.getNode(mode, FxTreeNode.ROOT_NODE)
301: .getTotalChildCount() + "]";
302: if (mode == FxTreeMode.Live)
303: return; //children are to be removed in live mode
304: //delete parent but not children and check if they moved up in hierarchy
305: tree.remove(new FxTreeNodeEdit(node1_loaded), true, false);
306: node1_1_loaded = tree.getNode(mode, id1_1);
307: node1_3_loaded = tree.getNode(mode, id1_3);
308: assert node1_1_loaded.getParentNodeId() == FxTreeNode.ROOT_NODE;
309: assert node1_3_loaded.getParentNodeId() == FxTreeNode.ROOT_NODE;
310: //attach 1_3 as child to 1_2
311: tree.save(new FxTreeNodeEdit(node1_3_loaded)
312: .setParentNodeId(node1_1_loaded.getId()));
313: node1_1_loaded = tree.getTree(mode, id1_1, 3);
314: node1_3_loaded = tree.getNode(mode, id1_3);
315: assert node1_1_loaded.getChildren().size() == 1;
316: assert node1_1_loaded.getChildren().get(0).getId() == node1_3_loaded
317: .getId();
318: assert node1_3_loaded.getPath().equals("/1/3") : "Expected [/1/3] got: ["
319: + node1_3_loaded.getPath() + "]";
320: //delete 1_1 with children and check that 1_3 is gone too
321: tree.remove(new FxTreeNodeEdit(node1_1_loaded), true, true);
322: assert !tree.exist(mode, id1_3);
323: assert tree.getNode(mode, FxTreeNode.ROOT_NODE)
324: .getTotalChildCount() == 0 : "Expected to have 0 children, got: ["
325: + tree.getNode(mode, FxTreeNode.ROOT_NODE)
326: .getTotalChildCount() + "]";
327: tree.clear(mode);
328: }
329:
330: /**
331: * Test node and subtree activation
332: *
333: * @throws FxApplicationException on errors
334: */
335: @Test
336: public void activationTest() throws FxApplicationException {
337: //clear live and edit tree
338: tree.clear(FxTreeMode.Edit);
339: tree.clear(FxTreeMode.Live);
340: //test activation without subtree
341: long[] ids = tree.createNodes(FxTreeMode.Edit,
342: FxTreeNode.ROOT_NODE, 0, "/Test1/Test2/Test3");
343: tree.activate(FxTreeMode.Edit, ids[0], false);
344: Assert.assertEquals(true, tree.exist(FxTreeMode.Live, ids[0]));
345: Assert.assertEquals(false, tree.exist(FxTreeMode.Live, ids[1]));
346: Assert.assertEquals(false, tree.exist(FxTreeMode.Live, ids[2]));
347: //test activation with subtree
348: ids = tree.createNodes(FxTreeMode.Edit, FxTreeNode.ROOT_NODE,
349: 0, "/ATest1/ATest2/ATest3");
350: tree.activate(FxTreeMode.Edit, ids[0], true);
351: Assert.assertEquals(true, tree.exist(FxTreeMode.Live, ids[0]));
352: Assert.assertEquals(true, tree.exist(FxTreeMode.Live, ids[1]));
353: Assert.assertEquals(true, tree.exist(FxTreeMode.Live, ids[2]));
354: //there should be 2 nodes not active (Test2 and Test3), see if they get activated after activating all
355: tree.activate(FxTreeMode.Edit, FxTreeNode.ROOT_NODE, true);
356: Assert.assertEquals(6, tree.getTree(FxTreeMode.Live,
357: FxTreeNode.ROOT_NODE, 3).getTotalChildCount());
358: }
359:
360: /**
361: * Test behaviour when contents that are references of a tree node are removed
362: *
363: * @throws FxApplicationException on errors
364: */
365: @Test
366: public void contentRemoval() throws FxApplicationException {
367: //clear live and edit tree
368: tree.clear(FxTreeMode.Edit);
369: tree.clear(FxTreeMode.Live);
370: //TODO: code me!
371: }
372:
373: /**
374: * Create a lot of nodes trying to provoke errors
375: *
376: * @throws FxApplicationException on errors
377: */
378: @Test
379: public void massCreateTest() throws FxApplicationException {
380: FxTreeMode mode = FxTreeMode.Edit;
381: tree.clear(mode);
382: String path = "/t1/t2/t3/t4/t5/t6/t7/t8/t9/t10";
383: for (int i = 0; i < 3; i++) {
384: path = path + "/f" + (i + 1);
385: tree.createNodes(mode, FxTreeNode.ROOT_NODE, 0, path);
386: FxTreeNode parent = tree.getNode(mode, tree.getIdByPath(
387: mode, path));
388: createSubNodes(parent, 40);
389: }
390: }
391:
392: /**
393: * Create count subnodes attached to parent
394: *
395: * @param parent parent node
396: * @param count number of subnodes to create
397: * @throws FxApplicationException on errors
398: */
399: private void createSubNodes(FxTreeNode parent, int count)
400: throws FxApplicationException {
401: FxTreeNodeEdit node;
402: for (int i = 0; i < count; i++) {
403: // System.out.println("Creating "+parent.getPath()+"/Node"+i);
404: node = FxTreeNodeEdit.createNewChildNode(parent).setName(
405: "Node" + i);
406: tree.save(node);
407: }
408: }
409: }
|