DefaultDependencyChecker implements DependencyChecker { @Override public void check(PluginsAware pluginRegistry, PluginMetadata pluginMetadata) throws PluginException { List<Dependency> missingRequiredDependencies = new ArrayList<>(); requiredDependencies: for (Dependency requiredDependency : pluginMetadata.getRequiredDependencies()) { Optional<PluginMetadata> requiredDependencyCandidate = pluginRegistry .getPlugin(requiredDependency.getName()); if (!requiredDependencyCandidate.isPresent()) { missingRequiredDependencies.add(requiredDependency); } else { switch (compareVersions(requiredDependency, requiredDependencyCandidate.get())) { case EQUAL: case IN_RANGE: continue requiredDependencies; default: missingRequiredDependencies.add(requiredDependency); } } } List<Dependency> presentConflictingDependencies = new ArrayList<>(); for (Dependency conflictingDependency : pluginMetadata.getConflictingDependencies()) { Optional<PluginMetadata> conflictingDependencyCandidate = pluginRegistry .getPlugin(conflictingDependency.getName()); if (conflictingDependencyCandidate.isPresent()) { switch (compareVersions(conflictingDependency, conflictingDependencyCandidate.get())) { case EQUAL: case IN_RANGE: presentConflictingDependencies.add(conflictingDependency); } } } if (!missingRequiredDependencies.isEmpty() || !presentConflictingDependencies.isEmpty()) { throw new PluginException( String.format( "Dependency issue: %d missing dependencies, %s conflicting dependencies", missingRequiredDependencies.size(), presentConflictingDependencies.size() ) ).withPlugin(pluginMetadata) .withDependencyProblem( new DependencyProblemContainer(missingRequiredDependencies, presentConflictingDependencies) ); } }  @Override void check(PluginsAware pluginRegistry, PluginMetadata pluginMetadata);  }
@Test public void testRequiredDependencyMissing() { PluginsAware pluginRegistry = mock(PluginsAware.class); doReturn(Optional.empty()).when(pluginRegistry).getPlugin(eq(MISSING_NAME)); doReturn(getPluginIdentityMetadata(FIXED_VERSION_NAME)) .when(pluginRegistry).getPlugin(eq(FIXED_VERSION_NAME)); doReturn(getPluginIdentityMetadata(VERSION_RANGE_NAME)) .when(pluginRegistry).getPlugin(eq(VERSION_RANGE_NAME)); List<Dependency> missingDependencies = new ArrayList<>(); try { new DefaultDependencyChecker().check(pluginRegistry, getPluginDependenciesMetadata(new Dependency[]{ MISSING_DEPENDENCY, DEPENDENCY_WITH_FIXED_VERSION, DEPENDENCY_WITH_VERSION_RANGE }, new Dependency[0])); } catch (PluginException e) { assertTrue(e.getDependencyProblem().isPresent()); assertTrue(e.getPluginMetadata().isPresent()); missingDependencies.addAll(e.getDependencyProblem().get().getMissingDependencies()); } assertThat(missingDependencies, hasSize(2)); assertThat(missingDependencies, contains(MISSING_DEPENDENCY, DEPENDENCY_WITH_FIXED_VERSION)); }
@Test public void testConflictingDependencyPresent() { PluginsAware pluginRegistry = mock(PluginsAware.class); doReturn(getPluginIdentityMetadata(FIXED_VERSION_NAME)) .when(pluginRegistry).getPlugin(eq(FIXED_VERSION_NAME)); doReturn(getPluginIdentityMetadata(VERSION_RANGE_NAME)) .when(pluginRegistry).getPlugin(eq(VERSION_RANGE_NAME)); List<Dependency> conflictingDependencies = new ArrayList<>(); try { new DefaultDependencyChecker().check(pluginRegistry, getPluginDependenciesMetadata(new Dependency[0], new Dependency[]{ DEPENDENCY_WITH_FIXED_VERSION, DEPENDENCY_WITH_VERSION_RANGE })); } catch (PluginException e) { assertTrue(e.getDependencyProblem().isPresent()); conflictingDependencies.addAll(e.getDependencyProblem().get().getConflictingDependencies()); } assertThat(conflictingDependencies, hasSize(1)); assertThat(conflictingDependencies, contains(DEPENDENCY_WITH_VERSION_RANGE)); }