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

📄 testcreatewriteresource.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestCreateWriteResource.java,v $
 * Date   : $Date: 2007-09-10 11:48:20 $
 * Version: $Revision: 1.23 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Management System
 *
 * Copyright (c) 2002 - 2007 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.file;

import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.file.types.CmsResourceTypeXmlPage;
import org.opencms.lock.CmsLockException;
import org.opencms.main.CmsIllegalArgumentException;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsPermissionViolationException;
import org.opencms.security.I_CmsPrincipal;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceConfigurableFilter;
import org.opencms.test.OpenCmsTestResourceFilter;
import org.opencms.util.CmsUUID;

import java.util.ArrayList;
import java.util.List;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * Unit tests for the create and import methods.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.23 $
 */
public class TestCreateWriteResource extends OpenCmsTestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestCreateWriteResource(String arg0) {

        super(arg0);
    }

    /**
     * Test suite for this test class.<p>
     * 
     * @return the test suite
     */
    public static Test suite() {

        OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);

        TestSuite suite = new TestSuite();
        suite.setName(TestCreateWriteResource.class.getName());

        suite.addTest(new TestCreateWriteResource("testCreateResourceLockedFolder"));
        suite.addTest(new TestCreateWriteResource("testImportResource"));
        suite.addTest(new TestCreateWriteResource("testImportResourceAgain"));
        suite.addTest(new TestCreateWriteResource("testImportSibling"));
        suite.addTest(new TestCreateWriteResource("testImportFolder"));
        suite.addTest(new TestCreateWriteResource("testImportFolderAgain"));
        suite.addTest(new TestCreateWriteResource("testCreateResource"));
        suite.addTest(new TestCreateWriteResource("testCreateResourceAgain"));
        suite.addTest(new TestCreateWriteResource("testCreateFolder"));
        suite.addTest(new TestCreateWriteResource("testCreateFolderAgain"));
        suite.addTest(new TestCreateWriteResource("testCreateDotnameResources"));
        suite.addTest(new TestCreateWriteResource("testOverwriteInvisibleResource"));
        suite.addTest(new TestCreateWriteResource("testCreateResourceWithSpecialChars"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

                setupOpenCms("simpletest", "/sites/default/");
            }

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Test creation of invalid resources that have only dots in their name.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateDotnameResources() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing creating a resource with only dots in the name");

        Exception error = null;
        try {
            cms.createResource("/folder1/.", CmsResourceTypeFolder.getStaticTypeId(), null, null);
        } catch (CmsIllegalArgumentException e) {
            assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
            error = e;
        }
        assertNotNull(error);
        error = null;
        try {
            cms.createResource("/folder1/..", CmsResourceTypePlain.getStaticTypeId(), null, null);
        } catch (CmsIllegalArgumentException e) {
            assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
            error = e;
        }
        assertNotNull(error);
        error = null;
        try {
            cms.createResource("/folder1/.../", CmsResourceTypeFolder.getStaticTypeId(), null, null);
        } catch (CmsIllegalArgumentException e) {
            assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
            error = e;
        }
        assertNotNull(error);
        error = null;
        try {
            cms.createResource("/folder1/....", CmsResourceTypePlain.getStaticTypeId(), null, null);
        } catch (CmsIllegalArgumentException e) {
            assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
            error = e;
        }
        assertNotNull(error);
        error = null;
        try {
            cms.createResource("/folder1/...../", CmsResourceTypeFolder.getStaticTypeId(), null, null);
        } catch (CmsIllegalArgumentException e) {
            assertEquals(org.opencms.db.Messages.ERR_CREATE_RESOURCE_1, e.getMessageContainer().getKey());
            error = e;
        }
        assertNotNull(error);
    }

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

        CmsObject cms = getCmsObject();
        echo("Testing creating a folder");

        String resourcename = "/folder1/test2/";
        long timestamp = System.currentTimeMillis() - 1;

        cms.createResource(resourcename, CmsResourceTypeFolder.getStaticTypeId(), null, null);

        // check the created folder
        CmsFolder folder = cms.readFolder(resourcename);

        assertEquals(folder.getState(), CmsResource.STATE_NEW);
        assertTrue(folder.getDateLastModified() > timestamp);
        assertTrue(folder.getDateCreated() > timestamp);

        // 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 
        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());

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

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

    /**
     * Test the create a folder again.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateFolderAgain() throws Throwable {

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

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

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

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

        // 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, CmsResourceTypeFolder.getStaticTypeId(), 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 "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
        assertDateCreated(cms, resourcename, original.getDateCreated());
        // the user created must be the original
        assertUserCreated(cms, resourcename, cms.readUser(original.getUserCreated()));

        // compare id's
        CmsResource created = cms.readResource(resourcename);
        if (!created.getResourceId().equals(original.getResourceId())) {
            fail("A created folder that replaced a deleted folder must 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 the create resource method.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testCreateResource() throws Throwable {

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

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

        String contentStr = "Hello this is my other 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();

⌨️ 快捷键说明

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