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