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

📄 testlock.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
            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 {
            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, true);
        } 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.undoChanges(source, CmsResource.UNDO_CONTENT_RECURSIVE);
        cms.unlockResource(source);

        // make sure original resource is still unchanged
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES_ALL);
    }

    /**
     * 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, CmsLockType.EXCLUSIVE);
        assertLock(cms, sibling1, CmsLockType.SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLockType.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, CmsLockType.EXCLUSIVE);
        assertLock(cms, source, CmsLockType.SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLockType.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, CmsLockType.EXCLUSIVE);
        assertLock(cms, source, CmsLockType.SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLockType.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, CmsLockType.EXCLUSIVE);
        assertLock(cms, source, CmsLockType.SHARED_EXCLUSIVE);
        assertLock(cms, sibling1, CmsLockType.SHARED_EXCLUSIVE);
    }

    /**
     * Tests lock status after a new file has been deleted in offline project.<p>
     * 
     * Issue description:
     * User A creates a new file, but deletes it without ever publishing it.
     * Now user B create a new file with the same name / path.
     * The file was still in the lock manager but for user A, this generated 
     * an error for user B.<p>
     * 
     * Solution:
     * Remove new files that are deleted from the lock manager.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockWithDeletedNewFiles() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing lock status of a deleted new file");

        String source = "/folder1/newfile.html";

        // create a new resource as default test user
        cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
        // the source file must now have an exclusive lock
        assertLock(cms, source, CmsLockType.EXCLUSIVE);
        // now delete the created resource
        cms.deleteResource(source, CmsResource.DELETE_REMOVE_SIBLINGS);

        // now login as user "test2"
        cms.loginUser("test2", "test2");
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        // now create the resource again
        cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());

        // the newly created resource must now be locked to user "test2"
        assertLock(cms, source, CmsLockType.EXCLUSIVE);
    }

    /**
     * Tests creating a new temporary file in a folder locked by another user.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testTempFileCreationInLockedFolder() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing creating a new file in a folder locked by another user");

        String fileName = "/folder2/~creationtest.html";

        // NOTE: folder still locked by test1 from previous test case

        cms.loginUser("test2", "test2");
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        try {
            cms.createResource(fileName, CmsResourceTypePlain.getStaticTypeId());
            fail("it is not allowed to create a resource in a folder locked by other user");
        } catch (CmsLockException e) {
            // ok, ignore
        }

        // it should be allowed for the root admin
        cms = getCmsObject();
        cms.createResource(fileName, CmsResourceTypePlain.getStaticTypeId());
        assertLock(cms, fileName, CmsLockType.INHERITED, cms.readUser("test1"));
    }
}

⌨️ 快捷键说明

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