KickJava   Java API By Example, From Geeks To Geeks.

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


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.List 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.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 /**
39  * Simple lock service test
40  *
41  * @author Roy Wetherall
42  */

43 public class LockServiceImplTest extends BaseSpringTest
44 {
45     /**
46      * Services used in tests
47      */

48     private NodeService nodeService;
49     private LockService lockService;
50     private AuthenticationService authenticationService;
51     
52     /**
53      * Data used in tests
54      */

55     private NodeRef parentNode;
56     private NodeRef childNode1;
57     private NodeRef childNode2;
58     private NodeRef noAspectNode;
59     
60     private static final String JavaDoc GOOD_USER_NAME = "goodUser";
61     private static final String JavaDoc BAD_USER_NAME = "badUser";
62     private static final String JavaDoc PWD = "password";
63     
64     NodeRef rootNodeRef;
65     private StoreRef storeRef;
66
67     /**
68      * Called during the transaction setup
69      */

70     protected void onSetUpInTransaction() throws Exception JavaDoc
71     {
72         this.nodeService = (NodeService)applicationContext.getBean("dbNodeService");
73         this.lockService = (LockService)applicationContext.getBean("lockService");
74         this.authenticationService = (AuthenticationService)applicationContext.getBean("authenticationService");
75         
76         // Set the authentication
77
AuthenticationComponent authComponent = (AuthenticationComponent)this.applicationContext.getBean("authenticationComponent");
78         authComponent.setSystemUserAsCurrentUser();
79         
80         // Create the node properties
81
HashMap JavaDoc<QName, Serializable JavaDoc> nodeProperties = new HashMap JavaDoc<QName, Serializable JavaDoc>();
82         nodeProperties.put(QName.createQName("{test}property1"), "value1");
83         
84         // Create a workspace that contains the 'live' nodes
85
storeRef = this.nodeService.createStore(StoreRef.PROTOCOL_WORKSPACE, "Test_" + System.currentTimeMillis());
86         
87         // Get a reference to the root node
88
rootNodeRef = this.nodeService.getRootNode(storeRef);
89         
90         // Create node
91
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 JavaDoc<QName, Serializable JavaDoc>());
98         HashMap JavaDoc<QName, Serializable JavaDoc> audProps = new HashMap JavaDoc<QName, Serializable JavaDoc>();
99         audProps.put(ContentModel.PROP_CREATOR, "Monkey");
100         this.nodeService.addAspect(this.parentNode, ContentModel.ASPECT_AUDITABLE, audProps);
101         assertNotNull(this.parentNode);
102         
103         // Add some children to the node
104
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 JavaDoc<QName, Serializable JavaDoc>());
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 JavaDoc<QName, Serializable JavaDoc>());
119         assertNotNull(this.childNode2);
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     
139     /**
140      * Test lock
141      */

142     public void testLock()
143     {
144         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
145         
146         // Check that the node is not currently locked
147
assertEquals(
148                 LockStatus.NO_LOCK,
149                 this.lockService.getLockStatus(this.parentNode));
150  
151         
152         // Test valid lock
153
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         // Test lock when already locked
165
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         // Test already locked by this user
178
try
179         {
180             this.lockService.lock(this.parentNode, LockType.WRITE_LOCK);
181         }
182         catch (Exception JavaDoc exception)
183         {
184             fail("No error should be thrown when a node is re-locked by the current lock owner.");
185         }
186         
187         // Test with no apect node
188
this.lockService.lock(this.noAspectNode, LockType.WRITE_LOCK);
189     }
190
191     /**
192      * Test lock with lockChildren == true
193      */

194     // TODO
195
public void testLockChildren()
196     {
197     }
198     
199     /**
200      * Test lock with collection
201      */

202     // TODO
203
public void testLockMany()
204     {
205     }
206     
207     /**
208      * Test unlock node
209      */

210     public void testUnlock()
211     {
212         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
213         
214         // Lock the parent node
215
testLock();
216         
217         TestWithUserUtils.authenticateUser(BAD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
218         
219         // Try and unlock a locked node
220
try
221         {
222             this.lockService.unlock(this.parentNode);
223             // This will pass in the open workd
224
//fail("A user cannot unlock a node that is currently lock by another user.");
225
}
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         // Unlock the node
234
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 and unlock node with no lock
248
try
249         {
250             this.lockService.unlock(this.parentNode);
251         }
252         catch (Exception JavaDoc exception)
253         {
254             fail("Unlocking an unlocked node should not result in an exception being raised.");
255         }
256         
257         // Test with no apect node
258
this.lockService.unlock(this.noAspectNode);
259     }
260     
261     // TODO
262
public void testUnlockChildren()
263     {
264     }
265     
266     // TODO
267
public void testUnlockMany()
268     {
269     }
270     
271     /**
272      * Test getLockStatus
273      */

274     public void testGetLockStatus()
275     {
276         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
277         
278         // Check an unlocked node
279
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         // Check for locked status
287
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         // Check for lock owner status
293
LockStatus lockStatus3 = this.lockService.getLockStatus(this.parentNode);
294         assertEquals(LockStatus.LOCK_OWNER, lockStatus3);
295                 
296         // Test with no apect node
297
this.lockService.getLockStatus(this.noAspectNode);
298         
299         // Test method overload
300
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 JavaDoc<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 JavaDoc<NodeRef> locked2 = this.lockService.getLocks(this.storeRef);
317         assertNotNull(locked2);
318         assertEquals(3, locked2.size());
319         
320         List JavaDoc<NodeRef> locked3 = this.lockService.getLocks(this.storeRef, LockType.WRITE_LOCK);
321         assertNotNull(locked3);
322         assertEquals(2, locked3.size());
323         
324         List JavaDoc<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 JavaDoc<NodeRef> locked5 = this.lockService.getLocks(this.storeRef);
331         assertNotNull(locked5);
332         assertEquals(0, locked5.size());
333     }
334     
335     /**
336      * Test getLockType
337      */

338     public void testGetLockType()
339     {
340         TestWithUserUtils.authenticateUser(GOOD_USER_NAME, PWD, rootNodeRef, this.authenticationService);
341         
342         // Get the lock type (should be null since the object is not locked)
343
LockType lockType1 = this.lockService.getLockType(this.parentNode);
344         assertNull(lockType1);
345         
346         // Lock the object for writing
347
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         // Unlock the node
353
this.lockService.unlock(this.parentNode);
354         LockType lockType3 = this.lockService.getLockType(this.parentNode);
355         assertNull(lockType3);
356         
357         // Lock the object for read only
358
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         // Test with no apect node
364
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         // Wait for 2 second before re-testing the status
379
try {Thread.sleep(2*1000);} catch (Exception JavaDoc 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         // Re-lock and then update the time to expire before lock expires
388
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             // Expected
399
}
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         // Wait for 2 second before re-testing the status
409
try {Thread.sleep(2*1000);} catch (Exception JavaDoc 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