001package org.eclipse.aether.util.graph.visitor; 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 java.util.Objects.requireNonNull; 023import static org.junit.Assert.*; 024 025import java.util.List; 026 027import org.eclipse.aether.graph.DependencyFilter; 028import org.eclipse.aether.graph.DependencyNode; 029import org.eclipse.aether.internal.test.util.DependencyGraphParser; 030import org.junit.Test; 031 032public class PathRecordingDependencyVisitorTest 033{ 034 035 private DependencyNode parse( String resource ) 036 throws Exception 037 { 038 return new DependencyGraphParser( "visitor/path-recorder/" ).parseResource( resource ); 039 } 040 041 private void assertPath( List<DependencyNode> actual, String... expected ) 042 { 043 assertEquals( actual.toString(), expected.length, actual.size() ); 044 for ( int i = 0; i < expected.length; i++ ) 045 { 046 DependencyNode node = actual.get( i ); 047 assertEquals( actual.toString(), expected[i], node.getDependency().getArtifact().getArtifactId() ); 048 } 049 } 050 051 @Test 052 public void testGetPaths_RecordsMatchesBeneathUnmatchedParents() 053 throws Exception 054 { 055 DependencyNode root = parse( "simple.txt" ); 056 057 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() ); 058 root.accept( visitor ); 059 060 List<List<DependencyNode>> paths = visitor.getPaths(); 061 assertEquals( paths.toString(), 2, paths.size() ); 062 assertPath( paths.get( 0 ), "a", "b", "x" ); 063 assertPath( paths.get( 1 ), "a", "x" ); 064 } 065 066 @Test 067 public void testGetPaths_DoesNotRecordMatchesBeneathMatchedParents() 068 throws Exception 069 { 070 DependencyNode root = parse( "nested.txt" ); 071 072 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() ); 073 root.accept( visitor ); 074 075 List<List<DependencyNode>> paths = visitor.getPaths(); 076 assertEquals( paths.toString(), 1, paths.size() ); 077 assertPath( paths.get( 0 ), "x" ); 078 } 079 080 @Test 081 public void testGetPaths_RecordsMatchesBeneathMatchedParentsIfRequested() 082 throws Exception 083 { 084 DependencyNode root = parse( "nested.txt" ); 085 086 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher(), false ); 087 root.accept( visitor ); 088 089 List<List<DependencyNode>> paths = visitor.getPaths(); 090 assertEquals( paths.toString(), 3, paths.size() ); 091 assertPath( paths.get( 0 ), "x" ); 092 assertPath( paths.get( 1 ), "x", "a", "y" ); 093 assertPath( paths.get( 2 ), "x", "y" ); 094 } 095 096 @Test 097 public void testFilterCalledWithProperParentStack() 098 throws Exception 099 { 100 DependencyNode root = parse( "parents.txt" ); 101 102 final StringBuilder buffer = new StringBuilder( 256 ); 103 DependencyFilter filter = new DependencyFilter() 104 { 105 public boolean accept( DependencyNode node, List<DependencyNode> parents ) 106 { 107 requireNonNull( node, "node cannot be null" ); 108 requireNonNull( parents, "parents cannot be null" ); 109 for ( DependencyNode parent : parents ) 110 { 111 buffer.append( parent.getDependency().getArtifact().getArtifactId() ); 112 } 113 buffer.append( "," ); 114 return false; 115 } 116 }; 117 118 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( filter ); 119 root.accept( visitor ); 120 121 assertEquals( ",a,ba,cba,a,ea,", buffer.toString() ); 122 } 123 124 @Test 125 public void testGetPaths_HandlesCycles() 126 throws Exception 127 { 128 DependencyNode root = parse( "cycle.txt" ); 129 130 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher(), false ); 131 root.accept( visitor ); 132 133 List<List<DependencyNode>> paths = visitor.getPaths(); 134 assertEquals( paths.toString(), 4, paths.size() ); 135 assertPath( paths.get( 0 ), "a", "b", "x" ); 136 assertPath( paths.get( 1 ), "a", "x" ); 137 assertPath( paths.get( 2 ), "a", "x", "b", "x" ); 138 assertPath( paths.get( 3 ), "a", "x", "x" ); 139 } 140 141 @Test 142 public void testGetPaths_HandlesCycles_threePaths() 143 throws Exception 144 { 145 DependencyNode root = parse( "cycle-3paths.txt" ); 146 147 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() ); 148 root.accept( visitor ); 149 150 List<List<DependencyNode>> paths = visitor.getPaths(); 151 assertEquals( paths.toString(), 1, paths.size() ); 152 assertPath( paths.get( 0 ), "a", "b"); 153 } 154 155 private static class ArtifactMatcher 156 implements DependencyFilter 157 { 158 public boolean accept( DependencyNode node, List<DependencyNode> parents ) 159 { 160 return node.getDependency() != null && node.getDependency().getArtifact().getGroupId().equals( "match" ); 161 } 162 } 163 164}