View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Hamcrest matcher to help create fluent assertions about {@link ProjectBuildingResult} instances.
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  }