1 17 package org.alfresco.repo.lock; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 23 import org.alfresco.model.ContentModel; 24 import org.alfresco.repo.security.authentication.AuthenticationComponent; 25 import org.alfresco.service.cmr.lock.LockService; 26 import org.alfresco.service.cmr.lock.LockStatus; 27 import org.alfresco.service.cmr.lock.LockType; 28 import org.alfresco.service.cmr.lock.UnableToAquireLockException; 29 import org.alfresco.service.cmr.lock.UnableToReleaseLockException; 30 import org.alfresco.service.cmr.repository.NodeRef; 31 import org.alfresco.service.cmr.repository.NodeService; 32 import org.alfresco.service.cmr.repository.StoreRef; 33 import org.alfresco.service.cmr.security.AuthenticationService; 34 import org.alfresco.service.namespace.QName; 35 import org.alfresco.util.BaseSpringTest; 36 import org.alfresco.util.TestWithUserUtils; 37 38 43 public class LockServiceImplTest extends BaseSpringTest 44 { 45 48 private NodeService nodeService; 49 private LockService lockService; 50 private AuthenticationService authenticationService; 51 52 55 private NodeRef parentNode; 56 private NodeRef childNode1; 57 private NodeRef childNode2; 58 private NodeRef noAspectNode; 59 60 private static final String GOOD_USER_NAME = "goodUser"; 61 private static final String BAD_USER_NAME = "badUser"; 62 private static final String PWD = "password"; 63 64 NodeRef rootNodeRef; 65 private StoreRef storeRef; 66 67 70 protected void onSetUpInTransaction() throws Exception 71 { 72 this.nodeService = (NodeService)applicationContext.getBean("dbNodeService"); 73 this.lockService = (LockService)applicationContext.getBean("lockService"); 74 this.authenticationService = (AuthenticationService)applicationContext.getBean("authenticationService"); 75 76 AuthenticationComponent authComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent"); 78 authComponent.setSystemUserAsCurrentUser(); 79 80 HashMap <QName, Serializable > nodeProperties = new HashMap <QName, Serializable >(); 82 nodeProperties.put(QName.createQName("{test}property1"), "value1"); 83 84 storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 86 87 rootNodeRef = this.nodeService.getRootNode(storeRef); 89 90 this.parentNode = this.nodeService.createNode( 92 rootNodeRef, 93 ContentModel.ASSOC_CHILDREN, 94 QName.createQName("{}ParentNode"), 95 ContentModel.TYPE_CONTAINER, 96 nodeProperties).getChildRef(); 97 this.nodeService.addAspect(this.parentNode, ContentModel.ASPECT_LOCKABLE, new HashMap <QName, Serializable >()); 98 HashMap <QName, Serializable > audProps = new HashMap <QName, Serializable >(); 99 audProps.put(ContentModel.PROP_CREATOR, "Monkey"); 100 this.nodeService.addAspect(this.parentNode, ContentModel.ASPECT_AUDITABLE, audProps); 101 assertNotNull(this.parentNode); 102 103 this.childNode1 = this.nodeService.createNode( 105 this.parentNode, 106 ContentModel.ASSOC_CHILDREN, 107 QName.createQName("{}ChildNode1"), 108 ContentModel.TYPE_CONTAINER, 109 nodeProperties).getChildRef(); 110 this.nodeService.addAspect(this.childNode1, ContentModel.ASPECT_LOCKABLE, new HashMap <QName, Serializable >()); 111 assertNotNull(this.childNode1); 112 this.childNode2 = this.nodeService.createNode( 113 this.parentNode, 114 ContentModel.ASSOC_CHILDREN, 115 QName.createQName("{}ChildNode2"), 116 ContentModel.TYPE_CONTAINER, 117 nodeProperties).getChildRef(); 118 this.nodeService.addAspect(this.childNode2, ContentModel.ASPECT_LOCKABLE, new HashMap <QName, Serializable >()); 119 assertNotNull(this.childNode2); 120 121 this.noAspectNode = this.nodeService.createNode( 123 rootNodeRef, 124 ContentModel.ASSOC_CHILDREN, 125 QName.createQName("{}noAspectNode"), 126 ContentModel.TYPE_CONTAINER, 127 nodeProperties).getChildRef(); 128 assertNotNull(this.noAspectNode); 129 130 TestWithUserUtils.createUser(GOOD_USER_NAME, PWD, rootNodeRef, this.nodeService, this.authenticationService); 132 TestWithUserUtils.createUser(BAD_USER_NAME, PWD, rootNodeRef, this.nodeService, this.authenticationService); 133 134 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 136 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 137 } 138 139 142 public void testLock() 143 { 144 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 145 146 assertEquals( 148 LockStatus.NO_LOCK, 149 this.lockService.getLockStatus(this.parentNode)); 150 151 152 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 154 assertEquals( 155 LockStatus.LOCK_OWNER, 156 this.lockService.getLockStatus(this.parentNode)); 157 158 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 159 160 assertEquals( 161 LockStatus.LOCKED, 162 this.lockService.getLockStatus(this.parentNode)); 163 164 try 166 { 167 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 168 fail("The user should not be able to lock the node since it is already locked by another user."); 169 } 170 catch (UnableToAquireLockException exception) 171 { 172 System.out.println(exception.getMessage()); 173 } 174 175 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 176 177 try 179 { 180 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 181 } 182 catch (Exception exception) 183 { 184 fail("No error should be thrown when a node is re-locked by the current lock owner."); 185 } 186 187 this.lockService.lock(this.noAspectNode, LockType.WRITE_LOCK); 189 } 190 191 194 public void testLockChildren() 196 { 197 } 198 199 202 public void testLockMany() 204 { 205 } 206 207 210 public void testUnlock() 211 { 212 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 213 214 testLock(); 216 217 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 218 219 try 221 { 222 this.lockService.unlock(this.parentNode); 223 } 226 catch (UnableToReleaseLockException exception) 227 { 228 System.out.println(exception.getMessage()); 229 } 230 231 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 232 233 this.lockService.unlock(this.parentNode); 235 assertEquals( 236 LockStatus.NO_LOCK, 237 this.lockService.getLockStatus(this.parentNode)); 238 239 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 240 241 assertEquals( 242 LockStatus.NO_LOCK, 243 this.lockService.getLockStatus(this.parentNode)); 244 245 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 246 247 try 249 { 250 this.lockService.unlock(this.parentNode); 251 } 252 catch (Exception exception) 253 { 254 fail("Unlocking an unlocked node should not result in an exception being raised."); 255 } 256 257 this.lockService.unlock(this.noAspectNode); 259 } 260 261 public void testUnlockChildren() 263 { 264 } 265 266 public void testUnlockMany() 268 { 269 } 270 271 274 public void testGetLockStatus() 275 { 276 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 277 278 LockStatus lockStatus1 = this.lockService.getLockStatus(this.parentNode); 280 assertEquals(LockStatus.NO_LOCK, lockStatus1); 281 282 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 283 284 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 285 286 LockStatus lockStatus2 = this.lockService.getLockStatus(this.parentNode); 288 assertEquals(LockStatus.LOCKED, lockStatus2); 289 290 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 291 292 LockStatus lockStatus3 = this.lockService.getLockStatus(this.parentNode); 294 assertEquals(LockStatus.LOCK_OWNER, lockStatus3); 295 296 this.lockService.getLockStatus(this.noAspectNode); 298 299 LockStatus lockStatus4 = this.lockService.getLockStatus(this.parentNode); 301 assertEquals(LockStatus.LOCK_OWNER, lockStatus4); 302 } 303 304 public void testGetLocks() 305 { 306 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 307 308 List <NodeRef> locked1 = this.lockService.getLocks(this.storeRef); 309 assertNotNull(locked1); 310 assertEquals(0, locked1.size()); 311 312 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 313 this.lockService.lock(this.childNode1, LockType.WRITE_LOCK); 314 this.lockService.lock(this.childNode2, LockType.READ_ONLY_LOCK); 315 316 List <NodeRef> locked2 = this.lockService.getLocks(this.storeRef); 317 assertNotNull(locked2); 318 assertEquals(3, locked2.size()); 319 320 List <NodeRef> locked3 = this.lockService.getLocks(this.storeRef, LockType.WRITE_LOCK); 321 assertNotNull(locked3); 322 assertEquals(2, locked3.size()); 323 324 List <NodeRef> locked4 = this.lockService.getLocks(this.storeRef, LockType.READ_ONLY_LOCK); 325 assertNotNull(locked4); 326 assertEquals(1, locked4.size()); 327 328 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 329 330 List <NodeRef> locked5 = this.lockService.getLocks(this.storeRef); 331 assertNotNull(locked5); 332 assertEquals(0, locked5.size()); 333 } 334 335 338 public void testGetLockType() 339 { 340 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 341 342 LockType lockType1 = this.lockService.getLockType(this.parentNode); 344 assertNull(lockType1); 345 346 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK); 348 LockType lockType2 = this.lockService.getLockType(this.parentNode); 349 assertNotNull(lockType2); 350 assertEquals(LockType.WRITE_LOCK, lockType2); 351 352 this.lockService.unlock(this.parentNode); 354 LockType lockType3 = this.lockService.getLockType(this.parentNode); 355 assertNull(lockType3); 356 357 this.lockService.lock(this.parentNode, LockType.READ_ONLY_LOCK); 359 LockType lockType4 = this.lockService.getLockType(this.parentNode); 360 assertNotNull(lockType4); 361 assertEquals(LockType.READ_ONLY_LOCK, lockType4); 362 363 this.lockService.getLockType(this.noAspectNode); 365 } 366 367 public void testTimeToExpire() 368 { 369 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 370 371 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK, 1); 372 assertEquals(LockStatus.LOCK_OWNER, this.lockService.getLockStatus(this.parentNode)); 373 374 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 375 376 assertEquals(LockStatus.LOCKED, this.lockService.getLockStatus(this.parentNode)); 377 378 try {Thread.sleep(2*1000);} catch (Exception exception){}; 380 381 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 382 assertEquals(LockStatus.LOCK_EXPIRED, this.lockService.getLockStatus(this.parentNode)); 383 384 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 385 assertEquals(LockStatus.LOCK_EXPIRED, this.lockService.getLockStatus(this.parentNode)); 386 387 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 389 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK, 0); 390 try 391 { 392 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 393 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK, 1); 394 fail("Can not update lock info if not lock owner"); 395 } 396 catch (UnableToAquireLockException exception) 397 { 398 } 400 401 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 402 this.lockService.lock(this.parentNode, LockType.WRITE_LOCK, 1); 403 assertEquals(LockStatus.LOCK_OWNER, this.lockService.getLockStatus(this.parentNode)); 404 405 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 406 assertEquals(LockStatus.LOCKED, this.lockService.getLockStatus(this.parentNode)); 407 408 try {Thread.sleep(2*1000);} catch (Exception exception){}; 410 411 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 412 assertEquals(LockStatus.LOCK_EXPIRED, this.lockService.getLockStatus(this.parentNode)); 413 414 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 415 assertEquals(LockStatus.LOCK_EXPIRED, this.lockService.getLockStatus(this.parentNode)); 416 } 417 } 418 | Popular Tags |