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.apache.maven.model.building.ModelProblem;
22  import org.hamcrest.BaseMatcher;
23  import org.hamcrest.Description;
24  import org.hamcrest.Matcher;
25  
26  import static java.util.stream.Collectors.joining;
27  
28  /**
29   * Hamcrest matcher to help create fluent assertions about {@link ProjectBuildingResult} instances.
30   */
31  class ProjectBuildingResultWithProblemMessageMatcher extends BaseMatcher<ProjectBuildingResult> {
32      private final String problemMessage;
33  
34      ProjectBuildingResultWithProblemMessageMatcher(String problemMessage) {
35          this.problemMessage = problemMessage;
36      }
37  
38      @Override
39      public boolean matches(Object o) {
40          if (!(o instanceof ProjectBuildingResult)) {
41              return false;
42          }
43  
44          final ProjectBuildingResult r = (ProjectBuildingResult) o;
45  
46          return r.getProblems().stream().anyMatch(p -> p.getMessage().contains(problemMessage));
47      }
48  
49      @Override
50      public void describeTo(Description description) {
51          description.appendText("a ProjectBuildingResult with message ").appendValue(problemMessage);
52      }
53  
54      @Override
55      public void describeMismatch(final Object o, final Description description) {
56          if (!(o instanceof ProjectBuildingResult)) {
57              super.describeMismatch(o, description);
58          } else {
59              final ProjectBuildingResult r = (ProjectBuildingResult) o;
60              description.appendText("was a ProjectBuildingResult with messages ");
61              String messages = r.getProblems().stream()
62                      .map(ModelProblem::getMessage)
63                      .map(m -> "\"" + m + "\"" + System.lineSeparator())
64                      .collect(joining(", "));
65              description.appendText(messages);
66          }
67      }
68  
69      static Matcher<ProjectBuildingResult> projectBuildingResultWithProblemMessage(String message) {
70          return new ProjectBuildingResultWithProblemMessageMatcher(message);
71      }
72  }