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