Search in sources :

Example 1 with Matcher

use of org.hamcrest.Matcher in project crate by crate.

the class SymbolMatchers method isFunction.

public static Matcher<Symbol> isFunction(final String name, @Nullable final List<DataType> argumentTypes) {
    if (argumentTypes == null) {
        return isFunction(name);
    }
    Matcher[] argMatchers = new Matcher[argumentTypes.size()];
    ListIterator<DataType> it = argumentTypes.listIterator();
    while (it.hasNext()) {
        int i = it.nextIndex();
        DataType type = it.next();
        argMatchers[i] = hasDataType(type);
    }
    //noinspection unchecked
    return isFunction(name, argMatchers);
}
Also used : FeatureMatcher(org.hamcrest.FeatureMatcher) Matcher(org.hamcrest.Matcher) DataType(io.crate.types.DataType)

Example 2 with Matcher

use of org.hamcrest.Matcher in project elasticsearch by elastic.

the class DeprecationHttpIT method testUniqueDeprecationResponsesMergedTogether.

/**
     * Attempts to do a scatter/gather request that expects unique responses per sub-request.
     */
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/19222")
public void testUniqueDeprecationResponsesMergedTogether() throws IOException {
    final String[] indices = new String[randomIntBetween(2, 5)];
    // add at least one document for each index
    for (int i = 0; i < indices.length; ++i) {
        indices[i] = "test" + i;
        // create indices with a single shard to reduce noise; the query only deprecates uniquely by index anyway
        assertTrue(prepareCreate(indices[i]).setSettings(Settings.builder().put("number_of_shards", 1)).get().isAcknowledged());
        int randomDocCount = randomIntBetween(1, 2);
        for (int j = 0; j < randomDocCount; ++j) {
            index(indices[i], "type", Integer.toString(j), "{\"field\":" + j + "}");
        }
    }
    refresh(indices);
    final String commaSeparatedIndices = Stream.of(indices).collect(Collectors.joining(","));
    final String body = "{\"query\":{\"bool\":{\"filter\":[{\"" + TestDeprecatedQueryBuilder.NAME + "\":{}}]}}}";
    // trigger all index deprecations
    Response response = getRestClient().performRequest("GET", "/" + commaSeparatedIndices + "/_search", Collections.emptyMap(), new StringEntity(body, ContentType.APPLICATION_JSON));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(OK.getStatus()));
    final List<String> deprecatedWarnings = getWarningHeaders(response.getHeaders());
    final List<Matcher<String>> headerMatchers = new ArrayList<>(indices.length);
    for (String index : indices) {
        headerMatchers.add(containsString(LoggerMessageFormat.format("[{}] index", (Object) index)));
    }
    assertThat(deprecatedWarnings, hasSize(headerMatchers.size()));
    for (Matcher<String> headerMatcher : headerMatchers) {
        assertThat(deprecatedWarnings, hasItem(headerMatcher));
    }
}
Also used : Response(org.elasticsearch.client.Response) StringEntity(org.apache.http.entity.StringEntity) Matcher(org.hamcrest.Matcher) ArrayList(java.util.ArrayList) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with Matcher

use of org.hamcrest.Matcher in project freud by LMAX-Exchange.

the class ClassByteCodeMethodMatchers method methodInvokedWithParams.

