KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > copy > CopyServiceImplTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.copy;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.action.evaluator.NoConditionEvaluator;
26 import org.alfresco.repo.action.executer.AddFeaturesActionExecuter;
27 import org.alfresco.repo.action.executer.CopyActionExecuter;
28 import org.alfresco.repo.action.executer.MoveActionExecuter;
29 import org.alfresco.repo.dictionary.DictionaryDAO;
30 import org.alfresco.repo.dictionary.M2Aspect;
31 import org.alfresco.repo.dictionary.M2Association;
32 import org.alfresco.repo.dictionary.M2ChildAssociation;
33 import org.alfresco.repo.dictionary.M2Model;
34 import org.alfresco.repo.dictionary.M2Property;
35 import org.alfresco.repo.dictionary.M2Type;
36 import org.alfresco.repo.rule.RuleModel;
37 import org.alfresco.repo.security.authentication.AuthenticationComponent;
38 import org.alfresco.service.cmr.action.Action;
39 import org.alfresco.service.cmr.action.ActionCondition;
40 import org.alfresco.service.cmr.action.ActionService;
41 import org.alfresco.service.cmr.dictionary.DataTypeDefinition;
42 import org.alfresco.service.cmr.repository.AssociationRef;
43 import org.alfresco.service.cmr.repository.ChildAssociationRef;
44 import org.alfresco.service.cmr.repository.ContentData;
45 import org.alfresco.service.cmr.repository.ContentReader;
46 import org.alfresco.service.cmr.repository.ContentService;
47 import org.alfresco.service.cmr.repository.ContentWriter;
48 import org.alfresco.service.cmr.repository.CopyService;
49 import org.alfresco.service.cmr.repository.NodeRef;
50 import org.alfresco.service.cmr.repository.NodeService;
51 import org.alfresco.service.cmr.repository.StoreRef;
52 import org.alfresco.service.cmr.rule.Rule;
53 import org.alfresco.service.cmr.rule.RuleService;
54 import org.alfresco.service.cmr.rule.RuleType;
55 import org.alfresco.service.namespace.NamespaceService;
56 import org.alfresco.service.namespace.QName;
57 import org.alfresco.service.namespace.RegexQNamePattern;
58 import org.alfresco.util.BaseSpringTest;
59
60 /**
61  * Node operations service unit tests
62  *
63  * @author Roy Wetherall
64  */

65 public class CopyServiceImplTest extends BaseSpringTest
66 {
67     /**
68      * Services used by the tests
69      */

70     private NodeService nodeService;
71     private CopyService copyService;
72     private DictionaryDAO dictionaryDAO;
73     private ContentService contentService;
74     private RuleService ruleService;
75     private ActionService actionService;
76     private AuthenticationComponent authenticationComponent;
77     
78     /**
79      * Data used by the tests
80      */

81     private StoreRef storeRef;
82     private NodeRef sourceNodeRef;
83     private NodeRef rootNodeRef;
84     private NodeRef targetNodeRef;
85     private NodeRef nonPrimaryChildNodeRef;
86     private NodeRef childNodeRef;
87     private NodeRef destinationNodeRef;
88     
89     /**
90      * Types and properties used by the tests
91      */

92     private static final String JavaDoc TEST_TYPE_NAMESPACE = "testTypeNamespaceURI";
93     private static final QName TEST_TYPE_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testType");
94     private static final QName PROP1_QNAME_MANDATORY = QName.createQName(TEST_TYPE_NAMESPACE, "prop1Mandatory");
95     private static final QName PROP2_QNAME_OPTIONAL = QName.createQName(TEST_TYPE_NAMESPACE, "prop2Optional");
96     
97     private static final QName TEST_ASPECT_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testAspect");
98     private static final QName PROP3_QNAME_MANDATORY = QName.createQName(TEST_TYPE_NAMESPACE, "prop3Mandatory");
99     private static final QName PROP4_QNAME_OPTIONAL = QName.createQName(TEST_TYPE_NAMESPACE, "prop4Optional");
100     
101     private static final QName PROP_QNAME_MY_NODE_REF = QName.createQName(TEST_TYPE_NAMESPACE, "myNodeRef");
102     private static final QName PROP_QNAME_MY_ANY = QName.createQName(TEST_TYPE_NAMESPACE, "myAny");
103     
104     private static final QName TEST_MANDATORY_ASPECT_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testMandatoryAspect");
105     private static final QName PROP5_QNAME_MANDATORY = QName.createQName(TEST_TYPE_NAMESPACE, "prop5Mandatory");
106     
107     private static final String JavaDoc TEST_VALUE_1 = "testValue1";
108     private static final String JavaDoc TEST_VALUE_2 = "testValue2";
109     private static final String JavaDoc TEST_VALUE_3 = "testValue3";
110     
111     private static final QName TEST_CHILD_ASSOC_TYPE_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "contains");
112     private static final QName TEST_CHILD_ASSOC_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testChildAssocName");
113     private static final QName TEST_ASSOC_TYPE_QNAME = QName.createQName(TEST_TYPE_NAMESPACE, "testAssocName");
114     private static final QName TEST_CHILD_ASSOC_QNAME2 = QName.createQName(TEST_TYPE_NAMESPACE, "testChildAssocName2");
115     
116     private static final ContentData CONTENT_DATA_TEXT = new ContentData(null, "text/plain", 0L, "UTF-8");
117     
118     /**
119      * Test content
120      */

