DependenciesValidator implements MavenValidator { @Override public void validate(final MavenEnvironment env) throws ValidationException { final Collection<String> excludes = env.excludes("dependencies"); if (!env.outdir().exists() || "pom".equals(env.project().getPackaging()) || excludes.contains(".*") ) { Logger.info(this, "No dependency analysis in this project"); return; } final Collection<String> unused = Collections2.filter( DependenciesValidator.unused(env), Predicates.not(new DependenciesValidator.ExcludePredicate(excludes)) ); if (!unused.isEmpty()) { Logger.warn( this, "Unused declared dependencies found:%s%s", DependenciesValidator.SEP, StringUtils.join(unused, DependenciesValidator.SEP) ); } final Collection<String> used = Collections2.filter( DependenciesValidator.used(env), Predicates.not(new DependenciesValidator.ExcludePredicate(excludes)) ); if (!used.isEmpty()) { Logger.warn( this, "Used undeclared dependencies found:%s%s", DependenciesValidator.SEP, StringUtils.join(used, DependenciesValidator.SEP) ); } final int failures = used.size() + unused.size(); if (failures > 0) { throw new ValidationException( "%d dependency problem(s) found", failures ); } Logger.info(this, "No dependency problems found"); }  @Override void validate(final MavenEnvironment env);  }
@Test public void passesIfNoDependencyProblemsFound() throws Exception { final ProjectDependencyAnalysis analysis = Mockito.mock(ProjectDependencyAnalysis.class); final ProjectDependencyAnalyzer analyzer = this.analyzer(analysis); final MavenEnvironment env = new MavenEnvironmentMocker().inPlexus( DependenciesValidatorTest.ROLE, DependenciesValidatorTest.HINT, analyzer ).mock(); new DependenciesValidator().validate(env); }
@Test(expected = ValidationException.class) public void catchesDependencyProblemsAndThrowsException() throws Exception { final ProjectDependencyAnalysis analysis = Mockito.mock(ProjectDependencyAnalysis.class); final Set<Artifact> unused = new HashSet<>(); unused.add(Mockito.mock(Artifact.class)); Mockito.doReturn(unused).when(analysis).getUsedUndeclaredArtifacts(); final ProjectDependencyAnalyzer analyzer = this.analyzer(analysis); final MavenEnvironment env = new MavenEnvironmentMocker().inPlexus( DependenciesValidatorTest.ROLE, DependenciesValidatorTest.HINT, analyzer ).mock(); new DependenciesValidator().validate(env); }
@Test public void ignoresRuntimeScope() throws Exception { final ProjectDependencyAnalysis analysis = Mockito.mock(ProjectDependencyAnalysis.class); final Artifact artifact = Mockito.mock(Artifact.class); final Set<Artifact> unused = new HashSet<>(); unused.add(artifact); Mockito.doReturn(unused).when(analysis).getUnusedDeclaredArtifacts(); Mockito.doReturn(Artifact.SCOPE_RUNTIME).when(artifact).getScope(); final ProjectDependencyAnalyzer analyzer = this.analyzer(analysis); final MavenEnvironment env = new MavenEnvironmentMocker().inPlexus( DependenciesValidatorTest.ROLE, DependenciesValidatorTest.HINT, analyzer ).mock(); new DependenciesValidator().validate(env); }
@Test public void excludesUsedUndeclaredDependencies() throws Exception { final ProjectDependencyAnalysis analysis = Mockito.mock(ProjectDependencyAnalysis.class); final Set<Artifact> used = new HashSet<>(); final ArtifactStub artifact = new ArtifactStub(); artifact.setGroupId("group"); artifact.setArtifactId("artifact"); artifact.setScope(DependenciesValidatorTest.SCOPE); artifact.setVersion("2.3.4"); artifact.setType(DependenciesValidatorTest.TYPE); used.add(artifact); Mockito.doReturn(used).when(analysis).getUsedUndeclaredArtifacts(); final ProjectDependencyAnalyzer analyzer = this.analyzer(analysis); final MavenEnvironment env = new MavenEnvironmentMocker().inPlexus( DependenciesValidatorTest.ROLE, DependenciesValidatorTest.HINT, analyzer ).mock(); new DependenciesValidator().validate( new MavenEnvironment.Wrap( new Environment.Mock().withExcludes( Joiner.on(':').join( artifact.getGroupId(), artifact.getArtifactId() ) ), env ) ); }
@Test public void excludesUnusedDeclaredDependencies() throws Exception { final ProjectDependencyAnalysis analysis = Mockito.mock(ProjectDependencyAnalysis.class); final Set<Artifact> unused = new HashSet<>(); final ArtifactStub artifact = new ArtifactStub(); artifact.setGroupId("othergroup"); artifact.setArtifactId("otherartifact"); artifact.setScope(DependenciesValidatorTest.SCOPE); artifact.setVersion("1.2.3"); artifact.setType(DependenciesValidatorTest.TYPE); unused.add(artifact); Mockito.doReturn(unused).when(analysis).getUnusedDeclaredArtifacts(); final ProjectDependencyAnalyzer analyzer = this.analyzer(analysis); final MavenEnvironment env = new MavenEnvironmentMocker().inPlexus( DependenciesValidatorTest.ROLE, DependenciesValidatorTest.HINT, analyzer ).mock(); new DependenciesValidator().validate( new MavenEnvironment.Wrap( new Environment.Mock().withExcludes( Joiner.on(':').join( artifact.getGroupId(), artifact.getArtifactId() ) ), env ) ); }