KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > TestLock


1 /*
2  * File : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestLock.java,v $
3  * Date : $Date: 2006/03/27 14:52:46 $
4  * Version: $Revision: 1.20 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31  
32 package org.opencms.file;
33
34 import org.opencms.file.types.CmsResourceTypePlain;
35 import org.opencms.lock.CmsLock;
36 import org.opencms.lock.CmsLockException;
37 import org.opencms.main.OpenCms;
38 import org.opencms.security.CmsAccessControlEntry;
39 import org.opencms.security.CmsPermissionSet;
40 import org.opencms.security.CmsPermissionViolationException;
41 import org.opencms.security.I_CmsPrincipal;
42 import org.opencms.test.OpenCmsTestCase;
43 import org.opencms.test.OpenCmsTestProperties;
44 import org.opencms.test.OpenCmsTestResourceFilter;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.Iterator JavaDoc;
48 import java.util.List JavaDoc;
49
50 import junit.extensions.TestSetup;
51 import junit.framework.Test;
52 import junit.framework.TestSuite;
53
54 /**
55  * Unit tests for lock operation.<p>
56  *
57  * @author Alexander Kandzior
58  *
59  * @version $Revision: 1.20 $
60  */

61 public class TestLock extends OpenCmsTestCase {
62   
63     /**
64      * Default JUnit constructor.<p>
65      *
66      * @param arg0 JUnit parameters
67      */

68     public TestLock(String JavaDoc arg0) {
69         super(arg0);
70     }
71     
72     /**
73      * Test suite for this test class.<p>
74      *
75      * @return the test suite
76      */

77     public static Test suite() {
78         OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
79         
80         TestSuite suite = new TestSuite();
81         suite.setName(TestLock.class.getName());
82              
83         suite.addTest(new TestLock("testLockWithDeletedNewFiles"));
84         suite.addTest(new TestLock("testLockForFile"));
85         suite.addTest(new TestLock("testLockForFolder"));
86         suite.addTest(new TestLock("testLockForFolderPrelockedShared"));
87         suite.addTest(new TestLock("testLockForFolderPrelockedExclusive"));
88         suite.addTest(new TestLock("testLockSteal"));
89         suite.addTest(new TestLock("testLockRequired"));
90         suite.addTest(new TestLock("testLockInherit"));
91         suite.addTest(new TestLock("testLockForSiblings"));
92         suite.addTest(new TestLock("testLockForBaseOperations"));
93       
94         
95         TestSetup wrapper = new TestSetup(suite) {
96             
97             protected void setUp() {
98                 setupOpenCms("simpletest", "/sites/default/");
99             }
100             
101             protected void tearDown() {
102                 removeOpenCms();
103             }
104         };
105         
106         return wrapper;
107     }
108         
109     /**
110      * Tests lock status after a new file has been deleted in offline project.<p>
111      *
112      * Issue description:
113      * User A creates a new file, but deletes it without ever publishing it.
114      * Now user B create a new file with the same name / path.
115      * The file was still in the lock manager but for user A, this generated
116      * an error for user B.<p>
117      *
118      * Solution:
119      * Remove new files that are deleted from the lock manager.<p>
120      *
121      * @throws Throwable if something goes wrong
122      */

123     public void testLockWithDeletedNewFiles() throws Throwable JavaDoc {
124         
125         CmsObject cms = getCmsObject();
126         echo("Testing lock status of a deleted new file");
127         
128         String JavaDoc source = "/folder1/newfile.html";
129
130         // create a new resource as default test user
131
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
132         // the source file must now have an exclusive lock
133
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
134         // now delete the created resource
135
cms.deleteResource(source, CmsResource.DELETE_REMOVE_SIBLINGS);
136         
137         // now login as user "test2"
138
cms.loginUser("test2", "test2");
139         cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
140                 
141         // now create the resource again
142
cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
143         
144         // the newly created resource must now be locked to user "test2"
145
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
146     }
147     
148     /**
149      * Tests lock status of a resource for basic operations.<p>
150      *
151      * @throws Throwable if something goes wrong
152      */

153     public void testLockForBaseOperations() throws Throwable JavaDoc {
154         
155         CmsObject cms = getCmsObject();
156         echo("Testing lock state for basic operations");
157         
158         String JavaDoc source = "/types/text.txt";
159         String JavaDoc destination1 = "/types/text_new1.txt";
160         String JavaDoc destination2 = "/types/text_new2.txt";
161         storeResources(cms, source);
162         
163         // copy source
164
cms.copyResource(source, destination1, CmsResource.COPY_AS_NEW);
165
166         // since source was not locked, destination must be locked exclusive
167
// and source must still be unlocked
168
assertLock(cms, source, CmsLock.TYPE_UNLOCKED);
169         assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);
170         
171         // copy source again
172
cms.lockResource(source);
173         cms.copyResource(source, destination2, CmsResource.COPY_AS_NEW);
174         
175         // both source and destination must be exlusive locked
176
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
177         assertLock(cms, destination2, CmsLock.TYPE_EXCLUSIVE);
178                 
179         // now some move tests
180
source = "/types/jsp.jsp";
181         destination1 = "/types/jsp_new1.html";
182
183         // lock resource
184
cms.lockResource(source);
185         cms.moveResource(source, destination1);
186         
187         // since source was locked, destination must be shared exclusive
188
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
189         assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);
190     }
191     
192     /**
193      * Tests lock status of a file and its siblings.<p>
194      *
195      * @throws Throwable if something goes wrong
196      */

