| 1 17 package org.alfresco.repo.lock; 18 19 import java.io.Serializable ; 20 import java.util.HashMap ; 21 import java.util.Map ; 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.LockType; 27 import org.alfresco.service.cmr.lock.NodeLockedException; 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.StoreRef; 32 import org.alfresco.service.cmr.security.AuthenticationService; 33 import org.alfresco.service.cmr.security.PermissionService; 34 import org.alfresco.service.cmr.version.VersionService; 35 import org.alfresco.service.namespace.QName; 36 import org.alfresco.util.BaseSpringTest; 37 import org.alfresco.util.TestWithUserUtils; 38 39 44 public class LockBehaviourImplTest extends BaseSpringTest 45 { 46 49 private LockService lockService; 50 51 54 private VersionService versionService; 55 56 59 private NodeService nodeService; 60 61 64 private AuthenticationService authenticationService; 65 66 private PermissionService permissionService; 67 68 71 private NodeRef nodeRef; 72 private NodeRef noAspectNode; 73 74 77 private StoreRef storeRef; 78 79 82 private static final String PWD = "password"; 83 private static final String GOOD_USER_NAME = "goodUser"; 84 private static final String BAD_USER_NAME = "badUser"; 85 86 NodeRef rootNodeRef; 87 88 @Override  89 protected void onSetUpInTransaction() throws Exception  90 { 91 this.nodeService = (NodeService)applicationContext.getBean("dbNodeService"); 92 this.lockService = (LockService)applicationContext.getBean("lockService"); 93 this.versionService = (VersionService)applicationContext.getBean("versionService"); 94 this.authenticationService = (AuthenticationService)applicationContext.getBean("authenticationService"); 95 this.permissionService = (PermissionService)applicationContext.getBean("permissionService"); 96 97 AuthenticationComponent authComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent"); 99 authComponent.setSystemUserAsCurrentUser(); 100 101 HashMap <QName, Serializable > nodeProperties = new HashMap <QName, Serializable >(); 103 nodeProperties.put(QName.createQName("{test}property1"), "value1"); 104 105 this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis()); 107 108 rootNodeRef = this.nodeService.getRootNode(this.storeRef); 110 111 this.nodeRef = this.nodeService.createNode( 113 rootNodeRef, 114 ContentModel.ASSOC_CHILDREN, 115 QName.createQName("{}ParentNode"), 116 ContentModel.TYPE_FOLDER, 117 nodeProperties).getChildRef(); 118 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_LOCKABLE, new HashMap <QName, Serializable >()); 119 assertNotNull(this.nodeRef); 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 permissionService.setPermission(rootNodeRef, GOOD_USER_NAME.toLowerCase(), PermissionService.ALL_PERMISSIONS, true); 139 permissionService.setPermission(rootNodeRef, BAD_USER_NAME.toLowerCase(), PermissionService.READ, true); 140 } 141 142 145 public void testCheckForLockNoUser() 146 { 147 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 148 149 this.lockService.checkForLock(this.nodeRef); 150 this.lockService.checkForLock(this.noAspectNode); 151 152 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 154 this.lockService.checkForLock(this.nodeRef); 155 156 this.lockService.unlock(this.nodeRef); 158 this.lockService.lock(this.nodeRef, LockType.READ_ONLY_LOCK); 159 try 160 { 161 this.lockService.checkForLock(this.nodeRef); 162 fail("The node locked exception should have been raised"); 163 } 164 catch (NodeLockedException exception) 165 { 166 } 168 169 this.lockService.unlock(this.nodeRef); 171 172 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 173 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 174 try 175 { 176 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 177 this.lockService.checkForLock(this.nodeRef); 178 fail("The node locked exception should have been raised"); 179 } 180 catch (NodeLockedException exception) 181 { 182 } 184 185 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 187 this.lockService.unlock(this.nodeRef); 188 this.lockService.lock(this.nodeRef, LockType.READ_ONLY_LOCK); 189 try 190 { 191 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 192 this.lockService.checkForLock(this.nodeRef); 193 fail("The node locked exception should have been raised"); 194 } 195 catch (NodeLockedException exception) 196 { 197 } 199 } 200 201 public void testCheckForLockWhenExpired() 202 { 203 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 204 205 this.lockService.lock(this.nodeRef, LockType.READ_ONLY_LOCK, 1); 206 try 207 { 208 this.lockService.checkForLock(this.nodeRef); 209 fail("Should be locked."); 210 } 211 catch (NodeLockedException e) 212 { 213 } 215 216 try {Thread.sleep(2*1000); } catch (Exception e) {}; 217 218 this.lockService.checkForLock(this.nodeRef); 220 } 221 222 225 public void testVersionServiceLockBehaviour01() 226 { 227 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 228 229 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null); 231 232 try 233 { 234 this.versionService.createVersion(this.nodeRef, new HashMap <String , Serializable >()); 235 } 236 catch (NodeLockedException exception) 237 { 238 fail("There is no lock so this should have worked."); 239 } 240 241 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 243 try 244 { 245 this.versionService.createVersion(this.nodeRef, new HashMap <String , Serializable >()); 246 } 247 catch (NodeLockedException exception) 248 { 249 fail("Tried to version as the lock owner so should work."); 250 } 251 this.lockService.unlock(this.nodeRef); 252 253 this.lockService.lock(this.nodeRef, LockType.READ_ONLY_LOCK); 255 try 256 { 257 this.versionService.createVersion(this.nodeRef, new HashMap <String , Serializable >()); 258 fail("Should have failed since this node has been locked with a read only lock."); 259 } 260 catch (NodeLockedException exception) 261 { 262 } 263 this.lockService.unlock(this.nodeRef); 264 } 265 266 269 public void testVersionServiceLockBehaviour02() 270 { 271 this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null); 273 274 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 276 try 277 { 278 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 279 280 this.versionService.createVersion(this.nodeRef, new HashMap <String , Serializable >()); 281 fail("Should have failed since this node has been locked by another user with a write lock."); 282 } 283 catch (NodeLockedException exception) 284 { 285 } 286 } 287 288 292 @SuppressWarnings ("unused") 293 public void testNodeServiceLockBehaviour() 294 { 295 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 296 297 ChildAssociationRef childAssocRef = this.nodeService.createNode( 299 this.nodeRef, 300 ContentModel.ASSOC_CONTAINS, 301 QName.createQName("{test}nodeServiceLockTest"), 302 ContentModel.TYPE_CONTAINER); 303 NodeRef nodeRef = childAssocRef.getChildRef(); 304 305 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 307 ChildAssociationRef childAssocRef2 = this.nodeService.createNode( 308 this.nodeRef, 309 ContentModel.ASSOC_CONTAINS, 310 QName.createQName("{test}nodeServiceLockTest"), 311 ContentModel.TYPE_CONTAINER); 312 NodeRef nodeRef2 = childAssocRef.getChildRef(); 313 314 this.nodeService.setProperty(this.nodeRef, QName.createQName("{test}prop1"), "value1"); 316 Map <QName, Serializable > propMap = new HashMap <QName, Serializable >(); 317 propMap.put(QName.createQName("{test}prop2"), "value2"); 318 this.nodeService.setProperties(this.nodeRef, propMap); 319 this.nodeService.removeAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE); 320 322 this.lockService.unlock(this.nodeRef); 324 325 TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 326 this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK); 327 328 TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService); 329 330 try 332 { 333 this.nodeService.createNode( 334 this.nodeRef, 335 ContentModel.ASSOC_CONTAINS, 336 QName.createQName("{test}nodeServiceLockTest"), 337 ContentModel.TYPE_CONTAINER); 338 fail("The parent is locked so a new child should not have been created."); 339 } 340 catch(NodeLockedException exception) 341 { 342 } 343 344 346 } 348 349 } 350 | Popular Tags |