public static FreudExtendedMatcher<ClassByteCodeMethod> methodInvokedWithParams(final Class expectedOwner, final String expectedMethodName, final Matcher<OperandStack>... expectedParamsPassed) {
    return new FreudExtendedMatcher<ClassByteCodeMethod>() {

        private String expectedOwnerName;

        {
            expectedOwnerName = typeEncoding(expectedOwner);
        }

        @Override
        protected boolean matchesSafely(final ClassByteCodeMethod item) {
            final boolean[] found = new boolean[1];
            found[0] = false;
            item.findInstruction(new AbstractInstructionVisitor() {

                @Override
                public void methodInvocation(final Instruction instruction, final String owner, final String methodName, final String... args) {
                    if (!found[0] && expectedOwnerName.equals(owner) && expectedMethodName.equals(methodName)) {
                        Instruction prevInstruction = item.getInstruction(instruction.getInstructionIndex() - 1);
                        OperandStack operandStack = prevInstruction.getOperandStack();
                        found[0] = true;
                        for (int i = expectedParamsPassed.length - 1; i >= 0; i--) {
                            Matcher<OperandStack> matcher = expectedParamsPassed[i];
                            if (!matcher.matches(operandStack)) {
                                found[0] = false;
                                break;
                            }
                            operandStack = operandStack.next();
                        }
                    }
                }
            });
            return found[0];
        }

        @Override
        public void describeTo(final Description description) {
            description.appendText("methodInvokedWithParams(" + expectedOwner.getName() + ", " + expectedMethodName + ")");
        }
    };
}
Also used : CastOperandStack(org.freud.analysed.classbytecode.method.instruction.CastOperandStack) OperandStack(org.freud.analysed.classbytecode.method.instruction.OperandStack) Description(org.hamcrest.Description) Matcher(org.hamcrest.Matcher) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) ClassByteCodeMethod(org.freud.analysed.classbytecode.method.ClassByteCodeMethod) Instruction(org.freud.analysed.classbytecode.method.instruction.Instruction) FreudExtendedMatcher(org.freud.java.matcher.FreudExtendedMatcher) AbstractInstructionVisitor(org.freud.analysed.classbytecode.method.instruction.AbstractInstructionVisitor)

Example 4 with Matcher

use of org.hamcrest.Matcher 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 5 with Matcher

use of org.hamcrest.Matcher in project spring-boot by spring-projects.

the class EndpointWebMvcAutoConfigurationTests method assertContent.

private void assertContent(String scheme, String url, int port, Object expected) throws Exception {
    SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
    HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
    ClientHttpRequest request = requestFactory.createRequest(new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
    try {
        ClientHttpResponse response = request.execute();
        if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
            throw new FileNotFoundException();
        }
        try {
            String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
            if (expected instanceof Matcher) {
                assertThat(actual).is(Matched.by((Matcher<?>) expected));
            } else {
                assertThat(actual).isEqualTo(expected);
            }
        } finally {
            response.close();
        }
    } catch (Exception ex) {
        if (expected == null) {
            if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
                return;
            }
        }
        throw ex;
    }
}
Also used : Matcher(org.hamcrest.Matcher) HttpClient(org.apache.http.client.HttpClient) FileNotFoundException(java.io.FileNotFoundException) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) ClientHttpRequest(org.springframework.http.client.ClientHttpRequest) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) URI(java.net.URI) ClientHttpResponse(org.springframework.http.client.ClientHttpResponse) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) FileNotFoundException(java.io.FileNotFoundException) WebServerException(org.springframework.boot.web.server.WebServerException) SocketException(java.net.SocketException) ExpectedException(org.junit.rules.ExpectedException)

Aggregations

Matcher (org.hamcrest.Matcher)28 Description (org.hamcrest.Description)7 View (android.view.View)6 Test (org.junit.Test)6 Espresso.onView (android.support.test.espresso.Espresso.onView)4 UiController (android.support.test.espresso.UiController)4 ViewAction (android.support.test.espresso.ViewAction)4 TextView (android.widget.TextView)4 MockedPulsarServiceBaseTest (com.yahoo.pulsar.broker.auth.MockedPulsarServiceBaseTest)4 NamespaceBundle (com.yahoo.pulsar.common.naming.NamespaceBundle)4 NamespaceName (com.yahoo.pulsar.common.naming.NamespaceName)4 URL (java.net.URL)4 Matchers.containsString (org.hamcrest.Matchers.containsString)4 Test (org.testng.annotations.Test)4 RestException (com.yahoo.pulsar.broker.web.RestException)3 ArrayList (java.util.ArrayList)3 Resources (android.content.res.Resources)2 ColorInt (android.support.annotation.ColorInt)2 MediumTest (android.support.test.filters.MediumTest)2 ViewGroup (android.view.ViewGroup)2