org.hamcrest.DiagnosingMatcher Java Examples

The following examples show how to use org.hamcrest.DiagnosingMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CloudFoundryAppNameGeneratorTest.java    From spring-cloud-deployer-cloudfoundry with Apache License 2.0 6 votes vote down vote up
private Matcher<String> matchesPattern(final String pattern) {
	return new DiagnosingMatcher<String>() {
		@Override
		protected boolean matches(Object item, Description mismatchDescription) {
			if (!((String) item).matches(pattern)) {
				mismatchDescription.appendValue(item).appendText(" did not match regex ").appendValue(pattern);
			}
			return ((String) item).matches(pattern);
		}

		@Override
		public void describeTo(Description description) {
			description.appendText("a string matching regex ").appendValue(pattern);
		}
	};
}
 
Example #2
Source File: ThrowableTranslatorTest.java    From reactive-ms-example with MIT License 5 votes vote down vote up
@Factory
private static DiagnosingMatcher<Object> translateTo(HttpStatus status) {
    return new DiagnosingMatcher<Object>() {
        private static final String EXCEPTION = "EXCEPTION";

        @Override
        public void describeTo(final Description description) {
            description.appendText("does not translate to ").appendText(status.toString());
        }

        @SuppressWarnings("unchecked")
        protected boolean matches(final Object item, final Description mismatch) {

            if (item instanceof Class) {
                if (((Class) item).getClass().isInstance(Throwable.class)) {
                    Class<? extends Throwable> type = (Class<? extends Throwable>) item;
                    try {
                        Throwable exception = type.getConstructor(String.class).newInstance(EXCEPTION);
                        Mono.just(exception).transform(ThrowableTranslator::translate).subscribe(translator -> {
                            assertThat(translator.getMessage(), is(EXCEPTION));
                            assertThat(translator.getHttpStatus(), is(status));
                        });
                    } catch (InstantiationException | IllegalAccessException |
                            InvocationTargetException | NoSuchMethodException cause) {
                        throw new AssertionError("This exception class has not constructor with a String", cause);
                    }
                    return true;
                }
            }
            mismatch.appendText(item.toString());
            return false;
        }
    };
}
 
Example #3
Source File: TableMatcher.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
public static DiagnosingMatcher<Table> hasRowThat(Matcher<?>... cells) {
	return new DiagnosingMatcher<Table>() {
		@Override
		protected boolean matches(Object item, Description mismatchDescription) {
			TableModel model = ((Table) item).getModel();
			outer: for (int row = 0; row < model.getRowCount(); row++) {
				mismatchDescription.appendText("\nRow " + row + ": ");
				for (int col = 0; col < cells.length; col++) {
					mismatchDescription.appendText("\n  Column " + col + ": ");
					cells[col].describeMismatch(model.getValue(row, col), mismatchDescription);
					if (!cells[col].matches(model.getValue(row, col))) {
						continue outer;
					}
				}
				return true;
			}
			return false;
		}

		@Override
		public void describeTo(Description description) {
			description.appendText("a table having at least one row that\n");
			for (int col = 0; col < cells.length; col++) {
				description.appendText("column " + col + ": ");
				cells[col].describeTo(description);
				description.appendText("\n");
			}
		}
	};
}
 
Example #4
Source File: Matchers.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
public static <T> Function1<T, String> diagnoseMismatch(final DiagnosingMatcher matcher) {
    return t -> {
        StringDescription mismatchDescription = new StringDescription();
        matcher.describeMismatch(t, mismatchDescription);
        return mismatchDescription.toString();
    };
}
 
Example #5
Source File: Matchers.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static <T> Function1<T, String> describeMismatch(final Matcher<? super T> matcher) {
    if (matcher instanceof DiagnosingMatcher)
        return diagnoseMismatch((DiagnosingMatcher) matcher);
    return returns1(StringDescription.asString(matcher));
}
 
Example #6
Source File: Matchers.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public static <T> Function1<T, String> diagnoseMismatch(Class<T> type, final DiagnosingMatcher matcher) {
    return diagnoseMismatch(matcher);
}