1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
package com.yahoo.vespa.maven.plugin.enforcer;// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
import org.junit.jupiter.api.Test;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author bjorncs
*/
class EnforceDependenciesTest {
@Test
void succeeds_when_all_dependencies_and_rules_match() {
Set<Artifact> dependencies = Set.of(
artifact("com.yahoo.vespa", "container-core", "8.0.0", "provided"),
artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
Set<String> rules = Set.of(
"com.yahoo.vespa:container-core:*:jar:provided",
"com.yahoo.vespa:*:*:jar:test");
assertDoesNotThrow(() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
}
@Test
void fails_on_unmatched_dependency() {
Set<Artifact> dependencies = Set.of(
artifact("com.yahoo.vespa", "container-core", "8.0.0", "provided"),
artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
Set<String> rules = Set.of("com.yahoo.vespa:*:*:jar:test");
EnforcerRuleException exception = assertThrows(
EnforcerRuleException.class,
() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
String expectedErrorMessage =
"""
Vespa dependency enforcer failed:
Dependencies not matching any rule:
- com.yahoo.vespa:container-core:jar:8.0.0:provided
""";
assertEquals(expectedErrorMessage, exception.getMessage());
}
@Test
void fails_on_unmatched_rule() {
Set<Artifact> dependencies = Set.of(
artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
Set<String> rules = Set.of(
"com.yahoo.vespa:container-core:*:jar:provided",
"com.yahoo.vespa:*:*:jar:test");
EnforcerRuleException exception = assertThrows(
EnforcerRuleException.class,
() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
String expectedErrorMessage =
"""
Vespa dependency enforcer failed:
Rules not matching any dependency:
- com.yahoo.vespa:container-core:*:jar:provided
""";
assertEquals(expectedErrorMessage, exception.getMessage());
}
@Test
void fails_on_version_mismatch() {
Set<Artifact> dependencies = Set.of(
artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
Set<String> rules = Set.of(
"com.yahoo.vespa:testutils:7.0.0:jar:test");
EnforcerRuleException exception = assertThrows(
EnforcerRuleException.class,
() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
String expectedErrorMessage =
"""
Vespa dependency enforcer failed:
Dependencies not matching any rule:
- com.yahoo.vespa:testutils:jar:8.0.0:test
Rules not matching any dependency:
- com.yahoo.vespa:testutils:7.0.0:jar:test
""";
assertEquals(expectedErrorMessage, exception.getMessage());
}
@Test
void fails_on_scope_mismatch() {
Set<Artifact> dependencies = Set.of(
artifact("com.yahoo.vespa", "testutils", "8.0.0", "test"));
Set<String> rules = Set.of(
"com.yahoo.vespa:testutils:8.0.0:jar:provided");
EnforcerRuleException exception = assertThrows(
EnforcerRuleException.class,
() -> EnforceDependencies.validateDependencies(dependencies, rules, true));
String expectedErrorMessage =
"""
Vespa dependency enforcer failed:
Dependencies not matching any rule:
- com.yahoo.vespa:testutils:jar:8.0.0:test
Rules not matching any dependency:
- com.yahoo.vespa:testutils:8.0.0:jar:provided
""";
assertEquals(expectedErrorMessage, exception.getMessage());
}
private static Artifact artifact(String groupId, String artifactId, String version, String scope) {
return new DefaultArtifact(
groupId, artifactId, version, scope, "jar", /*classifier*/null, new DefaultArtifactHandler("jar"));
}
}
|