org.junit.internal.matchers.TypeSafeMatcher Java Examples

The following examples show how to use org.junit.internal.matchers.TypeSafeMatcher. 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: JavaMatchers.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if object has proper toString defined. This matcher can be
 * replaced with JUnit 4.8
 */
public static Matcher<Object> hasToString() {
    return new TypeSafeMatcher<Object>() {

        @Override
        public boolean matchesSafely(Object objectToTest) {
            try {
                objectToTest.getClass().getDeclaredMethod("toString");
            } catch (Exception e) {
                return false;
            }
            String s = objectToTest.toString();
            if (s == null || s.length() == 0) {
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("proper toString()");
        }

    };
}
 
Example #2
Source File: JavaMatchers.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if array is non-empty. This matcher can be replaced with JUnit 4.8
 */
public static Matcher<Object[]> hasItemInArray() {
    return new TypeSafeMatcher<Object[]>() {

        @Override
        public boolean matchesSafely(Object[] arrayToTest) {
            if (arrayToTest == null || arrayToTest.length == 0) {
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("non-empty array");
        }

    };
}
 
Example #3
Source File: JavaMatchers.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if list is non-empty. This matcher can be replaced with JUnit 4.8
 */
public static Matcher<Collection<?>> hasItems() {
    return new TypeSafeMatcher<Collection<?>>() {

        @Override
        public boolean matchesSafely(Collection<?> collection) {
            if (collection == null || collection.isEmpty()) {
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("non-empty list");
        }

    };
}
 
Example #4
Source File: JavaMatchers.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if collection has the given number of items.
 */
public static Matcher<Collection<?>> hasItems(final int numberOfItems) {
    return new TypeSafeMatcher<Collection<?>>() {

        @Override
        public boolean matchesSafely(Collection<?> collection) {
            if (collection == null || collection.size() != numberOfItems) {
                return false;
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("expected a collection with "
                    + numberOfItems + " items");
        }

    };
}
 
Example #5
Source File: SSTableImportTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static Matcher<UntypedResultSet.Row> withElements(final int key, final String v1, final int v2) {
    return new TypeSafeMatcher<UntypedResultSet.Row>()
    {
        @Override
        public boolean matchesSafely(Row input)
        {
            if (!input.has("k") || !input.has("v1") || !input.has("v2"))
                return false;
            return input.getInt("k") == key
                    && input.getString("v1").equals(v1)
                    && input.getInt("v2") == v2;
        }

        @Override
        public void describeTo(Description description)
        {
            description.appendText(String.format("a row containing: %s, %s, %s", key, v1, v2));
        }
    };
    
}