Search in sources :

Example 6 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project neo4j by neo4j.

the class TransactionMatchers method graphContainsDeletedNodes.

public static Matcher<? super HTTP.Response> graphContainsDeletedNodes(final int amount) {
    return new TypeSafeMatcher<HTTP.Response>() {

        @Override
        protected boolean matchesSafely(HTTP.Response response) {
            try {
                Iterator<JsonNode> nodes = getJsonNodeWithName(response, "graph").get("nodes").iterator();
                for (int i = 0; i < amount; ++i) {
                    assertTrue(nodes.hasNext());
                    JsonNode node = nodes.next();
                    assertThat(node.get("deleted").asBoolean(), equalTo(Boolean.TRUE));
                }
                while (nodes.hasNext()) {
                    JsonNode node = nodes.next();
                    assertNull(node.get("deleted"));
                }
                return true;
            } catch (JsonParseException e) {
                return false;
            }
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) HTTP(org.neo4j.test.server.HTTP) JsonNode(org.codehaus.jackson.JsonNode) JsonParseException(org.neo4j.server.rest.domain.JsonParseException)

Example 7 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project neo4j by neo4j.

the class StreamMatchers method equalsStream.

public static Matcher<BoltResult> equalsStream(final String[] fieldNames, final Matcher... records) {
    return new TypeSafeMatcher<BoltResult>() {

        @Override
        protected boolean matchesSafely(BoltResult item) {
            if (!Arrays.equals(fieldNames, item.fieldNames())) {
                return false;
            }
            final Iterator<Matcher> expected = asList(records).iterator();
            final AtomicBoolean matched = new AtomicBoolean(true);
            try {
                item.accept(new BoltResult.Visitor() {

                    @Override
                    public void visit(Record record) {
                        if (!expected.hasNext() || !expected.next().matches(record)) {
                            matched.set(false);
                        }
                    }

                    @Override
                    public void addMetadata(String key, Object value) {
                    }
                });
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            // All records matched, and there are no more expected records.
            return matched.get() && !expected.hasNext();
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("Stream[").appendValueList(" fieldNames=[", ",", "]", fieldNames).appendList(", records=[", ",", "]", asList(records));
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 8 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project neo4j by neo4j.

the class MessageMatchers method msgFailure.

public static Matcher<ResponseMessage> msgFailure(final Status status, final String message) {
    return new TypeSafeMatcher<ResponseMessage>() {

        @Override
        protected boolean matchesSafely(ResponseMessage t) {
            assertThat(t, instanceOf(FailureMessage.class));
            FailureMessage msg = (FailureMessage) t;
            assertThat(msg.status(), equalTo(status));
            assertThat(msg.message(), containsString(message));
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("FAILURE");
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) FailureMessage(org.neo4j.bolt.v1.messaging.message.FailureMessage) ResponseMessage(org.neo4j.bolt.v1.messaging.message.ResponseMessage)

Example 9 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project neo4j by neo4j.

the class MessageMatchers method msgRecord.

public static Matcher<ResponseMessage> msgRecord(final Matcher<Record> matcher) {
    return new TypeSafeMatcher<ResponseMessage>() {

        @Override
        protected boolean matchesSafely(ResponseMessage t) {
            assertThat(t, instanceOf(RecordMessage.class));
            RecordMessage msg = (RecordMessage) t;
            assertThat(msg.record(), matcher);
            return true;
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("RECORD ");
            description.appendDescriptionOf(matcher);
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ResponseMessage(org.neo4j.bolt.v1.messaging.message.ResponseMessage) RecordMessage(org.neo4j.bolt.v1.messaging.message.RecordMessage)

Example 10 with TypeSafeMatcher

use of org.hamcrest.TypeSafeMatcher in project wire-android by wireapp.

the class CustomMatchers method otrSwitchWithId.

public static Matcher<View> otrSwitchWithId(final int id) {
    return new TypeSafeMatcher<View>() {

        Resources resources = null;

        @Override
        public void describeTo(Description description) {
            String idDescription = Integer.toString(id);
            if (resources != null) {
                try {
                    idDescription = resources.getResourceName(id);
                } catch (Resources.NotFoundException e) {
                    // No big deal, will just use the int value.
                    idDescription = String.format("%s (resource name not found)", id);
                }
            }
            description.appendText("with id: " + idDescription);
        }

        @Override
        public boolean matchesSafely(View view) {
            resources = view.getResources();
            ViewParent parent = view.getParent();
            return parent != null && parent instanceof OtrSwitch && id == ((OtrSwitch) parent).getId() && view instanceof SwitchCompat;
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.hamcrest.Description) ViewParent(android.view.ViewParent) Resources(android.content.res.Resources) OtrSwitch(com.waz.zclient.ui.views.e2ee.OtrSwitch) View(android.view.View) SwitchCompat(android.support.v7.widget.SwitchCompat)

Aggregations

TypeSafeMatcher (org.hamcrest.TypeSafeMatcher)82 Description (org.hamcrest.Description)79 View (android.view.View)44 ViewParent (android.view.ViewParent)33 ViewGroup (android.view.ViewGroup)30 Espresso.onView (android.support.test.espresso.Espresso.onView)25 TextView (android.widget.TextView)9 Test (org.junit.Test)9 ViewMatchers.withContentDescription (android.support.test.espresso.matcher.ViewMatchers.withContentDescription)7 JsonNode (org.codehaus.jackson.JsonNode)6 JsonParseException (org.neo4j.server.rest.domain.JsonParseException)6 HTTP (org.neo4j.test.server.HTTP)6 Resources (android.content.res.Resources)5 StringDescription (org.hamcrest.StringDescription)5 AdapterView (android.widget.AdapterView)4 RecyclerView (android.support.v7.widget.RecyclerView)3 Adapter (android.widget.Adapter)3 Espresso.onView (com.google.android.apps.common.testing.ui.espresso.Espresso.onView)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3