197     public void testLockForFile() throws Throwable JavaDoc {
198         
199         CmsObject cms = getCmsObject();
200         echo("Testing locking of files");
201         
202         String JavaDoc source = "/folder1/subfolder11/page1.html";
203         String JavaDoc sibling1 = "/folder1/subfolder12/page1.html";
204         String JavaDoc sibling2 = "/folder2/subfolder22/page1.html";
205         storeResources(cms, source);
206         
207         // lock source
208
cms.lockResource(source);
209
210         // the source file must have an exclusive lock
211
// all siblings must have shared locks
212
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
213         assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
214         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
215         
216         // now unlock the source
217
cms.unlockResource(source);
218         
219         // the source file and all sibling are unlocked now
220
assertLock(cms, source, CmsLock.TYPE_UNLOCKED);
221         assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
222         assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
223     }
224     
225     /**
226      * Tests lock status of a folder and its siblings.<p>
227      *
228      * @throws Throwable if something goes wrong
229      */

230     public void testLockForFolder() throws Throwable JavaDoc {
231         
232         CmsObject cms = getCmsObject();
233         echo("Testing locking of folders");
234         
235         String JavaDoc folder = "/folder1/subfolder12/";
236         String JavaDoc sibling1 = "/folder1/subfolder12/page1.html";
237         String JavaDoc sibling2 = "/folder2/subfolder22/page1.html";
238         storeResources(cms, folder);
239         
240         // lock folder
241
cms.lockResource(folder);
242
243         // the source folder must have an exclusive lock
244
assertLock(cms, folder, CmsLock.TYPE_EXCLUSIVE);
245         
246         // all resources in the folder must have an inherited lock
247
List JavaDoc resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
248         Iterator JavaDoc i = resources.iterator();
249         while (i.hasNext()) {
250             CmsResource res = (CmsResource)i.next();
251             assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED);
252         }
253         
254         // all siblings inside the folder must have an inherited lock
255
assertLock(cms, sibling1, CmsLock.TYPE_INHERITED);
256         // all siblings outside the folder must not locked
257
assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
258         
259         // now unlock the folder
260
cms.unlockResource(folder);
261         
262         // the source folder must be unlocked
263
assertLock(cms, folder, CmsLock.TYPE_UNLOCKED);
264         
265         // all resources in the folder must be ulocked
266
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
267         i = resources.iterator();
268         while (i.hasNext()) {
269             CmsResource res = (CmsResource)i.next();
270             assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED);
271         }
272         
273         // all siblings outside of the folder must not locked
274
assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
275         assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
276         
277     }
278     
279     /**
280      * Tests lock status of a folder and its siblings.<p>
281      *
282      * In this test, the folder has some prelocked siblings with shared locks in it
283      *
284      * @throws Throwable if something goes wrong
285      */