121     private static final String JavaDoc SOME_CONTENT = "This is some content ...";
122     
123     /**
124      * Sets the meta model DAO
125      *
126      * @param dictionaryDAO the meta model DAO
127      */

128     public void setDictionaryDAO(DictionaryDAO dictionaryDAO)
129     {
130         this.dictionaryDAO = dictionaryDAO;
131     }
132     
133     /**
134      * On setup in transaction implementation
135      */

136     @Override JavaDoc
137     protected void onSetUpInTransaction()
138         throws Exception JavaDoc
139     {
140         // Set the services
141
this.nodeService = (NodeService)this.applicationContext.getBean("dbNodeService");
142         this.copyService = (CopyService)this.applicationContext.getBean("copyService");
143         this.contentService = (ContentService)this.applicationContext.getBean("contentService");
144         this.ruleService = (RuleService)this.applicationContext.getBean("ruleService");
145         this.actionService = (ActionService)this.applicationContext.getBean("actionService");
146         this.authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
147         
148         this.authenticationComponent.setSystemUserAsCurrentUser();
149         
150         // Create the test model
151
createTestModel();
152         
153         // Create the store and get the root node reference
154
this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
155         this.rootNodeRef = this.nodeService.getRootNode(storeRef);
156         
157         // Create the node used for copying
158
ChildAssociationRef childAssocRef = this.nodeService.createNode(
159                 rootNodeRef,
160                 ContentModel.ASSOC_CHILDREN,
161                 QName.createQName("{test}test"),
162                 TEST_TYPE_QNAME,
163                 createTypePropertyBag());
164         this.sourceNodeRef = childAssocRef.getChildRef();
165         
166         // Create another bag of properties
167
Map JavaDoc<QName, Serializable JavaDoc> aspectProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
168         aspectProperties.put(PROP3_QNAME_MANDATORY, TEST_VALUE_1);
169         aspectProperties.put(PROP4_QNAME_OPTIONAL, TEST_VALUE_2);
170         
171         // Apply the test aspect
172
this.nodeService.addAspect(
173                 this.sourceNodeRef,
174                 TEST_ASPECT_QNAME,
175                 aspectProperties);
176         
177         this.nodeService.addAspect(sourceNodeRef, ContentModel.ASPECT_TITLED, null);
178         
179         // Add a child
180
ChildAssociationRef temp3 =this.nodeService.createNode(
181                 this.sourceNodeRef,
182                 TEST_CHILD_ASSOC_TYPE_QNAME,
183                 TEST_CHILD_ASSOC_QNAME,
184                 TEST_TYPE_QNAME,
185                 createTypePropertyBag());
186         this.childNodeRef = temp3.getChildRef();
187         
188         // Add a child that is primary
189
ChildAssociationRef temp2 = this.nodeService.createNode(
190                 rootNodeRef,
191                 ContentModel.ASSOC_CHILDREN,
192                 QName.createQName("{test}testNonPrimaryChild"),
193                 TEST_TYPE_QNAME,
194                 createTypePropertyBag());
195         
196         this.nonPrimaryChildNodeRef = temp2.getChildRef();
197         this.nodeService.addChild(
198                 this.sourceNodeRef,
199                 this.nonPrimaryChildNodeRef,
200                 TEST_CHILD_ASSOC_TYPE_QNAME,
201                 TEST_CHILD_ASSOC_QNAME2);
202         
203         // Add a target assoc
204
ChildAssociationRef temp = this.nodeService.createNode(
205                 rootNodeRef,
206                 ContentModel.ASSOC_CHILDREN,
207                 QName.createQName("{test}testAssoc"),
208                 TEST_TYPE_QNAME,
209                 createTypePropertyBag());
210         this.targetNodeRef = temp.getChildRef();
211         this.nodeService.createAssociation(this.sourceNodeRef, this.targetNodeRef, TEST_ASSOC_TYPE_QNAME);
212         
213         // Create a node we can use as the destination in a copy
214
Map JavaDoc<QName, Serializable JavaDoc> destinationProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
215         destinationProps.put(PROP1_QNAME_MANDATORY, TEST_VALUE_1);
216         destinationProps.put(PROP5_QNAME_MANDATORY, TEST_VALUE_3);
217         destinationProps.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
218         ChildAssociationRef temp5 = this.nodeService.createNode(
219                 this.rootNodeRef,
220                 ContentModel.ASSOC_CHILDREN,
221                 QName.createQName("{test}testDestinationNode"),
222                 TEST_TYPE_QNAME,
223                 destinationProps);
224         this.destinationNodeRef = temp5.getChildRef();
225     }
226     
227     @Override JavaDoc
228     protected void onTearDownInTransaction() throws Exception JavaDoc
229     {
230         authenticationComponent.clearCurrentSecurityContext();
231         super.onTearDownInTransaction();
232     }
233     
234     /**
235      * Helper method that creates a bag of properties for the test type
236      *
237      * @return bag of properties
238      */

