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

📄 testundochanges.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestUndoChanges.java,v $
 * Date   : $Date: 2006/03/27 14:52:46 $
 * Version: $Revision: 1.20 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 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.CmsResourceTypePlain;
import org.opencms.lock.CmsLock;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsTestResourceFilter;
import org.opencms.test.OpenCmsTestResourceStorage;

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

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

/**
 * Unit test for the "undoChanges" method of the CmsObject.<p>
 * 
 * @author Michael Emmerich 
 * @version $Revision: 1.20 $
 */
public class TestUndoChanges extends OpenCmsTestCase {

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

        suite.addTest(new TestUndoChanges("testUndoChangesResource"));
        suite.addTest(new TestUndoChanges("testUndoChangesOnNewResource"));
        suite.addTest(new TestUndoChanges("testUndoChangesFolder"));
        suite.addTest(new TestUndoChanges("testUndoChangesFolderRecursive"));
        suite.addTest(new TestUndoChanges("testUndoChangesAfterCopyNewOverDeleted"));
        suite.addTest(new TestUndoChanges("testUndoChangesAfterCopySiblingOverDeleted"));
        suite.addTest(new TestUndoChanges("testUndoChangesWithAce"));
        suite.addTest(new TestUndoChanges("testUndoChangesSharedProperty"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

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

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Tests undo changes on a new resource, this must lead to an exception!<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testUndoChangesOnNewResource() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing for exception when trying undo changes on a new resource");

        String source = "/types/new.html";

        // create a new, plain resource
        cms.createResource(source, CmsResourceTypePlain.getStaticTypeId());
        assertLock(cms, source, CmsLock.TYPE_EXCLUSIVE);

        try {
            cms.undoChanges(source, false);
        } catch (CmsVfsException e) {
            if (e.getMessageContainer().getKey().equals(org.opencms.db.Messages.ERR_UNDO_CHANGES_FOR_RESOURCE_1)) {
                // this is the expected result, so the test is successful
                return;
            }
        }
        fail("Did not catch expected exception trying undo changes on a new resource!");
    }

    /**
     * Tests undo changes after a resource was deleted and another 
     * resource was copied over the deleted file "as new".<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testUndoChangesAfterCopyNewOverDeleted() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing undo changes after overwriting a deleted file with a new file");

        String source = "/folder1/page2.html";
        String destination = "/folder1/page1.html";

        storeResources(cms, source);
        storeResources(cms, destination);

        cms.lockResource(destination);

        // delete and owerwrite
        cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertState(cms, destination, CmsResource.STATE_DELETED);

        cms.copyResource(source, destination, CmsResource.COPY_AS_NEW);

        // now undo all changes on the resource
        cms.undoChanges(destination, false);

        // now ensure source and destionation are in the original state
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_EQUAL);
        assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);

        // publishing may reveal problems with the id's
        cms.publishProject();
    }

    /**
     * Tests undo changes after a resource was deleted and another 
     * resource was copied over the deleted file "as sibling".<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testUndoChangesAfterCopySiblingOverDeleted() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing undo changes after overwriting a deleted file with a sibling");

        String source = "/folder1/page2.html";
        String destination = "/folder1/page1.html";

        storeResources(cms, source);
        storeResources(cms, destination);

        cms.lockResource(destination);

        // delete and owerwrite with a sibling
        cms.deleteResource(destination, CmsResource.DELETE_PRESERVE_SIBLINGS);
        assertState(cms, destination, CmsResource.STATE_DELETED);

        cms.copyResource(source, destination, CmsResource.COPY_AS_SIBLING);

        // now undo all changes on the resource
        cms.undoChanges(destination, false);

        // now ensure source and destionation are in the original state
        assertFilter(cms, source, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);
        assertFilter(cms, destination, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);

        // publishing may reveal problems with the id's
        cms.publishProject();
    }

    /**
     * Test the touch method to touch a single resource.<p>
     * @param tc the OpenCmsTestCase
     * @param cms the CmsObject
     * @param resource1 the resource to touch
     * @throws Throwable if something goes wrong
     */
    public static void undoChanges(OpenCmsTestCase tc, CmsObject cms, String resource1) throws Throwable {

        // create a global storage and store the resource
        tc.createStorage("undoChanges");
        tc.switchStorage("undoChanges");
        tc.storeResources(cms, resource1);
        tc.switchStorage(OpenCmsTestResourceStorage.DEFAULT_STORAGE);

        // now do a touch on the resource
        TestTouch.touchResource(tc, cms, resource1);

        // change a property
        CmsProperty property1 = new CmsProperty("Title", "undoChanges", null);
        TestProperty.writeProperty(tc, cms, resource1, property1);

        // now undo everything
        cms.lockResource(resource1);
        cms.undoChanges(resource1, false);
        cms.unlockResource(resource1);

        tc.switchStorage("undoChanges");

        // now evaluate the result
        tc.assertFilter(cms, resource1, OpenCmsTestResourceFilter.FILTER_UNDOCHANGES);

⌨️ 快捷键说明

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