286     public void testLockForFolderPrelockedShared() throws Throwable JavaDoc {
287         
288         CmsObject cms = getCmsObject();
289         echo("Testing locking of folders with shared prelocks");
290         
291         String JavaDoc folder = "/folder1/subfolder12/";
292         String JavaDoc source = "/folder1/subfolder11/page1.html";
293         String JavaDoc sibling1 = "/folder1/subfolder12/page1.html";
294         String JavaDoc sibling2 = "/folder2/subfolder22/page1.html";
295         storeResources(cms, folder);
296         
297    
298         // lock source
299
cms.lockResource(source);
300
301         // the source file must have an exclusive lock
302
// all siblings must have shared exclusive locks
303
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
304         assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
305         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
306         
307         // lock folder
308
cms.lockResource(folder);
309         
310         // all resources in the folder must have an inherited or shared inherited lock
311
List JavaDoc resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
312         Iterator JavaDoc i = resources.iterator();
313         while (i.hasNext()) {
314             CmsResource res = (CmsResource)i.next();
315             // the sibling to the resource "source" must have an shared inherited lock
316
// all other resources must have a inherited lock
317
if (cms.getSitePath(res).equals(sibling1)) {
318                 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_SHARED_INHERITED);
319             } else {
320                 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED);
321             }
322         }
323
324         // The siblings outside the folder keppt their previous lockstate
325
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
326         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
327         
328         // now unlock the folder
329
cms.unlockResource(folder);
330         
331         // the source folder must be unlocked
332
assertLock(cms, folder, CmsLock.TYPE_UNLOCKED);
333         
334         // all resources in the folder must be unlocked, except those siblings that had a
335
// shared inherited lock, they must get a shared exclusive lock
336
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
337         i = resources.iterator();
338         while (i.hasNext()) {
339             CmsResource res = (CmsResource)i.next();
340             if (cms.getSitePath(res).equals(sibling1)) {
341                 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_SHARED_EXCLUSIVE);
342             } else {
343                 assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED);
344             }
345         }
346         
347         // The siblings outside the folder keppt their previous lockstate
348
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
349         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
350        
351         // now unlock the source
352
cms.unlockResource(source);
353         
354         // the source file and all sibling are unlocked now
355
assertLock(cms, source, CmsLock.TYPE_UNLOCKED);
356         assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
357         assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
358         
359     }
360     
361     /**
362      * Tests lock status of a folder and its siblings.<p>
363      *
364      * In this test, the folder has some prelocked siblings with exclusive locks in it
365      *
366      * @throws Throwable if something goes wrong
367      */

