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

📄 testdefaultpluginmanager.java

📁 模块化您的应用系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.opensymphony.tonic;

import com.opensymphony.tonic.descriptors.MockUnusedModuleDescriptor;
import com.opensymphony.tonic.loaders.SinglePluginLoader;
import com.opensymphony.tonic.loaders.ClassLoadingPluginLoader;
import com.opensymphony.tonic.loaders.classloading.AbstractTestClassLoader;
import com.opensymphony.tonic.mock.MockAnimalModuleDescriptor;
import com.opensymphony.tonic.mock.MockBear;
import com.opensymphony.tonic.mock.MockMineralModuleDescriptor;
import com.opensymphony.tonic.impl.MemoryPluginStateStore;
import com.opensymphony.tonic.impl.PropertiesTonicPropertyManager;
import com.opensymphony.tonic.util.FileUtils;
import junit.framework.TestCase;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;

public class TestDefaultPluginManager extends AbstractTestClassLoader
{
    private ClassLoadingPluginLoader classLoadingPluginLoader;

    protected void tearDown() throws Exception
    {
        if (classLoadingPluginLoader != null)
        {
            classLoadingPluginLoader.shutDown();
        }

        super.tearDown();
    }

    public void testRetrievePlugins() throws PluginParseException, IOException
    {
        List pluginLoaders = new ArrayList();
        pluginLoaders.add(new SinglePluginLoader("test-tonic-plugin.xml"));
        pluginLoaders.add(new SinglePluginLoader("test-disabled-plugin.xml"));

        DefaultModuleDescriptorFactory moduleDescriptorFactory = new DefaultModuleDescriptorFactory();
        moduleDescriptorFactory.addModuleDescriptor("animal", MockAnimalModuleDescriptor.class);
        moduleDescriptorFactory.addModuleDescriptor("mineral", MockMineralModuleDescriptor.class);
        PluginManager manager = new DefaultPluginManager(new MemoryPluginStateStore(), pluginLoaders, moduleDescriptorFactory, new PropertiesTonicPropertyManager());
        manager.init();

        assertEquals(2, manager.getPlugins().size());
        assertEquals(1, manager.getEnabledPlugins().size());
        manager.enablePlugin("test.disabled.plugin");
        assertEquals(2, manager.getEnabledPlugins().size());
    }

