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

📄 opencmstestcase.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/test/org/opencms/test/OpenCmsTestCase.java,v $
 * Date   : $Date: 2006/07/19 13:19:12 $
 * Version: $Revision: 1.92 $
 *
 * 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.test;

import org.opencms.db.CmsDbPool;
import org.opencms.file.CmsFile;
import org.opencms.file.CmsGroup;
import org.opencms.file.CmsObject;
import org.opencms.file.CmsProject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.file.CmsResourceFilter;
import org.opencms.file.CmsUser;
import org.opencms.file.types.CmsResourceTypeBinary;
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.CmsShell;
import org.opencms.main.CmsSystemInfo;
import org.opencms.main.OpenCms;
import org.opencms.report.CmsShellReport;
import org.opencms.security.CmsAccessControlEntry;
import org.opencms.security.CmsAccessControlList;
import org.opencms.security.CmsPermissionSet;
import org.opencms.setup.CmsSetupDb;
import org.opencms.util.CmsDateUtil;
import org.opencms.util.CmsFileUtil;
import org.opencms.util.CmsPropertyUtils;
import org.opencms.util.CmsUUID;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import junit.framework.TestCase;

import org.apache.commons.collections.ExtendedProperties;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.util.NodeComparator;

/** 
 * Extends the JUnit standard with methods to handle an OpenCms database
 * test instance.<p>
 * 
 * The required configuration files are located in the 
 * <code>${test.data.path}/WEB-INF</code> folder structure.<p>
 * 
 * To run this test you might have to change the database connection
 * values in the provided <code>${test.data.path}/WEB-INF/config/opencms.properties</code> file.<p>
 * 
 * @author Alexander Kandzior 
 * 
 * @version $Revision: 1.92 $
 * 
 * @since 6.0.0
 */
public class OpenCmsTestCase extends TestCase {

    /** Class to bundle the connection information. */
    protected class ConnectionData {

        /** The name of the database. */
        public String m_dbName;

        /** The database driver. */
        public String m_jdbcDriver;

        /** The database url. */
        public String m_jdbcUrl;

        /** Additional database params. */
        public String m_jdbcUrlParams;

        /** The name of the user. */
        public String m_userName;

        /** The password of the user. */
        public String m_userPassword;
    }

    /**
     * Extension of <code>NodeComparator</code> to store unequal nodes.<p>
     */
    class InternalNodeComparator extends NodeComparator {

        /** Unequal node1. */
        public Node m_node1 = null;

        /** Unequal node2. */
        public Node m_node2 = null;

        /**
         * @see org.dom4j.util.NodeComparator#compare(org.dom4j.Node, org.dom4j.Node)
         */
        public int compare(Node n1, Node n2) {

            int result = super.compare(n1, n2);
            if (result != 0 && m_node1 == null) {
                m_node1 = n1;
                m_node2 = n2;
            }
            return result;
        }
    }

    /** Key for tests on MySql database. */
    public static final String DB_MYSQL = "mysql";

    /** Key for tests on Oracle database. */
    public static final String DB_ORACLE = "oracle";

    /** The OpenCms/database configuration. */
    public static ExtendedProperties m_configuration = null;

    /** DB product used for the tests. */
    public static String m_dbProduct = DB_MYSQL;

    /** Name of the default tablespace (oracle only). */
    public static String m_defaultTablespace;

    /** Name of the index tablespace (oracle only). */
    public static String m_indexTablespace;

    /** The internal storages. */
    public static HashMap m_resourceStorages;

    /** Name of the temporary tablespace (oracle only). */
    public static String m_tempTablespace;

    /** Additional connection data. */
    protected static ConnectionData m_additionalConnection;

    /** The user connection data. */
    protected static ConnectionData m_defaultConnection;

    /** The setup connection data. */
    protected static ConnectionData m_setupConnection;

    /** The additional connection name. */
    private static String m_additionalConnectionName = "additional";

    /** The file date of the configuration files. */
    private static long[] m_dateConfigFiles;

    /** The path to the default setup data files. */
    private static String m_setupDataPath;

    /** The initialized OpenCms shell instance. */
    private static CmsShell m_shell;

    /** The list of paths to the additional test data files. */
    private static List m_testDataPath;

    /** The current resource storage. */
    public OpenCmsTestResourceStorage m_currentResourceStrorage;

    /**
     * Default JUnit constructor.<p>
     * 
     * @param arg0 JUnit parameters
     */
    public OpenCmsTestCase(String arg0) {

        this(arg0, true);
    }

