1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.internal.test.util;
20
21 import java.io.IOException;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.graph.Dependency;
28 import org.eclipse.aether.graph.DependencyNode;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import static org.junit.Assert.*;
33
34
35
36 public class DependencyGraphParserTest {
37
38 private DependencyGraphParser parser;
39
40 @Before
41 public void setup() {
42 this.parser = new DependencyGraphParser();
43 }
44
45 @Test
46 public void testOnlyRoot() throws IOException {
47 String def = "gid:aid:jar:1 scope";
48
49 DependencyNode node = parser.parseLiteral(def);
50
51 assertNotNull(node);
52 assertEquals(0, node.getChildren().size());
53
54 Dependency dependency = node.getDependency();
55 assertNotNull(dependency);
56 assertEquals("scope", dependency.getScope());
57
58 Artifact artifact = dependency.getArtifact();
59 assertNotNull(artifact);
60
61 assertEquals("gid", artifact.getGroupId());
62 assertEquals("aid", artifact.getArtifactId());
63 assertEquals("jar", artifact.getExtension());
64 assertEquals("1", artifact.getVersion());
65 }
66
67 @Test
68 public void testOptionalScope() throws IOException {
69 String def = "gid:aid:jar:1";
70
71 DependencyNode node = parser.parseLiteral(def);
72
73 assertNotNull(node);
74 assertEquals(0, node.getChildren().size());
75
76 Dependency dependency = node.getDependency();
77 assertNotNull(dependency);
78 assertEquals("", dependency.getScope());
79 }
80
81 @Test
82 public void testWithChildren() throws IOException {
83 String def =
84 "gid1:aid1:ext1:ver1 scope1\n" + "+- gid2:aid2:ext2:ver2 scope2\n" + "\\- gid3:aid3:ext3:ver3 scope3\n";
85
86 DependencyNode node = parser.parseLiteral(def);
87 assertNotNull(node);
88
89 int idx = 1;
90
91 assertNodeProperties(node, idx++);
92
93 List<DependencyNode> children = node.getChildren();
94 assertEquals(2, children.size());
95
96 for (DependencyNode child : children) {
97 assertNodeProperties(child, idx++);
98 }
99 }
100
101 @Test
102 public void testDeepChildren() throws IOException {
103 String def = "gid1:aid1:ext1:ver1\n" + "+- gid2:aid2:ext2:ver2 scope2\n" + "| \\- gid3:aid3:ext3:ver3\n"
104 + "\\- gid4:aid4:ext4:ver4 scope4";
105
106 DependencyNode node = parser.parseLiteral(def);
107 assertNodeProperties(node, 1);
108
109 assertEquals(2, node.getChildren().size());
110 assertNodeProperties(node.getChildren().get(1), 4);
111 DependencyNode lvl1Node = node.getChildren().get(0);
112 assertNodeProperties(lvl1Node, 2);
113
114 assertEquals(1, lvl1Node.getChildren().size());
115 assertNodeProperties(lvl1Node.getChildren().get(0), 3);
116 }
117
118 private void assertNodeProperties(DependencyNode node, int idx) {
119 assertNodeProperties(node, String.valueOf(idx));
120 }
121
122 private void assertNodeProperties(DependencyNode node, String suffix) {
123 assertNotNull(node);
124 Dependency dependency = node.getDependency();
125 assertNotNull(dependency);
126 if (!"".equals(dependency.getScope())) {
127 assertEquals("scope" + suffix, dependency.getScope());
128 }
129
130 Artifact artifact = dependency.getArtifact();
131 assertNotNull(artifact);
132
133 assertEquals("gid" + suffix, artifact.getGroupId());
134 assertEquals("aid" + suffix, artifact.getArtifactId());
135 assertEquals("ext" + suffix, artifact.getExtension());
136 assertEquals("ver" + suffix, artifact.getVersion());
137 }
138
139 @Test
140 public void testComments() throws IOException {
141 String def = "# first line\n#second line\ngid:aid:ext:ver # root artifact asdf:qwer:zcxv:uip";
142
143 DependencyNode node = parser.parseLiteral(def);
144
145 assertNodeProperties(node, "");
146 }
147
148 @Test
149 public void testId() throws IOException {
150 String def = "gid:aid:ext:ver (id)\n\\- ^id";
151 DependencyNode node = parser.parseLiteral(def);
152 assertNodeProperties(node, "");
153
154 assertNotNull(node.getChildren());
155 assertEquals(1, node.getChildren().size());
156
157 assertSame(node, node.getChildren().get(0));
158 }
159
160 @Test
161 public void testResourceLoading() throws IOException {
162 String prefix = "org/eclipse/aether/internal/test/util/";
163 String name = "testResourceLoading.txt";
164
165 DependencyNode node = parser.parseResource(prefix + name);
166 assertEquals(0, node.getChildren().size());
167 assertNodeProperties(node, "");
168 }
169
170 @Test
171 public void testResourceLoadingWithPrefix() throws IOException {
172 String prefix = "org/eclipse/aether/internal/test/util/";
173 parser = new DependencyGraphParser(prefix);
174
175 String name = "testResourceLoading.txt";
176
177 DependencyNode node = parser.parseResource(name);
178 assertEquals(0, node.getChildren().size());
179 assertNodeProperties(node, "");
180 }
181
182 @Test
183 public void testProperties() throws IOException {
184 String def = "gid:aid:ext:ver props=test:foo,test2:fizzle";
185 DependencyNode node = parser.parseLiteral(def);
186
187 assertNodeProperties(node, "");
188
189 Map<String, String> properties = node.getDependency().getArtifact().getProperties();
190 assertNotNull(properties);
191 assertEquals(2, properties.size());
192
193 assertTrue(properties.containsKey("test"));
194 assertEquals("foo", properties.get("test"));
195 assertTrue(properties.containsKey("test2"));
196 assertEquals("fizzle", properties.get("test2"));
197 }
198
199 @Test
200 public void testSubstitutions() throws IOException {
201 parser.setSubstitutions(Arrays.asList("subst1", "subst2"));
202 String def = "%s:%s:ext:ver";
203 DependencyNode root = parser.parseLiteral(def);
204 Artifact artifact = root.getDependency().getArtifact();
205 assertEquals("subst2", artifact.getArtifactId());
206 assertEquals("subst1", artifact.getGroupId());
207
208 def = "%s:aid:ext:ver\n\\- %s:aid:ext:ver";
209 root = parser.parseLiteral(def);
210
211 assertEquals("subst1", root.getDependency().getArtifact().getGroupId());
212 assertEquals(
213 "subst2",
214 root.getChildren().get(0).getDependency().getArtifact().getGroupId());
215 }
216
217 @Test
218 public void testMultiple() throws IOException {
219 String prefix = "org/eclipse/aether/internal/test/util/";
220 String name = "testResourceLoading.txt";
221
222 List<DependencyNode> nodes = parser.parseMultiResource(prefix + name);
223
224 assertEquals(2, nodes.size());
225 assertEquals("aid", nodes.get(0).getDependency().getArtifact().getArtifactId());
226 assertEquals("aid2", nodes.get(1).getDependency().getArtifact().getArtifactId());
227 }
228
229 @Test
230 public void testRootNullDependency() throws IOException {
231 String literal = "(null)\n+- gid:aid:ext:ver";
232 DependencyNode root = parser.parseLiteral(literal);
233
234 assertNull(root.getDependency());
235 assertEquals(1, root.getChildren().size());
236 }
237
238 @Test
239 public void testChildNullDependency() throws IOException {
240 String literal = "gid:aid:ext:ver\n+- (null)";
241 DependencyNode root = parser.parseLiteral(literal);
242
243 assertNotNull(root.getDependency());
244 assertEquals(1, root.getChildren().size());
245 assertNull(root.getChildren().get(0).getDependency());
246 }
247
248 @Test
249 public void testOptional() throws IOException {
250 String def = "gid:aid:jar:1 compile optional";
251
252 DependencyNode node = parser.parseLiteral(def);
253
254 assertNotNull(node);
255 assertEquals(0, node.getChildren().size());
256
257 Dependency dependency = node.getDependency();
258 assertNotNull(dependency);
259 assertEquals("compile", dependency.getScope());
260 assertTrue(dependency.isOptional());
261 }
262 }