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.Test;
27  
28  import static java.util.Objects.requireNonNull;
29  import static org.junit.Assert.*;
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(actual.toString(), expected.length, actual.size());
39          for (int i = 0; i < expected.length; i++) {
40              DependencyNode node = actual.get(i);
41              assertEquals(
42                      actual.toString(),
43                      expected[i],
44                      node.getDependency().getArtifact().getArtifactId());
45          }
46      }
47  
48      @Test
49      public void testGetPaths_RecordsMatchesBeneathUnmatchedParents() throws Exception {
50          DependencyNode root = parse("simple.txt");
51  
52          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
53          root.accept(visitor);
54  
55          List<List<DependencyNode>> paths = visitor.getPaths();
56          assertEquals(paths.toString(), 2, paths.size());
57          assertPath(paths.get(0), "a", "b", "x");
58          assertPath(paths.get(1), "a", "x");
59      }
60  
61      @Test
62      public void testGetPaths_DoesNotRecordMatchesBeneathMatchedParents() throws Exception {
63          DependencyNode root = parse("nested.txt");
64  
65          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
66          root.accept(visitor);
67  
68          List<List<DependencyNode>> paths = visitor.getPaths();
69          assertEquals(paths.toString(), 1, paths.size());
70          assertPath(paths.get(0), "x");
71      }
72  
73      @Test
74      public void testGetPaths_RecordsMatchesBeneathMatchedParentsIfRequested() throws Exception {
75          DependencyNode root = parse("nested.txt");
76  
77          PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher(), false);
78          root.accept(visitor);
79  
80          List<List<DependencyNode>> paths = visitor.getPaths();
81          assertEquals(paths.toString(), 3, paths.size());
82          assertPath(paths.get(0), "x");
83          assertPath(paths.get(1), "x", "a", "y");
84          assertPath(paths.get(2), "x", "y");
85      }
86  
87      @Test
88      public void testFilterCalledWithProperParentStack() throws Exception {
89          DependencyNode root = parse("parents.txt");
90  
91          final StringBuilder buffer = new StringBuilder(256);
92          DependencyFilter filter = new DependencyFilter() {
93              public boolean accept(DependencyNode node, List<DependencyNode> parents) {
94                  requireNonNull(node, "node cannot be null");
95                  requireNonNull(parents, "parents cannot be null");
96                  for (DependencyNode parent : parents) {
97                      buffer.append(parent.getDependency().getArtifact().getArtifactId());
98                  }
99                  buffer.append(",");
100                 return false;
101             }
102         };
103 
104         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(filter);
105         root.accept(visitor);
106 
107         assertEquals(",a,ba,cba,a,ea,", buffer.toString());
108     }
109 
110     @Test
111     public void testGetPaths_HandlesCycles() throws Exception {
112         DependencyNode root = parse("cycle.txt");
113 
114         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher(), false);
115         root.accept(visitor);
116 
117         List<List<DependencyNode>> paths = visitor.getPaths();
118         assertEquals(paths.toString(), 4, paths.size());
119         assertPath(paths.get(0), "a", "b", "x");
120         assertPath(paths.get(1), "a", "x");
121         assertPath(paths.get(2), "a", "x", "b", "x");
122         assertPath(paths.get(3), "a", "x", "x");
123     }
124 
125     @Test
126     public void testGetPaths_HandlesCycles_threePaths() throws Exception {
127         DependencyNode root = parse("cycle-3paths.txt");
128 
129         PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor(new ArtifactMatcher());
130         root.accept(visitor);
131 
132         List<List<DependencyNode>> paths = visitor.getPaths();
133         assertEquals(paths.toString(), 1, paths.size());
134         assertPath(paths.get(0), "a", "b");
135     }
136 
137     private static class ArtifactMatcher implements DependencyFilter {
138         public boolean accept(DependencyNode node, List<DependencyNode> parents) {
139             return node.getDependency() != null
140                     && node.getDependency().getArtifact().getGroupId().equals("match");
141         }
142     }
143 }