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());
}
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());
}
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);
}
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;
}
}
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();
}
}
Aggregations