KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > version > NodeServiceImplTest


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.version;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25
26 import org.alfresco.model.ContentModel;
27 import org.alfresco.service.cmr.repository.AssociationRef;
28 import org.alfresco.service.cmr.repository.ChildAssociationRef;
29 import org.alfresco.service.cmr.repository.NodeRef;
30 import org.alfresco.service.cmr.repository.NodeService;
31 import org.alfresco.service.cmr.repository.Path;
32 import org.alfresco.service.cmr.version.Version;
33 import org.alfresco.service.namespace.QName;
34 import org.alfresco.service.namespace.RegexQNamePattern;
35 import org.alfresco.util.debug.NodeStoreInspector;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 /**
40  * @author Roy Wetherall
41  */

42 public class NodeServiceImplTest extends BaseVersionStoreTest
43 {
44     private static Log logger = LogFactory.getLog(NodeServiceImplTest.class);
45     
46     /**
47      * Light weight version store node service
48      */

49     protected NodeService lightWeightVersionStoreNodeService = null;
50     
51     /**
52      * Error message
53      */

54     private final static String JavaDoc MSG_ERR =
55         "This operation is not supported by a version store implementation of the node service.";
56     
57     /**
58      * Dummy data used in failure tests
59      */

60     private NodeRef dummyNodeRef = null;
61     private QName dummyQName = null;
62     
63     /**
64      * Called during the transaction setup
65      */

66     protected void onSetUpInTransaction() throws Exception JavaDoc
67     {
68         super.onSetUpInTransaction();
69         
70         // Get the node service by name
71
this.lightWeightVersionStoreNodeService = (NodeService)this.applicationContext.getBean("versionNodeService");
72         
73         // Create some dummy data used during the tests
74
this.dummyNodeRef = new NodeRef(
75                 this.versionService.getVersionStoreReference(),
76                 "dummy");
77         this.dummyQName = QName.createQName("{dummy}dummy");
78     }
79     
80     /**
81      * Test getType
82      */

83     public void testGetType()
84     {
85         // Create a new versionable node
86
NodeRef versionableNode = createNewVersionableNode();
87         
88         // Create a new version
89
Version version = createVersion(versionableNode, this.versionProperties);
90     
91         // Get the type from the versioned state
92
QName versionedType = this.lightWeightVersionStoreNodeService.getType(version.getFrozenStateNodeRef());
93         assertNotNull(versionedType);
94         assertEquals(this.dbNodeService.getType(versionableNode), versionedType);
95     }
96     
97     /**
98      * Test getProperties
99      */

100     public void testGetProperties()
101     {
102         // Create a new versionable node
103
NodeRef versionableNode = createNewVersionableNode();
104         
105         // Get a list of the nodes properties
106
Map JavaDoc<QName, Serializable JavaDoc> origProps = this.dbNodeService.getProperties(versionableNode);
107         
108         // Create a new version
109
Version version = createVersion(versionableNode, this.versionProperties);
110         
111         // Get the properties of the versioned state
112
Map JavaDoc<QName, Serializable JavaDoc> versionedProperties = this.lightWeightVersionStoreNodeService.getProperties(version.getFrozenStateNodeRef());
113         //assertEquals(origProps.size(), versionedProperties.size());
114
for (QName key : origProps.keySet())
115         {
116             assertTrue(versionedProperties.containsKey(key));
117             assertEquals(origProps.get(key), versionedProperties.get(key));
118         }
119         
120         // TODO do futher versioning and check by changing values
121
}
122     
123     /**
124      * Test getProperty
125      */

126     public void testGetProperty()
127     {
128         // Create a new versionable node
129
NodeRef versionableNode = createNewVersionableNode();
130         
131         // Create a new version
132
Version version = createVersion(versionableNode, this.versionProperties);
133         
134         // Check the property values can be retrieved
135
Serializable JavaDoc value1 = this.lightWeightVersionStoreNodeService.getProperty(
136                 version.getFrozenStateNodeRef(),
137                 PROP_1);
138         assertEquals(VALUE_1, value1);
139         
140         // Check the multi values property specifically
141
Collection JavaDoc<String JavaDoc> multiValue = (Collection JavaDoc<String JavaDoc>)this.lightWeightVersionStoreNodeService.getProperty(version.getFrozenStateNodeRef(), MULTI_PROP);
142         assertNotNull(multiValue);
143         assertEquals(2, multiValue.size());
144         String JavaDoc[] array = multiValue.toArray(new String JavaDoc[multiValue.size()]);
145         assertEquals(MULTI_VALUE_1, array[0]);
146         assertEquals(MULTI_VALUE_2, array[1]);
147     }
148     
149     /**
150      * Test getChildAssocs
151      */

152     public void testGetChildAssocs()
153     {
154         if (logger.isDebugEnabled())
155         {
156             // Let's have a look at the version store ..
157
System.out.println(NodeStoreInspector.dumpNodeStore(
158                     this.dbNodeService,
159                     this.versionService.getVersionStoreReference()) + "\n\n");
160             logger.debug("");
161         }
162         
163         // Create a new versionable node
164
NodeRef versionableNode = createNewVersionableNode();
165         Collection JavaDoc<ChildAssociationRef> origionalChildren = this.dbNodeService.getChildAssocs(versionableNode);
166         assertNotNull(origionalChildren);
167         
168         // Store the origional children in a map for easy navigation later
169
HashMap JavaDoc<String JavaDoc, ChildAssociationRef> origionalChildAssocRefs = new HashMap JavaDoc<String JavaDoc, ChildAssociationRef>();
170         for (ChildAssociationRef ref : origionalChildren)
171         {
172             origionalChildAssocRefs.put(ref.getChildRef().getId(), ref);
173         }
174         
175         // Create a new version
176
Version version = createVersion(versionableNode, this.versionProperties);
177         
178         if (logger.isDebugEnabled())
179         {
180             // Let's have a look at the version store ..
181
System.out.println(NodeStoreInspector.dumpNodeStore(
182                     this.dbNodeService,
183                     this.versionService.getVersionStoreReference()));
184         }
185         
186         // Get the children of the versioned node
187
Collection JavaDoc<ChildAssociationRef> versionedChildren = this.lightWeightVersionStoreNodeService.getChildAssocs(version.getFrozenStateNodeRef());
188         assertNotNull(versionedChildren);
189         assertEquals(origionalChildren.size(), versionedChildren.size());
190         
191         for (ChildAssociationRef versionedChildRef : versionedChildren)
192         {
193             ChildAssociationRef origChildAssocRef = origionalChildAssocRefs.get(versionedChildRef.getChildRef().getId());
194             assertNotNull(origChildAssocRef);
195                         
196             assertEquals(
197                     origChildAssocRef.getChildRef(),
198                     versionedChildRef.getChildRef());
199             assertEquals(
200                     origChildAssocRef.isPrimary(),
201                     versionedChildRef.isPrimary());
202             assertEquals(
203                     origChildAssocRef.getNthSibling(),
204                     versionedChildRef.getNthSibling());
205         }
206     }
207     
208     /**
209      * Test getAssociationTargets
210      */

211     public void testGetAssociationTargets()
212     {
213         // Create a new versionable node
214
NodeRef versionableNode = createNewVersionableNode();
215         
216         // Store the current details of the target associations
217
List JavaDoc<AssociationRef> origAssocs = this.dbNodeService.getTargetAssocs(
218                 versionableNode,
219                 RegexQNamePattern.MATCH_ALL);
220         
221         // Create a new version
222
Version version = createVersion(versionableNode, this.versionProperties);
223
224         List JavaDoc<AssociationRef> assocs = this.lightWeightVersionStoreNodeService.getTargetAssocs(
225                 version.getFrozenStateNodeRef(),
226                 RegexQNamePattern.MATCH_ALL);
227         assertNotNull(assocs);
228         assertEquals(origAssocs.size(), assocs.size());
229     }
230     
231     /**
232      * Test hasAspect
233      */

234     public void testHasAspect()
235     {
236         // Create a new versionable node
237
NodeRef versionableNode = createNewVersionableNode();
238         
239         // Create a new version
240
Version version = createVersion(versionableNode, this.versionProperties);
241         
242         boolean test1 = this.lightWeightVersionStoreNodeService.hasAspect(
243                 version.getFrozenStateNodeRef(),
244                 ContentModel.ASPECT_UIFACETS);
245         assertFalse(test1);
246         
247         boolean test2 = this.lightWeightVersionStoreNodeService.hasAspect(
248                 version.getFrozenStateNodeRef(),
249                 ContentModel.ASPECT_VERSIONABLE);
250         assertTrue(test2);
251     }
252
253     /**
254      * Test getAspects
255      */

256     public void testGetAspects()
257     {
258         // Create a new versionable node
259
NodeRef versionableNode = createNewVersionableNode();
260         Set JavaDoc<QName> origAspects = this.dbNodeService.getAspects(versionableNode);
261         
262         // Create a new version
263
Version version = createVersion(versionableNode, this.versionProperties);
264         
265         Set JavaDoc<QName> aspects = this.lightWeightVersionStoreNodeService.getAspects(version.getFrozenStateNodeRef());
266         assertEquals(origAspects.size(), aspects.size());
267         
268         // TODO check that the set's contain the same items
269
}
270     
271     /**
272      * Test getParentAssocs
273      */

274     public void testGetParentAssocs()
275     {
276         // Create a new versionable node
277
NodeRef versionableNode = createNewVersionableNode();
278         
279         // Create a new version
280
Version version = createVersion(versionableNode, this.versionProperties);
281         NodeRef nodeRef = version.getFrozenStateNodeRef();
282         
283         List JavaDoc<ChildAssociationRef> results = this.lightWeightVersionStoreNodeService.getParentAssocs(nodeRef);
284         assertNotNull(results);
285         assertEquals(1, results.size());
286         ChildAssociationRef childAssoc = results.get(0);
287         assertEquals(nodeRef, childAssoc.getChildRef());
288         NodeRef versionStoreRoot = this.dbNodeService.getRootNode(this.versionService.getVersionStoreReference());
289         assertEquals(versionStoreRoot, childAssoc.getParentRef());
290     }
291     
292     /**
293      * Test getPrimaryParent
294      */

295     public void testGetPrimaryParent()
296     {
297         // Create a new versionable node
298
NodeRef versionableNode = createNewVersionableNode();
299         
300         // Create a new version
301
Version version = createVersion(versionableNode, this.versionProperties);
302         NodeRef nodeRef = version.getFrozenStateNodeRef();
303         
304         ChildAssociationRef childAssoc = this.lightWeightVersionStoreNodeService.getPrimaryParent(nodeRef);
305         assertNotNull(childAssoc);
306         assertEquals(nodeRef, childAssoc.getChildRef());
307         NodeRef versionStoreRoot = this.dbNodeService.getRootNode(this.versionService.getVersionStoreReference());
308         assertEquals(versionStoreRoot, childAssoc.getParentRef());
309     }
310     
311     /** ================================================
312      * These test ensure that the following operations
313      * are not supported as expected.
314      */

315     
316     /**
317      * Test createNode
318      */

319     public void testCreateNode()
320     {
321         try
322         {
323             this.lightWeightVersionStoreNodeService.createNode(
324                     dummyNodeRef,
325                     null,
326                     dummyQName,
327                     ContentModel.TYPE_CONTENT);
328             fail("This operation is not supported.");
329         }
330         catch (UnsupportedOperationException JavaDoc exception)
331         {
332             if (exception.getMessage() != MSG_ERR)
333             {
334                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
335             }
336         }
337     }
338     
339     /**
340      * Test addAspect
341      */

342     public void testAddAspect()
343     {
344         try
345         {
346             this.lightWeightVersionStoreNodeService.addAspect(
347                     dummyNodeRef,
348                     TEST_ASPECT_QNAME,
349                     null);
350             fail("This operation is not supported.");
351         }
352         catch (UnsupportedOperationException JavaDoc exception)
353         {
354             if (exception.getMessage() != MSG_ERR)
355             {
356                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
357             }
358         }
359     }
360     
361     /**
362      * Test removeAspect
363      */

364     public void testRemoveAspect()
365     {
366         try
367         {
368             this.lightWeightVersionStoreNodeService.removeAspect(
369                     dummyNodeRef,
370                     TEST_ASPECT_QNAME);
371             fail("This operation is not supported.");
372         }
373         catch (UnsupportedOperationException JavaDoc exception)
374         {
375             if (exception.getMessage() != MSG_ERR)
376             {
377                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
378             }
379         }
380     }
381     
382     /**
383      * Test delete node
384      */

385     public void testDeleteNode()
386     {
387         try
388         {
389             this.lightWeightVersionStoreNodeService.deleteNode(this.dummyNodeRef);
390             fail("This operation is not supported.");
391         }
392         catch (UnsupportedOperationException JavaDoc exception)
393         {
394             if (exception.getMessage() != MSG_ERR)
395             {
396                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
397             }
398         }
399     }
400     
401     /**
402      * Test addChild
403      */

404     public void testAddChild()
405     {
406         try
407         {
408             this.lightWeightVersionStoreNodeService.addChild(
409                     this.dummyNodeRef,
410                     this.dummyNodeRef,
411                     this.dummyQName,
412                     this.dummyQName);
413             fail("This operation is not supported.");
414         }
415         catch (UnsupportedOperationException JavaDoc exception)
416         {
417             if (exception.getMessage() != MSG_ERR)
418             {
419                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
420             }
421         }
422     }
423     
424     /**
425      * Test removeChild
426      */

427     public void testRemoveChild()
428     {
429         try
430         {
431             this.lightWeightVersionStoreNodeService.removeChild(
432                     this.dummyNodeRef,
433                     this.dummyNodeRef);
434             fail("This operation is not supported.");
435         }
436         catch (UnsupportedOperationException JavaDoc exception)
437         {
438             if (exception.getMessage() != MSG_ERR)
439             {
440                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
441             }
442         }
443     }
444     
445     /**
446      * Test setProperties
447      */

448     public void testSetProperties()
449     {
450         try
451         {
452             this.lightWeightVersionStoreNodeService.setProperties(
453                     this.dummyNodeRef,
454                     new HashMap JavaDoc<QName, Serializable JavaDoc>());
455             fail("This operation is not supported.");
456         }
457         catch (UnsupportedOperationException JavaDoc exception)
458         {
459             if (exception.getMessage() != MSG_ERR)
460             {
461                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
462             }
463         }
464     }
465     
466     /**
467      * Test setProperty
468      */

469     public void testSetProperty()
470     {
471         try
472         {
473             this.lightWeightVersionStoreNodeService.setProperty(
474                     this.dummyNodeRef,
475                     this.dummyQName,
476                     "dummy");
477             fail("This operation is not supported.");
478         }
479         catch (UnsupportedOperationException JavaDoc exception)
480         {
481             if (exception.getMessage() != MSG_ERR)
482             {
483                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
484             }
485         }
486     }
487     
488     /**
489      * Test createAssociation
490      */

491     public void testCreateAssociation()
492     {
493         try
494         {
495             this.lightWeightVersionStoreNodeService.createAssociation(
496                     this.dummyNodeRef,
497                     this.dummyNodeRef,
498                     this.dummyQName);
499             fail("This operation is not supported.");
500         }
501         catch (UnsupportedOperationException JavaDoc exception)
502         {
503             if (exception.getMessage() != MSG_ERR)
504             {
505                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
506             }
507         }
508     }
509     
510     /**
511      * Test removeAssociation
512      */

513     public void testRemoveAssociation()
514     {
515         try
516         {
517             this.lightWeightVersionStoreNodeService.removeAssociation(
518                     this.dummyNodeRef,
519                     this.dummyNodeRef,
520                     this.dummyQName);
521             fail("This operation is not supported.");
522         }
523         catch (UnsupportedOperationException JavaDoc exception)
524         {
525             if (exception.getMessage() != MSG_ERR)
526             {
527                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
528             }
529         }
530     }
531     
532     /**
533      * Test getAssociationSources
534      */

535     public void testGetAssociationSources()
536     {
537         try
538         {
539             this.lightWeightVersionStoreNodeService.getSourceAssocs(
540                     this.dummyNodeRef,
541                     this.dummyQName);
542             fail("This operation is not supported.");
543         }
544         catch (UnsupportedOperationException JavaDoc exception)
545         {
546             if (exception.getMessage() != MSG_ERR)
547             {
548                 fail("Unexpected exception raised during method excution: " + exception.getMessage());
549             }
550         }
551     }
552     
553     /**
554      * Test getPath
555      */

556     public void testGetPath()
557     {
558         Path path = this.lightWeightVersionStoreNodeService.getPath(this.dummyNodeRef);
559     }
560     
561     /**
562      * Test getPaths
563      */

564     public void testGetPaths()
565     {
566         List JavaDoc<Path> paths = this.lightWeightVersionStoreNodeService.getPaths(this.dummyNodeRef, false);
567     }
568 }
569
Popular Tags