    /**
     * JUnit constructor.<p>
     * @param arg0 JUnit parameters
     * @param initialize indicates if the configuration will be initialized
     */
    public OpenCmsTestCase(String arg0, boolean initialize) {

        super(arg0);
        if (initialize) {
            OpenCmsTestLogAppender.setBreakOnError(false);
            if (m_resourceStorages == null) {
                m_resourceStorages = new HashMap();
            }

            // initialize configuration
            initConfiguration();

            // set "OpenCmsLog" system property to enable the logger
            OpenCmsTestLogAppender.setBreakOnError(true);
        }
    }

    /**
     * Generates a sub tree of folders with files.<p>
     * 
     * @param cms the cms context
     * @param vfsFolder where to create the subtree
     * @param maxWidth an upper bound for the number of subfolder a folder should have
     * @param maxDepth an upper bound for depth of the genearted subtree
     * @param maxProps upper bound for number of properties to create for each resource
     * @param propertyDistribution a percentage: x% shared props and (1-x)% individuals props
     * @param maxNumberOfFiles upper bound for the number of files in each folder
     * @param fileTypeDistribution a percentage: x% binary files and (1-x)% text files
     * 
     * @return the number of really written files
     * 
     * @throws Exception if something goes wrong
     */
    public static int generateContent(
        CmsObject cms,
        String vfsFolder,
        int maxWidth,
        int maxDepth,
        int maxProps,
        double propertyDistribution,
        int maxNumberOfFiles,
        double fileTypeDistribution) throws Exception {

        int fileNameLength = 10;
        int propValueLength = 10;

        // end recursion
        if (maxDepth < 1) {
            return 0;
        }
        if (!vfsFolder.endsWith("/")) {
            vfsFolder += "/";
        }

        int writtenFiles = 0;

        int width = (int)(maxWidth * Math.random()) + 1;
        int depth = maxDepth - (int)(2 * Math.random());
        for (int i = 0; i < width; i++) {
            // generate folder
            String vfsName = vfsFolder + generateName(fileNameLength) + i;
            List props = generateProperties(cms, maxProps, propValueLength, propertyDistribution);
            cms.createResource(vfsName, CmsResourceTypeFolder.getStaticTypeId(), new byte[0], props);
            cms.unlockResource(vfsName);

            int numberOfFiles = (int)(maxNumberOfFiles * Math.random()) + 1;
            // generate binary files
            int numberOfBinaryFiles = (int)(numberOfFiles * fileTypeDistribution);
            writtenFiles += generateResources(
                cms,
                "org/opencms/search/pdf-test-112.pdf",
                vfsName,
                numberOfBinaryFiles,
                CmsResourceTypeBinary.getStaticTypeId(),
                maxProps,
                propertyDistribution);

            // generate text files
            writtenFiles += generateResources(cms, "org/opencms/search/extractors/test1.html", vfsName, numberOfFiles
                - numberOfBinaryFiles, CmsResourceTypePlain.getStaticTypeId(), maxProps, propertyDistribution);

            // in depth recursion
            writtenFiles += generateContent(
                cms,
                vfsName,
                maxWidth,
                depth - 1,
                maxProps,
                propertyDistribution,
                maxNumberOfFiles,
                fileTypeDistribution);

            System.out.println("" + writtenFiles + " files written in Folder " + vfsName);
        }
        return writtenFiles;
    }

    /**
     * Generates a sub tree of folders with files.<p>
     * 
     * @param cms the cms context
     * @param vfsFolder name of the folder
     * @param numberOfFiles the number of files to generate
     * @param fileTypeDistribution a percentage: x% binary files and (1-x)% text files
     * 
     * @return the number of files generated
     * 
     * @throws Exception if something goes wrong
     */
    public static int generateContent(CmsObject cms, String vfsFolder, int numberOfFiles, double fileTypeDistribution)
    throws Exception {

        int maxProps = 10;
        double propertyDistribution = 0.0;
        int writtenFiles = 0;

        int numberOfBinaryFiles = (int)(numberOfFiles * fileTypeDistribution);

        // generate binary files
        writtenFiles += generateResources(
            cms,
            "org/opencms/search/pdf-test-112.pdf",
            vfsFolder,
            numberOfBinaryFiles,
            CmsResourceTypeBinary.getStaticTypeId(),
            maxProps,
            propertyDistribution);

        // generate text files
        writtenFiles += generateResources(cms, "org/opencms/search/extractors/test1.html", vfsFolder, numberOfFiles
            - numberOfBinaryFiles, CmsResourceTypePlain.getStaticTypeId(), maxProps, propertyDistribution);

        System.out.println("" + writtenFiles + " files written in Folder " + vfsFolder);

⌨️ 快捷键说明

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