View Javadoc
1   package org.eclipse.aether.util.graph.visitor;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.util.Objects.requireNonNull;
23  import static org.junit.Assert.*;
24  
25  import java.util.List;
26  
27  import org.eclipse.aether.graph.DependencyFilter;
28  import org.eclipse.aether.graph.DependencyNode;
29  import org.eclipse.aether.internal.test.util.DependencyGraphParser;
30  import org.junit.Test;
31  
32  public class PathRecordingDependencyVisitorTest
33  {
34  
35      private DependencyNode parse( String resource )
36          throws Exception
37      {
38          return new DependencyGraphParser( "visitor/path-recorder/" ).parseResource( resource );
39      }
40  
41      private void assertPath( List<DependencyNode> actual, String... expected )
42      {
43          assertEquals( actual.toString(), expected.length, actual.size() );
44          for ( int i = 0; i < expected.length; i++ )
45          {
46              DependencyNode node = actual.get( i );
47              assertEquals( actual.toString(), expected[i], node.getDependency().getArtifact().getArtifactId() );
48          }
49      }
50  
51      @Test
52      public void testGetPaths_RecordsMatchesBeneathUnmatchedParents()
53          throws Exception
54      {
55          DependencyNode root = parse( "simple.txt" );
56  
57          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() );
58          root.accept( visitor );
59  
60          List<List<DependencyNode>> paths = visitor.getPaths();
61          assertEquals( paths.toString(), 2, paths.size() );
62          assertPath( paths.get( 0 ), "a", "b", "x" );
63          assertPath( paths.get( 1 ), "a", "x" );
64      }
65  
66      @Test
67      public void testGetPaths_DoesNotRecordMatchesBeneathMatchedParents()
68          throws Exception
69      {
70          DependencyNode root = parse( "nested.txt" );
71  
72          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() );
73          root.accept( visitor );
74  
75          List<List<DependencyNode>> paths = visitor.getPaths();
76          assertEquals( paths.toString(), 1, paths.size() );
77          assertPath( paths.get( 0 ), "x" );
78      }
79  
80      @Test
81      public void testGetPaths_RecordsMatchesBeneathMatchedParentsIfRequested()
82          throws Exception
83      {
84          DependencyNode root = parse( "nested.txt" );
85  
86          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher(), false );
87          root.accept( visitor );
88  
89          List<List<DependencyNode>> paths = visitor.getPaths();
90          assertEquals( paths.toString(), 3, paths.size() );
91          assertPath( paths.get( 0 ), "x" );
92          assertPath( paths.get( 1 ), "x", "a", "y" );
93          assertPath( paths.get( 2 ), "x", "y" );
94      }
95  
96      @Test
97      public void testFilterCalledWithProperParentStack()
98          throws Exception
99      {
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 }