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)) {
42 return false;
43 }
44
45 final ProjectBuildingResult r = (ProjectBuildingResult) o;
46
47 return r.getProblems().stream()
48 .anyMatch(p -> p.getLineNumber() == lineNumber && p.getColumnNumber() == columnNumber);
49 }
50
51 @Override
52 public void describeTo(Description description) {
53 description
54 .appendText("a ProjectBuildingResult with location ")
55 .appendText(formatLocation(columnNumber, lineNumber));
56 }
57
58 private String formatLocation(int columnNumber, int lineNumber) {
59 return String.format("line %d, column %d", lineNumber, columnNumber);
60 }
61
62 @Override
63 public void describeMismatch(final Object o, final Description description) {
64 if (!(o instanceof ProjectBuildingResult)) {
65 super.describeMismatch(o, description);
66 } else {
67 final ProjectBuildingResult r = (ProjectBuildingResult) o;
68 description.appendText("was a ProjectBuildingResult with locations ");
69 String messages = r.getProblems().stream()
70 .map(p -> formatLocation(p.getColumnNumber(), p.getLineNumber()))
71 .collect(joining(", "));
72 description.appendText(messages);
73 }
74 }
75
76 static Matcher<ProjectBuildingResult> projectBuildingResultWithLocation(int columnNumber, int lineNumber) {
77 return new ProjectBuildingResultWithLocationMatcher(columnNumber, lineNumber);
78 }
79 }