From 96cabfb17792b106b6d378738b399a32cefffc51 Mon Sep 17 00:00:00 2001 From: ghubstan <36207203+ghubstan@users.noreply.github.com> Date: Sun, 12 Jul 2020 21:44:21 -0300 Subject: [PATCH] Support @Skip on test classes and methods Add super class for all test types (method, scenario, end-to-end), and an class & method level annotation for skipping tests. --- .../main/java/bisq/apitest/ApiTestCase.java | 81 +++++++++++++++++++ .../java/bisq/apitest/annotation/Skip.java | 38 +++++++++ 2 files changed, 119 insertions(+) create mode 100644 apitest/src/main/java/bisq/apitest/ApiTestCase.java create mode 100644 apitest/src/main/java/bisq/apitest/annotation/Skip.java diff --git a/apitest/src/main/java/bisq/apitest/ApiTestCase.java b/apitest/src/main/java/bisq/apitest/ApiTestCase.java new file mode 100644 index 0000000000..b196c4a34f --- /dev/null +++ b/apitest/src/main/java/bisq/apitest/ApiTestCase.java @@ -0,0 +1,81 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.apitest; + +import java.util.function.Predicate; + +import java.lang.reflect.Method; + +import static java.lang.String.format; +import static java.util.concurrent.TimeUnit.MILLISECONDS; + + + +import bisq.apitest.annotation.Skip; + +public class ApiTestCase { + + protected static final char CHECK = '\u2714'; + protected static final char CROSS_MARK = '\u274c'; + + public int countTestCases; + public int countFailedTestCases; + public int countSkippedTestCases; + public int countPassedTestCases; + + private final Predicate> skipAll = (c) -> c.getAnnotation(Skip.class) != null; + private final Predicate skip = (m) -> m.getAnnotation(Skip.class) != null; + + protected boolean isSkipped(String methodName) { + try { + if (skipAll.test(this.getClass()) || skip.test(getMethod(methodName))) { + countSkippedTestCases++; + return true; + } else { + return false; + } + } finally { + countTestCases++; // Increment the test case count, skipped or not. + } + } + + protected Method getMethod(String methodName) { + try { + return this.getClass().getMethod(methodName); + } catch (NoSuchMethodException ex) { + throw new IllegalStateException(format("No method '%s' exists in class '%s'", + methodName, this.getClass().getName()), + ex); + } + } + + protected String reportString() { + return format("Total %d Passed %d Failed %d Skipped %d", + countTestCases, + countPassedTestCases, + countFailedTestCases, + countSkippedTestCases); + } + + protected void sleep(long ms) { + try { + MILLISECONDS.sleep(ms); + } catch (InterruptedException ignored) { + } + } +} diff --git a/apitest/src/main/java/bisq/apitest/annotation/Skip.java b/apitest/src/main/java/bisq/apitest/annotation/Skip.java new file mode 100644 index 0000000000..a08784154d --- /dev/null +++ b/apitest/src/main/java/bisq/apitest/annotation/Skip.java @@ -0,0 +1,38 @@ +/* + * This file is part of Bisq. + * + * Bisq is free software: you can redistribute it and/or modify it + * under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or (at + * your option) any later version. + * + * Bisq is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public + * License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with Bisq. If not, see . + */ + +package bisq.apitest.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Sometimes you want to temporarily disable a test or a group of tests. Methods that + * are annotated with @Skip will not be executed as tests. + * Also, you can annotate a class containing test methods with @Skip + * and none of the containing tests will be executed. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD, ElementType.TYPE}) +public @interface Skip { + /** + * The optional reason why the test is skipped. + */ + String value() default ""; +}