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 java.io.File;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30 import junit.framework.TestCase;
31
32 import org.apache.maven.artifact.Artifact;
33 import org.apache.maven.model.Dependency;
34 import org.apache.maven.model.DependencyManagement;
35 import org.apache.maven.model.Exclusion;
36 import org.apache.maven.plugins.dependency.analyze.AnalyzeDepMgt;
37 import org.apache.maven.plugins.dependency.testUtils.DependencyArtifactStubFactory;
38 import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub;
39 import org.apache.maven.project.MavenProject;
40
41 public class TestAnalyzeDepMgt
42 extends TestCase
43 {
44
45 AnalyzeDepMgt mojo;
46
47 DependencyArtifactStubFactory stubFactory;
48
49 Dependency exclusion;
50
51 Exclusion ex;
52
53 Artifact exclusionArtifact;
54
55 DependencyManagement depMgt;
56
57 DependencyManagement depMgtNoExclusions;
58
59 protected void setUp()
60 throws Exception
61 {
62
63 mojo = new AnalyzeDepMgt();
64 MavenProject project = new DependencyProjectStub();
65
66 stubFactory = new DependencyArtifactStubFactory( new File( "" ), false );
67
68 Set<Artifact> allArtifacts = stubFactory.getMixedArtifacts();
69 Set<Artifact> directArtifacts = stubFactory.getClassifiedArtifacts();
70
71 exclusionArtifact = stubFactory.getReleaseArtifact();
72 directArtifacts.add( exclusionArtifact );
73 ex = new Exclusion();
74 ex.setArtifactId( exclusionArtifact.getArtifactId() );
75 ex.setGroupId( exclusionArtifact.getGroupId() );
76
77 exclusion = new Dependency();
78 exclusion.setArtifactId( exclusionArtifact.getArtifactId() );
79 exclusion.setGroupId( exclusionArtifact.getGroupId() );
80 exclusion.setType( exclusionArtifact.getType() );
81 exclusion.setClassifier( "" );
82 exclusion.setVersion( "3.0" );
83
84 exclusion.addExclusion( ex );
85 List<Dependency> list = new ArrayList<Dependency>();
86 list.add( exclusion );
87
88 depMgt = new DependencyManagement();
89 depMgt.setDependencies( list );
90
91 project.setArtifacts( allArtifacts );
92 project.setDependencyArtifacts( directArtifacts );
93
94 mojo.setProject( project );
95
96 }
97
98 public void testGetManagementKey()
99 throws IOException
100 {
101 Dependency dep = new Dependency();
102 dep.setArtifactId( "artifact" );
103 dep.setClassifier( "class" );
104 dep.setGroupId( "group" );
105 dep.setType( "type" );
106
107
108 dep.setVersion( "1.1" );
109
110 Artifact artifact =
111 stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "class" );
112
113
114 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
115
116
117
118
119 dep.setType( "t" );
120 assertFalse( dep.getManagementKey().equals( mojo.getArtifactManagementKey( artifact ) ) );
121
122 dep.setType( "type" );
123 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
124
125 dep.setArtifactId( "a" );
126 assertFalse( dep.getManagementKey().equals( mojo.getArtifactManagementKey( artifact ) ) );
127
128 dep.setArtifactId( "artifact" );
129 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
130
131 dep.setClassifier( "c" );
132 assertFalse( dep.getManagementKey().equals( mojo.getArtifactManagementKey( artifact ) ) );
133
134 dep.setClassifier( "class" );
135 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
136
137 dep.setGroupId( "g" );
138 assertFalse( dep.getManagementKey().equals( mojo.getArtifactManagementKey( artifact ) ) );
139
140 dep.setGroupId( "group" );
141 dep.setClassifier( null );
142 artifact = stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", null );
143 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
144
145 dep.setClassifier( "" );
146 artifact = stubFactory.createArtifact( "group", "artifact", "1.0", Artifact.SCOPE_COMPILE, "type", "" );
147 assertEquals( dep.getManagementKey(), mojo.getArtifactManagementKey( artifact ) );
148 }
149
150 public void testAddExclusions()
151 {
152
153 assertEquals( 0, mojo.addExclusions( null ).size() );
154
155 List<Exclusion> list = new ArrayList<Exclusion>();
156 list.add( ex );
157 Map<String, Exclusion> map = mojo.addExclusions( list );
158
159 assertEquals( 1, map.size() );
160 assertTrue( map.containsKey( mojo.getExclusionKey( ex ) ) );
161 assertSame( ex, map.get( mojo.getExclusionKey( ex ) ) );
162 }
163
164 public void testGetExclusionErrors()
165 {
166 List<Exclusion> list = new ArrayList<Exclusion>();
167 list.add( ex );
168
169
170 Map<String, Exclusion> map = mojo.addExclusions( list );
171
172 List<Artifact> l = mojo.getExclusionErrors( map, mojo.getProject().getArtifacts() );
173
174 assertEquals( 1, l.size() );
175
176 assertEquals( mojo.getExclusionKey( ex ), mojo.getExclusionKey( l.get( 0 ) ) );
177 }
178
179 public void testGetMismatch()
180 throws IOException
181 {
182 Map<String, Dependency> depMgtMap = new HashMap<String, Dependency>();
183
184 depMgtMap.put( exclusion.getManagementKey(), exclusion );
185
186 Map<Artifact, Dependency> results = mojo.getMismatch( depMgtMap, mojo.getProject().getArtifacts() );
187
188 assertEquals( 1, results.size() );
189
190 assertTrue( results.containsKey( stubFactory.getReleaseArtifact() ) );
191 assertSame( exclusion, results.get( stubFactory.getReleaseArtifact() ) );
192 }
193
194 public void testMojo()
195 throws IOException
196 {
197 mojo.setIgnoreDirect( false );
198 try
199 {
200
201 mojo.execute();
202 }
203 catch ( Exception e )
204 {
205 e.printStackTrace();
206 fail( "Caught Unexpected Exception:" + e.getLocalizedMessage() );
207 }
208
209 try
210 {
211 DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
212 project.setDependencyManagement( depMgt );
213
214 mojo.execute();
215 }
216 catch ( Exception e )
217 {
218 e.printStackTrace();
219 fail( "Caught Unexpected Exception:" + e.getLocalizedMessage() );
220 }
221
222 try
223 {
224 DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
225 project.setDependencyManagement( depMgt );
226
227 mojo.setFailBuild( true );
228 mojo.execute();
229 fail( "Expected exception to fail the build." );
230 }
231 catch ( Exception e )
232 {
233 System.out.println( "Caught Expected Exception:" + e.getLocalizedMessage() );
234 }
235
236 try
237 {
238 DependencyProjectStub project = (DependencyProjectStub) mojo.getProject();
239 project.setDependencyManagement( depMgt );
240
241 mojo.setFailBuild( true );
242 mojo.setIgnoreDirect( true );
243 mojo.execute();
244 }
245 catch ( Exception e )
246 {
247 e.printStackTrace();
248 fail( "Caught Unexpected Exception:" + e.getLocalizedMessage() );
249 }
250 }
251 }