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.io.UnsupportedEncodingException; 026import java.util.Arrays; 027import java.util.List; 028import java.util.Map; 029 030import org.eclipse.aether.artifact.Artifact; 031import org.eclipse.aether.graph.Dependency; 032import org.eclipse.aether.graph.DependencyNode; 033import org.eclipse.aether.internal.test.util.DependencyGraphParser; 034import org.junit.Before; 035import org.junit.Test; 036 037/** 038 */ 039public class DependencyGraphParserTest 040{ 041 042 private DependencyGraphParser parser; 043 044 @Before 045 public void setup() 046 { 047 this.parser = new DependencyGraphParser(); 048 } 049 050 @Test 051 public void testOnlyRoot() 052 throws IOException 053 { 054 String def = "gid:aid:jar:1 scope"; 055 056 DependencyNode node = parser.parseLiteral( def ); 057 058 assertNotNull( node ); 059 assertEquals( 0, node.getChildren().size() ); 060 061 Dependency dependency = node.getDependency(); 062 assertNotNull( dependency ); 063 assertEquals( "scope", dependency.getScope() ); 064 065 Artifact artifact = dependency.getArtifact(); 066 assertNotNull( artifact ); 067 068 assertEquals( "gid", artifact.getGroupId() ); 069 assertEquals( "aid", artifact.getArtifactId() ); 070 assertEquals( "jar", artifact.getExtension() ); 071 assertEquals( "1", artifact.getVersion() ); 072 } 073 074 @Test 075 public void testOptionalScope() 076 throws IOException 077 { 078 String def = "gid:aid:jar:1"; 079 080 DependencyNode node = parser.parseLiteral( def ); 081 082 assertNotNull( node ); 083 assertEquals( 0, node.getChildren().size() ); 084 085 Dependency dependency = node.getDependency(); 086 assertNotNull( dependency ); 087 assertEquals( "", dependency.getScope() ); 088 } 089 090 @Test 091 public void testWithChildren() 092 throws IOException 093 { 094 String def = 095 "gid1:aid1:ext1:ver1 scope1\n" + "+- gid2:aid2:ext2:ver2 scope2\n" + "\\- gid3:aid3:ext3:ver3 scope3\n"; 096 097 DependencyNode node = parser.parseLiteral( def ); 098 assertNotNull( node ); 099 100 int idx = 1; 101 102 assertNodeProperties( node, idx++ ); 103 104 List<DependencyNode> children = node.getChildren(); 105 assertEquals( 2, children.size() ); 106 107 for ( DependencyNode child : children ) 108 { 109 assertNodeProperties( child, idx++ ); 110 } 111 112 } 113 114 @Test 115 public void testDeepChildren() 116 throws IOException 117 { 118 String def = 119 "gid1:aid1:ext1:ver1\n" + "+- gid2:aid2:ext2:ver2 scope2\n" + "| \\- gid3:aid3:ext3:ver3\n" 120 + "\\- gid4:aid4:ext4:ver4 scope4"; 121 122 DependencyNode node = parser.parseLiteral( def ); 123 assertNodeProperties( node, 1 ); 124 125 assertEquals( 2, node.getChildren().size() ); 126 assertNodeProperties( node.getChildren().get( 1 ), 4 ); 127 DependencyNode lvl1Node = node.getChildren().get( 0 ); 128 assertNodeProperties( lvl1Node, 2 ); 129 130 assertEquals( 1, lvl1Node.getChildren().size() ); 131 assertNodeProperties( lvl1Node.getChildren().get( 0 ), 3 ); 132 } 133 134 private void assertNodeProperties( DependencyNode node, int idx ) 135 { 136 assertNodeProperties( node, String.valueOf( idx ) ); 137 } 138 139 private void assertNodeProperties( DependencyNode node, String suffix ) 140 { 141 assertNotNull( node ); 142 Dependency dependency = node.getDependency(); 143 assertNotNull( dependency ); 144 if ( !"".equals( dependency.getScope() ) ) 145 { 146 assertEquals( "scope" + suffix, dependency.getScope() ); 147 } 148 149 Artifact artifact = dependency.getArtifact(); 150 assertNotNull( artifact ); 151 152 assertEquals( "gid" + suffix, artifact.getGroupId() ); 153 assertEquals( "aid" + suffix, artifact.getArtifactId() ); 154 assertEquals( "ext" + suffix, artifact.getExtension() ); 155 assertEquals( "ver" + suffix, artifact.getVersion() ); 156 } 157 158 @Test 159 public void testComments() 160 throws IOException 161 { 162 String def = "# first line\n#second line\ngid:aid:ext:ver # root artifact asdf:qwer:zcxv:uip"; 163 164 DependencyNode node = parser.parseLiteral( def ); 165 166 assertNodeProperties( node, "" ); 167 } 168 169 @Test 170 public void testId() 171 throws IOException 172 { 173 String def = "gid:aid:ext:ver (id)\n\\- ^id"; 174 DependencyNode node = parser.parseLiteral( def ); 175 assertNodeProperties( node, "" ); 176 177 assertNotNull( node.getChildren() ); 178 assertEquals( 1, node.getChildren().size() ); 179 180 assertSame( node, node.getChildren().get( 0 ) ); 181 } 182 183 @Test 184 public void testResourceLoading() 185 throws UnsupportedEncodingException, IOException 186 { 187 String prefix = "org/eclipse/aether/internal/test/util/"; 188 String name = "testResourceLoading.txt"; 189 190 DependencyNode node = parser.parseResource( prefix + name ); 191 assertEquals( 0, node.getChildren().size() ); 192 assertNodeProperties( node, "" ); 193 } 194 195 @Test 196 public void testResourceLoadingWithPrefix() 197 throws UnsupportedEncodingException, IOException 198 { 199 String prefix = "org/eclipse/aether/internal/test/util/"; 200 parser = new DependencyGraphParser( prefix ); 201 202 String name = "testResourceLoading.txt"; 203 204 DependencyNode node = parser.parseResource( name ); 205 assertEquals( 0, node.getChildren().size() ); 206 assertNodeProperties( node, "" ); 207 } 208 209 @Test 210 public void testProperties() 211 throws IOException 212 { 213 String def = "gid:aid:ext:ver props=test:foo,test2:fizzle"; 214 DependencyNode node = parser.parseLiteral( def ); 215 216 assertNodeProperties( node, "" ); 217 218 Map<String, String> properties = node.getDependency().getArtifact().getProperties(); 219 assertNotNull( properties ); 220 assertEquals( 2, properties.size() ); 221 222 assertTrue( properties.containsKey( "test" ) ); 223 assertEquals( "foo", properties.get( "test" ) ); 224 assertTrue( properties.containsKey( "test2" ) ); 225 assertEquals( "fizzle", properties.get( "test2" ) ); 226 } 227 228 @Test 229 public void testSubstitutions() 230 throws IOException 231 { 232 parser.setSubstitutions( Arrays.asList( "subst1", "subst2" ) ); 233 String def = "%s:%s:ext:ver"; 234 DependencyNode root = parser.parseLiteral( def ); 235 Artifact artifact = root.getDependency().getArtifact(); 236 assertEquals( "subst2", artifact.getArtifactId() ); 237 assertEquals( "subst1", artifact.getGroupId() ); 238 239 def = "%s:aid:ext:ver\n\\- %s:aid:ext:ver"; 240 root = parser.parseLiteral( def ); 241 242 assertEquals( "subst1", root.getDependency().getArtifact().getGroupId() ); 243 assertEquals( "subst2", root.getChildren().get( 0 ).getDependency().getArtifact().getGroupId() ); 244 } 245 246 @Test 247 public void testMultiple() 248 throws IOException 249 { 250 String prefix = "org/eclipse/aether/internal/test/util/"; 251 String name = "testResourceLoading.txt"; 252 253 List<DependencyNode> nodes = parser.parseMultiResource( prefix + name ); 254 255 assertEquals( 2, nodes.size() ); 256 assertEquals( "aid", nodes.get( 0 ).getDependency().getArtifact().getArtifactId() ); 257 assertEquals( "aid2", nodes.get( 1 ).getDependency().getArtifact().getArtifactId() ); 258 } 259 260 @Test 261 public void testRootNullDependency() 262 throws IOException 263 { 264 String literal = "(null)\n+- gid:aid:ext:ver"; 265 DependencyNode root = parser.parseLiteral( literal ); 266 267 assertNull( root.getDependency() ); 268 assertEquals( 1, root.getChildren().size() ); 269 } 270 271 @Test 272 public void testChildNullDependency() 273 throws IOException 274 { 275 String literal = "gid:aid:ext:ver\n+- (null)"; 276 DependencyNode root = parser.parseLiteral( literal ); 277 278 assertNotNull( root.getDependency() ); 279 assertEquals( 1, root.getChildren().size() ); 280 assertNull( root.getChildren().get( 0 ).getDependency() ); 281 } 282 283 @Test 284 public void testOptional() 285 throws IOException 286 { 287 String def = "gid:aid:jar:1 compile optional"; 288 289 DependencyNode node = parser.parseLiteral( def ); 290 291 assertNotNull( node ); 292 assertEquals( 0, node.getChildren().size() ); 293 294 Dependency dependency = node.getDependency(); 295 assertNotNull( dependency ); 296 assertEquals( "compile", dependency.getScope() ); 297 assertEquals( true, dependency.isOptional() ); 298 } 299 300}