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

📄 opencmstestcase.java

📁 找了很久才找到到源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                cms,
                vfsName,
                maxWidth,
                depth - 1,
                maxProps,
                propertyDistribution,
                maxNumberOfFiles,
                fileTypeDistribution);

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

    /**
     * Generate a new random name.<p>
     * 
     * @param maxLen upper bound for the length of the name
     * 
     * @return a random name
     */
    public static String generateName(int maxLen) {

        String name = "";
        Random rnd = new Random();
        int len = rnd.nextInt(maxLen) + 1;
        for (int j = 0; j < len; j++) {
            name += (char)(rnd.nextInt(26) + 97);
        }
        return name;
    }

    /**
     * Generates random properties.<p>
     * 
     * @param cms the cms context
     * @param maxProps upper bound for number of properties to create for each resource
     * @param propValueLength upper bound for the number of char for the values
     * @param propertyDistribution a percentage: x% shared props and (1-x)% individuals props
     * 
     * @return a list of <code>{@link CmsProperty}</code> objects
     * 
     * @throws CmsException if something goes wrong
     */
    public static List generateProperties(CmsObject cms, int maxProps, int propValueLength, double propertyDistribution)
    throws CmsException {

        List propList = cms.readAllPropertyDefinitions();

        List props = new ArrayList();
        if (maxProps > propList.size()) {
            maxProps = propList.size();
        }
        Random rnd = new Random();
        int propN = rnd.nextInt(maxProps) + 1;
        for (int j = 0; j < propN; j++) {
            CmsPropertyDefinition propDef = (CmsPropertyDefinition)propList.get((int)(Math.random() * propList.size()));
            propList.remove(propDef);
            if (Math.random() < propertyDistribution) {
                // only resource prop
                props.add(new CmsProperty(propDef.getName(), null, generateName(propValueLength)));
            } else {
                // resource and structure props
                props.add(new CmsProperty(
                    propDef.getName(),
                    generateName(propValueLength),
                    generateName(propValueLength)));
            }
        }

        return props;
    }

    /**
     * Generates n new resources in a given folder.<p>
     * 
     * @param cms the cms context
     * @param rfsName the rfs file for the content
     * @param vfsFolder the folder to create the resources in
     * @param n number of resources to generate
     * @param type the type of the resource
     * @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
     * 
     * @return the number of really written files
     * 
     * @throws Exception if something goes wrong
     */
    public static int generateResources(
        CmsObject cms,
        String rfsName,
        String vfsFolder,
        int n,
        int type,
        int maxProps,
        double propertyDistribution) throws Exception {

        int fileNameLength = 10;
        int propValueLength = 10;

        if (!vfsFolder.endsWith("/")) {
            vfsFolder += "/";
        }
        int writtenFiles = 0;
        System.out.println("Importing Files");
        for (int i = 0; i < n; i++) {
            String vfsName = vfsFolder + generateName(fileNameLength) + i;
            if (rfsName.lastIndexOf('.') > 0) {
                vfsName += rfsName.substring(rfsName.lastIndexOf('.'));
            }
            List props = generateProperties(cms, maxProps, propValueLength, propertyDistribution);
            try {
                OpenCmsTestCase.importTestResource(cms, rfsName, vfsName, type, props);
                writtenFiles++;
            } catch (Exception e) {
                System.out.println("error! " + e.getMessage());
            }
        }
        return writtenFiles;
    }

    /**
     * Generates n new users for a given group.<p>
     * 
     * @param cms the cms context
     * @param groupName the group name, group will be creating if group does not exists
     * @param n number of users to generate
     * 
     * @throws CmsException if something goes wrong
     */
    public static void generateUsers(CmsObject cms, String groupName, int n) throws CmsException {

        CmsGroup group = null;
        try {
            group = cms.readGroup(groupName);
        } catch (Exception e) {
            // ignore
        }
        if (group == null) {
            cms.createGroup(groupName, groupName, 0, null);
        }
        for (int i = 0; i < n; i++) {
            String name = generateName(10) + i;
            cms.createUser(name, "pwd" + i, "test user " + i, null);
            cms.addUserToGroup(name, groupName);
        }
    }

    /**
     * Returns the currently used database/configuration.<p>
     * 
     * @return he currently used database/configuration
     */
    public static String getDbProduct() {

        return m_dbProduct;
    }

    /**
     * Initializes the path to the test data configuration files
     * using the default path.<p>
     */
    public static synchronized void initTestDataPath() {

        if (m_testDataPath == null) {
            m_testDataPath = new ArrayList(4);

            // test wether we are instantiated within the 
            // AllTest suite and therefore the OpenCmsTestProperties are 
            // already set up:
            try {
                OpenCmsTestProperties.getInstance();
            } catch (RuntimeException rte) {
                OpenCmsTestProperties.initialize(org.opencms.test.AllTests.TEST_PROPERTIES_PATH);
            }
            // set data path 
            addTestDataPath(OpenCmsTestProperties.getInstance().getTestDataPath());
        }
    }

    /**
     * Removes the initialized OpenCms database and all 
     * temporary files created during the test run.<p>
     */
    public static void removeOpenCms() {

        // ensure logging does not throw exceptions
        OpenCmsTestLogAppender.setBreakOnError(false);

        // output a message
        m_shell.printPrompt();
        System.out.println("----- Test cases finished -----");

        // exit the shell
        m_shell.exit();

        // remove the database
        removeDatabase();

        // copy the configuration files to re-create the original configuration
        String configFolder = getTestDataPath("WEB-INF" + File.separator + "config." + m_dbProduct + File.separator);
        copyConfiguration(configFolder);

        // remove potentially created "classes, "lib", "backup" etc. folder
        String path;
        path = getTestDataPath("WEB-INF/classes/");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }
        path = getTestDataPath("WEB-INF/logs/publish");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }
        path = getTestDataPath("WEB-INF/lib/");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }
        path = getTestDataPath("WEB-INF/" + CmsSystemInfo.FOLDER_CONFIG + "backup/");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }
        path = getTestDataPath("WEB-INF/index/");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }
        path = getTestDataPath("export/");
        if (path != null) {
            CmsFileUtil.purgeDirectory(new File(path));
        }

        try {
            // sleep 0.5 seconds - sometimes other Threads need to finish before the next test case can start
            Thread.sleep(500);
        } catch (InterruptedException e) {
            // ignore
        }
    }

    /**
     * Restarts the OpenCms shell.<p>
     */
    public static void restartOpenCms() {

        // turn off exceptions after error logging during setup (won't work otherwise)
        OpenCmsTestLogAppender.setBreakOnError(false);
        // output a message 
        System.out.println("\n\n\n----- Restarting OpenCms -----");

        // kill any old shell that might have remained from a previous test 
        if (m_shell != null) {
            try {
                m_shell.exit();
                m_shell = null;
            } catch (Throwable t) {
                // ignore
            }
        }

        // create a shell instance
        m_shell = new CmsShell(getTestDataPath("WEB-INF" + File.separator), null, null, "${user}@${project}>", null);

        // turn on exceptions after error logging
        OpenCmsTestLogAppender.setBreakOnError(true);
    }

    /**
     * Sets up a complete OpenCms instance with configuration from the config-ori folder, 
     * creating the usual projects, and importing a default database.<p>
     * 
     * @param importFolder the folder to import in the "real" FS
     * @param targetFolder the target folder of the import in the VFS
     * @return an initialized OpenCms context with "Admin" user in the "Offline" project with the site root set to "/" 
     */
    public static CmsObject setupOpenCms(String importFolder, String targetFolder) {

        return setupOpenCms(importFolder, targetFolder, getTestDataPath("WEB-INF/config." + m_dbProduct + "/"), true);
    }

    /**
     * Sets up a complete OpenCms instance with configuration from the config-ori folder, 
     * creating the usual projects, and importing a default database.<p>
     * 
     * @param importFolder the folder to import in the "real" FS
     * @param targetFolder the target folder of the import in the VFS
     * @param publish flag to signalize if the publish script should be called
     * @return an initialized OpenCms context with "Admin" user in the "Offline" project with the site root set to "/" 
     */
    public static CmsObject setupOpenCms(String importFolder, String targetFolder, boolean publish) {

        return setupOpenCms(importFolder, targetFolder, getTestDataPath("WEB-INF/config." + m_dbProduct + "/"), publish);
    }

    /**
     * Sets up a complete OpenCms instance, creating the usual projects,
     * and importing a default database.<p>
     * 
     * @param importFolder the folder to import in the "real" FS
     * @param targetFolder the target folder of the import in the VFS
     * @param configFolder the folder to copy the configuration files
     * @param publish publish only if set
     * 
     * @return an initialized OpenCms context with "Admin" user in the "Offline" project with the site root set to "/" 
     */
    public static CmsObject setupOpenCms(String importFolder, String targetFolder, String configFolder, boolean publish) {

        // intialize a new resource storage
        m_resourceStorages = new HashMap();

        // turn off exceptions after error logging during setup (won't work otherwise)
        OpenCmsTestLogAppender.setBreakOnError(false);
        // output a message 
        System.out.println("\n\n\n----- Starting test case: Importing OpenCms VFS data -----");

        // kill any old shell that might have remained from a previous test 
        if (m_shell != null) {
            try {
                m_shell.exit();
                m_shell = null;
            } catch (Throwable t) {
                // ignore
            }
        }

        // copy the configuration files
        copyConfiguration(configFolder);

        // create a new database first
        setupDatabase();

        // create a shell instance
        m_shell = new CmsShell(getTestDataPath("WEB-INF" + File.separator), null, null, "${user}@${project}>", null);

        // open the test script 
        File script;
        FileInputStream stream = null;
        CmsObject cms = null;

        try {
            // start the shell with the base script
            script = new File(getTestDataPath("scripts/script_base.txt"));
            stream = new FileInputStream(script);
            m_shell.start(stream);

            // add the default folders by script
            script = new File(getTestDataPath("scripts/script_default_folders.txt"));
            stream = new FileInputStream(script);

⌨️ 快捷键说明

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