DependencyResolver { public Result resolve(List<PluginDescriptor> plugins) { dependenciesGraph = new DirectedGraph<>(); dependentsGraph = new DirectedGraph<>(); Map<String, PluginDescriptor> pluginByIds = new HashMap<>(); for (PluginDescriptor plugin : plugins) { addPlugin(plugin); pluginByIds.put(plugin.getPluginId(), plugin); } log.debug("Graph: {}", dependenciesGraph); List<String> sortedPlugins = dependenciesGraph.reverseTopologicalSort(); log.debug("Plugins order: {}", sortedPlugins); Result result = new Result(sortedPlugins); resolved = true; if (sortedPlugins != null) { for (String pluginId : sortedPlugins) { if (!pluginByIds.containsKey(pluginId)) { result.addNotFoundDependency(pluginId); } } } for (PluginDescriptor plugin : plugins) { String pluginId = plugin.getPluginId(); String existingVersion = plugin.getVersion(); List<String> dependents = new ArrayList<>(getDependents(pluginId)); while (!dependents.isEmpty()) { String dependentId = dependents.remove(0); PluginDescriptor dependent = pluginByIds.get(dependentId); String requiredVersion = getDependencyVersionSupport(dependent, pluginId); boolean ok = checkDependencyVersion(requiredVersion, existingVersion); if (!ok) { result.addWrongDependencyVersion(new WrongDependencyVersion(pluginId, dependentId, existingVersion, requiredVersion)); } } } return result; } DependencyResolver(VersionManager versionManager); Result resolve(List<PluginDescriptor> plugins); List<String> getDependencies(String pluginId); List<String> getDependents(String pluginId);  }
@Test public void notFoundDependencies() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2, p3"); List<PluginDescriptor> plugins = new ArrayList<>(); plugins.add(pd1); DependencyResolver.Result result = resolver.resolve(plugins); assertFalse(result.getNotFoundDependencies().isEmpty()); assertEquals(result.getNotFoundDependencies(), Arrays.asList("p2", "p3")); }
@Test public void cyclicDependencies() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setPluginVersion("0.0.0") .setDependencies("p2"); PluginDescriptor pd2 = new DefaultPluginDescriptor() .setPluginId("p2") .setPluginVersion("0.0.0") .setDependencies("p3"); PluginDescriptor pd3 = new DefaultPluginDescriptor() .setPluginId("p3") .setPluginVersion("0.0.0") .setDependencies("p1"); List<PluginDescriptor> plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); plugins.add(pd3); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.hasCyclicDependency()); }
@Test public void wrongDependencyVersion() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2@>=1.5.0 & <1.6.0"); PluginDescriptor pd2 = new DefaultPluginDescriptor() .setPluginId("p2") .setPluginVersion("1.4.0"); List<PluginDescriptor> plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); DependencyResolver.Result result = resolver.resolve(plugins); assertFalse(result.getWrongVersionDependencies().isEmpty()); }
@Test public void goodDependencyVersion() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2@2.0.0"); PluginDescriptor pd2 = new DefaultPluginDescriptor() .setPluginId("p2") .setPluginVersion("2.0.0"); List<PluginDescriptor> plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.getWrongVersionDependencies().isEmpty()); }
@Test public void sortedPlugins() { PluginDescriptor pd1 = new DefaultPluginDescriptor() .setPluginId("p1") .setDependencies("p2"); PluginDescriptor pd2 = new DefaultPluginDescriptor() .setPluginId("p2") .setPluginVersion("0.0.0"); List<PluginDescriptor> plugins = new ArrayList<>(); plugins.add(pd1); plugins.add(pd2); DependencyResolver.Result result = resolver.resolve(plugins); assertTrue(result.getNotFoundDependencies().isEmpty()); assertEquals(result.getSortedPlugins(), Arrays.asList("p2", "p1")); }