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