Search in sources :

Example 16 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class PluginManagerTest method shouldNotUninstallSystemPlugin.

@Test
public void shouldNotUninstallSystemPlugin() throws Exception {
    // given
    given(plugin.hasState(PluginState.TEMPORARY)).willReturn(false);
    given(plugin.isSystemPlugin()).willReturn(true);
    PluginDependencyResult pluginDependencyResult = PluginDependencyResultImpl.satisfiedDependencies();
    given(pluginDependencyManager.getDependenciesToDisable(Mockito.eq(newArrayList((Plugin) plugin)), Mockito.any(SimplePluginStatusResolver.class))).willReturn(pluginDependencyResult);
    // when
    PluginOperationResult pluginOperationResult = pluginManager.uninstallPlugin("pluginname");
    // then
    verify(plugin, never()).changeStateTo(PluginState.DISABLED);
    verify(pluginDao, never()).delete(plugin);
    verify(pluginAccessor, never()).removePlugin(plugin);
    assertFalse(pluginOperationResult.isSuccess());
    assertEquals(PluginOperationStatus.SYSTEM_PLUGIN_UNINSTALLING, pluginOperationResult.getStatus());
    assertEquals(0, pluginOperationResult.getPluginDependencyResult().getDependenciesToUninstall().size());
}
Also used : PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) PluginOperationResult(com.qcadoo.plugin.api.PluginOperationResult) SimplePluginStatusResolver(com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver) Test(org.junit.Test)

Example 17 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class PluginManagerTest method shouldUninstallMultiplePlugins.

@Test
public void shouldUninstallMultiplePlugins() throws Exception {
    // given
    given(plugin.hasState(PluginState.TEMPORARY)).willReturn(false);
    given(plugin.hasState(PluginState.ENABLED)).willReturn(true);
    given(plugin.getFilename()).willReturn("filename");
    given(anotherPlugin.hasState(PluginState.TEMPORARY)).willReturn(true);
    given(anotherPlugin.hasState(PluginState.ENABLED)).willReturn(false);
    given(anotherPlugin.getFilename()).willReturn("anotherFilename");
    InternalPlugin nextPlugin = mock(InternalPlugin.class, "nextPlugin");
    given(nextPlugin.hasState(PluginState.TEMPORARY)).willReturn(false);
    given(nextPlugin.hasState(PluginState.ENABLED)).willReturn(true);
    given(nextPlugin.getFilename()).willReturn("nextPluginFilename");
    given(pluginAccessor.getPlugin("nextPluginname")).willReturn(nextPlugin);
    PluginDependencyResult pluginDependencyResult = PluginDependencyResultImpl.satisfiedDependencies();
    given(pluginDependencyManager.getDependenciesToUninstall(Mockito.eq(newArrayList((Plugin) plugin, (Plugin) anotherPlugin, (Plugin) nextPlugin)), Mockito.any(SimplePluginStatusResolver.class))).willReturn(pluginDependencyResult);
    given(pluginDependencyManager.sortPluginsInDependencyOrder(newArrayList((Plugin) plugin, (Plugin) anotherPlugin, (Plugin) nextPlugin))).willReturn(newArrayList((Plugin) nextPlugin, (Plugin) plugin, (Plugin) anotherPlugin));
    // when
    PluginOperationResult pluginOperationResult = pluginManager.uninstallPlugin("pluginname", "anotherPluginname", "nextPluginname");
    // then
    InOrder inOrder = inOrder(plugin, nextPlugin);
    inOrder.verify(plugin).changeStateTo(PluginState.DISABLED);
    inOrder.verify(nextPlugin).changeStateTo(PluginState.DISABLED);
    verify(anotherPlugin, never()).changeStateTo(PluginState.DISABLED);
    verify(pluginDao).delete(plugin);
    verify(pluginAccessor).removePlugin(plugin);
    verify(pluginDao).delete(nextPlugin);
    verify(pluginAccessor).removePlugin(nextPlugin);
    verify(pluginDao).delete(anotherPlugin);
    verify(pluginAccessor).removePlugin(anotherPlugin);
    verify(pluginFileManager).uninstallPlugin("filename", "anotherFilename", "nextPluginFilename");
    assertTrue(pluginOperationResult.isSuccess());
    assertEquals(PluginOperationStatus.SUCCESS_WITH_RESTART, pluginOperationResult.getStatus());
    assertEquals(0, pluginOperationResult.getPluginDependencyResult().getDependenciesToUninstall().size());
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) InOrder(org.mockito.InOrder) PluginOperationResult(com.qcadoo.plugin.api.PluginOperationResult) SimplePluginStatusResolver(com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) Test(org.junit.Test)

