⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testlock.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        
        // lock folder
        cms.lockResource(folder);
        
        // all resources in the folder must have an inherited lock        
        List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
        Iterator i = resources.iterator();
        while (i.hasNext()) {
            CmsResource res = (CmsResource)i.next();
            assertLock(cms, cms.getSitePath(res), CmsLock.TYPE_INHERITED);
        }

        // The siblings outside the folder are unlocked
        assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
        assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);
       
       
        // now unlock the folder
        cms.unlockResource(folder);
        
        // the source folder must be unlocked
        assertLock(cms, folder, CmsLock.TYPE_UNLOCKED);
        
        // all resources in the folder must be unlocked
        resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
        i = resources.iterator();
        while (i.hasNext()) {
            CmsResource res = (CmsResource)i.next();
            assertLock(cms, cms.getSitePath(res),  CmsLock.TYPE_UNLOCKED);
        }
        
        // The siblings outside the folder keppt their previous lockstate
        assertLock(cms, sibling1, CmsLock.TYPE_UNLOCKED);
        assertLock(cms, sibling2, CmsLock.TYPE_UNLOCKED);

    }    
 
    /**
     * Tests to steal a lock.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockSteal() throws Throwable {
        
        CmsObject cms = getCmsObject();     
        echo("Testing stealing a lock");
        
        String source = "/folder1/subfolder11/page1.html";
        String sibling1 = "/folder1/subfolder12/page1.html";
        String sibling2 = "/folder2/subfolder22/page1.html";
        storeResources(cms, source);
        
        // get the offline project
        CmsProject offlineProject = cms.readProject("Offline");
        
        // login as user "test1"
        cms.loginUser("test1" , "test1");
        cms.getRequestContext().setCurrentProject(offlineProject);
       
        // lock source
        cms.lockResource(source);

        // the source file must have an exclusive lock
        // all siblings must have shared locks
        assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
 
        // login as user "admin"
        cms.loginUser("Admin" , "admin");
        cms.getRequestContext().setCurrentProject(offlineProject);
        
        // steal lock from first sibling
        cms.changeLock(sibling1);
        
        // the sibling1 file must have an exclusive lock
        // all siblings of it must have shared locks
        assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
        
        // now revoke write permissions for user "test2"
        cms.chacc(source, I_CmsPrincipal.PRINCIPAL_USER, "test2", 0, CmsPermissionSet.PERMISSION_WRITE, CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE + CmsAccessControlEntry.ACCESS_FLAGS_INHERIT);

        // switch to user "test2"
        cms.loginUser("test2" , "test2");
        cms.getRequestContext().setCurrentProject(offlineProject);
                
        Exception error = null;
        try {
            // try to steal lock from the source
            cms.changeLock(source);
        } catch (CmsPermissionViolationException e) {
            error = e;
        }
        assertNotNull(error);
        try {
            // try to steal lock from the first sibling
            cms.changeLock(sibling1);
        } catch (CmsPermissionViolationException e) {
            error = e;
        }
        assertNotNull(error);
        try {
            // try to steal lock from the second sibling
            cms.changeLock(sibling2);
        } catch (CmsPermissionViolationException e) {
            error = e;
        }
        assertNotNull(error);        
        
        // login as user "Admin" again
        cms.loginUser("Admin" , "admin");
        cms.getRequestContext().setCurrentProject(offlineProject);
        
        // assert the locks are still there
        assertLock(cms, sibling1, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLock.TYPE_SHARED_EXCLUSIVE);
        
        // login as user "test1" again
        cms.loginUser("test1" , "test1");
        cms.getRequestContext().setCurrentProject(offlineProject);
        
        // steal lock from second sibling
        cms.changeLock(sibling2);
        
        // assert the locks for siblings are there
        assertLock(cms, sibling2, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, sibling1, CmsLock.TYPE_SHARED_EXCLUSIVE);
    } 
    
    /**
     * Tests lock status of a resource during sibling creation.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockForSiblings() throws Throwable {
        
        CmsObject cms = getCmsObject();     
        echo("Testing lock state after sibling creation");
        
        String source = "/folder2/index.html";
        String destination1 = "/folder2/index_sib1.html";
        String destination2 = "/folder2/index_sib2.html";
        storeResources(cms, source);
        
        // copy source
        cms.copyResource(source, destination1, CmsResource.COPY_AS_SIBLING);

        // since source was not locked, destination must be locked exclusive
        // and source must be locked shared
        assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);
        
        // copy source again
        cms.copyResource(source, destination2, CmsResource.COPY_AS_SIBLING);  
        
        // since one sibling was already exclusive locked, 
        // new sibling must be shared locked
        assertLock(cms, source, CmsLock.TYPE_SHARED_EXCLUSIVE);
        assertLock(cms, destination1, CmsLock.TYPE_EXCLUSIVE);        
        assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE);
        
        // same stuff but in a different order 
        source = "/folder2/page1.html";
        destination1 = "/folder2/page1_sib1.html";
        destination2 = "/folder2/page1_sib2.html";
        
        // this time source is already locked
        cms.lockResource(source);
        cms.createSibling(source, destination1, null);
        
        // since source was locked, destination must be shared exclusive
        assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE);    
        
        // create another sibling
        cms.createSibling(destination1, destination2, null);    
        // since one sibling was already exclusive locked, 
        // new sibling must be shared locked
        assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);
        assertLock(cms, destination1, CmsLock.TYPE_SHARED_EXCLUSIVE);        
        assertLock(cms, destination2, CmsLock.TYPE_SHARED_EXCLUSIVE);    
    }    
    
    /**
     * Tests an inherited lock in a resource delete scenario.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockInherit() throws Throwable {
        
        CmsObject cms = getCmsObject();     
        echo("Testing inherited lock delete scenario");
        
        String source = "/folder1/index.html";
        String folder = "/folder1/";
        storeResources(cms, source);        

        // first delete the resource
        cms.lockResource(source);
        cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
       
        // now lock the folder
        cms.lockResource(folder);
        
        // make sure the deleted file has an inherited lock
        assertLock(cms, source, CmsLock.TYPE_INHERITED);
    }
        
    /**
     * Ensures that a lock is required for all write/control operations.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockRequired() throws Throwable {

        CmsObject cms = getCmsObject();     
        echo("Testing if a lock is required for write/control operations");
        
        String source = "/index.html";
        storeResources(cms, source);        
        long timestamp = System.currentTimeMillis();
        
        // make sure source is not locked
        assertLock(cms, source, CmsLock.TYPE_UNLOCKED);                
        
        CmsFile file = cms.readFile(source);
                
        boolean needLock;
        
        needLock = false;
        try {
            cms.setDateLastModified(source, timestamp, false);            
        } catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        }        
        if (! needLock) {
            fail("Touch operation on resource permitted without a lock on the current user!");
        }         
        
        needLock = false;
        try {
            cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
        } catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        }        
        if (! needLock) {
            fail("Delete operation on resource permitted without a lock on the current user!");
        } 

        needLock = false;
        try {
            cms.writeFile(file);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Write operation on resource permitted without a lock on the current user!");
        }   

        needLock = false;
        try {
            cms.moveResource(source, "index_dest.html");
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Move operation on resource permitted without a lock on the current user!");
        } 

        needLock = false;
        try {
            cms.writePropertyObject(source, new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title", null));
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Write property operation on resource permitted without a lock on the current user!");
        }       
        
        needLock = false;
        try {
            List properties = new ArrayList();
            properties.add(new CmsProperty(CmsPropertyDefinition.PROPERTY_TITLE, "New title 2", null));
            cms.writePropertyObjects(source, properties);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Write property list operation on resource permitted without a lock on the current user!");
        }           

        needLock = false;
        try {
            cms.chflags(source, 1234);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Change flags operation on resource permitted without a lock on the current user!");
        }        

        needLock = false;
        try {
            cms.chtype(source, CmsResourceTypePlain.getStaticTypeId());
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Change type operation on resource permitted without a lock on the current user!");
        }  

        needLock = false;
        try {
            cms.replaceResource(source, CmsResourceTypePlain.getStaticTypeId(), "Kaputt".getBytes(), null);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Replace operation on resource permitted without a lock on the current user!");
        }     

        needLock = false;
        try {
            cms.changeLastModifiedProjectId(source);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Change last modified in project operation on resource permitted without a lock on the current user!");
        }           

        needLock = false;
        try {
            CmsPermissionSet permissions = new CmsPermissionSet(CmsPermissionSet.PERMISSION_WRITE, CmsPermissionSet.PERMISSION_READ);
            cms.chacc(source, I_CmsPrincipal.PRINCIPAL_GROUP, OpenCms.getDefaultUsers().getGroupAdministrators(), permissions.getAllowedPermissions(), permissions.getDeniedPermissions(), CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Change permissions operation on resource permitted without a lock on the current user!");
        }
        
        needLock = false;
        try {
            cms.undeleteResource(source);
        }  catch (CmsLockException e) {
            // must throw a security exception because resource is not locked
            needLock = true;
        } 
        if (! needLock) {
            fail("Unlock operation on resource permitted without a lock on the current user!");
        }  
        
        // make sure original resource is unchanged
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
        
        // now perform a delete operation with lock
        cms.lockResource(source);
        cms.deleteResource(source, CmsResource.DELETE_PRESERVE_SIBLINGS);
        
        // now undelete the resource
        cms.lockResource(source);        
        cms.undeleteResource(source);
        cms.unlockResource(source);
        
        // make sure original resource is still unchanged
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);        
    }      
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -