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