KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
23
24 import org.alfresco.model.ContentModel;
25 import org.alfresco.repo.transaction.TransactionUtil;
26 import org.alfresco.service.cmr.repository.ContentReader;
27 import org.alfresco.service.cmr.repository.ContentWriter;
28 import org.alfresco.service.cmr.repository.NodeRef;
29 import org.alfresco.service.cmr.version.Version;
30 import org.alfresco.service.cmr.version.VersionHistory;
31 import org.alfresco.service.cmr.version.VersionServiceException;
32 import org.alfresco.service.namespace.QName;
33
34 /**
35  * versionService test class.
36  *
37  * @author Roy Wetherall
38  */

39 public class VersionServiceImplTest extends BaseVersionStoreTest
40 {
41     private static final String JavaDoc UPDATED_VALUE_1 = "updatedValue1";
42     private static final String JavaDoc UPDATED_VALUE_2 = "updatedValue2";
43     private static final String JavaDoc UPDATED_VALUE_3 = "updatedValue3";
44     private static final String JavaDoc UPDATED_CONTENT_1 = "updatedContent1";
45     private static final String JavaDoc UPDATED_CONTENT_2 = "updatedContent2";
46
47     /**
48      * Tests the creation of the initial version of a versionable node
49      */

50     public void testCreateIntialVersion()
51     {
52         NodeRef versionableNode = createNewVersionableNode();
53         createVersion(versionableNode);
54     }
55     
56     /**
57      * Test creating a version history with many versions from the same workspace
58      */

59     public void testCreateManyVersionsSameWorkspace()
60     {
61         NodeRef versionableNode = createNewVersionableNode();
62         createVersion(versionableNode);
63         // TODO mess with some of the properties and stuff as you version
64
createVersion(versionableNode);
65         // TODO mess with some of the properties and stuff as you version
66
createVersion(versionableNode);
67     }
68     
69     // TODO test versioning a non versionable node ie: no version apsect
70

71     // TODO test versioning numberious times with branchs implies by different workspaces
72

73     /**
74      * Test versioning the children of a verionable node
75      */

76     public void testVersioningChildren()
77     {
78         NodeRef versionableNode = createNewVersionableNode();
79         
80         // Snap shot data
81
int expectedVersionNumber = peekNextVersionNumber();
82         String JavaDoc expectedVersionLabel = peekNextVersionLabel(versionableNode, expectedVersionNumber, versionProperties);
83         long beforeVersionTime = System.currentTimeMillis();
84         
85         // Version the node and its children
86
Collection JavaDoc<Version> versions = this.versionService.createVersion(
87                 versionableNode,
88                 this.versionProperties,
89                 true);
90         
91         // Check the returned versions are correct
92
CheckVersionCollection(expectedVersionNumber, expectedVersionLabel, beforeVersionTime, versions);
93         
94         // TODO check the version history is correct
95
}
96     
97     /**
98      * Test versioning many nodes in one go
99      */

100     public void testVersioningManyNodes()
101     {
102         NodeRef versionableNode = createNewVersionableNode();
103         
104         // Snap shot data
105
int expectedVersionNumber = peekNextVersionNumber();
106         String JavaDoc expectedVersionLabel = peekNextVersionLabel(versionableNode, expectedVersionNumber, versionProperties);
107         long beforeVersionTime = System.currentTimeMillis();
108         
109         // Version the list of nodes created
110
Collection JavaDoc<Version> versions = this.versionService.createVersion(
111                 this.versionableNodes.values(),
112                 this.versionProperties);
113         
114         // Check the returned versions are correct
115
CheckVersionCollection(expectedVersionNumber, expectedVersionLabel, beforeVersionTime, versions);
116
117         // TODO check the version histories
118
}
119     
120     /**
121      * Helper method to check the validity of the list of newly created versions.
122      *
123      * @param expectedVersionNumber the expected version number that all the versions should have
124      * @param beforeVersionTime the time before the versions where created
125      * @param versions the collection of version objects
126      */

127     private void CheckVersionCollection(int expectedVersionNumber, String JavaDoc expectedVersionLabel, long beforeVersionTime, Collection JavaDoc<Version> versions)
128     {
129         for (Version version : versions)
130         {
131             // Get the frozen id from the version
132
String JavaDoc frozenNodeId = (String JavaDoc)version.getVersionProperty(VersionModel.PROP_FROZEN_NODE_ID);
133             assertNotNull("Unable to retrieve the frozen node id from the created version.", frozenNodeId);
134             
135             // Get the origional node ref (based on the forzen node)
136
NodeRef origionaNodeRef = this.versionableNodes.get(frozenNodeId);
137             assertNotNull("The versionable node ref that relates to the frozen node id can not be found.", origionaNodeRef);
138             
139             // Check the new version
140
checkNewVersion(beforeVersionTime, expectedVersionNumber, expectedVersionLabel, version, origionaNodeRef);
141         }
142     }
143     
144     /**
145      * Tests the version history
146      */

147     public void testNoVersionHistory()
148     {
149         NodeRef nodeRef = createNewVersionableNode();
150         
151         VersionHistory vh = this.versionService.getVersionHistory(nodeRef);
152         assertNull(vh);
153     }
154     
155     /**
156      * Tests getVersionHistory when all the entries in the version history
157      * are from the same workspace.
158      */

159     public void testGetVersionHistorySameWorkspace()
160     {
161         NodeRef versionableNode = createNewVersionableNode();
162         
163         Version version1 = addToVersionHistory(versionableNode, null);
164         Version version2 = addToVersionHistory(versionableNode, version1);
165         Version version3 = addToVersionHistory(versionableNode, version2);
166         Version version4 = addToVersionHistory(versionableNode, version3);
167         addToVersionHistory(versionableNode, version4);
168     }
169     
170     /**
171      * Adds another version to the version history then checks that getVersionHistory is returning
172      * the correct data.
173      *
174      * @param versionableNode the versionable node reference
175      * @param parentVersion the parent version
176      */

177     private Version addToVersionHistory(NodeRef versionableNode, Version parentVersion)
178     {
179         Version createdVersion = createVersion(versionableNode);
180         
181         VersionHistory vh = this.versionService.getVersionHistory(versionableNode);
182         assertNotNull("The version history should not be null since we know we have versioned this node.", vh);
183         
184         if (parentVersion == null)
185         {
186             // Check the root is the newly created version
187
Version root = vh.getRootVersion();
188             assertNotNull(
189                     "The root version should never be null, since every version history ust have a root version.",
190                     root);
191             assertEquals(createdVersion.getVersionLabel(), root.getVersionLabel());
192         }
193         
194         // Get the version from the version history
195
Version version = vh.getVersion(createdVersion.getVersionLabel());
196         assertNotNull(version);
197         assertEquals(createdVersion.getVersionLabel(), version.getVersionLabel());
198         
199         // Check that the version is a leaf node of the version history (since it is newly created)
200
Collection JavaDoc<Version> suc = vh.getSuccessors(version);
201         assertNotNull(suc);
202         assertEquals(0, suc.size());
203         
204         // Check that the predessor is the passed parent version (if root version should be null)
205
Version pre = vh.getPredecessor(version);
206         if (parentVersion == null)
207         {
208             assertNull(pre);
209         }
210         else
211         {
212             assertNotNull(pre);
213             assertEquals(parentVersion.getVersionLabel(), pre.getVersionLabel());
214         }
215         
216         if (parentVersion != null)
217         {
218             // Check that the successors of the parent are the created version
219
Collection JavaDoc<Version> parentSuc = vh.getSuccessors(parentVersion);
220             assertNotNull(parentSuc);
221             assertEquals(1, parentSuc.size());
222             Version tempVersion = (Version)parentSuc.toArray()[0];
223             assertEquals(version.getVersionLabel(), tempVersion.getVersionLabel());
224         }
225         
226         return createdVersion;
227     }
228     
229     /**
230      * Test revert
231      */

232     @SuppressWarnings JavaDoc("unused")
233     public void testRevert()
234     {
235         // Create a versionable node
236
NodeRef versionableNode = createNewVersionableNode();
237         
238         // Store the node details for later
239
Set JavaDoc<QName> origAspects = this.dbNodeService.getAspects(versionableNode);
240         
241         // Create the initial version
242
Version version1 = createVersion(versionableNode);
243         
244         // Change the property and content values
245
this.dbNodeService.setProperty(versionableNode, PROP_1, UPDATED_VALUE_1);
246         this.dbNodeService.setProperty(versionableNode, PROP_2, null);
247         ContentWriter contentWriter = this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true);
248         assertNotNull(contentWriter);
249         contentWriter.putContent(UPDATED_CONTENT_1);
250         
251         // Change the aspects on the node
252
this.dbNodeService.addAspect(versionableNode, ContentModel.ASPECT_SIMPLE_WORKFLOW, null);
253         
254         // Store the node details for later
255
Set JavaDoc<QName> origAspects2 = this.dbNodeService.getAspects(versionableNode);
256         
257         // Create a new version
258
Version version2 = createVersion(versionableNode);
259         
260         // Change the property and content values
261
this.dbNodeService.setProperty(versionableNode, PROP_1, UPDATED_VALUE_2);
262         this.dbNodeService.setProperty(versionableNode, PROP_2, UPDATED_VALUE_3);
263         this.dbNodeService.setProperty(versionableNode, PROP_3, null);
264         ContentWriter contentWriter2 = this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true);
265         assertNotNull(contentWriter2);
266         contentWriter2.putContent(UPDATED_CONTENT_2);
267         
268         String JavaDoc versionLabel = (String JavaDoc)this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL);
269         
270         // Revert to the previous version
271
this.versionService.revert(versionableNode);
272         
273         // Check that the version label is unchanged
274
assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL));
275         
276         // Check that the properties have been reverted
277
assertEquals(UPDATED_VALUE_1, this.dbNodeService.getProperty(versionableNode, PROP_1));
278         assertNull(this.dbNodeService.getProperty(versionableNode, PROP_2));
279         assertEquals(VALUE_3, this.dbNodeService.getProperty(versionableNode, PROP_3));
280         
281         // Check that the content has been reverted
282
ContentReader contentReader1 = this.contentService.getReader(versionableNode, ContentModel.PROP_CONTENT);
283         assertNotNull(contentReader1);
284         assertEquals(UPDATED_CONTENT_1, contentReader1.getContentString());
285         
286         // Check that the aspects have been reverted correctly
287
Set JavaDoc<QName> aspects1 = this.dbNodeService.getAspects(versionableNode);
288         assertEquals(aspects1.size(), origAspects2.size());
289         
290         // Revert to the first version
291
this.versionService.revert(versionableNode, version1);
292         
293         // Check that the version label is correct
294
assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL));
295         
296         // Check that the properties are correct
297
assertEquals(VALUE_1, this.dbNodeService.getProperty(versionableNode, PROP_1));
298         assertEquals(VALUE_2, this.dbNodeService.getProperty(versionableNode, PROP_2));
299         assertEquals(VALUE_3, this.dbNodeService.getProperty(versionableNode, PROP_3));
300         
301         // Check that the content is correct
302
ContentReader contentReader2 = this.contentService.getReader(versionableNode, ContentModel.PROP_CONTENT);
303         assertNotNull(contentReader2);
304         assertEquals(TEST_CONTENT, contentReader2.getContentString());
305         
306         // Check that the aspects have been reverted correctly
307
Set JavaDoc<QName> aspects2 = this.dbNodeService.getAspects(versionableNode);
308         assertEquals(aspects2.size(), origAspects.size());
309         
310         // Check that the version label is still the same
311
assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL));
312     }
313     
314     /**
315      * Test restore
316      */

