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         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
115 
116         dep.setType("type");
117         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
118 
119         dep.setArtifactId("a");
120         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
121 
122         dep.setArtifactId("artifact");
123         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
124 
125         dep.setClassifier("c");
126         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
127 
128         dep.setClassifier("class");
129         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
130 
131         dep.setGroupId("g");
132         assertNotEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
133 
134         dep.setGroupId("group");
135         dep.setClassifier(null);
136         artifact = stubFactory.createArtifact("group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", null);
137         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
138 
139         dep.setClassifier("");
140         artifact = stubFactory.createArtifact("group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "");
141         assertEquals(dep.getManagementKey(), mojo.getArtifactManagementKey(artifact));
142     }
143 
144     public void testAddExclusions() {
145 
146         assertEquals(0, mojo.addExclusions(null).size());
147 
148         List<Exclusion> list = new ArrayList<>();
149         list.add(ex);
150         Map<String, Exclusion> map = mojo.addExclusions(list);
151 
152         assertEquals(1, map.size());
153         assertTrue(map.containsKey(mojo.getExclusionKey(ex)));
154         assertSame(ex, map.get(mojo.getExclusionKey(ex)));
155     }
156 
157     public void testGetExclusionErrors() {
158         List<Exclusion> list = new ArrayList<>();
159         list.add(ex);
160 
161         // already tested this method so I can trust it.
162         Map<String, Exclusion> map = mojo.addExclusions(list);
163 
164         List<Artifact> l = mojo.getExclusionErrors(map, mojo.getProject().getArtifacts());
165 
166         assertEquals(1, l.size());
167 
168         assertEquals(mojo.getExclusionKey(ex), mojo.getExclusionKey(l.get(0)));
169     }
170 
171     public void testGetMismatch() throws IOException {
172         Map<String, Dependency> depMgtMap = new HashMap<>();
173 
174         depMgtMap.put(exclusion.getManagementKey(), exclusion);
175 
176         Map<Artifact, Dependency> results =
177                 mojo.getMismatch(depMgtMap, mojo.getProject().getArtifacts());
178 
179         assertEquals(1, results.size());
180         // the release artifact is used to create the exclusion
181         assertTrue(results.containsKey(stubFactory.getReleaseArtifact()));
182         assertSame(exclusion, results.get(stubFactory.getReleaseArtifact()));
183     }
184 
185     public void testMojo() throws IOException, MojoExecutionException, MojoFailureException {
186         mojo.setIgnoreDirect(false);
187         // test with nothing in depMgt
188         mojo.execute();
189 
190         DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
191         project.setDependencyManagement(depMgt);
192         // test with exclusion
193         mojo.execute();
194 
195         try {
196             // test with exclusion
197             mojo.setFailBuild(true);
198             mojo.execute();
199             fail("Expected exception to fail the build.");
200         } catch (MojoExecutionException e) {
201         }
202 
203         mojo.setFailBuild(true);
204         mojo.setIgnoreDirect(true);
205         mojo.execute();
206     }
207 }