239     private Map JavaDoc<QName, Serializable JavaDoc> createTypePropertyBag()
240     {
241         Map JavaDoc<QName, Serializable JavaDoc> result = new HashMap JavaDoc<QName, Serializable JavaDoc>();
242         result.put(PROP1_QNAME_MANDATORY, TEST_VALUE_1);
243         result.put(PROP2_QNAME_OPTIONAL, TEST_VALUE_2);
244         result.put(PROP5_QNAME_MANDATORY, TEST_VALUE_3);
245         result.put(ContentModel.PROP_CONTENT, CONTENT_DATA_TEXT);
246         return result;
247     }
248     
249     /**
250      * Creates the test model used by the tests
251      */

252     private void createTestModel()
253     {
254         M2Model model = M2Model.createModel("test:nodeoperations");
255         model.createNamespace(TEST_TYPE_NAMESPACE, "test");
256         model.createImport(NamespaceService.DICTIONARY_MODEL_1_0_URI, NamespaceService.DICTIONARY_MODEL_PREFIX);
257         model.createImport(NamespaceService.SYSTEM_MODEL_1_0_URI, NamespaceService.SYSTEM_MODEL_PREFIX);
258         model.createImport(NamespaceService.CONTENT_MODEL_1_0_URI, NamespaceService.CONTENT_MODEL_PREFIX);
259
260         M2Type testType = model.createType("test:" + TEST_TYPE_QNAME.getLocalName());
261         testType.setParentName("cm:" + ContentModel.TYPE_CONTENT.getLocalName());
262         
263         M2Property prop1 = testType.createProperty("test:" + PROP1_QNAME_MANDATORY.getLocalName());
264         prop1.setMandatory(true);
265         prop1.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
266         prop1.setMultiValued(false);
267         
268         M2Property prop2 = testType.createProperty("test:" + PROP2_QNAME_OPTIONAL.getLocalName());
269         prop2.setMandatory(false);
270         prop2.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
271         prop2.setMandatory(false);
272         
273         M2Property propNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_NODE_REF.getLocalName());
274         propNodeRef.setMandatory(false);
275         propNodeRef.setType("d:" + DataTypeDefinition.NODE_REF.getLocalName());
276         propNodeRef.setMandatory(false);
277         
278         M2Property propAnyNodeRef = testType.createProperty("test:" + PROP_QNAME_MY_ANY.getLocalName());
279         propAnyNodeRef.setMandatory(false);
280         propAnyNodeRef.setType("d:" + DataTypeDefinition.ANY.getLocalName());
281         propAnyNodeRef.setMandatory(false);
282         
283         M2ChildAssociation childAssoc = testType.createChildAssociation("test:" + TEST_CHILD_ASSOC_TYPE_QNAME.getLocalName());
284         childAssoc.setTargetClassName("sys:base");
285         childAssoc.setTargetMandatory(false);
286         
287         M2Association assoc = testType.createAssociation("test:" + TEST_ASSOC_TYPE_QNAME.getLocalName());
288         assoc.setTargetClassName("sys:base");
289         assoc.setTargetMandatory(false);
290         
291         M2Aspect testAspect = model.createAspect("test:" + TEST_ASPECT_QNAME.getLocalName());
292         
293         M2Property prop3 = testAspect.createProperty("test:" + PROP3_QNAME_MANDATORY.getLocalName());
294         prop3.setMandatory(true);
295         prop3.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
296         prop3.setMultiValued(false);
297         
298         M2Property prop4 = testAspect.createProperty("test:" + PROP4_QNAME_OPTIONAL.getLocalName());
299         prop4.setMandatory(false);
300         prop4.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
301         prop4.setMultiValued(false);
302
303         M2Aspect testMandatoryAspect = model.createAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
304         M2Property prop5 = testMandatoryAspect.createProperty("test:" + PROP5_QNAME_MANDATORY.getLocalName());
305         prop5.setType("d:" + DataTypeDefinition.TEXT.getLocalName());
306         prop5.setMandatory(true);
307
308         testType.addMandatoryAspect("test:" + TEST_MANDATORY_ASPECT_QNAME.getLocalName());
309         
310         dictionaryDAO.putModel(model);
311     }
312     
313     /**
314      * Test copy new node within store
315      */

