001package org.eclipse.aether.internal.test.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import static org.junit.Assert.*; 023 024import java.io.IOException; 025import java.util.Arrays; 026import java.util.List; 027import java.util.Map; 028 029import org.eclipse.aether.artifact.Artifact; 030import org.eclipse.aether.graph.Dependency; 031import org.eclipse.aether.graph.DependencyNode; 032import org.eclipse.aether.internal.test.util.DependencyGraphParser; 033import org.junit.Before; 034import org.junit.Test; 035 036/** 037 */ 038public class DependencyGraphParserTest 039{ 040 041 private DependencyGraphParser parser; 042 043 @Before 044 public void setup() 045 { 046 this.parser = new DependencyGraphParser(); 047 } 048 049 @Test 050 public void testOnlyRoot() 051 throws IOException 052 { 053 String def = "gid:aid:jar:1 scope"; 054 055 DependencyNode node = parser.parseLiteral( def ); 056 057 assertNotNull( node ); 058 assertEquals( 0, node.getChildren().size() ); 059 060 Dependency dependency = node.getDependency(); 061 assertNotNull( dependency ); 062 assertEquals( "scope", dependency.getScope() ); 063 064 Artifact artifact = dependency.getArtifact(); 065 assertNotNull( artifact ); 066 067 assertEquals( "gid", artifact.getGroupId() ); 068 assertEquals( "aid", artifact.getArtifactId() ); 069 assertEquals( "jar", artifact.getExtension() ); 070 assertEquals( "1", artifact.getVersion() ); 071 } 072 073 @Test 074 public void testOptionalScope() 075 throws IOException 076 { 077 String def = "gid:aid:jar:1"; 078 079 DependencyNode node = parser.parseLiteral( def ); 080 081 assertNotNull( node ); 082 assertEquals( 0, node.getChildren().size() ); 083 084 Dependency dependency = node.getDependency(); 085 assertNotNull( dependency ); 086 assertEquals( "", dependency.getScope() ); 087 } 088 089 @Test 090 public void testWithChildren() 091 throws IOException 092 { 093 String def = 094 "gid1:aid1:ext1:ver1 scope1\n" + "+- gid2:aid2:ext2:ver2 scope2\n" + "\\- gid3:aid3:ext3:ver3 scope3\n"; 095 096 DependencyNode node = parser.parseLiteral( def ); 097 assertNotNull( node ); 098 099 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 assertEquals( true, dependency.isOptional() ); 297 } 298 299}