317     public void testRestore()
318     {
319         // Try and restore a node without any version history
320
try
321         {
322             this.versionService.restore(
323                     new NodeRef(this.testStoreRef, "123"),
324                     rootNodeRef,
325                     ContentModel.ASSOC_CHILDREN,
326                     QName.createQName("{test}MyVersionableNode"));
327             fail("An exception should have been raised since this node has no version history.");
328         }
329         catch (VersionServiceException exception)
330         {
331             // We where expecting this exception
332
}
333         
334         // Create a versionable node
335
NodeRef versionableNode = createNewVersionableNode();
336         
337         // Store the node details for later
338
Set JavaDoc<QName> origAspects = this.dbNodeService.getAspects(versionableNode);
339         
340         // Try and restore the node (fail since exist!!)
341
try
342         {
343             this.versionService.restore(
344                     versionableNode,
345                     rootNodeRef,
346                     ContentModel.ASSOC_CHILDREN,
347                     QName.createQName("{test}MyVersionableNode"));
348             fail("An exception should have been raised since this node exists and you can't restore a node that exists.");
349         }
350         catch (VersionServiceException exception)
351         {
352             // We where expecting this exception
353
}
354         
355         // Version it
356
this.versionService.createVersion(versionableNode, null);
357         
358         // Delete it
359
this.dbNodeService.deleteNode(versionableNode);
360         assertFalse(this.dbNodeService.exists(versionableNode));
361         
362         // Try and resotre it
363
NodeRef restoredNode = this.versionService.restore(
364                 versionableNode,
365                 this.rootNodeRef,
366                 ContentModel.ASSOC_CHILDREN,
367                 QName.createQName("{test}MyVersionableNode"));
368         
369         assertNotNull(restoredNode);
370         assertTrue(this.dbNodeService.exists(restoredNode));
371         
372         // Check that the properties are correct
373
assertEquals(VALUE_1, this.dbNodeService.getProperty(restoredNode, PROP_1));
374         assertEquals(VALUE_2, this.dbNodeService.getProperty(restoredNode, PROP_2));
375         assertEquals(VALUE_3, this.dbNodeService.getProperty(restoredNode, PROP_3));
376         
377         // Check that the content is correct
378
ContentReader contentReader2 = this.contentService.getReader(restoredNode, ContentModel.PROP_CONTENT);
379         assertNotNull(contentReader2);
380         assertEquals(TEST_CONTENT, contentReader2.getContentString());
381         
382         // Check that the aspects have been reverted correctly
383
Set JavaDoc<QName> aspects2 = this.dbNodeService.getAspects(restoredNode);
384         assertEquals(aspects2.size(), origAspects.size());
385     }
386     
387     /**
388      * Test deleteVersionHistory
389      */

