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 r) {
41              return r.getProblems().stream().anyMatch(p -> p.getMessage().contains(problemMessage));
42          } else {
43              return false;
44          }
45      }
46  
47      @Override
48      public void describeTo(Description description) {
49          description.appendText("a ProjectBuildingResult with message ").appendValue(problemMessage);
50      }
51  
52      @Override
53      public void describeMismatch(final Object o, final Description description) {
54          if (o instanceof ProjectBuildingResult r) {
55              description.appendText("was a ProjectBuildingResult with messages ");
56              String messages = r.getProblems().stream()
57                      .map(ModelProblem::getMessage)
58                      .map(m -> "\"" + m + "\"" + System.lineSeparator())
59                      .collect(joining(", "));
60              description.appendText(messages);
61          } else {
62              super.describeMismatch(o, description);
63          }
64      }
65  
66      static Matcher<ProjectBuildingResult> projectBuildingResultWithProblemMessage(String message) {
67          return new ProjectBuildingResultWithProblemMessageMatcher(message);
68      }
69  }