368     public void testLockForFolderPrelockedExclusive() throws Throwable JavaDoc {
369         
370         CmsObject cms = getCmsObject();
371         echo("Testing locking of folders with exclusive prelocks");
372         
373         String JavaDoc folder = "/folder1/subfolder12/";
374         String JavaDoc source = "/folder1/subfolder12/page1.html";
375         String JavaDoc sibling1 = "/folder1/subfolder11/page1.html";
376         String JavaDoc sibling2 = "/folder2/subfolder22/page1.html";
377         storeResources(cms, folder);
378         
379    
380         // lock source
381
cms.lockResource(source);
382
383         // the source file must have an exclusive lock
384
// all siblings must have shared exclusive locks
385
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
386         assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
387         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
388         
389         // lock folder
390
cms.lockResource(folder);
391         
392         // all resources in the folder must have an inherited lock
393
List JavaDoc resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
394         Iterator JavaDoc i = resources.iterator();
395         while (i.hasNext()) {
396             CmsResource res = (CmsResource)i.next();
397             assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED);
398         }
399
400         // The siblings outside the folder are unlocked
401
assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
402         assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
403        
404        
405         // now unlock the folder
406
cms.unlockResource(folder);
407         
408         // the source folder must be unlocked
409
assertLock(cms, folder, CmsLock.TYPE_UNLOCKED);
410         
411         // all resources in the folder must be unlocked
412
resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
413         i = resources.iterator();
414         while (i.hasNext()) {
415             CmsResource res = (CmsResource)i.next();
416             assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_UNLOCKED);
417         }
418         
419         // The siblings outside the folder keppt their previous lockstate
420
assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
421         assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
422
423     }
424  
425     /**
426      * Tests to steal a lock.<p>
427      *
428      * @throws Throwable if something goes wrong
429      */

430     public void testLockSteal() throws Throwable JavaDoc {
431         
432         CmsObject cms = getCmsObject();
433         echo("Testing stealing a lock");
434         
435         String JavaDoc source = "/folder1/subfolder11/page1.html";
436         String JavaDoc sibling1 = "/folder1/subfolder12/page1.html";
437         String JavaDoc sibling2 = "/folder2/subfolder22/page1.html";
438         storeResources(cms, source);
439         
440         // get the offline project
441
CmsProject offlineProject = cms.readProject("Offline");
442         
443         // login as user "test1"
444
cms.loginUser("test1" , "test1");
445         cms.getRequestContext().setCurrentProject(offlineProject);
446        
447         // lock source
448
cms.lockResource(source);
449
450         // the source file must have an exclusive lock
451
// all siblings must have shared locks
452
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
453         assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
454         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
455  
456         // login as user "admin"
457
cms.loginUser("Admin" , "admin");
458         cms.getRequestContext().setCurrentProject(offlineProject);
459         
460         // steal lock from first sibling
461
cms.changeLock(sibling1);
462         
463         // the sibling1 file must have an exclusive lock
464
// all siblings of it must have shared locks
465
assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE);
466         assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
467         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
468         
469         // now revoke write permissions for user "test2"
470
cms.chacc(source, I_CmsPrincipal.PRINCIPAL_USER, "test2", 0, CmsPermissionSet.PERMISSION_WRITE, CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE + CmsAccessControlEntry.ACCESS_FLAGS_INHERIT);
471
472         // switch to user "test2"
473
cms.loginUser("test2" , "test2");
474         cms.getRequestContext().setCurrentProject(offlineProject);
475                 
476         Exception JavaDoc error = null;
477         try {
478             // try to steal lock from the source
479
cms.changeLock(source);
480         } catch (CmsPermissionViolationException e) {
481             error = e;
482         }
483         assertNotNull(error);
484         try {
485             // try to steal lock from the first sibling
486
cms.changeLock(sibling1);
487         } catch (CmsPermissionViolationException e) {
488             error = e;
489         }
490         assertNotNull(error);
491         try {
492             // try to steal lock from the second sibling
493
cms.changeLock(sibling2);
494         } catch (CmsPermissionViolationException e) {
495             error = e;
496         }
497         assertNotNull(error);
498         
499         // login as user "Admin" again
500
cms.loginUser("Admin" , "admin");
501         cms.getRequestContext().setCurrentProject(offlineProject);
502         
503         // assert the locks are still there
504
assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE);
505         assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
506         assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
507         
508         // login as user "test1" again
509
cms.loginUser("test1" , "test1");
510         cms.getRequestContext().setCurrentProject(offlineProject);
511         
512         // steal lock from second sibling
513
cms.changeLock(sibling2);
514         
515         // assert the locks for siblings are there
516
assertLock(cms, sibling2, CmsLock.TYPE_EXCLUSIVE);
517         assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
518         assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
519     }
520     
521     /**
522      * Tests lock status of a resource during sibling creation.<p>
523      *
524      * @throws Throwable if something goes wrong
525      */

