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