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

📄 teststateaware.java

📁 模块化您的应用系统
💻 JAVA
字号:
package com.opensymphony.tonic;

import com.opensymphony.tonic.loaders.PluginLoader;
import com.opensymphony.tonic.impl.MemoryPluginStateStore;
import com.opensymphony.tonic.impl.StaticPlugin;
import com.opensymphony.tonic.impl.PropertiesTonicPropertyManager;
import com.opensymphony.tonic.mock.MockAnimalModuleDescriptor;
import com.opensymphony.tonic.descriptors.AbstractModuleDescriptor;

import java.util.ArrayList;
import java.util.Collection;

import junit.framework.TestCase;

/**
 * Tests that the tonic manager properly notifies StateAware tonic modules of state
 * transitions.
 */
public class TestStateAware extends TestCase
{
    private StateAwareDescriptor enabledByDefaultModule;
    private StateAwareDescriptor disabledModule;
    private PluginManager manager;
    private Plugin plugin1;

    interface Combination extends StateAware, ModuleDescriptor{};

    protected void setUp() throws Exception
    {
        enabledByDefaultModule = makeMockModule(Combination.class, "key1", "enabling", true);
        disabledModule = makeMockModule(Combination.class, "key1", "disabled", false);

        plugin1 = new StaticPlugin();
        plugin1.setKey("key1");
        plugin1.setEnabled(true);

        PluginLoader pluginLoader = setupPluginLoader(plugin1);
        ArrayList pluginLoaders = new ArrayList();
        pluginLoaders.add(pluginLoader);

        DefaultModuleDescriptorFactory moduleDescriptorFactory = new DefaultModuleDescriptorFactory();
        moduleDescriptorFactory.addModuleDescriptor("animal", MockAnimalModuleDescriptor.class);

        manager = new DefaultPluginManager(new MemoryPluginStateStore(), pluginLoaders, moduleDescriptorFactory, new PropertiesTonicPropertyManager());
    }

    /**
     * Any StateAware tonic module that is active when the tonic manager is initialised should
     * recieve an enabled message
     */
    public void testStateAwareOnInit() throws PluginParseException
    {
        plugin1.addModuleDescriptor(enabledByDefaultModule);
        plugin1.addModuleDescriptor(disabledModule);

        manager.init();
        assertEquals(StateAwareDescriptor.STATE_ENABLED, enabledByDefaultModule.getState());
        assertEquals(StateAwareDescriptor.STATE_NONE, disabledModule.getState());
    }

    /**
     * Any StateAware plugin moudle that is explicitly enabled or disabled through the tonic manager
     * should receive the appropriate message
     */
    public void testStateAwareOnPluginModule() throws PluginParseException
    {
        plugin1.addModuleDescriptor(disabledModule);
        manager.init();

        manager.enablePluginModule(disabledModule.getCompleteKey());
        assertEquals(StateAwareDescriptor.STATE_ENABLED, disabledModule.getState());

        manager.disablePluginModule(disabledModule.getCompleteKey());
        assertEquals(StateAwareDescriptor.STATE_DISABLED, disabledModule.getState());
    }

    /**
     * If a tonic is disabled, any modules that are currently enabled should be sent the disabled
     * message
     */
    public void testStateAwareOnPluginEnableDisable() throws PluginParseException
    {
        plugin1.addModuleDescriptor(enabledByDefaultModule);
        plugin1.addModuleDescriptor(disabledModule);

        manager.init();
        assertEquals(StateAwareDescriptor.STATE_ENABLED, enabledByDefaultModule.getState());
        assertEquals(StateAwareDescriptor.STATE_NONE, disabledModule.getState());

        manager.enablePlugin(plugin1.getKey());
        assertEquals(StateAwareDescriptor.STATE_ENABLED, enabledByDefaultModule.getState());
        assertEquals(StateAwareDescriptor.STATE_ENABLED, disabledModule.getState());

        manager.disablePlugin(plugin1.getKey());
        assertEquals(StateAwareDescriptor.STATE_DISABLED, enabledByDefaultModule.getState());
        assertEquals(StateAwareDescriptor.STATE_DISABLED, disabledModule.getState());
    }

    private StateAwareDescriptor makeMockModule(Class moduleClass, String pluginKey, String moduleKey, boolean enabledByDefault)
    {
        StateAwareDescriptor descriptor = new StateAwareDescriptor(pluginKey, moduleKey, enabledByDefault);
        return descriptor;
    }

    private PluginLoader setupPluginLoader(final Plugin plugin1)
    {
        PluginLoader pluginLoader = new PluginLoader() { //TODO: should this loader support removal and addition?

            public Collection loadAllPlugins(ModuleDescriptorFactory moduleDescriptorFactory) throws PluginParseException
            {
                ArrayList list = new ArrayList();
                list.add(plugin1);
                return list;
            }

            public boolean supportsAddition()
            {
                return false;
            }

            public boolean supportsRemoval()
            {
                return false;
            }

            public Collection removeMissingPlugins()
            {
                return null;
            }

            public Collection addFoundPlugins(ModuleDescriptorFactory moduleDescriptorFactory)
            {
                return null;
            }

            public void removePlugin(Plugin plugin) throws PluginException
            {
                throw new PluginException("This PluginLoader does not support removal");
            }
        };
        return pluginLoader;
    }
}

class StateAwareDescriptor extends AbstractModuleDescriptor implements StateAware
{
    public static final int STATE_NONE = 0;
    public static final int STATE_ENABLED = 1;
    public static final int STATE_DISABLED = 2;
    private int state = STATE_NONE;
    private String moduleKey;
    private String pluginKey;
    private boolean enabledByDefault;

    public StateAwareDescriptor(String pluginKey, String moduleKey, boolean enabledByDefault)
    {
        this.moduleKey = moduleKey;
        this.pluginKey = pluginKey;
        this.enabledByDefault = enabledByDefault;

        if (enabledByDefault)
        {
            state = STATE_ENABLED;
        }
    }

    public String getKey()
    {
        return moduleKey;
    }

    public String getCompleteKey()
    {
        return pluginKey + ":" + moduleKey;
    }

    public boolean isEnabledByDefault()
    {
        return enabledByDefault;
    }

    public int getState()
    {
        return state;
    }

    public Object getModule()
    {
        return null;
    }

    public void enabled()
    {
        this.state = STATE_ENABLED;
    }

    public void disabled()
    {
        this.state = STATE_DISABLED;
    }
}

⌨️ 快捷键说明

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