526     public void testLockForSiblings() throws Throwable JavaDoc {
527         
528         CmsObject cms = getCmsObject();
529         echo("Testing lock state after sibling creation");
530         
531         String JavaDoc source = "/folder2/index.html";
532         String JavaDoc destination1 = "/folder2/index_sib1.html";
533         String JavaDoc destination2 = "/folder2/index_sib2.html";
534         storeResources(cms, source);
535         
536         // copy source
537
cms.copyResource(source, destination1, CmsResource.COPY_AS_SIBLING);
538
539         // since source was not locked, destination must be locked exclusive
540
// and source must be locked shared
541
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
542         assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);
543         
544         // copy source again
545
cms.copyResource(source, destination2, CmsResource.COPY_AS_SIBLING);
546         
547         // since one sibling was already exclusive locked,
548
// new sibling must be shared locked
549
assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
550         assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);
551         assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE);
552         
553         // same stuff but in a different order
554
source = "/folder2/page1.html";
555         destination1 = "/folder2/page1_sib1.html";
556         destination2 = "/folder2/page1_sib2.html";
557         
558         // this time source is already locked
559
cms.lockResource(source);
560         cms.createSibling(source, destination1, null);
561         
562         // since source was locked, destination must be shared exclusive
563
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
564         assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE);
565         
566         // create another sibling
567
cms.createSibling(destination1, destination2, null);
568         // since one sibling was already exclusive locked,
569
// new sibling must be shared locked
570
assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
571         assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE);
572         assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE);
573     }
574     
575     /**
576      * Tests an inherited lock in a resource delete scenario.<p>
577      *
578      * @throws Throwable if something goes wrong
579      */

580     public void testLockInherit() throws Throwable JavaDoc {
581         
582         CmsObject cms = getCmsObject();
583         echo("Testing inherited lock delete scenario");
584         
585         String JavaDoc source = "/folder1/index.html";
586         String JavaDoc folder = "/folder1/";
587         storeResources(cms, source);
588
589         // first delete the resource
590
cms.lockResource(source);
591         cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
592        
593         // now lock the folder
594
cms.lockResource(folder);
595         
596         // make sure the deleted file has an inherited lock
597
assertLock(cms, source, CmsLock.TYPE_INHERITED);
598     }
599         
600     /**
601      * Ensures that a lock is required for all write/control operations.<p>
602      *
603      * @throws Throwable if something goes wrong
604      */

