View Javadoc
1   package org.apache.maven.plugins.dependency.analyze;
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 static org.junit.Assert.assertNotEquals;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.util.ArrayList;
27  import java.util.HashMap;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Set;
31  
32  import junit.framework.TestCase;
33  import org.apache.maven.artifact.Artifact;
34  import org.apache.maven.model.Dependency;
35  import org.apache.maven.model.DependencyManagement;
36  import org.apache.maven.model.Exclusion;
37  import org.apache.maven.plugin.MojoExecutionException;
38  import org.apache.maven.plugin.MojoFailureException;
39  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
40  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
41  import org.apache.maven.project.MavenProject;
42  
43  public class TestAnalyzeDepMgt
44      extends TestCase
45  {
46  
47      AnalyzeDepMgt mojo;
48  
49      DependencyArtifactStubFactory stubFactory;
50  
51      Dependency exclusion;
52  
53      Exclusion ex;
54  
55      Artifact exclusionArtifact;
56  
57      DependencyManagement depMgt;
58  
59      DependencyManagement depMgtNoExclusions;
60  
61      protected void setUp()
62          throws Exception
63      {
64  
65          mojo = new AnalyzeDepMgt();
66          MavenProject project = new DependencyProjectStub();
67  
68          stubFactory = new DependencyArtifactStubFactory( new File( "" ), false );
69  
70          Set<Artifact> allArtifacts = stubFactory.getMixedArtifacts();
71          Set<Artifact> directArtifacts = stubFactory.getClassifiedArtifacts();
72  
73          exclusionArtifact = stubFactory.getReleaseArtifact();
74          directArtifacts.add( exclusionArtifact );
75          ex = new Exclusion();
76          ex.setArtifactId( exclusionArtifact.getArtifactId() );
77          ex.setGroupId( exclusionArtifact.getGroupId() );
78  
79          exclusion = new Dependency();
80          exclusion.setArtifactId( exclusionArtifact.getArtifactId() );
81          exclusion.setGroupId( exclusionArtifact.getGroupId() );
82          exclusion.setType( exclusionArtifact.getType() );
83          exclusion.setClassifier( "" );
84          exclusion.setVersion( "3.0" );
85  
86          exclusion.addExclusion( ex );
87          List<Dependency> list = new ArrayList<>();
88          list.add( exclusion );
89  
90          depMgt = new DependencyManagement();
91          depMgt.setDependencies( list );
92  
93          project.setArtifacts( allArtifacts );
94          project.setDependencyArtifacts( directArtifacts );
95  
96          mojo.setProject( project );
97  
98      }
99  
100     public void testGetManagementKey()
101         throws IOException
102     {
103         Dependency dep = new Dependency();
104         dep.setArtifactId( "artifact" );
105         dep.setClassifier( "class" );
106         dep.setGroupId( "group" );
107         dep.setType( "type" );
108 
109         // version isn't used in the key, it can be different
110         dep.setVersion( "1.1" );
111 
112         Artifact artifact =
113             stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "class" );
114 
115         // basic case ok
116         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
117 
118         // now change each one and make sure it fails, then set it back and make
119         // sure it's ok before
120         // testing the next one
121         dep.setType( "t" );
122         assertNotEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
123 
124         dep.setType( "type" );
125         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
126 
127         dep.setArtifactId( "a" );
128         assertNotEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
129 
130         dep.setArtifactId( "artifact" );
131         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
132 
133         dep.setClassifier( "c" );
134         assertNotEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
135 
136         dep.setClassifier( "class" );
137         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
138 
139         dep.setGroupId( "g" );
140         assertNotEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
141 
142         dep.setGroupId( "group" );
143         dep.setClassifier( null );
144         artifact = stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", null );
145         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
146 
147         dep.setClassifier( "" );
148         artifact = stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "" );
149         assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
150     }
151 
152     public void testAddExclusions()
153     {
154 
155         assertEquals( 0, mojo.addExclusions( null ).size() );
156 
157         List<Exclusion> list = new ArrayList<>();
158         list.add( ex );
159         Map<String, Exclusion> map = mojo.addExclusions( list );
160 
161         assertEquals( 1, map.size() );
162         assertTrue( map.containsKey( mojo.getExclusionKey( ex ) ) );
163         assertSame( ex, map.get( mojo.getExclusionKey( ex ) ) );
164     }
165 
166     public void testGetExclusionErrors()
167     {
168         List<Exclusion> list = new ArrayList<>();
169         list.add( ex );
170 
171         // already tested this method so I can trust it.
172         Map<String, Exclusion> map = mojo.addExclusions( list );
173 
174         List<Artifact> l = mojo.getExclusionErrors( map, mojo.getProject().getArtifacts() );
175 
176         assertEquals( 1, l.size() );
177 
178         assertEquals( mojo.getExclusionKey( ex ), mojo.getExclusionKey( l.get( 0 ) ) );
179     }
180 
181     public void testGetMismatch()
182         throws IOException
183     {
184         Map<String, Dependency> depMgtMap = new HashMap<>();
185 
186         depMgtMap.put( exclusion.getManagementKey(), exclusion );
187 
188         Map<Artifact, Dependency> results = mojo.getMismatch( depMgtMap, mojo.getProject().getArtifacts() );
189 
190         assertEquals( 1, results.size() );
191         // the release artifact is used to create the exclusion
192         assertTrue( results.containsKey( stubFactory.getReleaseArtifact() ) );
193         assertSame( exclusion, results.get( stubFactory.getReleaseArtifact() ) );
194     }
195 
196     public void testMojo()
197         throws IOException, MojoExecutionException, MojoFailureException
198     {
199         mojo.setIgnoreDirect( false );
200         // test with nothing in depMgt
201         mojo.execute();
202 
203         DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
204         project.setDependencyManagement( depMgt );
205         // test with exclusion
206         mojo.execute();
207 
208         try
209         {
210             // test with exclusion
211             mojo.setFailBuild( true );
212             mojo.execute();
213             fail( "Expected exception to fail the build." );
214         }
215         catch ( MojoExecutionException e )
216         {
217         }
218 
219         mojo.setFailBuild( true );
220         mojo.setIgnoreDirect( true );
221         mojo.execute();
222     }
223 }