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