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

📄 testpublishmanager.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/publish/TestPublishManager.java,v $
 * Date   : $Date: 2007-08-13 16:30:12 $
 * Version: $Revision: 1.3 $
 *
 * 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.publish;

import org.opencms.db.CmsLoginMessage;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsResource;
import org.opencms.lock.CmsLockException;
import org.opencms.lock.CmsLockType;
import org.opencms.main.CmsContextInfo;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsPermissionSet;
import org.opencms.security.CmsSecurityException;
import org.opencms.security.I_CmsPrincipal;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;

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

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

/**
 * Unit tests for the publish manager.<p>
 * 
 * @author Michael Moossen 
 * 
 * @version $Revision: 1.3 $
 */
public class TestPublishManager extends OpenCmsTestCase { 
    
    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public TestPublishManager(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(TestPublishManager.class.getName());

        suite.addTest(new TestPublishManager("testPublishReport"));
        suite.addTest(new TestPublishManager("testAbortJob"));
        suite.addTest(new TestPublishManager("testRunning"));
        suite.addTest(new TestPublishManager("testStop"));
        suite.addTest(new TestPublishManager("testListener"));
        suite.addTest(new TestPublishManager("testInitialization1"));
        suite.addTest(new TestPublishManager("testInitialization2"));
     
        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

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

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Test aborting an enqueued publish job.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testAbortJob() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing aborting an enqueued publish job");

        String source = "/folder2/subfolder21/image1.gif";
        String destination = "/folder1/image1_new"; // + i + ".gif";

        assertFalse(OpenCms.getPublishManager().isRunning());
        // stop the publish engine in order to perform the checks
        OpenCms.getPublishManager().stopPublishing();
        
        // copy and publish n new resources
        int max = 10;
        for (int i = 0; i < max; i++) {
            cms.copyResource(source, destination + (i + 1) + ".gif", CmsResource.COPY_AS_NEW);
            OpenCms.getPublishManager().publishResource(cms, destination + (i + 1) + ".gif");
        }

        // get the last enqueued publish job
        List queue = OpenCms.getPublishManager().getPublishQueue();
        CmsPublishJobEnqueued publishJob = (CmsPublishJobEnqueued)queue.get(queue.size() - 1);

        // login another user
        cms.loginUser("test1", "test1");
        try {
            // try to abort the last enqueued publish job
            OpenCms.getPublishManager().abortPublishJob(cms, publishJob, true);
            fail("should not be possible to abort a publish job by another user");
        } catch (CmsSecurityException e) {
            // ok, ignore
        }
        // login again as admin, keep online project
        cms.loginUser("Admin", "admin");

        // abort the last enqueued publish job
        OpenCms.getPublishManager().abortPublishJob(cms, publishJob, true);

        // try to abort the publish job again
        try {
            OpenCms.getPublishManager().abortPublishJob(cms, publishJob, true);
            fail("should not be possible to abort a publish job that has been already aborted");
        } catch (CmsPublishException e) {
            // ok, ignore
        }

        // start the background publishing again
        OpenCms.getPublishManager().startPublishing();
        // wait until everything get published
        OpenCms.getPublishManager().waitWhileRunning();

        // check that all files except the last one has been published
        for (int i = 1; i < max; i++) {
            assertState(cms, destination + i + ".gif", CmsResource.STATE_UNCHANGED);
        }
        // the last one should not be published
        assertFalse(cms.existsResource(destination + max + ".gif"));

        // switch to the "Offline" project
        cms.getRequestContext().setCurrentProject(cms.readProject("Offline"));

        // check that all files except the last one has been published
        for (int i = 1; i < max; i++) {
            assertState(cms, destination + i + ".gif", CmsResource.STATE_UNCHANGED);
            // and unlocked
            assertLock(cms, destination + i + ".gif", CmsLockType.UNLOCKED);
        }
        // the last one should not be published
        assertState(cms, destination + max + ".gif", CmsResource.STATE_NEW);
        // and unlocked
        assertLock(cms, destination + max + ".gif", CmsLockType.UNLOCKED);
        
        // clean up
        OpenCms.getPublishManager().publishProject(cms);
        OpenCms.getPublishManager().waitWhileRunning();
    }

