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

📄 testpublishissues.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/file/TestPublishIssues.java,v $
 * Date   : $Date: 2006/03/27 14:52:46 $
 * Version: $Revision: 1.21 $
 *
 * 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.db.CmsPublishList;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.file.types.CmsResourceTypePlain;
import org.opencms.lock.CmsLock;
import org.opencms.main.CmsException;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.util.CmsUUID;

import java.util.Collections;
import java.util.List;

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

/**
 * Unit tests for special publish issues.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.21 $
 */
/**
 * Comment for <code>TestPermissions</code>.<p>
 */
public class TestPublishIssues extends OpenCmsTestCase {
  
    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */    
    public TestPublishIssues(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(TestPublishIssues.class.getName());
                
        suite.addTest(new TestPublishIssues("testPublishScenarioA"));
        suite.addTest(new TestPublishIssues("testPublishScenarioB"));
        suite.addTest(new TestPublishIssues("testPublishScenarioC"));        
        suite.addTest(new TestPublishIssues("testMultipleProjectCreation"));
        suite.addTest(new TestPublishIssues("testDirectPublishWithSiblings"));
        suite.addTest(new TestPublishIssues("testPublishScenarioD"));        
        suite.addTest(new TestPublishIssues("testPublishScenarioE"));        
        
        TestSetup wrapper = new TestSetup(suite) {
            
            protected void setUp() {
                setupOpenCms("simpletest", "/sites/default/");
            }
            
            protected void tearDown() {
                removeOpenCms();
            }
        };
        
        return wrapper;
    }
 
    /**
     * Tests publish scenario "A".<p>
     * 
     * This scenario is described as follows:
     * We have users "test1" and "test2".
     * We have two projects, "project1" and "project2".
     * Project "project1" consists of the folder "/".
     * Project "project2" consists of the folder "/folder1/subfolder11/".
     * User "test2" edits the file "/folder1/subfolder11/index.html".
     * After this, user "test1" locks the folder "/folder1" in "project1", and unlocks it again.
     * User "test2" logs in and now publishes "project2".<p>
     * 
     * Wanted result: the changed resource "/folder1/subfolder11/index.html" is published 
     * with "project2".<p> 
     * 
     * The test illustrates a change in the logic from OpenCms 5 to OpenCms 6:
     * In OpenCms 5, locking a file caused it to switch to the current users project.
     * In OpenCms 6, this is no longer true, at last not if you just lock a parent folder.
     * So in OpenCms 5, this test would fail, since the resource would be in "project1", not "project2".<p> 
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishScenarioA() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing publish scenario A");
        String projectRes1 = "/folder1/subfolder11/";
        String resource1 = projectRes1 + "index.html";
        String resource2 = "/folder1/";
        long timestamp = System.currentTimeMillis();
                
        // we use the default "Offline" project as "project1"
        CmsProject project1 = cms.readProject("Offline");
        
        // create "project2" as Admin user       
        cms.createProject(
            "project2", 
            "Test project 2 for scenario A", 
            OpenCms.getDefaultUsers().getGroupUsers(), 
            OpenCms.getDefaultUsers().getGroupUsers());

        CmsProject project2 = cms.readProject("project2");
        cms.getRequestContext().setCurrentProject(project2);
        cms.copyResourceToProject(projectRes1);
        
        // check if the project was created as planned
        List resources = cms.readProjectResources(project2);
        assertEquals(1, resources.size());
        assertEquals("/sites/default" + projectRes1, (String)resources.get(0));
        
        // login as user "test2"
        cms.loginUser("test2", "test2");
        cms.getRequestContext().setCurrentProject(project2);        
                
        // perform some edit on the file
        cms.lockResource(resource1);
        cms.setDateLastModified(resource1, System.currentTimeMillis(), false);
        
        // assert some basic status info
        assertDateLastModifiedAfter(cms, resource1, timestamp);
        assertProject(cms, resource1, project2);
        assertLock(cms, resource1, CmsLock.TYPE_EXCLUSIVE);
        assertState(cms, resource1, CmsResource.STATE_CHANGED);
        
        // now login as user "test1" (default will be in the "Offline" project)
        cms.loginUser("test1", "test1");
        cms.getRequestContext().setCurrentProject(project1);     
        
        // lock the folder
        cms.lockResource(resource2);
        
        // assert some basic status info
        assertProject(cms, resource1, project2);
        assertLock(cms, resource1, CmsLock.TYPE_INHERITED);        
        
        // now unlock the folder again
        cms.unlockResource(resource2);
        
        // back to the user "test2"
        cms.loginUser("test2", "test2");
        cms.getRequestContext().setCurrentProject(project2);            

        // get the publish list
        CmsPublishList publishList = cms.getPublishList();
        assertEquals(1, publishList.getFileList().size());
        
        // project should have no locked resources 
        int resourceProjectCount = cms.countLockedResources(project2.getId());    
        assertEquals(0, resourceProjectCount);
        
        // unlock the project
        cms.publishProject();
        
        // ensure the file was published - state must be "unchanged" 
        assertState(cms, resource1, CmsResource.STATE_UNCHANGED);
    }
    
    /**
     * Tests publish scenario "B".<p>
     * 
     * This scenario is described as follows:
     * We have users "test1" and "test2" and projects "projectA" and "projectB".
     * Both projects contain all resources.
     * We have two folders "/foldera/" and "/folderb/".
     * There is a resource "test.txt" that has a sibling in both folders. 
     * User "test1" locks folder "/foldera/" and user "test2" locks folder "/folderb".
     * Now both users try to edit the sibling of "test.txt" in their folder.
     * 
     * TODO: How are concurrent file modifications avoided on the sibling?.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testPublishScenarioB() throws Throwable {
        
        CmsObject cms = getCmsObject();
        echo("Testing publish scenario B");
        
        // set up the test case first
        String folderA = "/foldera/";
        String folderB = "/folderb/";
        String resourceA = folderA + "test.txt";
        String resourceB = folderB + "test.txt";
        
        // create the resource and the sibling
        cms.createResource(folderA, CmsResourceTypeFolder.getStaticTypeId());
        cms.createResource(folderB, CmsResourceTypeFolder.getStaticTypeId());
        cms.createResource(resourceA, CmsResourceTypePlain.getStaticTypeId());
        cms.createSibling(resourceA, resourceB, Collections.EMPTY_LIST);
        CmsFile cmsFile = cms.readFile(resourceA);
        cmsFile.setContents("Hello, this is a test!".getBytes());
        cms.writeFile(cmsFile);
        
        // now publish the project
        cms.unlockProject(cms.getRequestContext().currentProject().getId());
        cms.publishProject();
        
        // check if the setup was created as planned
        cmsFile = cms.readFile(resourceA);
        assertEquals(2, cmsFile.getSiblingCount());
        assertState(cms, resourceA, CmsResource.STATE_UNCHANGED);
        cmsFile = cms.readFile(resourceB);
        assertEquals(2, cmsFile.getSiblingCount());        
        assertState(cms, resourceB, CmsResource.STATE_UNCHANGED);
        
        // we use the default "Offline" project as "projectA"
//        CmsProject projectA = cms.readProject("Offline");        
        
        // create "projectB" as Admin user       
        cms.createProject(
            "projectB", 
            "Test project 2 for scenario B", 

⌨️ 快捷键说明

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