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

📄 testlock.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestLock.java,v $
 * Date   : $Date: 2007-09-11 12:41:00 $
 * Version: $Revision: 1.24 $
 *
 * 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.lock.CmsLockException;
import org.opencms.lock.CmsLockFilter;
import org.opencms.lock.CmsLockType;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsAccessControlEntry;
import org.opencms.security.CmsPermissionSet;
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.OpenCmsTestResourceFilter;

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

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

/**
 * Unit tests for lock operation.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.24 $
 */
public class TestLock extends OpenCmsTestCase {

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestLock(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(TestLock.class.getName());

        suite.addTest(new TestLock("testLockFilter"));
        suite.addTest(new TestLock("testLockWithDeletedNewFiles"));
        suite.addTest(new TestLock("testLockOtherUser"));
        suite.addTest(new TestLock("testLockForFile"));
        suite.addTest(new TestLock("testLockForFolder"));
        suite.addTest(new TestLock("testLockForFolderPrelockedShared"));
        suite.addTest(new TestLock("testLockForFolderPrelockedExclusive"));
        suite.addTest(new TestLock("testLockSteal"));
        suite.addTest(new TestLock("testLockRequired"));
        suite.addTest(new TestLock("testLockInherit"));
        suite.addTest(new TestLock("testLockForSiblings"));
        suite.addTest(new TestLock("testLockForBaseOperations"));
        suite.addTest(new TestLock("testCopyToLockedFolder"));
        suite.addTest(new TestLock("testCreationInLockedFolder"));
        suite.addTest(new TestLock("testTempFileCreationInLockedFolder"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

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

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Tests copying a file to a folder locked by another user.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCopyToLockedFolder() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing copying a file to a folder locked by another user");

        cms.loginUser("test1", "test1");
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        String source = "/index.html";
        String folder = "/folder2";
        String destination = "/copytest.html";

        cms.lockResource(folder);

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

        try {
            cms.copyResource(source, folder + destination);
            fail("it is not allowed to create a resource in a folder locked by other user");
        } catch (CmsLockException e) {
            // ok, ignore
        }
    }

    /**
     * Tests creating a new file in a folder locked by another user.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testCreationInLockedFolder() throws Exception {

        CmsObject cms = getCmsObject();
        echo("Testing creating a new file in a folder locked by another user");

        String fileName = "/folder2/creationtest.html";

        // NOTE: folder still locked by test1 from previous test case

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

        try {
            cms.createResource(fileName, CmsResourceTypePlain.getStaticTypeId());
            fail("it is not allowed to create a resource in a folder locked by other user");
        } catch (CmsLockException e) {
            // ok, ignore
        }
    }

    /**
     * Test the lock filter. see bug #1460.<p>
     * 
     * @throws Throwable is something goes wrong
     */
    public void testLockFilter() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing lock filter");

        String fileName = "testLockFilter";
        String folderName = "testLockFilterFolder";

        // create a file and a folder where the file name is a prefix of the folder name 
        cms.createResource(fileName, CmsResourceTypePlain.getStaticTypeId());
        cms.createResource(folderName, CmsResourceTypeFolder.RESOURCE_TYPE_ID);

        // publish only the folder, so the file remains locked
        OpenCms.getPublishManager().publishResource(cms, folderName);
        OpenCms.getPublishManager().waitWhileRunning();

        // check for lock in or on the folder
        List lockedRes = cms.getLockedResources(folderName, CmsLockFilter.FILTER_ALL);
        // should be empty
        assertTrue(lockedRes.isEmpty());
    }

    /**
     * Tests lock status of a resource for basic operations.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockForBaseOperations() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing lock state for basic operations");

        String source = "/types/text.txt";
        String destination1 = "/types/text_new1.txt";
        String destination2 = "/types/text_new2.txt";
        storeResources(cms, source);

        // copy source
        cms.copyResource(source, destination1, CmsResource.COPY_AS_NEW);

        // since source was not locked, destination must be locked exclusive
        // and source must still be unlocked
        assertLock(cms, source, CmsLockType.UNLOCKED);
        assertLock(cms, destination1, CmsLockType.EXCLUSIVE);

        // copy source again
        cms.lockResource(source);
        cms.copyResource(source, destination2, CmsResource.COPY_AS_NEW);

        // both source and destination must be exlusive locked
        assertLock(cms, source, CmsLockType.EXCLUSIVE);
        assertLock(cms, destination2, CmsLockType.EXCLUSIVE);

        // now some move tests
        source = "/types/jsp.jsp";
        destination1 = "/types/jsp_new1.html";

        // lock resource
        cms.lockResource(source);
        cms.moveResource(source, destination1);

        // since source was locked, destination must be shared exclusive
        assertLock(cms, destination1, CmsLockType.EXCLUSIVE);
    }

    /**
     * Tests lock status of a file and its siblings.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockForFile() throws Throwable {

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

        String source = "/folder1/subfolder11/page1.html";
        String sibling1 = "/folder1/subfolder12/page1.html";
        String sibling2 = "/folder2/subfolder22/page1.html";
        storeResources(cms, source);

        // lock source
        cms.lockResource(source);

        // the source file must have an exclusive lock
        // all siblings must have shared locks
        assertLock(cms, source, CmsLockType.EXCLUSIVE);
        assertLock(cms, sibling1, CmsLockType.SHARED_EXCLUSIVE);
        assertLock(cms, sibling2, CmsLockType.SHARED_EXCLUSIVE);

        // now unlock the source
        cms.unlockResource(source);

        // the source file and all sibling are unlocked now
        assertLock(cms, source, CmsLockType.UNLOCKED);
        assertLock(cms, sibling1, CmsLockType.UNLOCKED);
        assertLock(cms, sibling2, CmsLockType.UNLOCKED);
    }

    /**
     * Tests lock status of a folder and its siblings.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testLockForFolder() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing locking of folders");

        String folder = "/folder1/subfolder12/";
        String sibling1 = "/folder1/subfolder12/page1.html";
        String sibling2 = "/folder2/subfolder22/page1.html";
        storeResources(cms, folder);

        // lock folder
        cms.lockResource(folder);

        // the source folder must have an exclusive lock
        assertLock(cms, folder, CmsLockType.EXCLUSIVE);

        // all resources in the folder must have an inherited lock        
        List resources = cms.getResourcesInFolder(folder, CmsResourceFilter.DEFAULT);
        Iterator i = resources.iterator();
        while (i.hasNext()) {
            CmsResource res = (CmsResource)i.next();
            assertLock(cms, cms.getSitePath(res), CmsLockType.INHERITED);
        }

        // all siblings inside the folder must have an inherited lock
        assertLock(cms, sibling1, CmsLockType.INHERITED);
        // all siblings outside the folder must not locked
        assertLock(cms, sibling2, CmsLockType.UNLOCKED);

        // now unlock the folder 
        cms.unlockResource(folder);

        // the source folder must be unlocked
        assertLock(cms, folder, CmsLockType.UNLOCKED);

⌨️ 快捷键说明

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