View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.analyze;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.Set;
28  
29  import junit.framework.TestCase;
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.model.DependencyManagement;
33  import org.apache.maven.model.Exclusion;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
37  import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
38  import org.apache.maven.project.MavenProject;
39  
40  import static org.junit.Assert.assertNotEquals;
41  
42  public class TestAnalyzeDepMgt extends TestCase {
43  
44      AnalyzeDepMgt mojo;
45  
46      DependencyArtifactStubFactory stubFactory;
47  
48      Dependency exclusion;
49  
50      Exclusion ex;
51  
52      Artifact exclusionArtifact;
53  
54      DependencyManagement depMgt;
55  
56      DependencyManagement depMgtNoExclusions;
57  
58      protected void setUp() throws Exception {
59  
60          mojo = new AnalyzeDepMgt();
61          MavenProject project = new DependencyProjectStub();
62  
63          stubFactory = new DependencyArtifactStubFactory(new File(""), false);
64  
65          Set<Artifact> allArtifacts = stubFactory.getMixedArtifacts();
66          Set<Artifact> directArtifacts = stubFactory.getClassifiedArtifacts();
67  
68          exclusionArtifact = stubFactory.getReleaseArtifact();
69          directArtifacts.add(exclusionArtifact);
70          ex = new Exclusion();
71          ex.setArtifactId(exclusionArtifact.getArtifactId());
72          ex.setGroupId(exclusionArtifact.getGroupId());
73  
74          exclusion = new Dependency();
75          exclusion.setArtifactId(exclusionArtifact.getArtifactId());
76          exclusion.setGroupId(exclusionArtifact.getGroupId());
77          exclusion.setType(exclusionArtifact.getType());
78          exclusion.setClassifier("");
79          exclusion.setVersion("3.0");
80  
81          exclusion.addExclusion(ex);
82          List<Dependency> list = new ArrayList<>();
83          list.add(exclusion);
84  
85          depMgt = new DependencyManagement();
86          depMgt.setDependencies(list);
87  
88          project.setArtifacts(allArtifacts);
89          project.setDependencyArtifacts(directArtifacts);
90  
91          mojo.setProject(project);
92      }
93  
94      public void testGetManagementKey() throws IOException {
95          Dependency dep = new Dependency();
96          dep.setArtifactId("artifact");
97          dep.setClassifier("class");
98          dep.setGroupId("group");
99          dep.setType("type");
100 
101         // version isn't used in the key, it can be different
102         dep.setVersion("1.1");
103 
104         Artifact artifact =
105                 stubFactory.createArtifact("group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "class");
106 
107         // basic case ok
108         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
109 
110         // now change each one and make sure it fails, then set it back and make
111         // sure it's ok before
112         // testing the next one
113         dep.setType("t");
114         dep.clearManagementKey();
115         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
116 
117         dep.setType("type");
118         dep.clearManagementKey();
119         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
120 
121         dep.setArtifactId("a");
122         dep.clearManagementKey();
123         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
124 
125         dep.setArtifactId("artifact");
126         dep.clearManagementKey();
127         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
128 
129         dep.setClassifier("c");
130         dep.clearManagementKey();
131         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
132 
133         dep.setClassifier("class");
134         dep.clearManagementKey();
135         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
136 
137         dep.setGroupId("g");
138         dep.clearManagementKey();
139         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
140 
141         dep.setGroupId("group");
142         dep.setClassifier(null);
143         dep.clearManagementKey();
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         dep.clearManagementKey();
149         artifact = stubFactory.createArtifact("group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "");
150         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
151     }
152 
153     public void testAddExclusions() {
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         List<Exclusion> list = new ArrayList<>();
168         list.add(ex);
169 
170         // already tested this method so I can trust it.
171         Map<String, Exclusion> map = mojo.addExclusions(list);
172 
173         List<Artifact> l = mojo.getExclusionErrors(map, mojo.getProject().getArtifacts());
174 
175         assertEquals(1, l.size());
176 
177         assertEquals(mojo.getExclusionKey(ex), mojo.getExclusionKey(l.get(0)));
178     }
179 
180     public void testGetMismatch() throws IOException {
181         Map<String, Dependency> depMgtMap = new HashMap<>();
182 
183         depMgtMap.put(exclusion.getManagementKey(), exclusion);
184 
185         Map<Artifact, Dependency> results =
186                 mojo.getMismatch(depMgtMap, mojo.getProject().getArtifacts());
187 
188         assertEquals(1, results.size());
189         // the release artifact is used to create the exclusion
190         assertTrue(results.containsKey(stubFactory.getReleaseArtifact()));
191         assertSame(exclusion, results.get(stubFactory.getReleaseArtifact()));
192     }
193 
194     public void testMojo() throws IOException, MojoExecutionException, MojoFailureException {
195         mojo.setIgnoreDirect(false);
196         // test with nothing in depMgt
197         mojo.execute();
198 
199         DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
200         project.setDependencyManagement(depMgt);
201         // test with exclusion
202         mojo.execute();
203 
204         try {
205             // test with exclusion
206             mojo.setFailBuild(true);
207             mojo.execute();
208             fail("Expected exception to fail the build.");
209         } catch (MojoExecutionException e) {
210         }
211 
212         mojo.setFailBuild(true);
213         mojo.setIgnoreDirect(true);
214         mojo.execute();
215     }
216 }