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

📄 testpublishing.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
        cms.getRequestContext().setCurrentProject(project);
        cms.createResource(res3, CmsResourceTypePlain.getStaticTypeId());

        // change to offline project, copy resource as sibling
        // this will also change the project in which the source was lastmodified (now "Offline")
        String res4 = "/testPublishProjectLastmodified.txt";
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
        cms.copyResource(res3, res4, CmsResource.COPY_AS_SIBLING);

        // check the current state of the resources
        assertState(cms, res3, CmsResource.STATE_NEW);
        assertState(cms, res4, CmsResource.STATE_NEW);

        // publish the test project
        cms.getRequestContext().setCurrentProject(project);
        cms.unlockProject(project.getUuid());
        OpenCms.getPublishManager().publishProject(cms);
        OpenCms.getPublishManager().waitWhileRunning();

        // the resource inside the offline project must not be published (not in test project)
        assertState(cms, res3, CmsResource.STATE_NEW);

        // the sibling outside the offline project must not be published (not in test project)
        assertState(cms, res4, CmsResource.STATE_NEW);

        // publish the root folder of the test project within the test project
        OpenCms.getPublishManager().publishResource(
            cms,
            path,
            true,
            new CmsShellReport(cms.getRequestContext().getLocale()));
        OpenCms.getPublishManager().waitWhileRunning();

        // all resources inside the folder must be published, even if not in test project
        assertState(cms, res3, CmsResource.STATE_NEW);

        // as well as all siblings, even if not in test project
        assertState(cms, res4, CmsResource.STATE_NEW);
    }

    /**
     * Tests publishing a folder containing resources modified within a distinct project.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishProjectLastmodifiedFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing publishing a folder containing resources modified within a distinct project");

        String path = "/folder1";
        String res1 = path + "/index.html";

        // change first resource in the offline project
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));
        cms.lockResource(res1);
        cms.setDateLastModified(res1, System.currentTimeMillis(), false);

        cms.unlockProject(cms.getRequestContext().currentProject().getUuid());
        storeResources(cms, res1);

        // create a new project
        CmsProject project = getTestProject(cms);
        cms.getRequestContext().setCurrentProject(project);
        cms.copyResourceToProject(path);

        // check the publish list
        // the resource modified in the Offline project should not be there
        CmsPublishList pubList = OpenCms.getPublishManager().getPublishList(cms, cms.readResource(path), false);
        assertTrue(pubList.isPublishSubResources());
        assertTrue(pubList.getAllResources().isEmpty());
    }

    /**
     * Test publishing of related files.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishRelatedFiles() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing publishing of related files");

        String resName = "index.html";

        // touch the file to publish        
        cms.lockResource(resName);
        cms.setDateLastModified(resName, System.currentTimeMillis(), false);
        CmsResource resource = cms.readResource(resName, CmsResourceFilter.DEFAULT);

        // get the publish list
        CmsPublishList pubList = OpenCms.getPublishManager().getPublishList(cms, resource, false);
        // just check the publish list
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        assertTrue(pubList.getFileList().contains(resource));

        // get the list of related resources, which should be still empty, since the related resource has unchanged state
        CmsPublishList relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, pubList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        assertTrue(pubList.getFileList().contains(resource));
        // check the related publish list
        assertTrue(relatedList.getAllResources().isEmpty());

        String relResName = "folder1/image2.gif";

        // touch the related resource
        cms.lockResource(relResName);
        cms.setDateLastModified(relResName, System.currentTimeMillis(), false);
        CmsResource relatedRes = cms.readResource(relResName, CmsResourceFilter.DEFAULT);

        // get the list of related resources again
        relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, pubList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        assertTrue(pubList.getFileList().contains(resource));
        // check the related publish list
        assertTrue(relatedList.getDeletedFolderList().isEmpty());
        assertTrue(relatedList.getFolderList().isEmpty());
        assertEquals(1, relatedList.getFileList().size());
        assertTrue(relatedList.getFileList().contains(relatedRes));

        CmsPublishList mergedList = OpenCms.getPublishManager().mergePublishLists(cms, pubList, relatedList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        assertTrue(pubList.getFileList().contains(resource));
        // check the related publish list has not been touched
        assertTrue(relatedList.getDeletedFolderList().isEmpty());
        assertTrue(relatedList.getFolderList().isEmpty());
        assertEquals(1, relatedList.getFileList().size());
        assertTrue(relatedList.getFileList().contains(relatedRes));
        // check the merged publish list
        assertTrue(mergedList.getDeletedFolderList().isEmpty());
        assertTrue(mergedList.getFolderList().isEmpty());
        assertEquals(2, mergedList.getFileList().size());
        assertTrue(mergedList.getFileList().contains(relatedRes));
        assertTrue(mergedList.getFileList().contains(resource));
    }

    /**
     * Test publishing of related files taken a whole folder structure.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishRelatedFilesInFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing publishing of related files taken a whole folder structure");

        String resName = "/folder1/";

        // touch the file to publish        
        cms.lockResource(resName);
        cms.setDateLastModified(resName, System.currentTimeMillis(), true);
        CmsResource resource = cms.readResource(resName, CmsResourceFilter.DEFAULT);

        // get the publish list
        CmsPublishList pubList = OpenCms.getPublishManager().getPublishList(cms, resource, false);
        // just check the publish list
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertFalse(pubList.getFolderList().isEmpty());
        assertFalse(pubList.getFileList().isEmpty());

        // get the list of related resources, which should be still empty, since the related resource has unchanged state
        CmsPublishList relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, pubList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertFalse(pubList.getFolderList().isEmpty());
        assertFalse(pubList.getFileList().isEmpty());
        // check the related publish list
        assertTrue(relatedList.getAllResources().isEmpty());

        CmsPublishList mergedList = OpenCms.getPublishManager().mergePublishLists(cms, pubList, relatedList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertFalse(pubList.getFolderList().isEmpty());
        assertFalse(pubList.getFileList().isEmpty());
        // check the related publish list
        assertTrue(relatedList.getAllResources().isEmpty());
        // check the merged publish list
        assertEquals(pubList.getDeletedFolderList(), mergedList.getDeletedFolderList());
        assertEquals(pubList.getFolderList(), mergedList.getFolderList());
        assertEquals(pubList.getFileList(), mergedList.getFileList());
    }

    /**
     * Test publishing of related files in an unpublished folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishRelatedFilesInNewFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing publishing of related files in an unpublished folder");

        String folderName = "/testFolder/";
        String fileName = "/testFolder/testFile.gif";
        String srcName = "sourceTest.html";

        // create folder and file        
        cms.createResource(folderName, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        cms.createResource(fileName, CmsResourceTypeImage.getStaticTypeId());

        // create source file
        String content = CmsXmlPageFactory.createDocument(Locale.ENGLISH, CmsEncoder.ENCODING_UTF_8);
        CmsResource source = cms.createResource(
            srcName,
            CmsResourceTypeXmlPage.getStaticTypeId(),
            content.getBytes(CmsEncoder.ENCODING_UTF_8),
            null);

        CmsFile file = cms.readFile(srcName);
        CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file);
        String element = "test";
        page.addValue(element, Locale.ENGLISH);

        // set the strong link
        content = "<img src='" + fileName + "'>";
        page.setStringValue(cms, element, Locale.ENGLISH, content);

        file.setContents(page.marshal());
        cms.writeFile(file);

        // get the publish list
        CmsPublishList pubList = OpenCms.getPublishManager().getPublishList(cms, source, false);
        // just check the publish list
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());

        // get the list of related resources
        CmsPublishList relatedList = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, pubList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        // check the related publish list
        assertTrue(relatedList.getDeletedFolderList().isEmpty());
        assertEquals(1, relatedList.getFolderList().size());
        assertEquals(1, relatedList.getFileList().size());

        CmsPublishList mergedList = OpenCms.getPublishManager().mergePublishLists(cms, pubList, relatedList);
        // check the publish list has not been touched
        assertTrue(pubList.getDeletedFolderList().isEmpty());
        assertTrue(pubList.getFolderList().isEmpty());
        assertEquals(1, pubList.getFileList().size());
        // check the related publish list
        assertTrue(relatedList.getDeletedFolderList().isEmpty());
        assertEquals(1, relatedList.getFolderList().size());
        assertEquals(1, relatedList.getFileList().size());
        // check the merged publish list
        assertTrue(mergedList.getDeletedFolderList().isEmpty());
        assertEquals(1, mergedList.getFolderList().size());
        assertEquals(2, mergedList.getFileList().size());
    }

    /**
     * Test publishing a replaced file.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishReplacedFile() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing publishing a replaced file");

        String resourcename = "testReplace.txt";
        byte[] orgCnt = "original content".getBytes();
        byte[] newCnt = "new content".getBytes();

        // create new file
        cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), orgCnt, null);
        // assert offline
        assertContent(cms, resourcename, orgCnt);
        // publish
        OpenCms.getPublishManager().publishResource(cms, resourcename);
        OpenCms.getPublishManager().waitWhileRunning();

        // assert online
        cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
        assertContent(cms, resourcename, orgCnt);
        cms = getCmsObject();

        // replace
        cms.lockResource(resourcename);
        cms.replaceResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), newCnt, null);
        // assert offline
        assertContent(cms, resourcename, newCnt);
        // publish again
        OpenCms.getPublishManager().publishResource(cms, resourcename);
        OpenCms.getPublishManager().waitWhileRunning();

        // assert online
        cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
        assertContent(cms, resourcename, newCnt);
    }

    /**
     * Test publishing a single resource with an iteration.<p>

⌨️ 快捷键说明

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