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

📄 testmoverename.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    public void testMoveToDeletedFolder() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing to move a file into an as deleted marked folder");

        String deletedFolder = "/folder1/subfolder11/";
        String file = "index.html";

        cms.lockResource(deletedFolder);
        cms.deleteResource(deletedFolder, CmsResource.DELETE_PRESERVE_SIBLINGS);

        cms.lockResource(file);
        try {
            // moving a file to a deleted folder must cause an exception
            cms.moveResource(file, deletedFolder + "abc.html");
            fail("moving a file to a deleted folder must cause an exception");
        } catch (CmsVfsResourceNotFoundException e) {
            // ok
        }

        // restore the starting state
        cms.undoChanges(deletedFolder, CmsResource.UNDO_CONTENT_RECURSIVE);
        cms.unlockResource(deletedFolder);
        cms.unlockResource(file);
    }

    /**
     * Tests to move a resource with no write permission on the destination folder.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testMoveWithoutPermissions() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing to move a resource with no write permission on the destination folder");

        // Creating paths
        String folder = "/mytestfolder3/";
        String destinationFolder = "/folder1/";
        String file = "index3.html";

        // create the new files as test2
        cms.loginUser("test2", "test2");
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        cms.createResource(folder, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        cms.createResource(folder + file, CmsResourceTypePlain.getStaticTypeId());

        // login as Admin
        cms.loginUser("Admin", "admin");
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        // remove write permission for test2
        cms.lockResource(destinationFolder);
        cms.chacc(destinationFolder, I_CmsPrincipal.PRINCIPAL_USER, "test2", "+r-w+v+i");
        cms.unlockResource(destinationFolder);

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

        cms.lockResource(folder + file);
        // move the file
        try {
            cms.moveResource(folder + file, destinationFolder + file);
            fail("to move a resource with no write permission on the destination folder should fail");
        } catch (CmsPermissionViolationException e) {
            // ok
        }
    }

    /**
     * Tests a "multiple move" on a resource.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testMultipleMoveResource() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing multiple move of a resource");

        String source = "/folder1/page1.html";
        String destination1 = "/folder1/page1_move.html";
        String destination2 = "/page1_move.html";

        storeResources(cms, source);

        cms.lockResource(source);
        cms.moveResource(source, destination1);

        // check lock
        assertFalse(cms.getLockedResources("/folder1", CmsLockFilter.FILTER_ALL).contains(source));

        cms.moveResource(destination1, destination2);

        // check lock
        assertFalse(cms.getLockedResources("/folder1", CmsLockFilter.FILTER_ALL).contains(destination1));

        // source resource:
        try {
            // source resource must be gone
            cms.readResource(source, CmsResourceFilter.ALL);
            fail("source resource still there");
        } catch (CmsException e) {
            // ok
        }

        // destination1 resource:
        try {
            // source resource must be gone
            cms.readResource(destination1, CmsResourceFilter.ALL);
            fail("source resource still there");
        } catch (CmsException e) {
            // ok
        }

        // destination2 resource

        // project must be current project
        assertProject(cms, destination2, cms.getRequestContext().currentProject());
        // state must be "changed"
        assertState(cms, destination2, CmsResource.STATE_CHANGED);
        // assert lock state
        assertLock(cms, destination2, CmsLockType.EXCLUSIVE);
        // set filter mapping
        setMapping(destination2, source);
        // now assert the filter for the rest of the attributes        
        assertFilter(cms, destination2, OpenCmsTestResourceFilter.FILTER_MOVE_DESTINATION);

        // just for fun try to undo changes on the source resource
        resetMapping();
        cms.undoChanges(destination2, CmsResource.UNDO_MOVE_CONTENT);
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES_ALL);

        // check lock
        assertFalse(cms.getLockedResources("/", CmsLockFilter.FILTER_ALL).contains(destination2));
    }

    /**
     * Tests overwriting a moved resource.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testOverwriteMovedResource() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing overwritting a moved resource by other resource");

        String originalResource = "/xmlcontent/article_0001.html";
        String copySource = "/xmlcontent/article_0002.html";
        String intermediaryDestination = "/xmlcontent/article_0001_new.html";
        String finalDestination = "/xmlcontent/article_0001_new2.html";

        // move the resource 
        cms.lockResource(originalResource);

        cms.moveResource(originalResource, intermediaryDestination);

        // check lock
        assertFalse(cms.getLockedResources("/xmlcontent", CmsLockFilter.FILTER_ALL).contains(originalResource));

        cms.moveResource(intermediaryDestination, finalDestination);

        // check lock
        assertFalse(cms.getLockedResources("/xmlcontent", CmsLockFilter.FILTER_ALL).contains(intermediaryDestination));

        // try to overwrite by move
        try {
            cms.lockResource(copySource);
            cms.moveResource(copySource, originalResource);
            fail("creating a resource in the position of a moved resource that is not the original resource is not allowed.");
        } catch (CmsVfsOnlineResourceAlreadyExistsException e) {
            // ok
        }

        // try to overwrite by new
        try {
            cms.createResource(originalResource, CmsResourceTypePlain.getStaticTypeId());
            fail("creating a resource in the position of a moved resource that is not the original resource is not allowed.");
        } catch (CmsVfsOnlineResourceAlreadyExistsException e) {
            // ok
        }

        // try to overwrite by new the first target
        try {
            cms.createResource(intermediaryDestination, CmsResourceTypePlain.getStaticTypeId());
        } catch (CmsException e) {
            fail("creating a resource here must allowed.");
        }

        // try to overwrite by copying the new file
        try {
            cms.copyResource(intermediaryDestination, originalResource);
            fail("creating a resource in the position of a moved resource that is not the original resource is not allowed.");
        } catch (CmsVfsOnlineResourceAlreadyExistsException e) {
            // ok
        }

        // try to overwrite by the original file
        try {
            cms.moveResource(finalDestination, originalResource);
        } catch (CmsException e) {
            fail("creating a resource back to its original position is allowed.");
        }

        // check lock
        assertFalse(cms.getLockedResources("/xmlcontent", CmsLockFilter.FILTER_ALL).contains(finalDestination));
    }

    /**
     * Test the perma link.<p>
     * 
     * @throws Throwable if the test fails
     */
    public void testPermaLink() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing the perma links");

        String filename = "/folder1/page1.html";
        String filename2 = "/folder1/page1_moved.html";
        String uri;

        CmsResource res = cms.readResource(filename);
        uri = CmsPermalinkResourceHandler.PERMALINK_HANDLER + res.getStructureId() + ".html";
        cms.getRequestContext().setUri(uri);
        CmsResource res2 = new CmsPermalinkResourceHandler().initResource(null, cms, null, null);

        assertEquals(res.getStructureId(), res2.getStructureId());
        assertEquals(res.getResourceId(), res2.getResourceId());
        assertEquals(res.getRootPath(), res2.getRootPath());

        cms.lockResource(filename);
        cms.moveResource(filename, filename2);

        // check lock
        assertFalse(cms.getLockedResources("/folder1", CmsLockFilter.FILTER_ALL).contains(filename));

        cms.getRequestContext().setUri(uri);
        CmsResource res3 = new CmsPermalinkResourceHandler().initResource(null, cms, null, null);

        assertEquals(res.getStructureId(), res3.getStructureId());
        assertEquals(res.getResourceId(), res3.getResourceId());
        assertEquals(filename2, cms.getSitePath(res3));

        cms.undoChanges(filename2, CmsResource.UNDO_MOVE_CONTENT);
    }

    /**
     * Tests to publish a deleted folder with a unpublished moved resource.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testPublishDeletedFolderWithMovedResource() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing to publish a deleted folder with a unpublished moved resource");

        String deletedFolder = "/folder1/subfolder11/";
        String source = "index.html";
        String destination = "abc.html";

        cms.lockResource(deletedFolder + source);
        cms.moveResource(deletedFolder + source, destination);

        // check lock
        assertFalse(cms.getLockedResources(deletedFolder, CmsLockFilter.FILTER_ALL).contains(deletedFolder + source));

        cms.lockResource(deletedFolder);
        cms.deleteResource(deletedFolder, CmsResource.DELETE_PRESERVE_SIBLINGS);

        cms.unlockResource(deletedFolder);
        OpenCms.getPublishManager().publishResource(cms, deletedFolder);
        OpenCms.getPublishManager().waitWhileRunning();

        try {
            // undoing the changes to a deleted folder must cause an exception
            cms.undoChanges(destination, CmsResource.UNDO_MOVE_CONTENT);
            fail("undoing the changes to a deleted folder must cause an exception");
        } catch (CmsVfsResourceNotFoundException e) {
            // ok
        }

        cms.unlockResource(destination);
        OpenCms.getPublishManager().publishResource(cms, destination);
        OpenCms.getPublishManager().waitWhileRunning();
    }
}

⌨️ 快捷键说明

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