Search in sources :

Example 6 with BaseMatcher

use of org.hamcrest.BaseMatcher in project BuildRadiator by BuildRadiator.

the class HasNewRadiator method captureCreatedRadiator.

public static BaseMatcher<String> captureCreatedRadiator(CreatedRadiator createdRadiator) {
    return new BaseMatcher<String>() {

        @Override
        public boolean matches(Object o) {
            try {
                CreatedRadiator cr = new ObjectMapper().readValue((String) o, CreatedRadiator.class);
                createdRadiator.code = cr.code;
                createdRadiator.secret = cr.secret;
            } catch (IOException e) {
                fail("IOE encountered " + e.getMessage());
            }
            return true;
        }

        @Override
        public void describeTo(Description description) {
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) IOException(java.io.IOException) CreatedRadiator(com.paulhammant.buildradiator.model.CreatedRadiator) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with BaseMatcher

use of org.hamcrest.BaseMatcher in project jetty.project by eclipse.

the class FileSystemResourceTest method isAliasFor.

private Matcher<Resource> isAliasFor(final Resource resource) {
    return new BaseMatcher<Resource>() {

        @Override
        public boolean matches(Object item) {
            final Resource ritem = (Resource) item;
            final URI alias = ritem.getAlias();
            if (alias == null) {
                return ritem == null;
            } else {
                return alias.equals(resource.getURI());
            }
        }

        @Override
        public void describeTo(Description description) {
            description.appendText("getAlias should return ").appendValue(resource.getURI());
        }

        @Override
        public void describeMismatch(Object item, Description description) {
            description.appendText("was ").appendValue(((Resource) item).getAlias());
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) URI(java.net.URI)

Example 8 with BaseMatcher

use of org.hamcrest.BaseMatcher in project gocd by gocd.

the class GoConfigServiceTest method hasValues.

private Matcher<PipelineSelections> hasValues(final List<String> isVisible, final List<String> isNotVisible, final Date today, final Long userId) {
    return new BaseMatcher<PipelineSelections>() {

        public boolean matches(Object o) {
            PipelineSelections pipelineSelections = (PipelineSelections) o;
            assertHasSelected(pipelineSelections, isVisible);
            assertHasSelected(pipelineSelections, isNotVisible, false);
            assertThat(pipelineSelections.lastUpdated(), is(today));
            assertThat(pipelineSelections.userId(), is(userId));
            return true;
        }

        public void describeTo(Description description) {
        }
    };
}
Also used : PipelineSelections(com.thoughtworks.go.server.domain.user.PipelineSelections) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher)

Example 9 with BaseMatcher

use of org.hamcrest.BaseMatcher in project iosched by google.

the class MatchersHelper method approximateTimeUriMatcher.

/**
     * Some {@link Uri} include specific times, for example all sessions after a given time. Even
     * though time can be set in tests, {@link com.google.samples.apps.iosched.util.TimeUtils
     * .getCurrentTime()} takes into account the passage of time so two separate calls to it will
     * return slightly different values. This matcher applies an approximation of 10 seconds.
     * <p/>
     * Note: it assumes the time portion of the {@link Uri} is the last segment.
     */
public static Matcher<Uri> approximateTimeUriMatcher(final Uri expectedUri) {
    return new BaseMatcher<Uri>() {

        @Override
        public void describeTo(final Description description) {
            description.appendText("approximateTimeUriMatcher");
        }

        @Override
        public boolean matches(final Object item) {
            if (item instanceof Uri) {
                Uri actualUri = (Uri) item;
                // Check the last segment is a time
                long expectedTime = 0L;
                long actualTime = 0L;
                try {
                    expectedTime = Long.parseLong(expectedUri.getLastPathSegment());
                } catch (NumberFormatException e) {
                    return false;
                }
                try {
                    actualTime = Long.parseLong(actualUri.getLastPathSegment());
                } catch (NumberFormatException e) {
                    return false;
                }
                // If the times are within 10 seconds of each other, check other segments
                if (Math.abs(actualTime - expectedTime) < TimeUtils.SECOND * 10) {
                    List<String> actualSegments = actualUri.getPathSegments();
                    List<String> expectedSegments = expectedUri.getPathSegments();
                    // If same number of segments
                    if (actualSegments.size() == expectedSegments.size()) {
                        boolean diffFound = false;
                        // Check each segment except the last
                        for (int i = 0; i < actualSegments.size() - 1; i++) {
                            if (!actualSegments.get(i).equals(expectedSegments.get(i))) {
                                diffFound = true;
                            }
                        }
                        // They match only if no differences were found
                        return !diffFound;
                    }
                }
            }
            return false;
        }

        @Override
        public void describeMismatch(final Object item, final Description mismatchDescription) {
            mismatchDescription.appendText("item doesn't match uri " + expectedUri + " with an approximataion of 10 seconds for the last time segment");
        }
    };
}
Also used : Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) Uri(android.net.Uri)

Example 10 with BaseMatcher

use of org.hamcrest.BaseMatcher in project gradle by gradle.

the class LoggingTest method expectLogMessage.

private void expectLogMessage(final LogLevel level, final String text) {
    final Matcher<LogEvent> matcher = new BaseMatcher<LogEvent>() {

        public void describeTo(Description description) {
            description.appendText("level: ").appendValue(level).appendText(", text:").appendValue(text);
        }

        public boolean matches(Object o) {
            LogEvent event = (LogEvent) o;
            return event.getLogLevel() == level && event.getMessage().equals(text);
        }
    };
    context.checking(new Expectations() {

        {
            one(outputEventListener).onOutput(with(matcher));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) Description(org.hamcrest.Description) BaseMatcher(org.hamcrest.BaseMatcher) LogEvent(org.gradle.internal.logging.events.LogEvent)

Aggregations

BaseMatcher (org.hamcrest.BaseMatcher)25 Description (org.hamcrest.Description)25 Test (org.junit.Test)6 Bundle (android.os.Bundle)4 List (java.util.List)3 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)3 HttpMessage (org.parosproxy.paros.network.HttpMessage)3 PipelineSelections (com.thoughtworks.go.server.domain.user.PipelineSelections)2 URI (java.net.URI)2 StatementProcessor (org.neo4j.bolt.v1.runtime.StatementProcessor)2 Uri (android.net.Uri)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CreatedRadiator (com.paulhammant.buildradiator.model.CreatedRadiator)1 IOException (java.io.IOException)1 ClassDefinition (java.lang.instrument.ClassDefinition)1 NoRouteToHostException (java.net.NoRouteToHostException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1