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

📄 testsiblings.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        int sources = cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size();
        TestLinkValidation.setContent(cms, sib1Name, "<img src='" + targetName + "'>");
        assertEquals(sources + 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources++;
        List links = cms.getRelationsForResource(sib1Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        CmsRelation relation = new CmsRelation(sib1, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));

        cms.createSibling(sib1Name, sib2Name, Collections.EMPTY_LIST);
        CmsResource sib2 = cms.readResource(sib2Name);
        assertEquals(sources + 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources++;
        links = cms.getRelationsForResource(sib2Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        relation = new CmsRelation(sib2, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));

        cms.createSibling(sib1Name, sib3Name, Collections.EMPTY_LIST);
        CmsResource sib3 = cms.readResource(sib3Name);
        assertEquals(sources + 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources++;
        links = cms.getRelationsForResource(sib3Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        relation = new CmsRelation(sib3, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));

        // remove the link
        TestLinkValidation.setContent(cms, sib1Name, "<h1>hello world!</h1>");
        assertEquals(sources - 3, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources -= 3;
        assertTrue(cms.getRelationsForResource(sib1Name, CmsRelationFilter.TARGETS).isEmpty());
        assertTrue(cms.getRelationsForResource(sib2Name, CmsRelationFilter.TARGETS).isEmpty());
        assertTrue(cms.getRelationsForResource(sib3Name, CmsRelationFilter.TARGETS).isEmpty());

        // add the link again
        TestLinkValidation.setContent(cms, sib1Name, "<img src='" + targetName + "'>");
        assertEquals(sources + 3, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources += 3;
        links = cms.getRelationsForResource(sib1Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        relation = new CmsRelation(sib1, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));
        links = cms.getRelationsForResource(sib2Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        relation = new CmsRelation(sib2, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));
        links = cms.getRelationsForResource(sib3Name, CmsRelationFilter.TARGETS);
        assertEquals(1, links.size());
        relation = new CmsRelation(sib3, target, CmsRelationType.EMBEDDED_IMAGE);
        assertRelation(relation, (CmsRelation)links.get(0));

        cms.deleteResource(sib3Name, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertEquals(sources - 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources--;
        cms.deleteResource(sib2Name, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertEquals(sources - 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources--;
        cms.deleteResource(sib1Name, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertEquals(sources - 1, cms.getRelationsForResource(targetName, CmsRelationFilter.SOURCES).size());
        sources--;
    }

    /**
     * Error scenario where an import is deleted that contains siblings inside the same folders.<p>
     * 
     * @throws Exception if something goes wrong
     */
    public void testSiblingIssueAfterImport() throws Exception {

        echo("Testing sibling issue after import");

        CmsResourceTranslator oldFolderTranslator = OpenCms.getResourceManager().getFolderTranslator();

        CmsResourceTranslator folderTranslator = new CmsResourceTranslator(new String[] {
            "s#^/sites/default/content/bodys(.*)#/system/bodies$1#",
            "s#^/sites/default/pics/system(.*)#/system/workplace/resources$1#",
            "s#^/sites/default/pics(.*)#/system/galleries/pics$1#",
            "s#^/sites/default/download(.*)#/system/galleries/download$1#",
            "s#^/sites/default/externallinks(.*)#/system/galleries/externallinks$1#",
            "s#^/sites/default/htmlgalleries(.*)#/system/galleries/htmlgalleries$1#",
            "s#^/sites/default/content(.*)#/system$1#"}, false);

        // set modified folder translator
        OpenCms.getResourceManager().setTranslators(folderTranslator, OpenCms.getResourceManager().getFileTranslator());

        try {

            CmsObject cms = getCmsObject();

            cms.getRequestContext().setSiteRoot("/");

            // need to create the "galleries" folder manually
            cms.createResource("/system/galleries", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
            cms.unlockResource("/system/galleries");

            cms.getRequestContext().setSiteRoot("/sites/default");

            // import the files
            String importFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf("packages/testimport01.zip");
            OpenCms.getImportExportManager().importData(
                cms,
                importFile,
                "/",
                new CmsShellReport(cms.getRequestContext().getLocale()));

            // clean up for the next test
            cms.getRequestContext().setSiteRoot("/");
            cms.lockResource("/sites/default");
            cms.lockResource("/system");

            // using the option "DELETE_REMOVE_SIBLINGS" caused an error here!
            cms.deleteResource("/sites/default/importtest", CmsResource.DELETE_REMOVE_SIBLINGS);
            cms.deleteResource("/system/galleries/pics", CmsResource.DELETE_REMOVE_SIBLINGS);

            cms.unlockResource("/sites/default");
            cms.unlockResource("/system");
            OpenCms.getPublishManager().publishProject(cms);
            OpenCms.getPublishManager().waitWhileRunning();
        } finally {

            // reset the translation rules
            OpenCms.getResourceManager().setTranslators(
                oldFolderTranslator,
                OpenCms.getResourceManager().getFileTranslator());

        }
    }

    /**
     * Tests the "copy as new sibling" function.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testSiblingsCopy() throws Throwable {

        CmsObject cms = getCmsObject();
        String source = "/index.html";
        String target = "/index_sibling.html";
        echo("Copying " + source + " as a new sibling to " + target);
        copyResourceAsSibling(this, cms, source, target);
    }

    /**
     * Does an "undo changes" from the online project on a resource with more than 1 sibling.<p>
     */
    /*
     public static void undoChangesWithSiblings(...) throws Throwable {
     // this test should do the following:
     // - create a sibling of a resource
     // - e.g. touch the black/unchanged sibling so that it gets red/changed
     // - make an "undo changes" -> the last-modified-in-project ID in the resource record 
     // of the resource must be the ID of the current project, and not 0
     // - this is to ensure that the new/changed/deleted other sibling still have a valid
     // state which consist of the last-modified-in-project ID plus the resource state
     // - otherwise this may result in grey flags

     Another issue:
     What happens if a user A has an exclusive lock on a resource X,
     and user B does a "copy as sibling Y" of X, or "create 
     new sibling Y" of X. The lock status of the resource X is exclusive
     to A, but test implies that it would be switched to B after operation!
     Maybe copy as / create new sibling must not be allowed if original is
     currently locked by another user? 

     }
     */

    /**
     * Tests the "project last modified" state with sibling operations across different projects.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testSiblingProjects() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing the 'project last modified' state of siblings across different projects");

        String source = "/folder1/image1.gif";
        String target = "/folder2/image1_sibling2.gif";
        createSibling(this, cms, source, target);

        OpenCms.getPublishManager().publishResource(cms, target, true, new CmsShellReport(Locale.ENGLISH));
        OpenCms.getPublishManager().waitWhileRunning();

        assertState(cms, source, CmsResourceState.STATE_UNCHANGED);
        assertState(cms, target, CmsResourceState.STATE_UNCHANGED);
        assertLock(cms, source, CmsLockType.UNLOCKED);
        assertLock(cms, target, CmsLockType.UNLOCKED);

        // create a new org unit, that will automatically create a new project...
        CmsOrganizationalUnit ou = OpenCms.getOrgUnitManager().createOrganizationalUnit(
            cms,
            "/test",
            "test",
            0,
            "/folder1");

        // this user will have publish permissions on the test ou project, but not on the root Offline project
        cms.createUser("/test/myuser", "myuser", "blah-blah", null);
        cms.addUserToGroup("/test/myuser", ou.getName() + OpenCms.getDefaultUsers().getGroupUsers());
        // the default permissions for the user are: +r+w+v+c since it is indirect user of the /Users group, 
        // so we need to explicitly remove the w flag, but we can not do that on the sibling itself, 
        // since the ACEs are bound to the resource entries, so we do it on the folder
        cms.lockResource("/folder2/");
        cms.chacc("/folder2/", I_CmsPrincipal.PRINCIPAL_USER, "/test/myuser", "-w");

        OpenCms.getPublishManager().publishResource(cms, "/folder2/");
        OpenCms.getPublishManager().waitWhileRunning();

        CmsProject prj = cms.readProject(ou.getProjectId());

        cms.loginUser("/test/myuser", "myuser");
        cms.getRequestContext().setCurrentProject(prj);

        // check the permissions
        assertTrue(cms.hasPermissions(
            cms.readResource(source, CmsResourceFilter.ALL),
            CmsPermissionSet.ACCESS_WRITE,
            false,
            CmsResourceFilter.ALL));
        assertFalse(cms.hasPermissions(
            cms.readResource(target, CmsResourceFilter.ALL),
            CmsPermissionSet.ACCESS_WRITE,
            false,
            CmsResourceFilter.ALL));

        // change a resource attribute
        cms.lockResource(source);
        cms.setDateLastModified(source, System.currentTimeMillis(), false);
        // check the project las modified
        assertProject(cms, source, prj);
        // the sibling has the same project last modified
        assertProject(cms, target, prj);

        // check the publish list
        CmsPublishList pl = OpenCms.getPublishManager().getPublishList(cms, cms.readResource(source), true);
        assertEquals(2, pl.size());
        assertTrue(pl.getFileList().contains(cms.readResource(source)));
        assertTrue(pl.getFileList().contains(cms.readResource("/folder1/image1_sibling.gif")));

        // publish
        OpenCms.getPublishManager().publishResource(cms, source, true, new CmsShellReport(Locale.ENGLISH));
        OpenCms.getPublishManager().waitWhileRunning();
        assertState(cms, source, CmsResourceState.STATE_UNCHANGED);
        // the sibling has been published, since we just changed a resource attribute, this is ok so
        assertState(cms, target, CmsResourceState.STATE_UNCHANGED);

        // now the same for an structure property
        cms.lockResource(source);
        cms.writePropertyObject(source, new CmsProperty(CmsPropertyDefinition.PROPERTY_DESCRIPTION, "desc", null));
        // check the project las modified
        assertProject(cms, source, prj);
        // the sibling has the same project last modified
        assertProject(cms, target, prj);
        // the sibling is still unchanged since no shared property has been changed
        assertState(cms, target, CmsResourceState.STATE_UNCHANGED);

        // check the publish list
        pl = OpenCms.getPublishManager().getPublishList(cms, cms.readResource(source), true);
        assertEquals(1, pl.size());
        assertTrue(pl.getFileList().contains(cms.readResource(source)));

        // publish
        OpenCms.getPublishManager().publishResource(cms, source, true, new CmsShellReport(Locale.ENGLISH));
        OpenCms.getPublishManager().waitWhileRunning();

⌨️ 快捷键说明

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