1 package org.apache.maven.plugin.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.plugin.dependency.AbstractDependencyMojoTestCase;
31 import org.apache.maven.plugin.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, "<y:NodeLabel>testGroupId:snapshot:jar:2.0-SNAPSHOT:compile</y:NodeLabel>" ) );
125 assertTrue( findString( contents, "<y:NodeLabel>testGroupId:release:jar:1.0:compile</y:NodeLabel>" ) );
126 assertTrue( findString( contents, "<key for=\"node\" id=\"d0\" yfiles.type=\"nodegraphics\"/>" ) );
127 assertTrue( findString( contents, "<key for=\"edge\" id=\"d1\" yfiles.type=\"edgegraphics\"/>" ) );
128 }
129
130
131
132
133
134
135 public void _testTreeTGFSerializing()
136 throws Exception
137 {
138 List<String> contents = runTreeMojo( "tree1.tgf", "tgf" );
139 assertTrue( findString( contents, "testGroupId:project:jar:1.0:compile" ) );
140 assertTrue( findString( contents, "testGroupId:snapshot:jar:2.0-SNAPSHOT:compile" ) );
141 assertTrue( findString( contents, "testGroupId:release:jar:1.0:compile" ) );
142 }
143
144
145
146
147
148
149
150 private List<String> runTreeMojo( String outputFile, String format )
151 throws Exception
152 {
153 File testPom = new File( getBasedir(), "target/test-classes/unit/tree-test/plugin-config.xml" );
154 String outputFileName = testDir.getAbsolutePath() + outputFile;
155 TreeMojo mojo = (TreeMojo) lookupMojo( "tree", testPom );
156 setVariableValueToObject( mojo, "outputType", format );
157 setVariableValueToObject( mojo, "outputFile", new File( outputFileName ) );
158
159 assertNotNull( mojo );
160 assertNotNull( mojo.getProject() );
161 MavenProject project = mojo.getProject();
162 project.setArtifact( this.stubFactory.createArtifact( "testGroupId", "project", "1.0" ) );
163
164 Set<Artifact> artifacts = this.stubFactory.getScopedArtifacts();
165 Set<Artifact> directArtifacts = this.stubFactory.getReleaseAndSnapshotArtifacts();
166 artifacts.addAll( directArtifacts );
167
168 project.setArtifacts( artifacts );
169 project.setDependencyArtifacts( directArtifacts );
170
171 mojo.execute();
172
173 BufferedReader fp1 = new BufferedReader( new FileReader( outputFileName ) );
174 List<String> contents = new ArrayList<String>();
175
176 String line;
177 while ( ( line = fp1.readLine() ) != null )
178 {
179 contents.add( line );
180 }
181 fp1.close();
182
183 return contents ;
184 }
185
186
187
188
189
190
191 private boolean findString( List<String> contents, String str )
192 {
193 for ( String line : contents )
194 {
195 if (line.contains(str))
196 {
197
198 return true;
199 }
200 }
201
202
203 return false;
204 }
205
206
207
208 private void assertChildNodeEquals( String expectedNode, DependencyNode actualParentNode, int actualChildIndex )
209 {
210 DependencyNode actualNode = actualParentNode.getChildren().get( actualChildIndex );
211
212 assertNodeEquals( expectedNode, actualNode );
213 }
214
215 private void assertNodeEquals( String expectedNode, DependencyNode actualNode )
216 {
217 String[] tokens = expectedNode.split( ":" );
218
219 assertNodeEquals( tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], actualNode );
220 }
221
222 private void assertNodeEquals( String expectedGroupId, String expectedArtifactId, String expectedType,
223 String expectedVersion, String expectedScope, DependencyNode actualNode )
224 {
225 Artifact actualArtifact = actualNode.getArtifact();
226
227 assertEquals( "group id", expectedGroupId, actualArtifact.getGroupId() );
228 assertEquals( "artifact id", expectedArtifactId, actualArtifact.getArtifactId() );
229 assertEquals( "type", expectedType, actualArtifact.getType() );
230 assertEquals( "version", expectedVersion, actualArtifact.getVersion() );
231 assertEquals( "scope", expectedScope, actualArtifact.getScope() );
232 }
233 }