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

📄 testcreatewriteresource.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }

    /**
     * Test the create resource method for an already existing resource.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateResourceAgain() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing to create an existing resource again");

        String resourcename = "/folder1/test2.html";
        storeResources(cms, resourcename);
        long timestamp = System.currentTimeMillis();

        String contentStr = "Hello this is my NEW AND ALSO CHANGED other content";
        byte[] content = contentStr.getBytes();

        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
        cms.lockResource(resourcename);

        try {
            // resource exists and is not deleted, creation must throw exception
            cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);
        } catch (Throwable e) {
            if (!(e instanceof CmsVfsResourceAlreadyExistsException)) {
                fail("Existing resource '" + resourcename + "' was not detected!");
            }
        }

        // read resource for comparing id's later
        CmsResource original = cms.readResource(resourcename);

        // delete resource and try again
        cms.deleteResource(resourcename, CmsResource.DELETE_PRESERVE_SIBLINGS);
        cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);

        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "changed"
        assertState(cms, resourcename, CmsResource.STATE_CHANGED);
        // date last modified 
        assertDateLastModifiedAfter(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
        // date created
        assertDateCreatedAfter(cms, resourcename, timestamp);
        // the user created must be the current user
        assertUserCreated(cms, resourcename, cms.getRequestContext().currentUser());
        // check the content
        assertContent(cms, resourcename, content);

        // compare id's
        CmsResource created = cms.readResource(resourcename);
        if (created.getResourceId().equals(original.getResourceId())) {
            fail("A created resource that replaced a deleted resource must not have the same resource id!");
        }

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

        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }

    /**
     * Test resource creation in a locked folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateResourceLockedFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing resource creation in a locked folder");

        String foldername = "/folder1/";
        String resourcename = foldername + "newResLF.html";

        // lock the folder
        cms.lockResource(foldername);

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

        // try to create the file
        try {
            cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId());
            fail("should not be able to create a file in a locked folder.");
        } catch (CmsLockException e) {
            // ok, ignore
        }

        // now try as the user that owns the folder lock
        cms = getCmsObject();

        cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId());
    }

    /**
     * Test the create resource method.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateResourceWithSpecialChars() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing create resource with special character \"$\"");

        String resourcename = "/folder1/test$2.html";
        long timestamp = System.currentTimeMillis() - 1;

        String contentStr = "Hello this is my content";
        byte[] content = contentStr.getBytes();

        cms.createResource(resourcename, CmsResourceTypePlain.getStaticTypeId(), content, null);

        // ensure created resource type
        assertResourceType(cms, resourcename, CmsResourceTypePlain.getStaticTypeId());
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_NEW);
        // date last modified 
        assertDateLastModifiedAfter(cms, resourcename, timestamp);
        // date created
        assertDateCreatedAfter(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
        // check the content
        assertContent(cms, resourcename, content);

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

        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }

    /**
     * Test the import resource method with a folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing import resource for a folder");

        String resourcename = "/folder1/test1/";

        long timestamp = System.currentTimeMillis() - 87654321;

        // create a new resource
        CmsResource resource = new CmsResource(
            CmsUUID.getNullUUID(),
            CmsUUID.getNullUUID(),
            resourcename,
            CmsResourceTypeFolder.getStaticTypeId(),
            true,
            0,
            cms.getRequestContext().currentProject().getUuid(),
            CmsResource.STATE_NEW,
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            CmsResource.DATE_RELEASED_DEFAULT,
            CmsResource.DATE_EXPIRED_DEFAULT,
            1,
            -1,
            0,
            0);

        cms.importResource(resourcename, resource, null, null);

        // ensure created resource is a folder
        assertIsFolder(cms, resourcename);
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_NEW);
        // date last modified 
        assertDateLastModified(cms, resourcename, timestamp);
        // date created
        assertDateCreated(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());

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

        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }

    /**
     * Test the import resource method for an existing folder.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportFolderAgain() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing to import an existing folder again");

        String resourcename = "/folder1/test1/";

        storeResources(cms, resourcename);
        long timestamp = System.currentTimeMillis() - 12345678;

        CmsFolder folder = cms.readFolder(resourcename);

        // create a new folder
        CmsResource resource = new CmsResource(
            folder.getStructureId(),
            new CmsUUID(),
            resourcename,
            CmsResourceTypeFolder.getStaticTypeId(),
            true,
            0,
            cms.getRequestContext().currentProject().getUuid(),
            CmsResource.STATE_NEW,
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            CmsResource.DATE_RELEASED_DEFAULT,
            CmsResource.DATE_EXPIRED_DEFAULT,
            1,
            -1,
            0,
            0);

        cms.importResource(resourcename, resource, null, null);

        // ensure created resource is a folder
        assertIsFolder(cms, resourcename);
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_CHANGED);
        // date last modified 
        assertDateLastModified(cms, resourcename, timestamp);
        // the user last modified must be the current user
        assertUserLastModified(cms, resourcename, cms.getRequestContext().currentUser());
        // now evaluate the filter
        assertFilter(cms, resourcename, OpenCmsTestResourceFilter.FILTER_CREATE_RESOURCE);

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

        assertState(cms, resourcename, CmsResource.STATE_UNCHANGED);
    }

    /**
     * Test the import resource method.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testImportResource() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing import resource");

        String resourcename = "/folder1/test1.html";

        String contentStr = "Hello this is my content";
        byte[] content = contentStr.getBytes();
        long timestamp = System.currentTimeMillis() - 87654321;

        // create a new resource
        CmsResource resource = new CmsResource(
            CmsUUID.getNullUUID(),
            CmsUUID.getNullUUID(),
            resourcename,
            CmsResourceTypePlain.getStaticTypeId(),
            false,
            0,
            cms.getRequestContext().currentProject().getUuid(),
            CmsResource.STATE_NEW,
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            timestamp,
            cms.getRequestContext().currentUser().getId(),
            CmsResource.DATE_RELEASED_DEFAULT,
            CmsResource.DATE_EXPIRED_DEFAULT,
            1,
            content.length,
            0,
            0);

        cms.importResource(resourcename, resource, content, null);

        // ensure created resource type
        assertResourceType(cms, resourcename, CmsResourceTypePlain.getStaticTypeId());
        // project must be current project
        assertProject(cms, resourcename, cms.getRequestContext().currentProject());
        // state must be "new"
        assertState(cms, resourcename, CmsResource.STATE_NEW);
        // date last modified 
        assertDateLastModified(cms, resourcename, timestamp);

⌨️ 快捷键说明

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