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

📄 testcopy.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        try {
            cms.copyResource(source, destination);
            fail("it should not be possible to copy a folder into itself");
        } catch (CmsVfsException e) {
            // ok
        }

        String newFolder = "/folder1bla/";
        cms.createResource(newFolder, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        try {
            cms.copyResource(source, newFolder + "test");
        } catch (Exception e) {
            fail("/folder1bla/ should not be considered subfolder of /folder1/");
        }
    }

    /**
     * Tests to copy a folder with a (from other user) locked sibling.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCopyFolderWithLockedSibling() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing coping a folder with a (from other user) locked sibling");

        cms.loginUser("test1", "test1");
        cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));

        String sourceFolder = "/folder1";
        String file = "/index.html";

        cms.lockResource(sourceFolder + file);

        // switch user
        CmsUser user = cms.getRequestContext().currentUser();
        cms = getCmsObject();

        assertLock(cms, sourceFolder + file, CmsLockType.EXCLUSIVE, user);

        String destinationFolder = "/folder1_copy";
        cms.copyResource(sourceFolder, destinationFolder, CmsResource.COPY_AS_SIBLING);
        assertLock(cms, sourceFolder + file, CmsLockType.EXCLUSIVE, user);
        assertLock(cms, destinationFolder + file, CmsLockType.SHARED_INHERITED, user);

        String destinationFolder2 = "/folder1_copy2";
        cms.copyResource(sourceFolder, destinationFolder2, CmsResource.COPY_PRESERVE_SIBLING);
        assertLock(cms, sourceFolder + file, CmsLockType.EXCLUSIVE, user);
        assertLock(cms, destinationFolder + file, CmsLockType.SHARED_INHERITED, user);
        assertLock(cms, destinationFolder2 + file, CmsLockType.SHARED_INHERITED, user);

        cms.unlockResource(destinationFolder);
        assertLock(cms, sourceFolder + file, CmsLockType.EXCLUSIVE, user);
        assertLock(cms, destinationFolder + file, CmsLockType.SHARED_EXCLUSIVE, user);
        assertLock(cms, destinationFolder2 + file, CmsLockType.SHARED_INHERITED, user);

        cms.unlockResource(destinationFolder2);
        assertLock(cms, sourceFolder + file, CmsLockType.EXCLUSIVE, user);
        assertLock(cms, destinationFolder + file, CmsLockType.SHARED_EXCLUSIVE, user);
        assertLock(cms, destinationFolder2 + file, CmsLockType.SHARED_EXCLUSIVE, user);
    }

    /**
     * Tests the to copy a single resource to a destination that already exists but is
     * marked as deleted.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCopyOverwriteDeletedFile() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing overwriting a deleted file");

        cms.loginUser("test1", "test1");
        cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));

        String source1 = "/folder1/page2.html";
        String source2 = "/folder1/image1.gif";
        String source3 = "/folder1/page3.html";
        String destination = "/folder1/page1.html";

        storeResources(cms, source1);
        storeResources(cms, source2);
        storeResources(cms, source3);
        storeResources(cms, destination);

        cms.lockResource(destination);

        // delete and overwrite with a sibling of source 1
        cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertState(cms, destination, CmsResource.STATE_DELETED);

        cms.copyResource(source1, destination, CmsResource.COPY_AS_SIBLING);

        assertState(cms, destination, CmsResource.STATE_CHANGED);
        assertSiblingCount(cms, destination, 2);
        assertLock(cms, destination, CmsLockType.EXCLUSIVE);

        assertSiblingCountIncremented(cms, source1, 1);
        assertLock(cms, source1, CmsLockType.SHARED_EXCLUSIVE);

        assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
        assertFilter(cms, source1, destination, OpenCmsTestResourceFilter.FILTER_COPY_SOURCE_DESTINATION_AS_SIBLING);

        // delete again and owerwrite with a sibling of source 2
        cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertState(cms, destination, CmsResource.STATE_DELETED);

        cms.copyResource(source2, destination, CmsResource.COPY_AS_SIBLING);

        assertSiblingCountIncremented(cms, source1, 0);
        assertLock(cms, source1, CmsLockType.UNLOCKED);

        assertState(cms, destination, CmsResource.STATE_CHANGED);
        assertSiblingCount(cms, destination, 2);
        assertLock(cms, destination, CmsLockType.EXCLUSIVE);

        assertSiblingCountIncremented(cms, source2, 1);
        assertLock(cms, source2, CmsLockType.SHARED_EXCLUSIVE);

        assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES_ALL);
        assertFilter(cms, source2, OpenCmsTestResourceFilter.FILTER_EXISTING_SIBLING);
        assertFilter(cms, source2, destination, OpenCmsTestResourceFilter.FILTER_COPY_SOURCE_DESTINATION_AS_SIBLING);

        cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertState(cms, destination, CmsResource.STATE_DELETED);

        // delete yet again and overwrite with content of source 3 (not a sibling)
        cms.copyResource(source3, destination, CmsResource.COPY_AS_NEW);

        assertSiblingCountIncremented(cms, source1, 0);
        assertLock(cms, source1, CmsLockType.UNLOCKED);
        assertSiblingCountIncremented(cms, source2, 0);
        assertLock(cms, source2, CmsLockType.UNLOCKED);

        assertState(cms, destination, CmsResource.STATE_CHANGED);
        assertSiblingCount(cms, destination, 1);
        assertSiblingCount(cms, source3, 1);
        assertLock(cms, destination, CmsLockType.EXCLUSIVE);

        assertFilter(cms, source1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES_ALL);
        assertFilter(cms, source2, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES_ALL);
        assertFilter(cms, source3, OpenCmsTestResourceFilter.FILTER_EQUAL);
        assertFilter(cms, source3, destination, OpenCmsTestResourceFilter.FILTER_COPY_FILE_AS_NEW);
    }

    /**
     * Tests the to copy a single resource to a destination that already exists but is
     * marked as deleted and locked by another user.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCopyOverwriteLockedDeletedFile() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing to create a file over a deleted one");

        // Creating paths
        String deletedFile = "/folder1/page1.html";
        String sourceFile = "/folder1/page2.html";

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

        cms.changeLock(deletedFile);
        cms.deleteResource(deletedFile, CmsResource.DELETE_PRESERVE_SIBLINGS);

        cms.loginUser("Admin", "admin");
        cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));
        try {
            cms.copyResource(sourceFile, deletedFile);
            fail("should fail to copy a file over a locked deleted one");
        } catch (Exception e) {
            // ok
        }
    }

    /**
     * Tests the "copy single resource as new" operation.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCopySingleResourceAsNew() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing copy of a file as new");

        // create a project for all tests in this suite
        cms.createProject("testproject", "a test project", "Users", "Users", CmsProject.PROJECT_TYPE_NORMAL);
        cms.copyResourceToProject("/");
        cms.addUserToGroup("test1", OpenCms.getDefaultUsers().getGroupAdministrators());

        cms.loginUser("test1", "test1");
        cms.getRequestContext().setCurrentProject(cms.readProject("testproject"));

        String source = "/index.html";
        String destination = "/index_copy.html";
        long timestamp = System.currentTimeMillis();

        CmsUser admin = cms.readUser("Admin");
        // some assertions about the original state of the resource
        assertUserCreated(cms, source, admin);
        assertUserLastModified(cms, source, admin);
        CmsResource original = cms.readResource(source);

        storeResources(cms, source);
        cms.copyResource(source, destination);

        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);

        // project must be current project
        assertProject(cms, destination, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, destination, CmsResource.STATE_NEW);
        // date created must be new
        assertDateCreatedAfter(cms, destination, timestamp);
        // date last modified must be original date
        assertDateLastModified(cms, destination, original.getDateLastModified());
        // user created must be current user
        assertUserCreated(cms, destination, cms.getRequestContext().currentUser());
        // user last modified must be original user
        assertUserLastModified(cms, destination, cms.readUser(original.getUserLastModified()));
        // now assert the filter for the rest of the attributes        
        setMapping(destination, source);
        assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_COPY_FILE_AS_NEW);
        // assert lock state
        assertLock(cms, destination, CmsLockType.EXCLUSIVE);
    }
}

⌨️ 快捷键说明

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