605     public void testLockRequired() throws Throwable JavaDoc {
606
607         CmsObject cms = getCmsObject();
608         echo("Testing if a lock is required for write/control operations");
609         
610         String JavaDoc source = "/index.html";
611         storeResources(cms, source);
612         long timestamp = System.currentTimeMillis();
613         
614         // make sure source is not locked
615
assertLock(cms, source, CmsLock.TYPE_UNLOCKED);
616         
617         CmsFile file = cms.readFile(source);
618                 
619         boolean needLock;
620         
621         needLock = false;
622         try {
623             cms.setDateLastModified(source, timestamp, false);
624         } catch (CmsLockException e) {
625             // must throw a security exception because resource is not locked
626
needLock = true;
627         }
628         if (! needLock) {
629             fail("Touch operation on resource permitted without a lock on the current user!");
630         }
631         
632         needLock = false;
633         try {
634             cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
635         } catch (CmsLockException e) {
636             // must throw a security exception because resource is not locked
637
needLock = true;
638         }
639         if (! needLock) {
640             fail("Delete operation on resource permitted without a lock on the current user!");
641         }
642
643         needLock = false;
644         try {
645             cms.writeFile(file);
646         } catch (CmsLockException e) {
647             // must throw a security exception because resource is not locked
648
needLock = true;
649         }
650         if (! needLock) {
651             fail("Write operation on resource permitted without a lock on the current user!");
652         }
653
654         needLock = false;
655         try {
656             cms.moveResource(source, "index_dest.html");
657         } catch (CmsLockException e) {
658             // must throw a security exception because resource is not locked
659
needLock = true;
660         }
661         if (! needLock) {
662             fail("Move operation on resource permitted without a lock on the current user!");
663         }
664
665         needLock = false;
666         try {
667             cms.writePropertyObject(source, new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title", null));
668         } catch (CmsLockException e) {
669             // must throw a security exception because resource is not locked
670
needLock = true;
671         }
672         if (! needLock) {
673             fail("Write property operation on resource permitted without a lock on the current user!");
674         }
675         
676         needLock = false;
677         try {
678             List JavaDoc properties = new ArrayList JavaDoc();
679             properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title 2", null));
680             cms.writePropertyObjects(source, properties);
681         } catch (CmsLockException e) {
682             // must throw a security exception because resource is not locked
683
needLock = true;
684         }
685         if (! needLock) {
686             fail("Write property list operation on resource permitted without a lock on the current user!");
687         }
688
689         needLock = false;
690         try {
691             cms.chflags(source, 1234);
692         } catch (CmsLockException e) {
693             // must throw a security exception because resource is not locked
694
needLock = true;
695         }
696         if (! needLock) {
697             fail("Change flags operation on resource permitted without a lock on the current user!");
698         }
699
700         needLock = false;
701         try {
702             cms.chtype(source, CmsResourceTypePlain.getStaticTypeId());
703         } catch (CmsLockException e) {
704             // must throw a security exception because resource is not locked
705
needLock = true;
706         }
707         if (! needLock) {
708             fail("Change type operation on resource permitted without a lock on the current user!");
709         }
710
711         needLock = false;
712         try {
713             cms.replaceResource(source, CmsResourceTypePlain.getStaticTypeId(), "Kaputt".getBytes(), null);
714         } catch (CmsLockException e) {
715             // must throw a security exception because resource is not locked
716
needLock = true;
717         }
718         if (! needLock) {
719             fail("Replace operation on resource permitted without a lock on the current user!");
720         }
721
722         needLock = false;
723         try {
724             cms.changeLastModifiedProjectId(source);
725         } catch (CmsLockException e) {
726             // must throw a security exception because resource is not locked
727
needLock = true;
728         }
729         if (! needLock) {
730             fail("Change last modified in project operation on resource permitted without a lock on the current user!");
731         }
732
733         needLock = false;
734         try {
735             CmsPermissionSet permissions = new CmsPermissionSet(CmsPermissionSet.PERMISSION_WRITE, CmsPermissionSet.PERMISSION_READ);
736             cms.chacc(source, I_CmsPrincipal.PRINCIPAL_GROUP, OpenCms.getDefaultUsers().getGroupAdministrators(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE);
737         } catch (CmsLockException e) {
738             // must throw a security exception because resource is not locked
739
needLock = true;
740         }
741         if (! needLock) {
742             fail("Change permissions operation on resource permitted without a lock on the current user!");
743         }
744         
745         needLock = false;
746         try {
747             cms.undeleteResource(source);
748         } catch (CmsLockException e) {
749             // must throw a security exception because resource is not locked
750
needLock = true;
751         }
752         if (! needLock) {
753             fail("Unlock operation on resource permitted without a lock on the current user!");
754         }
755         
756         // make sure original resource is unchanged
757
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
758         
759         // now perform a delete operation with lock
760
cms.lockResource(source);
761         cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
762         
763         // now undelete the resource
764
cms.lockResource(source);
765         cms.undeleteResource(source);
766         cms.unlockResource(source);
767         
768         // make sure original resource is still unchanged
769
assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
770     }
771 }
772
773
Popular Tags