1 17 package org.alfresco.repo.version; 18 19 import java.io.Serializable ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Set ; 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 39 public class VersionServiceImplTest extends BaseVersionStoreTest 40 { 41 private static final String UPDATED_VALUE_1 = "updatedValue1"; 42 private static final String UPDATED_VALUE_2 = "updatedValue2"; 43 private static final String UPDATED_VALUE_3 = "updatedValue3"; 44 private static final String UPDATED_CONTENT_1 = "updatedContent1"; 45 private static final String UPDATED_CONTENT_2 = "updatedContent2"; 46 47 50 public void testCreateIntialVersion() 51 { 52 NodeRef versionableNode = createNewVersionableNode(); 53 createVersion(versionableNode); 54 } 55 56 59 public void testCreateManyVersionsSameWorkspace() 60 { 61 NodeRef versionableNode = createNewVersionableNode(); 62 createVersion(versionableNode); 63 createVersion(versionableNode); 65 createVersion(versionableNode); 67 } 68 69 71 73 76 public void testVersioningChildren() 77 { 78 NodeRef versionableNode = createNewVersionableNode(); 79 80 int expectedVersionNumber = peekNextVersionNumber(); 82 String expectedVersionLabel = peekNextVersionLabel(versionableNode, expectedVersionNumber, versionProperties); 83 long beforeVersionTime = System.currentTimeMillis(); 84 85 Collection <Version> versions = this.versionService.createVersion( 87 versionableNode, 88 this.versionProperties, 89 true); 90 91 CheckVersionCollection(expectedVersionNumber, expectedVersionLabel, beforeVersionTime, versions); 93 94 } 96 97 100 public void testVersioningManyNodes() 101 { 102 NodeRef versionableNode = createNewVersionableNode(); 103 104 int expectedVersionNumber = peekNextVersionNumber(); 106 String expectedVersionLabel = peekNextVersionLabel(versionableNode, expectedVersionNumber, versionProperties); 107 long beforeVersionTime = System.currentTimeMillis(); 108 109 Collection <Version> versions = this.versionService.createVersion( 111 this.versionableNodes.values(), 112 this.versionProperties); 113 114 CheckVersionCollection(expectedVersionNumber, expectedVersionLabel, beforeVersionTime, versions); 116 117 } 119 120 127 private void CheckVersionCollection(int expectedVersionNumber, String expectedVersionLabel, long beforeVersionTime, Collection <Version> versions) 128 { 129 for (Version version : versions) 130 { 131 String frozenNodeId = (String )version.getVersionProperty(VersionModel.PROP_FROZEN_NODE_ID); 133 assertNotNull("Unable to retrieve the frozen node id from the created version.", frozenNodeId); 134 135 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 checkNewVersion(beforeVersionTime, expectedVersionNumber, expectedVersionLabel, version, origionaNodeRef); 141 } 142 } 143 144 147 public void testNoVersionHistory() 148 { 149 NodeRef nodeRef = createNewVersionableNode(); 150 151 VersionHistory vh = this.versionService.getVersionHistory(nodeRef); 152 assertNull(vh); 153 } 154 155 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 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 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 Version version = vh.getVersion(createdVersion.getVersionLabel()); 196 assertNotNull(version); 197 assertEquals(createdVersion.getVersionLabel(), version.getVersionLabel()); 198 199 Collection <Version> suc = vh.getSuccessors(version); 201 assertNotNull(suc); 202 assertEquals(0, suc.size()); 203 204 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 Collection <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 232 @SuppressWarnings ("unused") 233 public void testRevert() 234 { 235 NodeRef versionableNode = createNewVersionableNode(); 237 238 Set <QName> origAspects = this.dbNodeService.getAspects(versionableNode); 240 241 Version version1 = createVersion(versionableNode); 243 244 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 this.dbNodeService.addAspect(versionableNode, ContentModel.ASPECT_SIMPLE_WORKFLOW, null); 253 254 Set <QName> origAspects2 = this.dbNodeService.getAspects(versionableNode); 256 257 Version version2 = createVersion(versionableNode); 259 260 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 versionLabel = (String )this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL); 269 270 this.versionService.revert(versionableNode); 272 273 assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL)); 275 276 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 ContentReader contentReader1 = this.contentService.getReader(versionableNode, ContentModel.PROP_CONTENT); 283 assertNotNull(contentReader1); 284 assertEquals(UPDATED_CONTENT_1, contentReader1.getContentString()); 285 286 Set <QName> aspects1 = this.dbNodeService.getAspects(versionableNode); 288 assertEquals(aspects1.size(), origAspects2.size()); 289 290 this.versionService.revert(versionableNode, version1); 292 293 assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL)); 295 296 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 ContentReader contentReader2 = this.contentService.getReader(versionableNode, ContentModel.PROP_CONTENT); 303 assertNotNull(contentReader2); 304 assertEquals(TEST_CONTENT, contentReader2.getContentString()); 305 306 Set <QName> aspects2 = this.dbNodeService.getAspects(versionableNode); 308 assertEquals(aspects2.size(), origAspects.size()); 309 310 assertEquals(versionLabel, this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL)); 312 } 313 314 317 public void testRestore() 318 { 319 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 } 333 334 NodeRef versionableNode = createNewVersionableNode(); 336 337 Set <QName> origAspects = this.dbNodeService.getAspects(versionableNode); 339 340 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 } 354 355 this.versionService.createVersion(versionableNode, null); 357 358 this.dbNodeService.deleteNode(versionableNode); 360 assertFalse(this.dbNodeService.exists(versionableNode)); 361 362 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 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 ContentReader contentReader2 = this.contentService.getReader(restoredNode, ContentModel.PROP_CONTENT); 379 assertNotNull(contentReader2); 380 assertEquals(TEST_CONTENT, contentReader2.getContentString()); 381 382 Set <QName> aspects2 = this.dbNodeService.getAspects(restoredNode); 384 assertEquals(aspects2.size(), origAspects.size()); 385 } 386 387 390 public void testDeleteVersionHistory() 391 { 392 NodeRef versionableNode = createNewVersionableNode(); 394 395 VersionHistory versionHistory1 = this.versionService.getVersionHistory(versionableNode); 397 assertNull(versionHistory1); 398 399 createVersion(versionableNode); 401 Version version1 = createVersion(versionableNode); 402 403 String versionLabel1 = (String )this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL); 405 assertNotNull(versionLabel1); 406 assertEquals(version1.getVersionLabel(), versionLabel1); 407 408 VersionHistory versionHistory2 = this.versionService.getVersionHistory(versionableNode); 410 assertNotNull(versionHistory2); 411 assertEquals(2, versionHistory2.getAllVersions().size()); 412 413 this.versionService.deleteVersionHistory(versionableNode); 415 416 VersionHistory versionHistory3 = this.versionService.getVersionHistory(versionableNode); 418 assertNull(versionHistory3); 419 420 String versionLabel2 = (String )this.dbNodeService.getProperty(versionableNode, ContentModel.PROP_VERSION_LABEL); 422 assertNull(versionLabel2); 423 424 createVersion(versionableNode); 426 Version version2 = createVersion(versionableNode); 427 428 VersionHistory versionHistory4 = this.versionService.getVersionHistory(versionableNode); 430 assertNotNull(versionHistory4); 431 assertEquals(2, versionHistory4.getAllVersions().size()); 432 433 String versionLabel3 = (String )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 final NodeRef versionableNode = createNewVersionableNode(); 444 445 setComplete(); 446 endTransaction(); 447 448 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object >() 449 { 450 public Object doWork() throws Exception 451 { 452 VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode); 454 assertNotNull(versionHistory); 455 assertEquals(1, versionHistory.getAllVersions().size()); 456 457 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 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object >() 468 { 469 public Object doWork() throws Exception 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 final NodeRef versionableNode = createNewVersionableNode(); 485 this.dbNodeService.setProperty(versionableNode, ContentModel.PROP_AUTO_VERSION, false); 486 487 setComplete(); 488 endTransaction(); 489 490 492 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object >() 493 { 494 public Object doWork() throws Exception 495 { 496 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 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object >() 507 { 508 public Object doWork() throws Exception 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 HashMap <QName, Serializable > props = new HashMap <QName, Serializable >(); 524 props.put(ContentModel.PROP_INITIAL_VERSION, false); 525 HashMap <QName, Serializable > props2 = new HashMap <QName, Serializable >(); 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 540 TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork<Object >() 541 { 542 public Object doWork() throws Exception 543 { 544 VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(nodeRef); 545 assertNull(versionHistory); 546 547 return null; 548 } 549 }); 550 551 } 552 } 553 | Popular Tags |