1 17 package org.alfresco.repo.coci; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.alfresco.model.ContentModel; 25 import org.alfresco.repo.content.MimetypeMap; 26 import org.alfresco.repo.security.authentication.AuthenticationComponent; 27 import org.alfresco.repo.transaction.TransactionUtil; 28 import org.alfresco.repo.version.VersionModel; 29 import org.alfresco.service.cmr.coci.CheckOutCheckInService; 30 import org.alfresco.service.cmr.lock.LockService; 31 import org.alfresco.service.cmr.repository.AssociationRef; 32 import org.alfresco.service.cmr.repository.ChildAssociationRef; 33 import org.alfresco.service.cmr.repository.ContentData; 34 import org.alfresco.service.cmr.repository.ContentReader; 35 import org.alfresco.service.cmr.repository.ContentService; 36 import org.alfresco.service.cmr.repository.ContentWriter; 37 import org.alfresco.service.cmr.repository.NodeRef; 38 import org.alfresco.service.cmr.repository.NodeService; 39 import org.alfresco.service.cmr.repository.StoreRef; 40 import org.alfresco.service.cmr.security.AuthenticationService; 41 import org.alfresco.service.cmr.security.PermissionService; 42 import org.alfresco.service.cmr.version.Version; 43 import org.alfresco.service.cmr.version.VersionService; 44 import org.alfresco.service.cmr.version.VersionType; 45 import org.alfresco.service.namespace.NamespaceService; 46 import org.alfresco.service.namespace.QName; 47 import org.alfresco.service.transaction.TransactionService; 48 import org.alfresco.util.BaseSpringTest; 49 import org.alfresco.util.GUID; 50 import org.alfresco.util.TestWithUserUtils; 51 52 57 public class CheckOutCheckInServiceImplTest extends BaseSpringTest 58 { 59 62 private NodeService nodeService; 63 private CheckOutCheckInService cociService; 64 private ContentService contentService; 65 private VersionService versionService; 66 private AuthenticationService authenticationService; 67 private LockService lockService; 68 private TransactionService transactionService; 69 private PermissionService permissionService; 70 71 74 private StoreRef storeRef; 75 private NodeRef rootNodeRef; 76 private NodeRef nodeRef; 77 private String userNodeRef; 78 79 82 private static final String TEST_VALUE_NAME = "myDocument.doc"; 83 private static final String TEST_VALUE_2 = "testValue2"; 84 private static final String TEST_VALUE_3 = "testValue3"; 85 private static final QName PROP_NAME_QNAME = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "name"); 86 private static final QName PROP2_QNAME = ContentModel.PROP_DESCRIPTION; 87 private static final String CONTENT_1 = "This is some content"; 88 private static final String CONTENT_2 = "This is the cotent modified."; 89 90 93 private String userName; 95 private static final String PWD = "password"; 96 97 100 @Override 101 protected void onSetUpInTransaction() 102 throws Exception 103 { 104 this.nodeService = (NodeService)this.applicationContext.getBean("nodeService"); 106 this.cociService = (CheckOutCheckInService)this.applicationContext.getBean("checkOutCheckInService"); 107 this.contentService = (ContentService)this.applicationContext.getBean("contentService"); 108 this.versionService = (VersionService)this.applicationContext.getBean("versionService"); 109 this.authenticationService = (AuthenticationService)this.applicationContext.getBean("authenticationService"); 110 this.lockService = (LockService)this.applicationContext.getBean("lockService"); 111 this.transactionService = (TransactionService)this.applicationContext.getBean("transactionComponent"); 112 this.permissionService = (PermissionService)this.applicationContext.getBean("permissionService"); 113 114 AuthenticationComponent authenticationComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent"); 116 authenticationComponent.setSystemUserAsCurrentUser(); 117 118 this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 120 this.rootNodeRef = this.nodeService.getRootNode(storeRef); 121 122 ChildAssociationRef childAssocRef = this.nodeService.createNode( 124 rootNodeRef, 125 ContentModel.ASSOC_CHILDREN, 126 QName.createQName("{test}test"), 127 ContentModel.TYPE_CONTENT); 128 this.nodeRef = childAssocRef.getChildRef(); 129 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_TITLED, null); 130 this.nodeService.setProperty(this.nodeRef, ContentModel.PROP_NAME, TEST_VALUE_NAME); 131 this.nodeService.setProperty(this.nodeRef, PROP2_QNAME, TEST_VALUE_2); 132 133 ContentWriter contentWriter = this.contentService.getWriter(this.nodeRef, ContentModel.PROP_CONTENT, true); 135 contentWriter.setMimetype("text/plain"); 136 contentWriter.setEncoding("UTF-8"); 137 contentWriter.putContent(CONTENT_1); 138 139 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null); 141 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, null); 142 143 this.userName = "cociTest" + GUID.generate(); 145 TestWithUserUtils.createUser(this.userName, PWD, this.rootNodeRef, this.nodeService, this.authenticationService); 146 TestWithUserUtils.authenticateUser(this.userName, PWD, this.rootNodeRef, this.authenticationService); 147 this.userNodeRef = TestWithUserUtils.getCurrentUser(this.authenticationService); 148 149 permissionService.setPermission(this.rootNodeRef, this.userName.toLowerCase(), PermissionService.ALL_PERMISSIONS, true); 150 permissionService.setPermission(this.nodeRef, this.userName.toLowerCase(), PermissionService.ALL_PERMISSIONS, true); 151 } 152 153 158 private Map <QName, Serializable > createTypePropertyBag() 159 { 160 Map <QName, Serializable > result = new HashMap <QName, Serializable >(); 161 result.put(PROP_NAME_QNAME, TEST_VALUE_NAME); 162 return result; 163 } 164 165 168 public void testCheckOut() 169 { 170 checkout(); 171 } 172 173 177 private NodeRef checkout() 178 { 179 NodeRef workingCopy = this.cociService.checkout( 181 this.nodeRef, 182 this.rootNodeRef, 183 ContentModel.ASSOC_CHILDREN, 184 QName.createQName("{test}workingCopy")); 185 assertNotNull(workingCopy); 186 187 189 assertTrue(this.nodeService.hasAspect(workingCopy, ContentModel.ASPECT_WORKING_COPY)); 191 assertTrue(this.nodeService.hasAspect(workingCopy, ContentModel.ASPECT_COPIEDFROM)); 192 193 assertEquals(this.userNodeRef, this.nodeService.getProperty(workingCopy, ContentModel.PROP_WORKING_COPY_OWNER)); 195 196 String workingCopyLabel = ((CheckOutCheckInServiceImpl)this.cociService).getWorkingCopyLabel(); 198 String workingCopyName = (String )this.nodeService.getProperty(workingCopy, PROP_NAME_QNAME); 199 if (workingCopyLabel == null || workingCopyLabel.length() == 0) 200 { 201 assertEquals("myDocument.doc", workingCopyName); 202 } 203 else 204 { 205 assertEquals( 206 "myDocument " + workingCopyLabel + ".doc", 207 workingCopyName); 208 } 209 210 ContentReader contentReader = this.contentService.getReader(this.nodeRef, ContentModel.PROP_CONTENT); 212 assertNotNull(contentReader); 213 ContentReader contentReader2 = this.contentService.getReader(workingCopy, ContentModel.PROP_CONTENT); 214 assertNotNull(contentReader2); 215 assertEquals( 216 "The content string of the working copy should match the original immediatly after checkout.", 217 contentReader.getContentString(), 218 contentReader2.getContentString()); 219 220 return workingCopy; 221 } 222 223 226 public void testCheckIn() 227 { 228 NodeRef workingCopy = checkout(); 229 230 Map <String , Serializable > versionProperties = new HashMap <String , Serializable >(); 232 versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version"); 233 this.cociService.checkin(workingCopy, versionProperties); 234 235 NodeRef workingCopy3 = checkout(); 237 238 this.nodeService.setProperty(workingCopy3, PROP_NAME_QNAME, TEST_VALUE_2); 239 this.nodeService.setProperty(workingCopy3, PROP2_QNAME, TEST_VALUE_3); 240 ContentWriter tempWriter = this.contentService.getWriter(workingCopy3, ContentModel.PROP_CONTENT, false); 241 assertNotNull(tempWriter); 242 tempWriter.putContent(CONTENT_2); 243 String contentUrl = tempWriter.getContentUrl(); 244 Map <String , Serializable > versionProperties3 = new HashMap <String , Serializable >(); 245 versionProperties3.put(Version.PROP_DESCRIPTION, "description"); 246 versionProperties3.put(VersionModel.PROP_VERSION_TYPE, VersionType.MAJOR); 247 NodeRef origNodeRef = this.cociService.checkin(workingCopy3, versionProperties3, contentUrl, true); 248 assertNotNull(origNodeRef); 249 250 ContentReader contentReader = this.contentService.getReader(origNodeRef, ContentModel.PROP_CONTENT); 252 assertNotNull(contentReader); 253 assertEquals(CONTENT_2, contentReader.getContentString()); 254 255 Version version = this.versionService.getCurrentVersion(origNodeRef); 257 assertNotNull(version); 258 assertEquals("description", version.getDescription()); 259 assertEquals(VersionType.MAJOR, version.getVersionType()); 260 NodeRef versionNodeRef = version.getFrozenStateNodeRef(); 261 assertNotNull(versionNodeRef); 262 263 ContentReader versionContentReader = this.contentService.getReader(versionNodeRef, ContentModel.PROP_CONTENT); 265 assertNotNull(versionContentReader); 266 assertEquals(CONTENT_2, versionContentReader.getContentString()); 267 268 assertEquals(TEST_VALUE_NAME, this.nodeService.getProperty(versionNodeRef, PROP_NAME_QNAME)); 270 assertEquals(TEST_VALUE_NAME, this.nodeService.getProperty(origNodeRef, PROP_NAME_QNAME)); 271 272 assertEquals(TEST_VALUE_3, this.nodeService.getProperty(versionNodeRef, PROP2_QNAME)); 274 assertEquals(TEST_VALUE_3, this.nodeService.getProperty(origNodeRef, PROP2_QNAME)); 275 276 this.cociService.cancelCheckout(workingCopy3); 278 279 NodeRef workingCopy2 = checkout(); 281 Map <String , Serializable > versionProperties2 = new HashMap <String , Serializable >(); 282 versionProperties2.put(Version.PROP_DESCRIPTION, "Another version test"); 283 this.cociService.checkin(workingCopy2, versionProperties2, null, true); 284 this.cociService.checkin(workingCopy2, new HashMap <String , Serializable >(), null, true); 285 } 286 287 public void testCheckOutCheckInWithTranslatableAspect() 288 { 289 NodeRef translationNodeRef = this.nodeService.createNode( 291 rootNodeRef, 292 ContentModel.ASSOC_CHILDREN, 293 QName.createQName("{test}translation"), 294 ContentModel.TYPE_CONTENT).getChildRef(); 295 296 this.nodeService.addAspect(this.nodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "translatable"), null); 297 this.nodeService.createAssociation(this.nodeRef, translationNodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "translations")); 298 299 NodeRef workingCopy = this.cociService.checkout( 301 this.nodeRef, 302 this.rootNodeRef, 303 ContentModel.ASSOC_CHILDREN, 304 QName.createQName("{test}workingCopy")); 305 306 307 Map <String , Serializable > versionProperties = new HashMap <String , Serializable >(); 309 versionProperties.put(Version.PROP_DESCRIPTION, "This is a test version"); 310 this.cociService.checkin(workingCopy, versionProperties); 311 } 312 313 316 public void testVersionAspectNotSetOnCheckIn() 317 { 318 Map <QName, Serializable > bagOfProps = createTypePropertyBag(); 320 bagOfProps.put(ContentModel.PROP_CONTENT, new ContentData(null, MimetypeMap.MIMETYPE_TEXT_PLAIN, 0L, "UTF-8")); 321 322 ChildAssociationRef childAssocRef = this.nodeService.createNode( 324 rootNodeRef, 325 ContentModel.ASSOC_CHILDREN, 326 QName.createQName("{test}test"), 327 ContentModel.TYPE_CONTENT, 328 bagOfProps); 329 NodeRef noVersionNodeRef = childAssocRef.getChildRef(); 330 331 NodeRef workingCopy = this.cociService.checkout(noVersionNodeRef); 333 this.cociService.checkin(workingCopy, new HashMap <String , Serializable >()); 334 335 assertNull(this.versionService.getVersionHistory(noVersionNodeRef)); 337 } 338 339 342 public void testCancelCheckOut() 343 { 344 NodeRef workingCopy = checkout(); 345 assertNotNull(workingCopy); 346 347 try 348 { 349 this.lockService.checkForLock(this.nodeRef); 350 fail("The origional should be locked now."); 351 } 352 catch (Throwable exception) 353 { 354 } 356 357 NodeRef origNodeRef = this.cociService.cancelCheckout(workingCopy); 358 assertEquals(this.nodeRef, origNodeRef); 359 360 this.lockService.checkForLock(origNodeRef); 362 } 363 364 367 public void testAutoCancelCheckOut() 368 { 369 NodeRef workingCopy = checkout(); 370 assertNotNull(workingCopy); 371 372 try 373 { 374 this.lockService.checkForLock(this.nodeRef); 375 fail("The origional should be locked now."); 376 } 377 catch (Throwable exception) 378 { 379 } 381 382 this.nodeService.deleteNode(workingCopy); 384 385 this.lockService.checkForLock(this.nodeRef); 387 388 } 389 390 393 public void testGetWorkingCopy() 394 { 395 NodeRef origNodeRef = this.nodeService.createNode( 396 this.rootNodeRef, 397 ContentModel.ASSOC_CHILDREN, 398 QName.createQName("{test}test2"), 399 ContentModel.TYPE_CONTENT).getChildRef(); 400 401 402 NodeRef wk1 = this.cociService.getWorkingCopy(origNodeRef); 403 assertNull(wk1); 404 405 final NodeRef workingCopy = this.cociService.checkout(origNodeRef); 407 408 setComplete(); 410 endTransaction(); 411 412 final NodeRef finalNodeRef = origNodeRef; 413 414 TransactionUtil.executeInUserTransaction( 415 this.transactionService, 416 new TransactionUtil.TransactionWork<Object >() 417 { 418 public Object doWork() 419 { 420 NodeRef wk2 = CheckOutCheckInServiceImplTest.this.cociService.getWorkingCopy(finalNodeRef); 421 assertNotNull(wk2); 422 assertEquals(workingCopy, wk2); 423 424 CheckOutCheckInServiceImplTest.this.cociService.cancelCheckout(workingCopy); 425 return null; 426 } 427 428 }); 429 430 NodeRef wk3 = this.cociService.getWorkingCopy(this.nodeRef); 431 assertNull(wk3); 432 } 433 434 } 435 | Popular Tags |