xtern commented on code in PR #7949: URL: https://github.com/apache/ignite-3/pull/7949#discussion_r3051134358
########## modules/jdbc/src/testFixtures/java/org/apache/ignite/jdbc/util/RowsProjectionMatcher.java: ########## @@ -0,0 +1,274 @@ +/* + * 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.ignite.jdbc.util; + +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import org.hamcrest.Description; +import org.hamcrest.SelfDescribing; +import org.hamcrest.TypeSafeMatcher; +import org.jetbrains.annotations.Nullable; + +/** Matcher for result set rows column projection. */ +public abstract class RowsProjectionMatcher<T> extends TypeSafeMatcher<List<T>> { + + private @Nullable MismatchDescriber mismatch; + + @Override + protected final boolean matchesSafely(List<T> projection) { + try { + doMatch(projection); + return true; + + } catch (MismatchException e) { + mismatch = e.describer(); + return false; + } + } + + /** + * Performs the actual matching. + * + * @throws MismatchException on first mismatch. + */ + abstract void doMatch(List<T> projection) throws MismatchException; + + @Override + protected void describeMismatchSafely(List<T> projection, Description description) { + assert mismatch != null; + description.appendText("Actual projection " + projection + " "); + mismatch.describeTo(description); + } + + /** Matcher verifying that checked projection contains all values from provided list. */ + @SafeVarargs + public static <T> RowsProjectionMatcher<T> hasValuesInAnyOrder(T... expectedVals) { + return hasValues("Rows projection with following values in any order ", (projection, expectedProjection) -> { + + Map<T, Integer> remaining = new HashMap<>(); + for (T value : expectedProjection) { + remaining.merge(value, 1, Integer::sum); + } + + int expectedSize = expectedProjection.size(); + int actualSize = projection.size(); + + for (int index = 0; index < actualSize; index++) { + + if (index >= expectedSize) { + throw MismatchException.tooManyRecords(actualSize, expectedSize, + projection.subList(index, actualSize) + ); + } + + T actual = projection.get(index); + Integer count = remaining.get(actual); + + if (count == null) { + throw MismatchException.unexpectedValue(actual, index, + remaining.entrySet().stream() + .flatMap(e -> Collections.nCopies(e.getValue(), e.getKey()).stream()) + .collect(Collectors.toList())); + } + + if (count == 1) { + remaining.remove(actual); + } else { + remaining.put(actual, count - 1); + } + } + + if (!remaining.isEmpty()) { + throw MismatchException.notEnoughRecords(actualSize, expectedSize, + remaining.entrySet().stream() + .flatMap(e -> Collections.nCopies(e.getValue(), e.getKey()).stream()) + .collect(Collectors.toList())); + } + + }, expectedVals); + } + + /** Matcher verifying that checked projection satisfies provided values order. */ Review Comment: ```suggestion /** Matcher verifies that the checked projection satisfies provided values order. */ ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