316     public void testCopyToNewNode()
317     {
318         // Copy to new node without copying children
319
NodeRef copy = this.copyService.copy(
320                 this.sourceNodeRef,
321                 this.rootNodeRef,
322                 ContentModel.ASSOC_CHILDREN,
323                 QName.createQName("{test}copyAssoc"));
324         checkCopiedNode(this.sourceNodeRef, copy, true, true, false);
325         
326         // Copy to new node, copying children
327
NodeRef copy2 = this.copyService.copy(
328                 this.sourceNodeRef,
329                 this.rootNodeRef,
330                 ContentModel.ASSOC_CHILDREN,
331                 QName.createQName("{test}copyAssoc"),
332                 true);
333         checkCopiedNode(this.sourceNodeRef, copy2, true, true, true);
334         
335         // Check that a copy of a copy works correctly
336
NodeRef copyOfCopy = this.copyService.copy(
337                 copy,
338                 this.rootNodeRef,
339                 ContentModel.ASSOC_CHILDREN,
340                 QName.createQName("{test}copyOfCopy"));
341         checkCopiedNode(copy, copyOfCopy, true, true, false);
342         
343         // TODO check copying from a versioned copy
344
// TODO check copying from a lockable copy
345

346         // Check copying from a node with content
347
ContentWriter contentWriter = this.contentService.getWriter(this.sourceNodeRef, ContentModel.PROP_CONTENT, true);
348         contentWriter.putContent(SOME_CONTENT);
349         NodeRef copyWithContent = this.copyService.copy(
350                 this.sourceNodeRef,
351                 this.rootNodeRef,
352                 ContentModel.ASSOC_CHILDREN,
353                 QName.createQName("{test}copyWithContent"));
354         checkCopiedNode(this.sourceNodeRef, copyWithContent, true, true, false);
355         ContentReader contentReader = this.contentService.getReader(copyWithContent, ContentModel.PROP_CONTENT);
356         assertNotNull(contentReader);
357         assertEquals(SOME_CONTENT, contentReader.getContentString());
358         
359         // TODO check copying to a different store
360

361         //System.out.println(
362
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
363
}
364     
365     public void testCopyNodeWithRules()
366     {
367         // Create a new rule and add it to the source noderef
368
Rule rule = this.ruleService.createRule(RuleType.INBOUND);
369         
370         Map JavaDoc<String JavaDoc, Serializable JavaDoc> props = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
371         props.put(AddFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
372         Action action = this.actionService.createAction(AddFeaturesActionExecuter.NAME, props);
373         rule.addAction(action);
374         
375         ActionCondition actionCondition = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
376         rule.addActionCondition(actionCondition);
377         
378         this.ruleService.saveRule(this.sourceNodeRef, rule);
379         
380         //System.out.println(
381
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
382
//System.out.println(" ------------------------------ ");
383

384         // Now copy the node that has rules associated with it
385
NodeRef copy = this.copyService.copy(
386                 this.sourceNodeRef,
387                 this.rootNodeRef,
388                 ContentModel.ASSOC_CHILDREN,
389                 QName.createQName("{test}withRulesCopy"),
390                 true);
391         
392         //System.out.println(
393
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
394

395         checkCopiedNode(this.sourceNodeRef, copy, true, true, true);
396         
397         //assertTrue(this.configurableService.isConfigurable(copy));
398
//assertNotNull(this.configurableService.getConfigurationFolder(copy));
399
//assertFalse(this.configurableService.getConfigurationFolder(this.sourceNodeRef) == this.configurableService.getConfigurationFolder(copy));
400

401         assertTrue(this.nodeService.hasAspect(copy, RuleModel.ASPECT_RULES));
402         assertTrue(this.ruleService.hasRules(copy));
403         assertTrue(this.ruleService.rulesEnabled(copy));
404         List JavaDoc<Rule> copiedRules = this.ruleService.getRules(copy);
405         assertEquals(1, copiedRules.size());
406         Rule copiedRule = copiedRules.get(0);
407         assertFalse(rule.getId() == copiedRule.getId());
408         assertEquals(rule.getAction(0).getActionDefinitionName(), copiedRule.getAction(0).getActionDefinitionName());
409         
410         // Now copy the node without copying the children and check that the rules have been copied
411
NodeRef copy2 = this.copyService.copy(
412                 this.sourceNodeRef,
413                 this.rootNodeRef,
414                 ContentModel.ASSOC_CHILDREN,
415                 QName.createQName("{test}withRuleCopyNoChildren"),
416                 false);
417         
418 // System.out.println(
419
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
420

421         checkCopiedNode(this.sourceNodeRef, copy2, true, true, false);
422         
423         //assertTrue(this.configurableService.isConfigurable(copy2));
424
//assertNotNull(this.configurableService.getConfigurationFolder(copy2));
425
//assertFalse(this.configurableService.getConfigurationFolder(this.sourceNodeRef) == this.configurableService.getConfigurationFolder(copy2));
426

427         assertTrue(this.nodeService.hasAspect(copy2, RuleModel.ASPECT_RULES));
428         assertTrue(this.ruleService.hasRules(copy2));
429         assertTrue(this.ruleService.rulesEnabled(copy2));
430         List JavaDoc<Rule> copiedRules2 = this.ruleService.getRules(copy2);
431         assertEquals(1, copiedRules.size());
432         Rule copiedRule2 = copiedRules2.get(0);
433         assertFalse(rule.getId() == copiedRule2.getId());
434         assertEquals(rule.getAction(0).getActionDefinitionName(), copiedRule2.getAction(0).getActionDefinitionName());
435     }
436     
437     public void testCopyToExistingNode()
438     {
439         // Copy nodes within the same store
440
this.copyService.copy(this.sourceNodeRef, this.destinationNodeRef);
441         checkCopiedNode(this.sourceNodeRef, this.destinationNodeRef, false, true, false);
442         
443         // TODO check copying from a copy
444
// TODO check copying from a versioned copy
445
// TODO check copying from a lockable copy
446
// TODO check copying from a node with content
447

448         // TODO check copying nodes between stores
449

450         //System.out.println(
451
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
452
}
453     
454     /**
455      * Test a potentially recursive copy
456      */

457     public void testRecursiveCopy()
458     {
459         // Need to create a potentially recursive node structure
460
NodeRef nodeOne = this.nodeService.createNode(
461                 this.rootNodeRef,
462                 ContentModel.ASSOC_CHILDREN,
463                 ContentModel.ASSOC_CHILDREN,
464                 ContentModel.TYPE_CONTAINER).getChildRef();
465         NodeRef nodeTwo = this.nodeService.createNode(
466                 nodeOne,
467                 ContentModel.ASSOC_CHILDREN,
468                 ContentModel.ASSOC_CHILDREN,
469                 ContentModel.TYPE_CONTAINER).getChildRef();
470         NodeRef nodeThree = this.nodeService.createNode(
471                 nodeTwo,
472                 ContentModel.ASSOC_CHILDREN,
473                 ContentModel.ASSOC_CHILDREN,
474                 ContentModel.TYPE_CONTAINER).getChildRef();
475         
476         // Issue a potentialy recursive copy
477
this.copyService.copy(nodeOne, nodeThree, ContentModel.ASSOC_CHILDREN, ContentModel.ASSOC_CHILDREN, true);
478         
479         //System.out.println(
480
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
481
}
482     
483     /**
484      * Test that realtive links between nodes are restored once the copy is completed
485      */

486     public void testRelativeLinks()
487     {
488         QName nodeOneAssocName = QName.createQName("{test}nodeOne");
489         QName nodeTwoAssocName = QName.createQName("{test}nodeTwo");
490         QName nodeThreeAssocName = QName.createQName("{test}nodeThree");
491         QName nodeFourAssocName = QName.createQName("{test}nodeFour");
492         
493         NodeRef nodeOne = this.nodeService.createNode(
494                 this.rootNodeRef,
495                 ContentModel.ASSOC_CHILDREN,
496                 nodeOneAssocName,
497                 TEST_TYPE_QNAME).getChildRef();
498         NodeRef nodeTwo = this.nodeService.createNode(
499                 nodeOne,
500                 TEST_CHILD_ASSOC_TYPE_QNAME,
501                 nodeTwoAssocName,
502                 TEST_TYPE_QNAME).getChildRef();
503         NodeRef nodeThree = this.nodeService.createNode(
504                 nodeTwo,
505                 TEST_CHILD_ASSOC_TYPE_QNAME,
506                 nodeThreeAssocName,
507                 TEST_TYPE_QNAME).getChildRef();
508         NodeRef nodeFour = this.nodeService.createNode(
509                 nodeOne,
510                 TEST_CHILD_ASSOC_TYPE_QNAME,
511                 nodeFourAssocName,
512                 TEST_TYPE_QNAME).getChildRef();
513         this.nodeService.addChild(nodeFour, nodeThree, TEST_CHILD_ASSOC_TYPE_QNAME, TEST_CHILD_ASSOC_QNAME);
514         this.nodeService.createAssociation(nodeTwo, nodeThree, TEST_ASSOC_TYPE_QNAME);
515         this.nodeService.setProperty(nodeOne, PROP_QNAME_MY_NODE_REF, nodeThree);
516         this.nodeService.setProperty(nodeOne, PROP_QNAME_MY_ANY, nodeThree);
517         
518         // Make node one actionable with a rule to copy nodes into node two
519
Map JavaDoc<String JavaDoc, Serializable JavaDoc> params = new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>(1);
520         params.put(MoveActionExecuter.PARAM_DESTINATION_FOLDER, nodeTwo);
521         params.put(MoveActionExecuter.PARAM_ASSOC_TYPE_QNAME, TEST_CHILD_ASSOC_TYPE_QNAME);
522         params.put(MoveActionExecuter.PARAM_ASSOC_QNAME, QName.createQName("{test}ruleCopy"));
523         Rule rule = this.ruleService.createRule(RuleType.INBOUND);
524         ActionCondition condition = this.actionService.createActionCondition(NoConditionEvaluator.NAME);
525         rule.addActionCondition(condition);
526         Action action = this.actionService.createAction(CopyActionExecuter.NAME, params);
527         rule.addAction(action);
528         this.ruleService.saveRule(nodeOne, rule);
529         
530         // Do a deep copy
531
NodeRef nodeOneCopy = this.copyService.copy(nodeOne, this.rootNodeRef, ContentModel.ASSOC_CHILDREN, QName.createQName("{test}copiedNodeOne"), true);
532         NodeRef nodeTwoCopy = null;
533         NodeRef nodeThreeCopy = null;
534         NodeRef nodeFourCopy = null;
535         
536         //System.out.println(
537
// NodeStoreInspector.dumpNodeStore(this.nodeService, this.storeRef));
538

539         List JavaDoc<ChildAssociationRef> nodeOneCopyChildren = this.nodeService.getChildAssocs(nodeOneCopy);
540         assertNotNull(nodeOneCopyChildren);
541         assertEquals(3, nodeOneCopyChildren.size());
542         for (ChildAssociationRef nodeOneCopyChild : nodeOneCopyChildren)
543         {
544             if (nodeOneCopyChild.getQName().equals(nodeTwoAssocName) == true)
545             {
546                 nodeTwoCopy = nodeOneCopyChild.getChildRef();
547                                 
548                 List JavaDoc<ChildAssociationRef> nodeTwoCopyChildren = this.nodeService.getChildAssocs(nodeTwoCopy);
549                 assertNotNull(nodeTwoCopyChildren);
550                 assertEquals(1, nodeTwoCopyChildren.size());
551                 for (ChildAssociationRef nodeTwoCopyChild : nodeTwoCopyChildren)
552                 {
553                     if (nodeTwoCopyChild.getQName().equals(nodeThreeAssocName) == true)
554                     {
555                         nodeThreeCopy = nodeTwoCopyChild.getChildRef();
556                     }
557                 }
558             }
559             else if (nodeOneCopyChild.getQName().equals(nodeFourAssocName) == true)
560             {
561                 nodeFourCopy = nodeOneCopyChild.getChildRef();
562             }
563         }
564         assertNotNull(nodeTwoCopy);
565         assertNotNull(nodeThreeCopy);
566         assertNotNull(nodeFourCopy);
567         
568         // Check the non primary child assoc
569
List JavaDoc<ChildAssociationRef> children = this.nodeService.getChildAssocs(
570                 nodeFourCopy,
571                 RegexQNamePattern.MATCH_ALL,
572                 TEST_CHILD_ASSOC_QNAME);
573         assertNotNull(children);
574         assertEquals(1, children.size());
575         ChildAssociationRef child = children.get(0);
576         assertEquals(child.getChildRef(), nodeThreeCopy);
577         
578         // Check the node ref property
579
NodeRef nodeRef = (NodeRef)this.nodeService.getProperty(nodeOneCopy, PROP_QNAME_MY_NODE_REF);
580         assertNotNull(nodeRef);
581         assertEquals(nodeThreeCopy, nodeRef);
582         
583         // Check the any property
584
NodeRef anyNodeRef = (NodeRef)this.nodeService.getProperty(nodeOneCopy, PROP_QNAME_MY_ANY);
585         assertNotNull(anyNodeRef);
586         assertEquals(nodeThreeCopy, anyNodeRef);
587         
588         // Check the target assoc
589
List JavaDoc<AssociationRef> assocs = this.nodeService.getTargetAssocs(nodeTwoCopy, TEST_ASSOC_TYPE_QNAME);
590         assertNotNull(assocs);
591         assertEquals(1, assocs.size());
592         AssociationRef assoc = assocs.get(0);
593         assertEquals(assoc.getTargetRef(), nodeThreeCopy);
594         
595         // Check that the rule parameter values have been made relative
596
List JavaDoc<Rule> rules = this.ruleService.getRules(nodeOneCopy);
597         assertNotNull(rules);
598         assertEquals(1, rules.size());
599         Rule copiedRule = rules.get(0);
600         assertNotNull(copiedRule);
601         List JavaDoc<Action> ruleActions = copiedRule.getActions();
602         assertNotNull(ruleActions);
603         assertEquals(1, ruleActions.size());
604         Action ruleAction = ruleActions.get(0);
605         NodeRef value = (NodeRef)ruleAction.getParameterValue(MoveActionExecuter.PARAM_DESTINATION_FOLDER);
606         assertNotNull(value);
607         assertEquals(nodeTwoCopy, value);
608     }
609     
610     /**
611      * Check that the copied node contains the state we are expecting
612      *
613      * @param sourceNodeRef the source node reference
614      * @param destinationNodeRef the destination node reference
615      */

616     private void checkCopiedNode(NodeRef sourceNodeRef, NodeRef destinationNodeRef, boolean newCopy, boolean sameStore, boolean copyChildren)
617     {
618         if (newCopy == true)
619         {
620             if (sameStore == true)
621             {
622                 // Check that the copy aspect has been applied to the copy
623
boolean hasCopyAspect = this.nodeService.hasAspect(destinationNodeRef, ContentModel.ASPECT_COPIEDFROM);
624                 assertTrue(hasCopyAspect);
625                 NodeRef copyNodeRef = (NodeRef)this.nodeService.getProperty(destinationNodeRef, ContentModel.PROP_COPY_REFERENCE);
626                 assertNotNull(copyNodeRef);
627                 assertEquals(sourceNodeRef, copyNodeRef);
628             }
629             else
630             {
631                 // Check that destiantion has the same id as the source
632
assertEquals(sourceNodeRef.getId(), destinationNodeRef.getId());
633             }
634         }
635         
636         boolean hasTestAspect = this.nodeService.hasAspect(destinationNodeRef, TEST_ASPECT_QNAME);
637         assertTrue(hasTestAspect);
638         
639         // Check that all the correct properties have been copied
640
Map JavaDoc<QName, Serializable JavaDoc> destinationProperties = this.nodeService.getProperties(destinationNodeRef);
641         assertNotNull(destinationProperties);
642         String JavaDoc value1 = (String JavaDoc)destinationProperties.get(PROP1_QNAME_MANDATORY);
643         assertNotNull(value1);
644         assertEquals(TEST_VALUE_1, value1);
645         String JavaDoc value2 = (String JavaDoc)destinationProperties.get(PROP2_QNAME_OPTIONAL);
646         assertNotNull(value2);
647         assertEquals(TEST_VALUE_2, value2);
648         String JavaDoc value3 = (String JavaDoc)destinationProperties.get(PROP3_QNAME_MANDATORY);
649         assertNotNull(value3);
650         assertEquals(TEST_VALUE_1, value3);
651         String JavaDoc value4 = (String JavaDoc)destinationProperties.get(PROP4_QNAME_OPTIONAL);
652         assertNotNull(value4);
653         assertEquals(TEST_VALUE_2, value4);
654         
655         // Check all the target associations have been copied
656
List JavaDoc<AssociationRef> destinationTargets = this.nodeService.getTargetAssocs(destinationNodeRef, TEST_ASSOC_TYPE_QNAME);
657         assertNotNull(destinationTargets);
658         assertEquals(1, destinationTargets.size());
659         AssociationRef nodeAssocRef = destinationTargets.get(0);
660         assertNotNull(nodeAssocRef);
661         assertEquals(this.targetNodeRef, nodeAssocRef.getTargetRef());
662         
663         // Check all the child associations have been copied
664
List JavaDoc<ChildAssociationRef> childAssocRefs = this.nodeService.getChildAssocs(destinationNodeRef);
665         assertNotNull(childAssocRefs);
666         int expectedSize = 2;
667         if (this.nodeService.hasAspect(destinationNodeRef, RuleModel.ASPECT_RULES) == true)
668         {
669             expectedSize = expectedSize + 1;
670         }
671         
672         assertEquals(expectedSize, childAssocRefs.size());
673         for (ChildAssociationRef ref : childAssocRefs)
674         {
675             if (ref.getQName().equals(TEST_CHILD_ASSOC_QNAME2) == true)
676             {
677                 // Since this child is non-primary in the source it will always be non-primary in the destination
678
assertFalse(ref.isPrimary());
679                 assertEquals(this.nonPrimaryChildNodeRef, ref.getChildRef());
680             }
681             else
682             {
683                 if (copyChildren == false)
684                 {
685                     if (ref.getTypeQName().equals(RuleModel.ASSOC_RULE_FOLDER) == true)
686                     {
687                         assertTrue(ref.isPrimary());
688                         assertTrue(this.childNodeRef.equals(ref.getChildRef()) == false);
689                     }
690                     else
691                     {
692                         assertFalse(ref.isPrimary());
693                         assertEquals(this.childNodeRef, ref.getChildRef());
694                     }
695                 }
696                 else
697                 {
698                     assertTrue(ref.isPrimary());
699                     assertTrue(this.childNodeRef.equals(ref.getChildRef()) == false);
700                     
701                     // TODO need to check that the copied child has all the correct details ..
702
}
703             }
704         }
705     }
706 }
707
Popular Tags