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