View Javadoc
1   package org.apache.maven.shared.dependency.analyzer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.DefaultArtifact;
28  import org.apache.maven.artifact.versioning.VersionRange;
29  import org.junit.Test;
30  
31  import static org.assertj.core.api.Assertions.assertThat;
32  
33  /**
34   * Tests <code>ProjectDependencyAnalysis</code>.
35   *
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   * @see ProjectDependencyAnalysis
38   */
39  public class ProjectDependencyAnalysisTest
40  {
41      @Test
42      public void testConstructor()
43      {
44          Set<Artifact> usedDeclaredArtifacts = new HashSet<>();
45          Set<Artifact> usedUndeclaredArtifacts = new HashSet<>();
46          Set<Artifact> unusedDeclaredArtifacts = new HashSet<>();
47          Set<Artifact> testArtifactsWithNonTestScope = new HashSet<>();
48  
49          ProjectDependencyAnalysis analysis =
50              new ProjectDependencyAnalysis( usedDeclaredArtifacts, usedUndeclaredArtifacts, unusedDeclaredArtifacts,
51                  testArtifactsWithNonTestScope );
52  
53          assertThat( analysis.getUsedDeclaredArtifacts() ).isEqualTo( usedDeclaredArtifacts );
54          assertThat( analysis.getUsedUndeclaredArtifacts() ).isEqualTo( usedUndeclaredArtifacts );
55          assertThat( analysis.getUnusedDeclaredArtifacts() ).isEqualTo( unusedDeclaredArtifacts );
56      }
57  
58      @Test
59      public void ignoreNonCompileShouldFilterOnlyUnusedDeclare()
60      {
61          Artifact artifactCompile = aTestArtifact( "test1", Artifact.SCOPE_COMPILE );
62          Artifact artifactProvided = aTestArtifact( "test2", Artifact.SCOPE_PROVIDED );
63          Artifact artifactTest = aTestArtifact( "test3", Artifact.SCOPE_TEST );
64  
65          ProjectDependencyAnalysis analysis = new ProjectDependencyAnalysis(
66              asSet( artifactCompile, artifactProvided, artifactTest ),
67              asSet( artifactCompile, artifactProvided, artifactTest ),
68              asSet( artifactCompile, artifactProvided, artifactTest ),
69              asSet( artifactCompile, artifactProvided, artifactTest ) );
70  
71          ProjectDependencyAnalysis compileOnlyAnalysis = analysis.ignoreNonCompile();
72  
73          assertThat( compileOnlyAnalysis.getUsedDeclaredArtifacts() ).hasSize( 3 );
74          assertThat( compileOnlyAnalysis.getUsedUndeclaredArtifacts() ).hasSize( 3 );
75  
76          assertThat( compileOnlyAnalysis.getUnusedDeclaredArtifacts() )
77              .hasSize( 1 )
78              .allSatisfy( a -> assertThat( a.getScope() ).isEqualTo( Artifact.SCOPE_COMPILE ) );
79  
80          assertThat( compileOnlyAnalysis.getTestArtifactsWithNonTestScope() )
81              .hasSize( 3 );
82      }
83  
84      private <T> Set<T> asSet( T... items )
85      {
86          return new HashSet<>( Arrays.asList( items ) );
87      }
88  
89      private Artifact aTestArtifact( String artifactId, String scope )
90      {
91          return new DefaultArtifact( "groupId", artifactId, VersionRange.createFromVersion( "1.0" ),
92              scope, "jar", "", null );
93      }
94  }