Repository: calcite Updated Branches: refs/heads/master 96fd5b7cf -> 8bbef2d6f (forced update)
[CALCITE-1046] Matchers for testing SQL query results Project: http://git-wip-us.apache.org/repos/asf/calcite/repo Commit: http://git-wip-us.apache.org/repos/asf/calcite/commit/f650bccd Tree: http://git-wip-us.apache.org/repos/asf/calcite/tree/f650bccd Diff: http://git-wip-us.apache.org/repos/asf/calcite/diff/f650bccd Branch: refs/heads/master Commit: f650bccd1bcb364fc6be9c92214843499bac6843 Parents: de77591 Author: Julian Hyde <[email protected]> Authored: Thu Jan 7 11:34:06 2016 -0800 Committer: Julian Hyde <[email protected]> Committed: Sun Feb 14 10:44:56 2016 -0800 ---------------------------------------------------------------------- .../org/apache/calcite/test/CalciteAssert.java | 1 + .../java/org/apache/calcite/test/Matchers.java | 84 ++++++++++++++++++++ 2 files changed, 85 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/calcite/blob/f650bccd/core/src/test/java/org/apache/calcite/test/CalciteAssert.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/CalciteAssert.java b/core/src/test/java/org/apache/calcite/test/CalciteAssert.java index 48d0f6c..03db1b9 100644 --- a/core/src/test/java/org/apache/calcite/test/CalciteAssert.java +++ b/core/src/test/java/org/apache/calcite/test/CalciteAssert.java @@ -350,6 +350,7 @@ public class CalciteAssert { return buf.toString(); } + /** @see Matchers#returnsUnordered(String...) */ static Function<ResultSet, Void> checkResultUnordered( final String... lines) { return new Function<ResultSet, Void>() { http://git-wip-us.apache.org/repos/asf/calcite/blob/f650bccd/core/src/test/java/org/apache/calcite/test/Matchers.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/calcite/test/Matchers.java b/core/src/test/java/org/apache/calcite/test/Matchers.java new file mode 100644 index 0000000..fc9e0fd --- /dev/null +++ b/core/src/test/java/org/apache/calcite/test/Matchers.java @@ -0,0 +1,84 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.calcite.test; + +import com.google.common.collect.Lists; + +import org.hamcrest.CustomTypeSafeMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Matchers for testing SQL queries. + */ +public class Matchers { + private Matchers() {} + + /** Allows passing the actual result from the {@code matchesSafely} method to + * the {@code describeMismatchSafely} method that will show the difference. */ + private static final ThreadLocal<Object> THREAD_ACTUAL = new ThreadLocal<>(); + + /** + * Creates a matcher that matches if the examined result set returns the + * given collection of rows in some order. + * + * <p>Closes the result set after reading. + * + * <p>For example: + * <pre>assertThat(statement.executeQuery("select empno from emp"), + * returnsUnordered("empno=1234", "empno=100"));</pre> + */ + public static Matcher<? super ResultSet> returnsUnordered(String... lines) { + final List<String> expectedList = Lists.newArrayList(lines); + Collections.sort(expectedList); + + return new CustomTypeSafeMatcher<ResultSet>(Arrays.toString(lines)) { + @Override protected void describeMismatchSafely(ResultSet item, + Description description) { + final Object value = THREAD_ACTUAL.get(); + THREAD_ACTUAL.remove(); + description.appendText("was ").appendValue(value); + } + + protected boolean matchesSafely(ResultSet resultSet) { + final List<String> actualList = Lists.newArrayList(); + try { + CalciteAssert.toStringList(resultSet, actualList); + resultSet.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + Collections.sort(actualList); + + THREAD_ACTUAL.set(actualList); + final boolean equals = actualList.equals(expectedList); + if (!equals) { + THREAD_ACTUAL.set(actualList); + } + return equals; + } + }; + } +} + +// End Matchers.java
