1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import org.hamcrest.BaseMatcher;
22 import org.hamcrest.Description;
23 import org.hamcrest.Matcher;
24
25 import static java.util.stream.Collectors.joining;
26
27
28
29
30 class ProjectBuildingResultWithLocationMatcher extends BaseMatcher<ProjectBuildingResult> {
31 private final int columnNumber;
32 private final int lineNumber;
33
34 ProjectBuildingResultWithLocationMatcher(int columnNumber, int lineNumber) {
35 this.columnNumber = columnNumber;
36 this.lineNumber = lineNumber;
37 }
38
39 @Override
40 public boolean matches(Object o) {
41 if (o instanceof ProjectBuildingResult r) {
42 return r.getProblems().stream()
43 .anyMatch(p -> p.getLineNumber() == lineNumber && p.getColumnNumber() == columnNumber);
44 } else {
45 return false;
46 }
47 }
48
49 @Override
50 public void describeTo(Description description) {
51 description
52 .appendText("a ProjectBuildingResult with location ")
53 .appendText(formatLocation(columnNumber, lineNumber));
54 }
55
56 private String formatLocation(int columnNumber, int lineNumber) {
57 return String.format("line %d, column %d", lineNumber, columnNumber);
58 }
59
60 @Override
61 public void describeMismatch(final Object o, final Description description) {
62 if (o instanceof ProjectBuildingResult r) {
63 description.appendText("was a ProjectBuildingResult with locations ");
64 String messages = r.getProblems().stream()
65 .map(p -> formatLocation(p.getColumnNumber(), p.getLineNumber()))
66 .collect(joining(", "));
67 description.appendText(messages);
68 } else {
69 super.describeMismatch(o, description);
70 }
71 }
72
73 static Matcher<ProjectBuildingResult> projectBuildingResultWithLocation(int columnNumber, int lineNumber) {
74 return new ProjectBuildingResultWithLocationMatcher(columnNumber, lineNumber);
75 }
76 }