Example 18 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginDependencyManager method getDependenciesToEnable.

private PluginDependencyResult getDependenciesToEnable(final List<Plugin> plugins, final Set<String> marked, final DirectedGraph<String, DefaultEdge> graph, final PluginStatusResolver pluginStatusResolver) {
    Set<PluginDependencyInformation> disabledDependencies = new HashSet<PluginDependencyInformation>();
    Set<PluginDependencyInformation> unsatisfiedDependencies = new HashSet<PluginDependencyInformation>();
    Set<String> argumentPluginIdentifiersSet = getArgumentIdentifiersSet(plugins);
    for (Plugin plugin : plugins) {
        if (marked.contains(plugin.getIdentifier())) {
            continue;
        }
        marked.add(plugin.getIdentifier());
        graph.addVertex(plugin.getIdentifier());
        for (PluginDependencyInformation dependencyInfo : plugin.getRequiredPlugins()) {
            Plugin requiredPlugin = pluginAccessor.getPlugin(dependencyInfo.getIdentifier());
            if (requiredPlugin == null) {
                if (!argumentPluginIdentifiersSet.contains(dependencyInfo.getIdentifier())) {
                    unsatisfiedDependencies.add(dependencyInfo);
                }
                continue;
            }
            graph.addVertex(dependencyInfo.getIdentifier());
            graph.addEdge(plugin.getIdentifier(), dependencyInfo.getIdentifier());
            if (!isPluginDisabled(requiredPlugin, pluginStatusResolver)) {
                continue;
            }
            if (dependencyInfo.contains(requiredPlugin.getVersion())) {
                disabledDependencies.add(dependencyInfo);
                PluginDependencyResult nextLevelDependencioesResult = getDependenciesToEnable(Collections.singletonList(requiredPlugin), marked, graph, pluginStatusResolver);
                if (!nextLevelDependencioesResult.getUnsatisfiedDependencies().isEmpty() || nextLevelDependencioesResult.isCyclic()) {
                    return nextLevelDependencioesResult;
                }
                disabledDependencies.addAll(nextLevelDependencioesResult.getDependenciesToEnable());
            } else {
                unsatisfiedDependencies.add(dependencyInfo);
            }
        }
    }
    if (!unsatisfiedDependencies.isEmpty()) {
        return PluginDependencyResultImpl.unsatisfiedDependencies(unsatisfiedDependencies);
    }
    if (disabledDependencies.isEmpty()) {
        return PluginDependencyResultImpl.satisfiedDependencies();
    }
    Iterator<PluginDependencyInformation> dependencyInfoIterator = disabledDependencies.iterator();
    while (dependencyInfoIterator.hasNext()) {
        if (argumentPluginIdentifiersSet.contains(dependencyInfoIterator.next().getIdentifier())) {
            dependencyInfoIterator.remove();
        }
    }
    return PluginDependencyResultImpl.dependenciesToEnable(disabledDependencies);
}
Also used : PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) PluginDependencyInformation(com.qcadoo.plugin.api.PluginDependencyInformation) HashSet(java.util.HashSet) Plugin(com.qcadoo.plugin.api.Plugin)

Example 19 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginDependencyManager method getDependenciesToEnable.

@Override
public PluginDependencyResult getDependenciesToEnable(final List<Plugin> plugins, final PluginStatusResolver pluginStatusResolver) {
    Set<String> marked = new HashSet<String>();
    DirectedGraph<String, DefaultEdge> graph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
    PluginDependencyResult pluginDependencyResult = getDependenciesToEnable(plugins, marked, graph, pluginStatusResolver);
    CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(graph);
    if (cycleDetector.detectCycles()) {
        LOG.warn("Dependency cycle detected: " + cycleDetector.findCycles());
        return PluginDependencyResultImpl.cyclicDependencies();
    } else {
        return pluginDependencyResult;
    }
}
Also used : PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) DefaultDirectedGraph(org.jgrapht.graph.DefaultDirectedGraph) DefaultEdge(org.jgrapht.graph.DefaultEdge) HashSet(java.util.HashSet) CycleDetector(org.jgrapht.alg.CycleDetector)

