View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util.graph.visitor;
20  
21  import java.util.List;
22  
23  import org.eclipse.aether.graph.DependencyFilter;
24  import org.eclipse.aether.graph.DependencyNode;
25  import org.eclipse.aether.internal.test.util.DependencyGraphParser;
26  import org.junit.jupiter.api.Test;
27  
28  import static java.util.Objects.requireNonNull;
29  import static org.junit.jupiter.api.Assertions.*;
30  
31  public class PathRecordingDependencyVisitorTest {
32  
33      private DependencyNode parse(String resource) throws Exception {
34          return new DependencyGraphParser("visitor/path-recorder/").parseResource(resource);
35      }
36  
37      private void assertPath(List<DependencyNode> actual, String... expected) {
38          assertEquals(expected.length, actual.size(), actual.toString());
39          for (int i = 0; i < expected.length; i++) {
40              DependencyNode node = actual.get(i);
41              assertEquals(expected[i], node.getDependency().getArtifact().getArtifactId(), actual.toString());
42          }
43      }
44  
45      @Test
46      void testGetPaths_RecordsMatchesBeneathUnmatchedParents() throws Exception {
47          DependencyNode root = parse("simple.txt");
48  
49          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
50          root.accept(visitor);
51  
52          List<List<DependencyNode>> paths = visitor.getPaths();
53          assertEquals(2, paths.size(), paths.toString());
54          assertPath(paths.get(0), "a", "b", "x");
55          assertPath(paths.get(1), "a", "x");
56      }
57  
58      @Test
59      void testGetPaths_DoesNotRecordMatchesBeneathMatchedParents() throws Exception {
60          DependencyNode root = parse("nested.txt");
61  
62          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
63          root.accept(visitor);
64  
65          List<List<DependencyNode>> paths = visitor.getPaths();
66          assertEquals(1, paths.size(), paths.toString());
67          assertPath(paths.get(0), "x");
68      }
69  
70      @Test
71      void testGetPaths_RecordsMatchesBeneathMatchedParentsIfRequested() throws Exception {
72          DependencyNode root = parse("nested.txt");
73  
74          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher(), false);
75          root.accept(visitor);
76  
77          List<List<DependencyNode>> paths = visitor.getPaths();
78          assertEquals(3, paths.size(), paths.toString());
79          assertPath(paths.get(0), "x");
80          assertPath(paths.get(1), "x", "a", "y");
81          assertPath(paths.get(2), "x", "y");
82      }
83  
84      @Test
85      void testFilterCalledWithProperParentStack() throws Exception {
86          DependencyNode root = parse("parents.txt");
87  
88          final StringBuilder buffer = new StringBuilder(256);
89          DependencyFilter filter = new DependencyFilter() {
90              public boolean accept(DependencyNode node, List<DependencyNode> parents) {
91                  requireNonNull(node, "node cannot be null");
92                  requireNonNull(parents, "parents cannot be null");
93                  for (DependencyNode parent : parents) {
94                      buffer.append(parent.getDependency().getArtifact().getArtifactId());
95                  }
96                  buffer.append(",");
97                  return false;
98              }
99          };
100 
101         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(filter);
102         root.accept(visitor);
103 
104         assertEquals(",a,ba,cba,a,ea,", buffer.toString());
105     }
106 
107     @Test
108     void testGetPaths_HandlesCycles() throws Exception {
109         DependencyNode root = parse("cycle.txt");
110 
111         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher(), false);
112         root.accept(visitor);
113 
114         List<List<DependencyNode>> paths = visitor.getPaths();
115         assertEquals(4, paths.size(), paths.toString());
116         assertPath(paths.get(0), "a", "b", "x");
117         assertPath(paths.get(1), "a", "x");
118         assertPath(paths.get(2), "a", "x", "b", "x");
119         assertPath(paths.get(3), "a", "x", "x");
120     }
121 
122     @Test
123     void testGetPaths_HandlesCycles_threePaths() throws Exception {
124         DependencyNode root = parse("cycle-3paths.txt");
125 
126         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
127         root.accept(visitor);
128 
129         List<List<DependencyNode>> paths = visitor.getPaths();
130         assertEquals(1, paths.size(), paths.toString());
131         assertPath(paths.get(0), "a", "b");
132     }
133 
134     private static class ArtifactMatcher implements DependencyFilter {
135         public boolean accept(DependencyNode node, List<DependencyNode> parents) {
136             return node.getDependency() != null
137                     && node.getDependency().getArtifact().getGroupId().equals("match");
138         }
139     }
140 }