1 package org.eclipse.aether.util.graph.visitor;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.junit.Assert.*;
23
24 import java.util.List;
25
26 import org.eclipse.aether.graph.DependencyFilter;
27 import org.eclipse.aether.graph.DependencyNode;
28 import org.eclipse.aether.internal.test.util.DependencyGraphParser;
29 import org.junit.Test;
30
31 public class PathRecordingDependencyVisitorTest
32 {
33
34 private DependencyNode parse( String resource )
35 throws Exception
36 {
37 return new DependencyGraphParser( "visitor/path-recorder/" ).parseResource( resource );
38 }
39
40 private void assertPath( List<DependencyNode> actual, String... expected )
41 {
42 assertEquals( actual.toString(), expected.length, actual.size() );
43 for ( int i = 0; i < expected.length; i++ )
44 {
45 DependencyNode node = actual.get( i );
46 assertEquals( actual.toString(), expected[i], node.getDependency().getArtifact().getArtifactId() );
47 }
48 }
49
50 @Test
51 public void testGetPaths_RecordsMatchesBeneathUnmatchedParents()
52 throws Exception
53 {
54 DependencyNode root = parse( "simple.txt" );
55
56 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() );
57 root.accept( visitor );
58
59 List<List<DependencyNode>> paths = visitor.getPaths();
60 assertEquals( paths.toString(), 2, paths.size() );
61 assertPath( paths.get( 0 ), "a", "b", "x" );
62 assertPath( paths.get( 1 ), "a", "x" );
63 }
64
65 @Test
66 public void testGetPaths_DoesNotRecordMatchesBeneathMatchedParents()
67 throws Exception
68 {
69 DependencyNode root = parse( "nested.txt" );
70
71 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher() );
72 root.accept( visitor );
73
74 List<List<DependencyNode>> paths = visitor.getPaths();
75 assertEquals( paths.toString(), 1, paths.size() );
76 assertPath( paths.get( 0 ), "x" );
77 }
78
79 @Test
80 public void testGetPaths_RecordsMatchesBeneathMatchedParentsIfRequested()
81 throws Exception
82 {
83 DependencyNode root = parse( "nested.txt" );
84
85 PathRecordingDependencyVisitor visitor = new PathRecordingDependencyVisitor( new ArtifactMatcher(), false );
86 root.accept( visitor );
87
88 List<List<DependencyNode>> paths = visitor.getPaths();
89 assertEquals( paths.toString(), 3, paths.size() );
90 assertPath( paths.get( 0 ), "x" );
91 assertPath( paths.get( 1 ), "x", "a", "y" );
92 assertPath( paths.get( 2 ), "x", "y" );
93 }
94
95 @Test
96 public void testFilterCalledWithProperParentStack()
97 throws Exception
98 {
99 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 private static class ArtifactMatcher
139 implements DependencyFilter
140 {
141 public boolean accept( DependencyNode node, List<DependencyNode> parents )
142 {
143 return node.getDependency() != null && node.getDependency().getArtifact().getGroupId().equals( "match" );
144 }
145 }
146
147 }