1   package com.lexicalscope.jewel.cli;
2   
3   import static ch.lambdaj.Lambda.selectFirst;
4   
5   import org.hamcrest.Description;
6   import org.hamcrest.Matcher;
7   import org.hamcrest.TypeSafeMatcher;
8   
9   import com.lexicalscope.jewel.UtilitiesForTesting;
10  
11  /*
12   * Copyright 2011 Tim Wood
13   *
14   * Licensed under the Apache License, Version 2.0 (the "License");
15   * you may not use this file except in compliance with the License.
16   * You may obtain a copy of the License at
17   *
18   * http://www.apache.org/licenses/LICENSE-2.0
19   *
20   * Unless required by applicable law or agreed to in writing, software
21   * distributed under the License is distributed on an "AS IS" BASIS,
22   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23   * See the License for the specific language governing permissions and
24   * limitations under the License. 
25   */
26  
27  public class ArgumentValidationExceptionMatcher {
28      public static Matcher<ArgumentValidationException> hasValidationException(final ValidationFailureType expectedErrorType) {
29          return new TypeSafeMatcher<ArgumentValidationException>() {
30              @Override public void describeTo(final Description description) {
31                  description
32                          .appendText(ArgumentValidationException.class.getSimpleName())
33                          .appendText(" with error type ")
34                          .appendValue(expectedErrorType);
35              }
36      
37              @Override protected boolean matchesSafely(final ArgumentValidationException item) {
38                  return selectFirst(item.getValidationFailures(), new TypeSafeMatcher<ValidationFailureImpl>() {
39                      @Override public void describeTo(final Description description) {
40                          description
41                                  .appendText(ValidationFailureImpl.class.getSimpleName())
42                                  .appendText(" with error type ")
43                                  .appendValue(expectedErrorType);
44                      }
45      
46                      @Override protected boolean matchesSafely(final ValidationFailureImpl actualError) {
47                          return actualError.getFailureType().equals(expectedErrorType);
48                      }
49                  }) != null;
50              }
51          };
52      }
53  
54      public static Matcher<ArgumentValidationException> validationExceptionWithMessage(final String message) {
55          return new TypeSafeMatcher<ArgumentValidationException>() {
56              @Override public void describeTo(final Description description) {
57                  description.appendText(ArgumentValidationException.class.getSimpleName() + " with message ").appendValue(
58                          message);
59              }
60  
61              @Override protected boolean matchesSafely(final ArgumentValidationException item) {
62                  return item.getMessage().equals(message);
63              }
64          };
65      }
66  
67      public static Matcher<ArgumentValidationException> validationExceptionWithMessageLines(final String... message) {
68          return validationExceptionWithMessage(UtilitiesForTesting.joinLines(message));
69      }
70  }