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

📄 testconcurrentoperations.java

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

import org.opencms.db.CmsPublishList;
import org.opencms.file.types.CmsResourceTypeFolder;
import org.opencms.main.OpenCms;
import org.opencms.publish.CmsPublishJobFinished;
import org.opencms.report.CmsShellReport;
import org.opencms.test.OpenCmsTestCase;
import org.opencms.test.OpenCmsTestProperties;
import org.opencms.test.OpenCmsThreadedTestCase;
import org.opencms.test.OpenCmsThreadedTestCaseSuite;
import org.opencms.util.CmsStringUtil;

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

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

/**
 * Unit test for concurrent operations of the CmsObject.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.3 $
 */
public class TestConcurrentOperations extends OpenCmsTestCase {

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

        suite.addTest(new TestConcurrentOperations("testConcurrentPublishResource"));
        suite.addTest(new TestConcurrentOperations("testConcurrentPublishResourceWithRelated"));
        suite.addTest(new TestConcurrentOperations("testConcurrentPublishProject"));
        suite.addTest(new TestConcurrentOperations("testConcurrentCreationIssue"));

        TestSetup wrapper = new TestSetup(suite) {

            protected void setUp() {

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

            protected void tearDown() {

                removeOpenCms();
            }
        };

        return wrapper;
    }

    /**
     * Concurrent creation test method.<p>
     * 
     * @param cms the OpenCms user context to use
     * @param count the count for this test
     * @param value used to pass a counter value between the councurrent tests
     * 
     * @throws Exception in case the resource could not be created (which is expected)
     */
    public void doConcurrentCreationOperation(CmsObject cms, Integer count, Integer[] value) throws Exception {

        System.out.println("Running doConcurrentCreationOperation() method call - count: " + count.intValue());
        int val = value[0].intValue();
        String name = "/testfolder/sub1/sub2/sub3/subtestfolder" + val;
        CmsResource res = cms.createResource(name, CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        value[0] = new Integer(++val);
        System.out.println("++++++++++++++++++ Finished creation of folder "
            + res.getRootPath()
            + " - count: "
            + count.intValue());
    }

    /**
     * Concurrent publish project test method.<p>
     * 
     * @param cms the OpenCms user context to use
     * @param count the count for this test
     * @param value used to pass a counter value between the councurrent tests
     * 
     * @throws Exception if something goes wrong, unexpected
     */
    public void doConcurrentPublishProjectOperation(CmsObject cms, Integer count, Integer[] value) throws Exception {

        System.out.println("Running doConcurrentPublishProjectOperation() method call - count: " + count.intValue());
        int val = value[0].intValue();
        OpenCms.getPublishManager().publishProject(cms);
        value[0] = new Integer(++val);
        System.out.println("++++++++++++++++++ Finished publish project - count: " + count.intValue());
    }

    /**
     * Concurrent publish resource test method.<p>
     * 
     * @param cms the OpenCms user context to use
     * @param count the count for this test
     * @param value used to pass a counter value between the councurrent tests
     * 
     * @throws Exception if something goes wrong, unexpected
     */
    public void doConcurrentPublishResourceOperation(CmsObject cms, Integer count, Integer[] value) throws Exception {

        System.out.println("Running doConcurrentPublishResourceOperation() method call - count: " + count.intValue());
        int val = value[0].intValue();

        OpenCms.getPublishManager().publishResource(cms, "index.html");

        value[0] = new Integer(++val);
        System.out.println("++++++++++++++++++ Finished publish resource - count: " + count.intValue());
    }

    /**
     * Concurrent publish resource test method.<p>
     * 
     * @param cms the OpenCms user context to use
     * @param count the count for this test
     * @param value used to pass a counter value between the councurrent tests
     * 
     * @throws Exception if something goes wrong, unexpected
     */
    public void doConcurrentPublishResourceWithRelatedOperation(CmsObject cms, Integer count, Integer[] value)
    throws Exception {

        System.out.println("thread " + count + ": starting");
        int val = value[0].intValue();

        // create a new publish list
        System.out.println("thread " + count + ": getting publish list");
        CmsPublishList publishList = OpenCms.getPublishManager().getPublishList(
            cms,
            cms.readResource("index.html", CmsResourceFilter.ALL),
            false);
        // get the related resources
        System.out.println("thread " + count + ": getting related resources");
        CmsPublishList relResources = OpenCms.getPublishManager().getRelatedResourcesToPublish(cms, publishList);
        // merge the two publish lists
        System.out.println("thread " + count + ": merging publish lists");
        publishList = OpenCms.getPublishManager().mergePublishLists(cms, publishList, relResources);
        // publish
        System.out.println("thread " + count + ": publishing");
        OpenCms.getPublishManager().publishProject(
            cms,
            new CmsShellReport(cms.getRequestContext().getLocale()),
            publishList);

        value[0] = new Integer(++val);
        System.out.println("thread " + count + ": finished");
    }

    /**
     * Tests concurrent creation a resource with the same name.<p>
     * 
     * @throws Exception if the test fails
     */
    public void testConcurrentCreationIssue() throws Exception {

        int count = 50;
        echo("Concurrent folder creation test: Testing concurrent folder creation with " + count + " threads");

        CmsObject cms = getCmsObject();

        cms.createResource("/testfolder/", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        cms.createResource("/testfolder/sub1/", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        cms.createResource("/testfolder/sub1/sub2/", CmsResourceTypeFolder.RESOURCE_TYPE_ID);
        cms.createResource("/testfolder/sub1/sub2/sub3/", CmsResourceTypeFolder.RESOURCE_TYPE_ID);

        String name = "doConcurrentCreationOperation";
        Integer[] value = new Integer[1];
        value[0] = new Integer(0);

        Object[] parameters = new Object[] {
            OpenCmsThreadedTestCaseSuite.PARAM_CMSOBJECT,
            OpenCmsThreadedTestCaseSuite.PARAM_COUNTER,
            value};
        OpenCmsThreadedTestCaseSuite suite = new OpenCmsThreadedTestCaseSuite(count, this, name, parameters);
        OpenCmsThreadedTestCase[] threads = suite.run();

        if (suite.getThrowable() != null) {
            throw new Exception(suite.getThrowable());
        }

        int c = 0;
        int ec = 0;

⌨️ 快捷键说明

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