You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by lz...@apache.org on 2023/03/20 08:30:54 UTC

[incubator-paimon] branch master updated: [test-utils] Introduce AssertionUtils (#663)

This is an automated email from the ASF dual-hosted git repository.

lzljs3620320 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 89b14e3dd [test-utils] Introduce AssertionUtils (#663)
89b14e3dd is described below

commit 89b14e3dd287e92b32060b0a80fc7141ae4549eb
Author: yuzelin <33...@users.noreply.github.com>
AuthorDate: Mon Mar 20 16:30:48 2023 +0800

    [test-utils] Introduce AssertionUtils (#663)
---
 paimon-flink/paimon-flink-common/pom.xml           |  20 +--
 .../apache/paimon/flink/ReadWriteTableITCase.java  |   4 +-
 paimon-test-utils/pom.xml                          |   6 +
 .../paimon/testutils/assertj/AssertionUtils.java   | 141 +++++++++++++++++++++
 4 files changed, 156 insertions(+), 15 deletions(-)

diff --git a/paimon-flink/paimon-flink-common/pom.xml b/paimon-flink/paimon-flink-common/pom.xml
index 54e3ecd28..1b909a1e4 100644
--- a/paimon-flink/paimon-flink-common/pom.xml
+++ b/paimon-flink/paimon-flink-common/pom.xml
@@ -132,6 +132,13 @@ under the License.
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.apache.paimon</groupId>
+            <artifactId>paimon-test-utils</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.hadoop</groupId>
             <artifactId>hadoop-hdfs-client</artifactId>
@@ -176,19 +183,6 @@ under the License.
             <type>test-jar</type>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.flink</groupId>
-            <artifactId>flink-test-utils</artifactId>
-            <version>${flink.version}</version>
-            <scope>test</scope>
-            <exclusions>
-                <exclusion>
-                    <groupId>junit</groupId>
-                    <artifactId>junit</artifactId>
-                </exclusion>
-            </exclusions>
-        </dependency>
-
         <dependency>
             <groupId>org.apache.flink</groupId>
             <artifactId>flink-connector-test-utils</artifactId>
diff --git a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
index ca5d17294..b394a28cd 100644
--- a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
+++ b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/ReadWriteTableITCase.java
@@ -24,11 +24,11 @@ import org.apache.paimon.flink.util.AbstractTestBase;
 import org.apache.paimon.fs.Path;
 import org.apache.paimon.fs.local.LocalFileIO;
 import org.apache.paimon.schema.SchemaManager;
+import org.apache.paimon.testutils.assertj.AssertionUtils;
 import org.apache.paimon.utils.BlockingIterator;
 
 import org.apache.flink.api.dag.Transformation;
 import org.apache.flink.configuration.Configuration;
-import org.apache.flink.core.testutils.FlinkAssertions;
 import org.apache.flink.streaming.api.datastream.DataStream;
 import org.apache.flink.streaming.api.datastream.DataStreamSink;
 import org.apache.flink.streaming.api.transformations.PartitionTransformation;
@@ -813,7 +813,7 @@ public class ReadWriteTableITCase extends AbstractTestBase {
                                         Collections.singletonList("dt"),
                                         streamingReadOverwrite))
                 .satisfies(
-                        FlinkAssertions.anyCauseMatches(
+                        AssertionUtils.anyCauseMatches(
                                 RuntimeException.class,
                                 "Doesn't support streaming read the changes from overwrite when the primary keys are not defined."));
     }
diff --git a/paimon-test-utils/pom.xml b/paimon-test-utils/pom.xml
index 25987352c..a05c714dc 100644
--- a/paimon-test-utils/pom.xml
+++ b/paimon-test-utils/pom.xml
@@ -38,6 +38,12 @@ under the License.
             <version>${junit5.version}</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.assertj</groupId>
+            <artifactId>assertj-core</artifactId>
+            <version>${assertj.version}</version>
+        </dependency>
+
         <dependency>
             <groupId>org.testcontainers</groupId>
             <artifactId>testcontainers</artifactId>
diff --git a/paimon-test-utils/src/main/java/org/apache/paimon/testutils/assertj/AssertionUtils.java b/paimon-test-utils/src/main/java/org/apache/paimon/testutils/assertj/AssertionUtils.java
new file mode 100644
index 000000000..57cce3ed8
--- /dev/null
+++ b/paimon-test-utils/src/main/java/org/apache/paimon/testutils/assertj/AssertionUtils.java
@@ -0,0 +1,141 @@
+/*
+ * 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.paimon.testutils.assertj;
+
+import org.assertj.core.api.AbstractThrowableAssert;
+import org.assertj.core.api.Assertions;
+import org.assertj.core.api.InstanceOfAssertFactory;
+import org.assertj.core.api.ListAssert;
+import org.assertj.core.api.ThrowingConsumer;
+
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Some reusable assertions and utilities for AssertJ. Copied from FlinkAssertions. */
+public final class AssertionUtils {
+
+    private AssertionUtils() {}
+
+    /** @see #chainOfCauses(Throwable) */
+    @SuppressWarnings({"rawtypes", "unused"})
+    public static final InstanceOfAssertFactory<Stream, ListAssert<Throwable>> STREAM_THROWABLE =
+            new InstanceOfAssertFactory<>(Stream.class, Assertions::<Throwable>assertThat);
+
+    /**
+     * Shorthand to assert the chain of causes includes a {@link Throwable} matching a specific
+     * {@link Class} and containing the provided message. Same as:
+     *
+     * <pre>{@code
+     * assertThatChainOfCauses(throwable)
+     *     .anySatisfy(
+     *          cause ->
+     *              assertThat(cause)
+     *                  .isInstanceOf(clazz)
+     *                  .hasMessageContaining(containsMessage));
+     * }</pre>
+     */
+    public static ThrowingConsumer<? super Throwable> anyCauseMatches(
+            Class<? extends Throwable> clazz, String containsMessage) {
+        return t ->
+                assertThatChainOfCauses(t)
+                        .as(
+                                "Any cause is instance of class '%s' and contains message '%s'",
+                                clazz, containsMessage)
+                        .anySatisfy(
+                                cause ->
+                                        assertThat(cause)
+                                                .isInstanceOf(clazz)
+                                                .hasMessageContaining(containsMessage));
+    }
+
+    /**
+     * Shorthand to assert the chain of causes includes a {@link Throwable} matching a specific
+     * {@link Class}. Same as:
+     *
+     * <pre>{@code
+     * assertThatChainOfCauses(throwable)
+     *     .anySatisfy(
+     *          cause ->
+     *              assertThat(cause)
+     *                  .isInstanceOf(clazz));
+     * }</pre>
+     */
+    public static ThrowingConsumer<? super Throwable> anyCauseMatches(
+            Class<? extends Throwable> clazz) {
+        return t ->
+                assertThatChainOfCauses(t)
+                        .as("Any cause is instance of class '%s'", clazz)
+                        .anySatisfy(cause -> assertThat(cause).isInstanceOf(clazz));
+    }
+
+    /**
+     * Shorthand to assert the chain of causes includes a {@link Throwable} matching a specific
+     * {@link Class} and containing the provided message. Same as:
+     *
+     * <pre>{@code
+     * assertThatChainOfCauses(throwable)
+     *     .anySatisfy(
+     *          cause ->
+     *              assertThat(cause)
+     *                  .hasMessageContaining(containsMessage));
+     * }</pre>
+     */
+    public static ThrowingConsumer<? super Throwable> anyCauseMatches(String containsMessage) {
+        return t ->
+                assertThatChainOfCauses(t)
+                        .as("Any cause contains message '%s'", containsMessage)
+                        .anySatisfy(t1 -> assertThat(t1).hasMessageContaining(containsMessage));
+    }
+
+    /**
+     * Shorthand to assert chain of causes. Same as:
+     *
+     * <pre>{@code
+     * assertThat(throwable)
+     *     .extracting(AssertionUtils::chainOfCauses, AssertionUtils.STREAM_THROWABLE)
+     * }</pre>
+     */
+    public static ListAssert<Throwable> assertThatChainOfCauses(Throwable root) {
+        return assertThat(root).extracting(AssertionUtils::chainOfCauses, STREAM_THROWABLE);
+    }
+
+    /**
+     * You can use this method in combination with {@link
+     * AbstractThrowableAssert#extracting(Function, InstanceOfAssertFactory)} to perform assertions
+     * on a chain of causes. For example:
+     *
+     * <pre>{@code
+     * assertThat(throwable)
+     *     .extracting(AssertionUtils::chainOfCauses, AssertionUtils.STREAM_THROWABLE)
+     * }</pre>
+     *
+     * @return the list is ordered from the current {@link Throwable} up to the root cause.
+     */
+    public static Stream<Throwable> chainOfCauses(Throwable throwable) {
+        if (throwable == null) {
+            return Stream.empty();
+        }
+        if (throwable.getCause() == null) {
+            return Stream.of(throwable);
+        }
+        return Stream.concat(Stream.of(throwable), chainOfCauses(throwable.getCause()));
+    }
+}