1 package org.apache.maven.plugins.dependency.tree;
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.BufferedReader;
24 import java.io.FileReader;
25 import java.util.Set;
26 import java.util.List;
27 import java.util.ArrayList;
28
29 import org.apache.maven.artifact.Artifact;
30 import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase;
31 import org.apache.maven.plugins.dependency.tree.TreeMojo;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.shared.dependency.graph.DependencyNode;
34
35
36
37
38
39
40
41
42 public class TestTreeMojo
43 extends AbstractDependencyMojoTestCase
44 {
45
46
47
48
49
50 protected void setUp()
51 throws Exception
52 {
53
54 super.setUp( "tree", false );
55 }
56
57
58
59 public void testVoid()
60 {
61
62 }
63
64
65
66
67
68
69 public void _testTreeTestEnvironment()
70 throws Exception
71 {
72 File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
73 TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
74
75 assertNotNull( mojo );
76 assertNotNull( mojo.getProject() );
77 MavenProject project = mojo.getProject();
78 project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
79
80 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
81 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
82 artifacts.addAll( directArtifacts );
83
84 project.setArtifacts( artifacts );
85 project.setDependencyArtifacts( directArtifacts );
86
87 mojo.execute();
88
89 DependencyNode rootNode = mojo.getDependencyGraph();
90 assertNodeEquals( "testGroupId:project:jar:1.0:compile", rootNode );
91 assertEquals( 2, rootNode.getChildren().size() );
92 assertChildNodeEquals( "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile", rootNode, 0 );
93 assertChildNodeEquals( "testGroupId:release:jar:1.0:compile", rootNode, 1 );
94 }
95
96
97
98
99
100
101 public void _testTreeDotSerializing()
102 throws Exception
103 {
104 List<String> contents = runTreeMojo( "tree1.dot", "dot" );
105 assertTrue( findString( contents, "digraph \"testGroupId:project:jar:1.0:compile\" {" ) );
106 assertTrue( findString( contents,
107 "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:snapshot:jar:2.0-SNAPSHOT:compile\"" ) );
108 assertTrue( findString( contents,
109 "\"testGroupId:project:jar:1.0:compile\" -> \"testGroupId:release:jar:1.0:compile\"" ) );
110 }
111
112
113
114
115
116
117 public void _testTreeGraphMLSerializing()
118 throws Exception
119 {
120 List<String> contents = runTreeMojo( "tree1.graphml", "graphml" );
121
122 assertTrue( findString( contents, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" ) );
123 assertTrue( findString( contents, "<y:NodeLabel>testGroupId:project:jar:1.0:compile</y:NodeLabel>" ) );
124 assertTrue( findString( contents,
125 "<y:NodeLabel>testGroupId:snapshot:jar:2.0-SNAPSHOT:compile</y:NodeLabel>" ) );
126 assertTrue( findString( contents, "<y:NodeLabel>testGroupId:release:jar:1.0:compile</y:NodeLabel>" ) );
127 assertTrue( findString( contents, "<key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/>" ) );
128 assertTrue( findString( contents, "<key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/>" ) );
129 }
130
131
132
133
134
135
136 public void _testTreeTGFSerializing()
137 throws Exception
138 {
139 List<String> contents = runTreeMojo( "tree1.tgf", "tgf" );
140 assertTrue( findString( contents, "testGroupId:project:jar:1.0:compile" ) );
141 assertTrue( findString( contents, "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile" ) );
142 assertTrue( findString( contents, "testGroupId:release:jar:1.0:compile" ) );
143 }
144
145
146
147
148
149
150
151
152
153 private List<String> runTreeMojo( String outputFile, String format )
154 throws Exception
155 {
156 File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
157 String outputFileName = testDir.getAbsolutePath() + outputFile;
158 TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
159 setVariableValueToObject( mojo, "outputType", format );
160 setVariableValueToObject( mojo, "outputFile", new File( outputFileName ) );
161
162 assertNotNull( mojo );
163 assertNotNull( mojo.getProject() );
164 MavenProject project = mojo.getProject();
165 project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
166
167 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
168 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
169 artifacts.addAll( directArtifacts );
170
171 project.setArtifacts( artifacts );
172 project.setDependencyArtifacts( directArtifacts );
173
174 mojo.execute();
175
176 BufferedReader fp1 = new BufferedReader( new FileReader( outputFileName ) );
177 List<String> contents = new ArrayList<String>();
178
179 String line;
180 while ( ( line = fp1.readLine() ) != null )
181 {
182 contents.add( line );
183 }
184 fp1.close();
185
186 return contents;
187 }
188
189
190
191
192
193
194
195 private boolean findString( List<String> contents, String str )
196 {
197 for ( String line : contents )
198 {
199 if ( line.contains( str ) )
200 {
201
202 return true;
203 }
204 }
205
206
207 return false;
208 }
209
210
211
212 private void assertChildNodeEquals( String expectedNode, DependencyNode actualParentNode, int actualChildIndex )
213 {
214 DependencyNode actualNode = actualParentNode.getChildren().get( actualChildIndex );
215
216 assertNodeEquals( expectedNode, actualNode );
217 }
218
219 private void assertNodeEquals( String expectedNode, DependencyNode actualNode )
220 {
221 String[] tokens = expectedNode.split( ":" );
222
223 assertNodeEquals( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], actualNode );
224 }
225
226 private void assertNodeEquals( String expectedGroupId, String expectedArtifactId, String expectedType,
227 String expectedVersion, String expectedScope, DependencyNode actualNode )
228 {
229 Artifact actualArtifact = actualNode.getArtifact();
230
231 assertEquals( "group id", expectedGroupId, actualArtifact.getGroupId() );
232 assertEquals( "artifact id", expectedArtifactId, actualArtifact.getArtifactId() );
233 assertEquals( "type", expectedType, actualArtifact.getType() );
234 assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
235 assertEquals( "scope", expectedScope, actualArtifact.getScope() );
236 }
237 }