390     public void testDeleteVersionHistory()
391     {
392         // Create a versionable node
393
NodeRef versionableNode = createNewVersionableNode();
394         
395         // Check that there is no version history
396
VersionHistory versionHistory1 = this.versionService.getVersionHistory(versionableNode);
397         assertNull(versionHistory1);
398         
399         // Create a couple of versions
400
createVersion(versionableNode);
401         Version version1 = createVersion(versionableNode);
402         
403         // Check that the version label is correct on the versionable node
404
String JavaDoc versionLabel1 = (String JavaDoc)this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL);
405         assertNotNull(versionLabel1);
406         assertEquals(version1.getVersionLabel(), versionLabel1);
407         
408         // Check that the version history has been created correctly
409
VersionHistory versionHistory2 = this.versionService.getVersionHistory(versionableNode);
410         assertNotNull(versionHistory2);
411         assertEquals(2, versionHistory2.getAllVersions().size());
412         
413         // Delete the version history
414
this.versionService.deleteVersionHistory(versionableNode);
415         
416         // Check that there is no version history available for the node
417
VersionHistory versionHistory3 = this.versionService.getVersionHistory(versionableNode);
418         assertNull(versionHistory3);
419         
420         // Check that the current version property on the versionable node is no longer set
421
String JavaDoc versionLabel2 = (String JavaDoc)this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL);
422         assertNull(versionLabel2);
423         
424         // Create a couple of versions
425
createVersion(versionableNode);
426         Version version2 = createVersion(versionableNode);
427         
428         // Check that the version history is correct
429
VersionHistory versionHistory4 = this.versionService.getVersionHistory(versionableNode);
430         assertNotNull(versionHistory4);
431         assertEquals(2, versionHistory4.getAllVersions().size());
432         
433         // Check that the version label is correct on the versionable node
434
String JavaDoc versionLabel3 = (String JavaDoc)this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL);
435         assertNotNull(versionLabel3);
436         assertEquals(version2.getVersionLabel(), versionLabel3);
437         
438     }
439     
440     public void testAutoVersionOnInitialVersionOn()
441     {
442         // Create a versionable node
443
final NodeRef versionableNode = createNewVersionableNode();
444         
445         setComplete();
446         endTransaction();
447
448         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
449         {
450             public Object JavaDoc doWork() throws Exception JavaDoc
451             {
452                 // Check that the initial version has not been created
453
VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode);
454                 assertNotNull(versionHistory);
455                 assertEquals(1, versionHistory.getAllVersions().size());
456                 
457                 // Add some content
458
ContentWriter contentWriter = VersionServiceImplTest.this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true);
459                 assertNotNull(contentWriter);
460                 contentWriter.putContent(UPDATED_CONTENT_1);
461                 
462                 return null;
463             }
464         });
465         
466         // Now lets have a look and make sure we have the correct number of entries in the version history
467
TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
468         {
469             public Object JavaDoc doWork() throws Exception JavaDoc
470             {
471                 VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode);
472                 assertNotNull(versionHistory);
473                 assertEquals(2, versionHistory.getAllVersions().size());
474                 
475                 return null;
476             }
477         
478         });
479     }
480     
481     public void testAutoVersionOff()
482     {
483         // Create a versionable node
484
final NodeRef versionableNode = createNewVersionableNode();
485         this.dbNodeService.setProperty(versionableNode, ContentModel.PROP_AUTO_VERSION, false);
486
487         setComplete();
488         endTransaction();
489
490         // The initial version should have been created now
491

492         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
493         {
494             public Object JavaDoc doWork() throws Exception JavaDoc
495             {
496                 // Add some content
497
ContentWriter contentWriter = VersionServiceImplTest.this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true);
498                 assertNotNull(contentWriter);
499                 contentWriter.putContent(UPDATED_CONTENT_1);
500                 
501                 return null;
502             }
503         });
504         
505         // Now lets have a look and make sure we have the correct number of entries in the version history
506
TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
507         {
508             public Object JavaDoc doWork() throws Exception JavaDoc
509             {
510                 VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode);
511                 assertNotNull(versionHistory);
512                 assertEquals(1, versionHistory.getAllVersions().size());
513                 
514                 return null;
515             }
516         
517         });
518     }
519     
520     public void testInitialVersionOff()
521     {
522         // Create node (this node has some content)
523
HashMap JavaDoc<QName, Serializable JavaDoc> props = new HashMap JavaDoc<QName, Serializable JavaDoc>();
524         props.put(ContentModel.PROP_INITIAL_VERSION, false);
525         HashMap JavaDoc<QName, Serializable JavaDoc> props2 = new HashMap JavaDoc<QName, Serializable JavaDoc>();
526         props2.put(ContentModel.PROP_NAME, "test.txt");
527         final NodeRef nodeRef = this.dbNodeService.createNode(
528                 rootNodeRef,
529                 ContentModel.ASSOC_CHILDREN,
530                 QName.createQName("{test}MyVersionableNode2"),
531                 TEST_TYPE_QNAME,
532                 props2).getChildRef();
533         this.dbNodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, props);
534         
535         setComplete();
536         endTransaction();
537
538         // The initial version should NOT have been created
539

540         TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object JavaDoc>()
541         {
542             public Object JavaDoc doWork() throws Exception JavaDoc
543             {
544                 VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(nodeRef);
545                 assertNull(versionHistory);
546                 
547                 return null;
548             }
549         });
550        
551     }
552 }
553
Popular Tags