    /**
     * Tests the reinitialization of the publish manager/engine.<p> 
     * 
     * @throws Throwable if something goes wrong
     */
    public void testInitialization1() throws Throwable {
     
        CmsObject cms = getCmsObject();
        echo("Testing initializing the publish manager/engine");
        
        // publish engine should not run currently
        assertFalse(OpenCms.getPublishManager().isRunning());
        // stop publishing
        OpenCms.getPublishManager().stopPublishing();

        String source = "/folder2/subfolder21/image1.gif";
        String destination1 = "/testInitialization1_"; // + i + ".gif";

        if (!cms.getLock(source).isNullLock()) {
            cms.unlockResource(source);
        }
        
        int max = 12; // be sure that it is the same as the system/publishhistory/history-size value + 2        
        // copy n new resources
        for (int i = 0; i < max; i++) {
            cms.copyResource(source, destination1 + (i + 1) + ".gif", CmsResource.COPY_AS_NEW);
            OpenCms.getPublishManager().publishResource(cms, destination1 + (i + 1) + ".gif");
        }
        
        // store current publishQueue
        List oldQueue = OpenCms.getPublishManager().getPublishQueue();
        // store current publish history
        List oldHistory = OpenCms.getPublishManager().getPublishHistory();
                
        // leads to reloading the queue and history data from the database
        OpenCms.getPublishManager().initialize(cms);
        
        // get reinitialized queue and history
        List newQueue = OpenCms.getPublishManager().getPublishQueue();
        List newHistory = OpenCms.getPublishManager().getPublishHistory();
        
        // compare old and new queue
        echo("Checking revived publish queue (" + oldQueue.size() + " items)");
        if (newQueue.size() != oldQueue.size()) {
            fail("Old and new queue have not the same size: Expected <" + oldQueue.size() + ">, was <" + newQueue.size() + ">");
        }
        Iterator n = newQueue.iterator();
        Iterator o = oldQueue.iterator();
        while (n.hasNext() && o.hasNext()) {
            CmsPublishJobEnqueued newJob = (CmsPublishJobEnqueued)n.next();
            CmsPublishJobEnqueued oldJob = (CmsPublishJobEnqueued)o.next();
            assertEquals(newJob, oldJob, true, true);
        }
        
        // compare old and new history  
        echo("Checking revived publish history (" + oldHistory.size() + " items)");
        if (newHistory.size() != oldHistory.size()) {
            fail("Old and new history have not the same size: Expected <" + oldHistory.size() + ">, was <" + newHistory.size() + ">");
        }
        n = newHistory.iterator();
        o = oldHistory.iterator();
        while (n.hasNext() && o.hasNext()) {
            CmsPublishJobFinished newJob = (CmsPublishJobFinished)n.next();
            CmsPublishJobFinished oldJob = (CmsPublishJobFinished)o.next();
            assertEquals(newJob, oldJob, false, true);
        }
        
        // start the publish engine and wait until all jobs are published
        OpenCms.getPublishManager().startPublishing();
        OpenCms.getPublishManager().waitWhileRunning();
    }        
        
    /**
     * Tests the reinitialization of the publish manager/engine.<p> 
     * 
     * @throws Throwable if something goes wrong
     */
    public void testInitialization2() throws Throwable {
     
        CmsObject cms = getCmsObject();
        echo("Testing initializing the publish manager/engine");        
        
        // publish engine should not run currently
        assertFalse(OpenCms.getPublishManager().isRunning());
        // stop publishing
        OpenCms.getPublishManager().stopPublishing();
        
        String source = "/folder2/subfolder21/image1.gif";
        String destination2 = "/testInitialization2_"; // + i + ".gif";
 
        if (!cms.getLock(source).isNullLock()) {
            cms.unlockResource(source);
        }
  
        int max = 12; // be sure that it is the same as the system/publishhistory/history-size value + 2
        // create a larger project
        for (int i = 0; i < max; i++) {
            cms.copyResource(source, destination2 + (i + 1) + ".gif", CmsResource.COPY_AS_NEW);
        }
        OpenCms.getPublishManager().publishProject(cms);  

        // store current publishQueue
        List oldQueue = OpenCms.getPublishManager().getPublishQueue();
                
        // add an event listener used to restart the engine when ther job was started
        OpenCms.getPublishManager().addPublishListener(new TestPublishEventListener2(cms));
        
        // now start publishing again and reinitialize the publish manager and engine while it is running
        OpenCms.getPublishManager().startPublishing();
        OpenCms.getPublishManager().waitWhileRunning();
        
        // get reinitialized queue and history
        List newQueue = OpenCms.getPublishManager().getPublishQueue();
        List newHistory = OpenCms.getPublishManager().getPublishHistory();
        
        // the project should not be in the publish queue anymore
        assertEquals(1, oldQueue.size());
        assertEquals(0, newQueue.size());

⌨️ 快捷键说明

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