1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.util.graph.transformer;
20
21 import java.util.Locale;
22
23 import org.eclipse.aether.collection.DependencyGraphTransformer;
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 org.junit.Assert.*;
29
30 public class JavaScopeSelectorTest extends AbstractDependencyGraphTransformerTest {
31
32 private enum Scope {
33 TEST,
34 PROVIDED,
35 RUNTIME,
36 COMPILE;
37
38 @Override
39 public String toString() {
40 return super.name().toLowerCase(Locale.ENGLISH);
41 }
42 }
43
44 @Override
45 protected DependencyGraphTransformer newTransformer() {
46 return new ConflictResolver(
47 new NearestVersionSelector(), new JavaScopeSelector(),
48 new SimpleOptionalitySelector(), new JavaScopeDeriver());
49 }
50
51 @Override
52 protected DependencyGraphParser newParser() {
53 return new DependencyGraphParser("transformer/scope-calculator/");
54 }
55
56 private void expectScope(String expected, DependencyNode root, int... coords) {
57 expectScope(null, expected, root, coords);
58 }
59
60 private void expectScope(String msg, String expected, DependencyNode root, int... coords) {
61 if (msg == null) {
62 msg = "";
63 }
64 try {
65 DependencyNode node = root;
66 node = path(node, coords);
67
68 assertEquals(
69 msg + "\nculprit: " + node.toString() + "\n",
70 expected,
71 node.getDependency().getScope());
72 } catch (IndexOutOfBoundsException | NullPointerException e) {
73 throw new IllegalArgumentException("illegal coordinates for child", e);
74 }
75 }
76
77 private DependencyNode path(DependencyNode node, int... coords) {
78 for (int coord : coords) {
79 node = node.getChildren().get(coord);
80 }
81 return node;
82 }
83
84 @Test
85 public void testScopeInheritanceProvided() throws Exception {
86 String resource = "inheritance.txt";
87
88 String expected = "test";
89 DependencyNode root = transform(parseResource(resource, "provided", "test"));
90 expectScope(parser.dump(root), expected, root, 0, 0);
91 }
92
93 @Test
94 public void testConflictWinningScopeGetsUsedForInheritance() throws Exception {
95 DependencyNode root = parseResource("conflict-and-inheritance.txt");
96 assertSame(root, transform(root));
97
98 expectScope("compile", root, 0, 0);
99 expectScope("compile", root, 0, 0, 0);
100 }
101
102 @Test
103 public void testScopeOfDirectDependencyWinsConflictAndGetsUsedForInheritanceToChildrenEverywhereInGraph()
104 throws Exception {
105 DependencyNode root = parseResource("direct-with-conflict-and-inheritance.txt");
106 assertSame(root, transform(root));
107
108 expectScope("test", root, 0, 0);
109 }
110
111 @Test
112 public void testCycleA() throws Exception {
113 DependencyNode root = parseResource("cycle-a.txt");
114 assertSame(root, transform(root));
115
116 expectScope("compile", root, 0);
117 expectScope("runtime", root, 1);
118 }
119
120 @Test
121 public void testCycleB() throws Exception {
122 DependencyNode root = parseResource("cycle-b.txt");
123 assertSame(root, transform(root));
124
125 expectScope("runtime", root, 0);
126 expectScope("compile", root, 1);
127 }
128
129 @Test
130 public void testCycleC() throws Exception {
131 DependencyNode root = parseResource("cycle-c.txt");
132 assertSame(root, transform(root));
133
134 expectScope("runtime", root, 0);
135 expectScope("runtime", root, 0, 0);
136 expectScope("runtime", root, 1);
137 expectScope("runtime", root, 1, 0);
138 }
139
140 @Test
141 public void testCycleD() throws Exception {
142 DependencyNode root = parseResource("cycle-d.txt");
143 assertSame(root, transform(root));
144
145 expectScope("compile", root, 0);
146 expectScope("compile", root, 0, 0);
147 }
148
149 @Test
150 public void testDirectNodesAlwaysWin() throws Exception {
151
152 for (Scope directScope : Scope.values()) {
153 String direct = directScope.toString();
154
155 DependencyNode root = parseResource("direct-nodes-winning.txt", direct);
156
157 String msg = String.format(
158 "direct node should be setting scope ('%s') for all nodes.\n" + parser.dump(root), direct);
159 assertSame(root, transform(root));
160 msg += "\ntransformed:\n" + parser.dump(root);
161
162 expectScope(msg, direct, root, 0);
163 }
164 }
165
166 @Test
167 public void testNonDirectMultipleInheritance() throws Exception {
168 for (Scope scope1 : Scope.values()) {
169 for (Scope scope2 : Scope.values()) {
170 DependencyNode root = parseResource("multiple-inheritance.txt", scope1.toString(), scope2.toString());
171
172 String expected = scope1.compareTo(scope2) >= 0 ? scope1.toString() : scope2.toString();
173 String msg = String.format("expected '%s' to win\n" + parser.dump(root), expected);
174
175 assertSame(root, transform(root));
176 msg += "\ntransformed:\n" + parser.dump(root);
177
178 expectScope(msg, expected, root, 0, 0);
179 }
180 }
181 }
182
183 @Test
184 public void testConflictScopeOrdering() throws Exception {
185 for (Scope scope1 : Scope.values()) {
186 for (Scope scope2 : Scope.values()) {
187 DependencyNode root = parseResource("dueling-scopes.txt", scope1.toString(), scope2.toString());
188
189 String expected = scope1.compareTo(scope2) >= 0 ? scope1.toString() : scope2.toString();
190 String msg = String.format("expected '%s' to win\n" + parser.dump(root), expected);
191
192 assertSame(root, transform(root));
193 msg += "\ntransformed:\n" + parser.dump(root);
194
195 expectScope(msg, expected, root, 0, 0);
196 }
197 }
198 }
199
200
201
202
203 @Test
204 public void testConflictingDirectNodes() throws Exception {
205 for (Scope scope1 : Scope.values()) {
206 for (Scope scope2 : Scope.values()) {
207 DependencyNode root =
208 parseResource("conflicting-direct-nodes.txt", scope1.toString(), scope2.toString());
209
210 String expected = scope1.toString();
211 String msg = String.format("expected '%s' to win\n" + parser.dump(root), expected);
212
213 assertSame(root, transform(root));
214 msg += "\ntransformed:\n" + parser.dump(root);
215
216 expectScope(msg, expected, root, 0);
217 }
218 }
219 }
220 }