Example 20 with PluginDependencyResult

use of com.qcadoo.plugin.api.PluginDependencyResult in project qcadoo by qcadoo.

the class DefaultPluginManager method enablePlugin.

@Override
public PluginOperationResult enablePlugin(final String... keys) {
    List<Plugin> plugins = new ArrayList<Plugin>();
    for (String key : keys) {
        Plugin plugin = pluginAccessor.getPlugin(key);
        if (!plugin.hasState(PluginState.ENABLED)) {
            plugins.add(plugin);
        }
    }
    if (plugins.isEmpty()) {
        return PluginOperationResultImpl.success();
    }
    PluginDependencyResult pluginDependencyResult = pluginDependencyManager.getDependenciesToEnable(plugins, pluginStatusResolver);
    if (pluginDependencyResult.isCyclic()) {
        return PluginOperationResultImpl.dependenciesCyclesExists();
    }
    if (!pluginDependencyResult.isDependenciesSatisfied()) {
        if (!pluginDependencyResult.getUnsatisfiedDependencies().isEmpty()) {
            return PluginOperationResultImpl.unsatisfiedDependencies(pluginDependencyResult);
        }
        if (!pluginDependencyResult.getDependenciesToEnable().isEmpty()) {
            return PluginOperationResultImpl.dependenciesToEnable(pluginDependencyResult);
        }
    }
    boolean shouldRestart = false;
    List<String> fileNames = new ArrayList<String>();
    for (Plugin plugin : plugins) {
        if (plugin.hasState(PluginState.TEMPORARY)) {
            fileNames.add(plugin.getFilename());
        }
    }
    if (!fileNames.isEmpty()) {
        if (!pluginFileManager.installPlugin(fileNames.toArray(new String[fileNames.size()]))) {
            return PluginOperationResultImpl.cannotInstallPlugin();
        }
        shouldRestart = true;
    }
    plugins = pluginDependencyManager.sortPluginsInDependencyOrder(plugins);
    for (Plugin plugin : plugins) {
        if (plugin.hasState(PluginState.TEMPORARY)) {
            ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLING);
        } else {
            try {
                ((InternalPlugin) plugin).changeStateTo(PluginState.ENABLED);
            } catch (Exception e) {
                LOG.error(e.getMessage());
                ((InternalPlugin) plugin).changeStateTo(PluginState.DISABLED);
                return PluginOperationResultImpl.pluginEnablingEncounteredErrors();
            }
        }
        pluginDao.save(plugin);
        pluginAccessor.savePlugin(plugin);
    }
    if (shouldRestart) {
        return PluginOperationResultImpl.successWithRestart();
    } else {
        return PluginOperationResultImpl.success();
    }
}
Also used : InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin) PluginDependencyResult(com.qcadoo.plugin.api.PluginDependencyResult) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) PluginException(com.qcadoo.plugin.internal.PluginException) QcadooPluginPlugin(com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin) Plugin(com.qcadoo.plugin.api.Plugin) InternalPlugin(com.qcadoo.plugin.internal.api.InternalPlugin)

Aggregations

PluginDependencyResult (com.qcadoo.plugin.api.PluginDependencyResult)46 Test (org.junit.Test)39 Plugin (com.qcadoo.plugin.api.Plugin)38 InternalPlugin (com.qcadoo.plugin.internal.api.InternalPlugin)36 PluginDependencyInformation (com.qcadoo.plugin.api.PluginDependencyInformation)28 HashSet (java.util.HashSet)23 PluginOperationResult (com.qcadoo.plugin.api.PluginOperationResult)15 SimplePluginStatusResolver (com.qcadoo.plugin.internal.dependencymanager.SimplePluginStatusResolver)15 ArrayList (java.util.ArrayList)11 VersionOfDependency (com.qcadoo.plugin.api.VersionOfDependency)7 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)4 QcadooPluginPlugin (com.qcadoo.model.beans.qcadooPlugin.QcadooPluginPlugin)4 InOrder (org.mockito.InOrder)3 Version (com.qcadoo.plugin.api.Version)2 PluginException (com.qcadoo.plugin.internal.PluginException)2 File (java.io.File)1 LinkedList (java.util.LinkedList)1 CycleDetector (org.jgrapht.alg.CycleDetector)1 DefaultDirectedGraph (org.jgrapht.graph.DefaultDirectedGraph)1 DefaultEdge (org.jgrapht.graph.DefaultEdge)1