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