1 package org.apache.maven.plugins.dependency.analyze;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
110 dep.setVersion( "1.1" );
111
112 Artifact artifact =
113 stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "class" );
114
115
116 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
117
118
119
120
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
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
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
201 mojo.execute();
202
203 DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
204 project.setDependencyManagement( depMgt );
205
206 mojo.execute();
207
208 try
209 {
210
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 }