1   package com.lexicalscope.jewel.cli;
2   
3   import static org.hamcrest.Matchers.allOf;
4   
5   import org.hamcrest.Description;
6   import org.hamcrest.Matcher;
7   import org.hamcrest.TypeSafeMatcher;
8   
9   /*
10   * Copyright 2012 Tim Wood
11   *
12   * Licensed under the Apache License, Version 2.0 (the "License");
13   * you may not use this file except in compliance with the License.
14   * You may obtain a copy of the License at
15   *
16   * http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing, software
19   * distributed under the License is distributed on an "AS IS" BASIS,
20   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   * See the License for the specific language governing permissions and
22   * limitations under the License.
23   */
24  
25  public class ValidationFailureMatcher {
26      public static Matcher<ValidationFailure> validationError(final ValidationFailureType failureType, final String message) {
27          return allOf(validationError(failureType), validationError(message));
28      }
29  
30      public static Matcher<ValidationFailure> validationError(final String message) {
31          return new TypeSafeMatcher<ValidationFailure>() {
32              @Override public void describeTo(final Description description) {
33                  description.appendText("validation failure with message ").appendValue(message);
34              }
35  
36              @Override protected boolean matchesSafely(final ValidationFailure item) {
37                  return item.getMessage().equals(message);
38              }};
39      }
40  
41      public static Matcher<ValidationFailure> validationError(final ValidationFailureType failureType) {
42          return new TypeSafeMatcher<ValidationFailure>() {
43              @Override public void describeTo(final Description description) {
44                  description.appendText("validation failure of type ").appendValue(failureType);
45              }
46  
47              @Override protected boolean matchesSafely(final ValidationFailure item) {
48                  return item.getFailureType().equals(failureType);
49              }};
50      }
51  }