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

📄 testmoduleoperations.java

📁 OpenCms 是一个J2EE的产品
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (dependencies.size() != 1) {
            fail("Module '" + moduleName + "' still needs 1 dependency, not " + dependencies.size());
        }

        if (!dependencies.contains(dep2)) {
            fail("Missing required dependency: " + dep2);
        }

        // finally add depdendency with right version
        addDependency(cms, dep2);

        dependencies = OpenCms.getModuleManager().checkDependencies(module, CmsModuleManager.DEPENDENCY_MODE_IMPORT);

        if (dependencies.size() != 0) {
            fail("Module '" + moduleName + "' must have dependencies fullfilled");
        }

        // now try the import again, this time it must work
        OpenCms.getImportExportManager().importData(cms, moduleFile, null, new CmsShellReport(cms.getRequestContext().getLocale()));

        module = OpenCms.getModuleManager().getModule(moduleName);
        // check the module data
        assertEquals(module.getNiceName(), "OpenCms configuration test module 2");
        assertEquals(module.getDescription(), "Test 2 for the OpenCms module import");
        assertEquals(module.getVersion(), new CmsModuleVersion("1.5"));
        assertTrue(module.getActionClass() == null);
        assertEquals(module.getAuthorName(), "Alexander Kandzior");
        assertEquals(module.getAuthorEmail(), "alex@opencms.org");
        assertEquals(module.getExportPoints().size(), 2);
        assertEquals(module.getResources().size(), 1);
        assertEquals(module.getResources().get(0), "/system/modules/org.opencms.test.modules.test2/");
        assertEquals(module.getParameter("param1"), "value1");
        assertEquals(module.getParameter("param2"), "value2");

        // now try to delete the dependencies, this must generate an error
        caughtException = false;
        try {
            OpenCms.getModuleManager().deleteModule(cms, dep1.getName(), false, new CmsShellReport(cms.getRequestContext().getLocale()));
        } catch (CmsConfigurationException e) {
            caughtException = true;
        }
        if (!caughtException) {
            fail("Deleting '" + dep1.getName() + "' must generate an error");
        }
        caughtException = false;
        try {
            OpenCms.getModuleManager().deleteModule(cms, dep2.getName(), false, new CmsShellReport(cms.getRequestContext().getLocale()));
        } catch (CmsConfigurationException e) {
            caughtException = true;
        }
        if (!caughtException) {
            fail("Deleting '" + dep2.getName() + "' must generate an error");
        }

        CmsModuleDependency dep4 = new CmsModuleDependency(
            "org.opencms.test.modules.test2",
            new CmsModuleVersion("1.5"));

        module = OpenCms.getModuleManager().getModule(dep1.getName());
        dependencies = OpenCms.getModuleManager().checkDependencies(module, CmsModuleManager.DEPENDENCY_MODE_DELETE);
        if (!dependencies.contains(dep4)) {
            fail("Dependency not checked: " + dep4);
        }

        module = OpenCms.getModuleManager().getModule(dep2.getName());
        dependencies = OpenCms.getModuleManager().checkDependencies(module, CmsModuleManager.DEPENDENCY_MODE_DELETE);
        if (!dependencies.contains(dep4)) {
            fail("Dependency not checked: " + dep4);
        }

        // delete the imported module
        OpenCms.getModuleManager().deleteModule(cms, moduleName, false, new CmsShellReport(cms.getRequestContext().getLocale()));

        // delete the dependencies (must work now since dependency is removed)
        OpenCms.getModuleManager().deleteModule(cms, dep1.getName(), false, new CmsShellReport(cms.getRequestContext().getLocale()));
        OpenCms.getModuleManager().deleteModule(cms, dep2.getName(), false, new CmsShellReport(cms.getRequestContext().getLocale()));

        // publish the current project
        cms.publishProject();
    }

    /**
     * Tests a module export (and then re-import).<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testModuleExport() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing export an re-import of a module");

        String moduleName = "org.opencms.test.modules.testExport";

        String moduleFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
            "packages/" + moduleName + ".zip");
        File file = new File(moduleFile);
        if (file.exists()) {
            // probably from a old test run that didn't end with a success
            file.delete();
        }

        CmsModule module = new CmsModule(
            moduleName,
            "A test module for export",
            "ModuleGroup",
            null,
            "This is the description",
            new CmsModuleVersion("1.0"),
            "Alexander Kandzior",
            "alex@opencms.org",
            System.currentTimeMillis(),
            null,
            0L,
            null,
            null,
            null,
            null);

        // add the module to the module manager
        OpenCms.getModuleManager().addModule(cms, module);

        // basic check if the module was created correctly
        if (!OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not created!");
        }

        // generate a module export handler
        CmsModuleImportExportHandler moduleExportHandler = new CmsModuleImportExportHandler();
        moduleExportHandler.setFileName(moduleFile);
        moduleExportHandler.setAdditionalResources(new String[0]);
        moduleExportHandler.setModuleName(moduleName.replace('\\', '/'));
        moduleExportHandler.setDescription("Module export of " + moduleExportHandler.getModuleName());

        // export the module
        OpenCms.getImportExportManager().exportData(cms, moduleExportHandler, new CmsShellReport(cms.getRequestContext().getLocale()));

        // now delete the module from the manager
        OpenCms.getModuleManager().deleteModule(cms, moduleName, false, new CmsShellReport(cms.getRequestContext().getLocale()));

        // ensure that the module was deleted
        if (OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not deleted!");
        }

        // now import the module again
        OpenCms.getImportExportManager().importData(cms, moduleFile, null, new CmsShellReport(cms.getRequestContext().getLocale()));

        // basic check if the module was imported correctly
        if (!OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not imported!");
        }

        CmsModule importedModule = OpenCms.getModuleManager().getModule(moduleName);
        // now check the other module data
        if (!module.isIdentical(importedModule)) {
            fail("Impoted module not identical to original module!");
        }

        if (file.exists()) {
            // clean up
            file.delete();
        } else {
            fail("Module export file was not written to expected location!");
        }
    }

    /**
     * Tests a module import.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testModuleImport() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing import of a module");

        String moduleName = "org.opencms.test.modules.test1";

        String moduleFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
            "packages/" + moduleName + ".zip");
        OpenCms.getImportExportManager().importData(cms, moduleFile, null, new CmsShellReport(cms.getRequestContext().getLocale()));

        // basic check if the module was imported correctly
        if (!OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not imported!");
        }

        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
        // now check the other module data
        assertEquals(module.getNiceName(), "OpenCms configuration test module 1");
        assertEquals(module.getDescription(), "Test 1 for the OpenCms module import");
        assertEquals(module.getVersion(), new CmsModuleVersion("1.0"));
        assertTrue(module.getActionClass() == null);
        assertEquals(module.getAuthorName(), "Alexander Kandzior");
        assertEquals(module.getAuthorEmail(), "alex@opencms.org");
        assertEquals(module.getExportPoints().size(), 2);
        assertEquals(module.getResources().size(), 1);
        assertEquals(module.getResources().get(0), "/system/modules/org.opencms.test.modules.test1/");
        assertEquals(module.getParameter("param1"), "value1");
        assertEquals(module.getParameter("param2"), "value2");
    }

    /**
     * Tests a module import with an unknown resource type class.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testModuleImportMissingResTypeClass() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing a module import with an unknown resource type class");

        String moduleName = "org.opencms.test.modules.test4";

        String moduleFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
            "packages/" + moduleName + ".zip");
        
        try {
            // importing this module will generate an expected error in the log
            OpenCmsTestLogAppender.setBreakOnError(false);
            OpenCms.getImportExportManager().importData(cms, moduleFile, null, new CmsShellReport(cms.getRequestContext().getLocale()));
        } finally {
            OpenCmsTestLogAppender.setBreakOnError(true);            
        }

        // basic check if the module was imported correctly
        if (!OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not imported!");
        }

        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
        // now check the other module data
        assertEquals(module.getNiceName(), "OpenCms configuration test module 4");
        assertEquals(module.getDescription(), "Test 4 for the OpenCms module import: Missing classes");
        assertEquals(module.getVersion(), new CmsModuleVersion("1.0"));
        assertEquals(module.getActionClass(), "org.opencms.missing.moduleClass");
        assertEquals(module.getAuthorName(), "Alexander Kandzior");
        assertEquals(module.getAuthorEmail(), "alex@opencms.org");
        assertEquals(module.getExportPoints().size(), 0);
        assertEquals(module.getResources().size(), 0);
        assertEquals(module.getParameter("param1"), "value1");
        assertEquals(module.getParameter("param2"), "value2");
                
        // check for the new resource type
        I_CmsResourceType missingType = OpenCms.getResourceManager().getResourceType("missing");
        assertNotNull(missingType);
        // check configured and actual class name
        assertEquals(missingType.getClassName(), "org.opencms.missing.resourceTypeClass");
        assertEquals(missingType.getTypeId(), 88);
        assertEquals(missingType.getLoaderId(), CmsDumpLoader.RESOURCE_LOADER_ID);
        assertEquals(missingType.getClass().getName(), CmsResourceTypeUnknown.class.getName());
    }
    
    /**
     * Tests a module import of an old (OpenCms 5.0) style module.<p>
     * 
     * @throws Throwable if something goes wrong
     */
    public void testOldModuleImport() throws Throwable {

        CmsObject cms = getCmsObject();
        echo("Testing import of an old OpenCms 5.0 module");

        String moduleName = "org.opencms.test.modules.testOld";

        String moduleFile = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
            "packages/" + moduleName + ".zip");
        OpenCms.getImportExportManager().importData(cms, moduleFile, null, new CmsShellReport(cms.getRequestContext().getLocale()));

        // basic check if the module was imported correctly
        if (!OpenCms.getModuleManager().hasModule(moduleName)) {
            fail("Module '" + moduleName + "' was not imported!");
        }

        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
        // now check the other module data
        assertEquals(module.getNiceName(), "OpenCms v5.0 test module");
        assertEquals(
            module.getDescription().trim(),
            "This is a module in the OpenCms v5.0.x style.\n\t\tThe XML format for modules has changed in OpenCms 6.0.");
        assertEquals(module.getVersion(), new CmsModuleVersion("2.05"));
        assertTrue(module.getActionClass() == null);
        assertEquals(module.getAuthorName(), "Alexander Kandzior");
        assertEquals(module.getAuthorEmail(), "alex@opencms.org");
        // check if "additionalresources" where converted to module resources        
        assertTrue(module.getResources().size() == 2);
        assertEquals(module.getResources().get(0), "/system/modules/org.opencms.test.modules.testOld/");
        assertEquals(module.getResources().get(1), "/alkacon-documentation/documentation-flexcache/");
    }

    /**
     * Adds a module dependency for the tests.<p>
     * 
     * @param cms the current OpenCms context
     * @param dep the dependency to check
     * 
     * @throws CmsConfigurationException in case something goes wrong
     * @throws CmsSecurityException in case something goes wrong
     */
    private void addDependency(CmsObject cms, CmsModuleDependency dep)
    throws CmsSecurityException, CmsConfigurationException {

        if (OpenCms.getModuleManager().hasModule(dep.getName())) {
            // remove other version of dependency if it exists
            OpenCms.getModuleManager().deleteModule(cms, dep.getName(), true, new CmsShellReport(cms.getRequestContext().getLocale()));
        }

        CmsModule module = new CmsModule(
            dep.getName(),
            "A test module dependency",
            "ModuleGroup",
            null,
            null,
            dep.getVersion(),
            "Alexander Kandzior",
            "alex@opencms.org",
            System.currentTimeMillis(),
            null,
            0L,
            null,
            null,
            null,
            null);

        // add the module to the module manager
        OpenCms.getModuleManager().addModule(cms, module);
    }
}

⌨️ 快捷键说明

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