    public void testEnabledDisabledRetrieval() throws PluginParseException, IOException
    {
        List pluginLoaders = new ArrayList();
        pluginLoaders.add(new SinglePluginLoader("test-tonic-plugin.xml"));

        DefaultModuleDescriptorFactory moduleDescriptorFactory = new DefaultModuleDescriptorFactory();
        moduleDescriptorFactory.addModuleDescriptor("animal", MockAnimalModuleDescriptor.class);
        moduleDescriptorFactory.addModuleDescriptor("mineral", MockMineralModuleDescriptor.class);
        moduleDescriptorFactory.addModuleDescriptor("bullshit", MockUnusedModuleDescriptor.class);
        PluginManager manager = new DefaultPluginManager(new MemoryPluginStateStore(), pluginLoaders, moduleDescriptorFactory, new PropertiesTonicPropertyManager());
        manager.init();

        // check non existant plugins don't show
        assertNull(manager.getPlugin("bull:shit"));
        assertNull(manager.getEnabledPlugin("bull:shit"));
        assertNull(manager.getPluginModule("bull:shit"));
        assertNull(manager.getEnabledPluginModule("bull:shit"));
        assertTrue(manager.getEnabledModuleDescriptorsByClass(TestCase.class).isEmpty());
        assertTrue(manager.getEnabledModuleDescriptorsByType("bullshit").isEmpty());
        assertTrue(manager.getEnabledModulesByClass(TestCase.class).isEmpty());

        final String pluginKey = "test.tonic.plugin";
        final String moduleKey = pluginKey + ":bear";

        // retrieve everything when enabled
        assertNotNull(manager.getPlugin(pluginKey));
        assertNotNull(manager.getEnabledPlugin(pluginKey));
        assertNotNull(manager.getPluginModule(moduleKey));
        assertNotNull(manager.getEnabledPluginModule(moduleKey));
        assertNull(manager.getEnabledPluginModule(pluginKey + ":shit"));
        assertFalse(manager.getEnabledModuleDescriptorsByClass(MockAnimalModuleDescriptor.class).isEmpty());
        assertFalse(manager.getEnabledModuleDescriptorsByType("animal").isEmpty());
        assertFalse(manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).isEmpty());
        assertEquals(new MockBear(), manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).get(0));

        // now only retrieve via always retrieve methods
        manager.disablePlugin(pluginKey);
        assertNotNull(manager.getPlugin(pluginKey));
        assertNull(manager.getEnabledPlugin(pluginKey));
        assertNotNull(manager.getPluginModule(moduleKey));
        assertNull(manager.getEnabledPluginModule(moduleKey));
        assertTrue(manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).isEmpty());
        assertTrue(manager.getEnabledModuleDescriptorsByClass(MockAnimalModuleDescriptor.class).isEmpty());
        assertTrue(manager.getEnabledModuleDescriptorsByType("animal").isEmpty());

        // now enable again and check back to start
        manager.enablePlugin(pluginKey);
        assertNotNull(manager.getPlugin(pluginKey));
        assertNotNull(manager.getEnabledPlugin(pluginKey));
        assertNotNull(manager.getPluginModule(moduleKey));
        assertNotNull(manager.getEnabledPluginModule(moduleKey));
        assertFalse(manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).isEmpty());
        assertFalse(manager.getEnabledModuleDescriptorsByClass(MockAnimalModuleDescriptor.class).isEmpty());
        assertFalse(manager.getEnabledModuleDescriptorsByType("animal").isEmpty());

        // now let's disable the module, but not the tonic
        manager.disablePluginModule(moduleKey);
        assertNotNull(manager.getPlugin(pluginKey));
        assertNotNull(manager.getEnabledPlugin(pluginKey));
        assertNotNull(manager.getPluginModule(moduleKey));
        assertNull(manager.getEnabledPluginModule(moduleKey));
        assertTrue(manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).isEmpty());
        assertTrue(manager.getEnabledModuleDescriptorsByClass(MockAnimalModuleDescriptor.class).isEmpty());
        assertTrue(manager.getEnabledModuleDescriptorsByType("animal").isEmpty());

        // now enable the module again
        manager.enablePluginModule(moduleKey);
        assertNotNull(manager.getPlugin(pluginKey));
        assertNotNull(manager.getEnabledPlugin(pluginKey));
        assertNotNull(manager.getPluginModule(moduleKey));
        assertNotNull(manager.getEnabledPluginModule(moduleKey));
        assertFalse(manager.getEnabledModulesByClass(com.opensymphony.tonic.mock.MockBear.class).isEmpty());
        assertFalse(manager.getEnabledModuleDescriptorsByClass(MockAnimalModuleDescriptor.class).isEmpty());
        assertFalse(manager.getEnabledModuleDescriptorsByType("animal").isEmpty());

    }

    public void testDuplicatePluginKeysAreBad() throws PluginParseException, IOException
    {
        List pluginLoaders = new ArrayList();
        DefaultModuleDescriptorFactory moduleDescriptorFactory = new DefaultModuleDescriptorFactory();
        moduleDescriptorFactory.addModuleDescriptor("mineral", MockMineralModuleDescriptor.class);
        pluginLoaders.add(new SinglePluginLoader("test-disabled-plugin.xml"));
        pluginLoaders.add(new SinglePluginLoader("test-disabled-plugin.xml"));
        PluginManager manager = new DefaultPluginManager(new MemoryPluginStateStore(), pluginLoaders, moduleDescriptorFactory, new PropertiesTonicPropertyManager());
        try
        {
            manager.init();
            fail("Should have died with duplicate key exception.");
        }
        catch (PluginParseException e)
        {
            assertEquals("Duplicate plugin key found: 'test.disabled.plugin'", e.getMessage());
        }
    }

    public void testGetPluginAndModules() throws PluginParseException, IOException
    {
        List pluginLoaders = new ArrayList();
        pluginLoaders.add(new SinglePluginLoader("test-tonic-plugin.xml"));
        DefaultModuleDescriptorFactory moduleDescriptorFactory = new DefaultModuleDescriptorFactory();
        moduleDescriptorFactory.addModuleDescriptor("animal", MockAnimalModuleDescriptor.class);
        moduleDescriptorFactory.addModuleDescriptor("mineral", MockMineralModuleDescriptor.class);

        PluginManager manager = new DefaultPluginManager(new MemoryPluginStateStore(), pluginLoaders, moduleDescriptorFactory, new PropertiesTonicPropertyManager());
        manager.init();

        Plugin plugin = manager.getPlugin("test.tonic.plugin");
        assertNotNull(plugin);
        assertEquals("Test Plugin", plugin.getName());

        ModuleDescriptor bear = plugin.getModuleDescriptor("bear");
        assertEquals(bear, manager.getPluginModule("test.tonic.plugin:bear"));

⌨️ 快捷键说明

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