KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > lock > LockBehaviourImplTest


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.lock;
18
19 import java.io.Serializable JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
40  * LockBehaviourImpl Unit Test.
41  *
42  * @author Roy Wetherall
43  */

44 public class LockBehaviourImplTest extends BaseSpringTest
45 {
46     /**
47      * The lock service
48      */

49     private LockService lockService;
50     
51     /**
52      * The version service
53      */

54     private VersionService versionService;
55     
56     /**
57      * The node service
58      */

59     private NodeService nodeService;
60
61     /**
62      * The authentication service
63      */

64     private AuthenticationService authenticationService;
65     
66     private PermissionService permissionService;
67     
68     /**
69      * Node references used in the tests
70      */

71     private NodeRef nodeRef;
72     private NodeRef noAspectNode;
73     
74     /**
75      * Store reference
76      */

77     private StoreRef storeRef;
78     
79     /**
80      * User details
81      */

82     private static final String JavaDoc PWD = "password";
83     private static final String JavaDoc GOOD_USER_NAME = "goodUser";
84     private static final String JavaDoc BAD_USER_NAME = "badUser";
85     
86     NodeRef rootNodeRef;
87    
88     @Override JavaDoc
89     protected void onSetUpInTransaction() throws Exception JavaDoc
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         // Set the authentication
98
AuthenticationComponent authComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
99         authComponent.setSystemUserAsCurrentUser();
100         
101         // Create the node properties
102
HashMap JavaDoc<QName, Serializable JavaDoc> nodeProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
103         nodeProperties.put(QName.createQName("{test}property1"), "value1");
104         
105         // Create a workspace that contains the 'live' nodes
106
this.storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
107         
108         // Get a reference to the root node
109
rootNodeRef = this.nodeService.getRootNode(this.storeRef);
110         
111         // Create node
112
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 JavaDoc<QName, Serializable JavaDoc>());
119         assertNotNull(this.nodeRef);
120         
121         // Create a node with no lockAspect
122
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         // Create the users
131
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         // Stash the user node ref's for later use
135
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     /**
143      * Test checkForLock (no user specified)
144      */

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         // Give the node a write lock (as the good user)
153
this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK);
154         this.lockService.checkForLock(this.nodeRef);
155         
156         // Give the node a read only lock (as the good user)
157
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             // Correct behaviour
167
}
168         
169         // Give the node a write lock (as the bad user)
170
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             // Correct behaviour
183
}
184         
185         // Give the node a read only lock (as the bad user)
186
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             // Correct behaviour
198
}
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             // Expected
214
}
215         
216         try {Thread.sleep(2*1000); } catch (Exception JavaDoc e) {};
217         
218         // Should now have expired so the node should no longer appear to be locked
219
this.lockService.checkForLock(this.nodeRef);
220     }
221     
222     /**
223      * Test version service lock checking
224      */

225     public void testVersionServiceLockBehaviour01()
226     {
227         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
228         
229         // Add the version aspect to the node
230
this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
231         
232         try
233         {
234             this.versionService.createVersion(this.nodeRef, new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>());
235         }
236         catch (NodeLockedException exception)
237         {
238             fail("There is no lock so this should have worked.");
239         }
240         
241         // Lock the node as the good user with a write lock
242
this.lockService.lock(this.nodeRef, LockType.WRITE_LOCK);
243         try
244         {
245             this.versionService.createVersion(this.nodeRef, new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>());
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         // Lock the node as the good user with a read only lock
254
this.lockService.lock(this.nodeRef, LockType.READ_ONLY_LOCK);
255         try
256         {
257             this.versionService.createVersion(this.nodeRef, new HashMap JavaDoc<String JavaDoc, Serializable JavaDoc>());
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     /**
267      * Test version service lock checking
268      */

269     public void testVersionServiceLockBehaviour02()
270     {
271         // Add the version aspect to the node
272
this.nodeService.addAspect(this.nodeRef, ContentModel.ASPECT_VERSIONABLE, null);
273         
274         // Lock the node as the bad user with a write lock
275
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 JavaDoc<String JavaDoc, Serializable JavaDoc>());
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     /**
289      * Test that the node service lock behaviour is as we expect
290      *
291      */

292     @SuppressWarnings JavaDoc("unused")
293     public void testNodeServiceLockBehaviour()
294     {
295         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
296         
297         // Check that we can create a new node and set of it properties when no lock is present
298
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         // Lets lock the parent node and check that whether we can still create a new node
306
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         // Lets check that we can do other stuff with the node since we have it locked
315
this.nodeService.setProperty(this.nodeRef, QName.createQName("{test}prop1"), "value1");
316         Map JavaDoc<QName, Serializable JavaDoc> propMap = new HashMap JavaDoc<QName, Serializable JavaDoc>();
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         // TODO there are various other calls that could be more vigirously checked
321

322         // Lock the node as the 'bad' user
323
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         // Lets check that we can't create a new child
331
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         // TODO various other tests along these lines ...
345

346         // TODO check that delete is also working
347
}
348     
349 }
350
Popular Tags