From 4cdff50d02e22b05a5e944ee39b1a9045575026b Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Mon, 21 Jul 2025 16:49:11 +0200 Subject: [PATCH 1/4] java: Use message comparators Reduces the custom code needed to compare messages. --- java/pom.xml | 2 +- .../main/java/io/cucumber/query/Query.java | 4 +- .../cucumber/query/TimestampComparator.java | 30 ------------- .../query/TimestampComparatorTest.java | 42 ------------------- 4 files changed, 3 insertions(+), 75 deletions(-) delete mode 100644 java/src/main/java/io/cucumber/query/TimestampComparator.java delete mode 100644 java/src/test/java/io/cucumber/query/TimestampComparatorTest.java diff --git a/java/pom.xml b/java/pom.xml index cfef396a..f34d1a04 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -51,7 +51,7 @@ io.cucumber messages - [24.0.0,29.0.0) + [28.1.0,29.0.0) diff --git a/java/src/main/java/io/cucumber/query/Query.java b/java/src/main/java/io/cucumber/query/Query.java index a0f4ccb9..c9135599 100644 --- a/java/src/main/java/io/cucumber/query/Query.java +++ b/java/src/main/java/io/cucumber/query/Query.java @@ -1,6 +1,7 @@ package io.cucumber.query; import io.cucumber.messages.Convertor; +import io.cucumber.messages.TestStepResultStatusComparator; import io.cucumber.messages.types.Attachment; import io.cucumber.messages.types.Envelope; import io.cucumber.messages.types.Examples; @@ -48,7 +49,6 @@ import static java.util.Collections.emptyList; import static java.util.Comparator.comparing; -import static java.util.Comparator.nullsFirst; import static java.util.Objects.requireNonNull; import static java.util.Optional.ofNullable; import static java.util.function.Function.identity; @@ -72,7 +72,7 @@ public final class Query { private static final Map ZEROES_BY_TEST_STEP_RESULT_STATUSES = Arrays.stream(TestStepResultStatus.values()) .collect(Collectors.toMap(identity(), (s) -> 0L)); - private final Comparator testStepResultComparator = nullsFirst(comparing(o -> o.getStatus().ordinal())); + private static final Comparator testStepResultComparator = comparing(TestStepResult::getStatus, new TestStepResultStatusComparator()); private final Map testCaseStartedById = new LinkedHashMap<>(); private final Map testCaseFinishedByTestCaseStartedId = new HashMap<>(); private final Map> testStepsFinishedByTestCaseStartedId = new HashMap<>(); diff --git a/java/src/main/java/io/cucumber/query/TimestampComparator.java b/java/src/main/java/io/cucumber/query/TimestampComparator.java deleted file mode 100644 index 29ed2794..00000000 --- a/java/src/main/java/io/cucumber/query/TimestampComparator.java +++ /dev/null @@ -1,30 +0,0 @@ -package io.cucumber.query; - -import io.cucumber.messages.types.Timestamp; - -import java.util.Comparator; - -class TimestampComparator implements Comparator { - @Override - public int compare(Timestamp a, Timestamp b) { - long sa = a.getSeconds(); - long sb = b.getSeconds(); - - if (sa < sb) { - return -1; - } else if (sb < sa) { - return 1; - } - - long na = a.getNanos(); - long nb = b.getNanos(); - - if (na < nb) { - return -1; - } else if (nb < na) { - return 1; - } - - return 0; - } -} diff --git a/java/src/test/java/io/cucumber/query/TimestampComparatorTest.java b/java/src/test/java/io/cucumber/query/TimestampComparatorTest.java deleted file mode 100644 index d48e6e99..00000000 --- a/java/src/test/java/io/cucumber/query/TimestampComparatorTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package io.cucumber.query; - -import io.cucumber.messages.types.Timestamp; -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -class TimestampComparatorTest { - - private final TimestampComparator comparator = new TimestampComparator(); - - @Test - void identity(){ - Timestamp a = new Timestamp(1L, 1L); - Timestamp b = new Timestamp(1L, 1L); - - assertThat(comparator.compare(a, b)).isEqualTo(0); - assertThat(comparator.compare(b, a)).isEqualTo(0); - } - - @Test - void onSeconds(){ - Timestamp a = new Timestamp(1L, 1L); - Timestamp b = new Timestamp(2L, 2L); - assertThat(comparator.compare(a, b)).isEqualTo(-1); - assertThat(comparator.compare(b, a)).isEqualTo(1); - } - - @Test - void onNanoSeconds(){ - Timestamp a = new Timestamp(1L, 1L); - Timestamp b1 = new Timestamp(1L, 0L); - Timestamp b2 = new Timestamp(1L, 2L); - - assertThat(comparator.compare(a, b1)).isEqualTo(1); - assertThat(comparator.compare(b1, a)).isEqualTo(-1); - - assertThat(comparator.compare(a, b2)).isEqualTo(-1); - assertThat(comparator.compare(b2, a)).isEqualTo(1); - - } -} From 787eebee8779b5f945d2b1b385355ad487da8039 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Mon, 21 Jul 2025 16:51:17 +0200 Subject: [PATCH 2/4] Update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14f33e0b..7b559015 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Changed +- Java increase dependency messages to at least v28.1 ([#87](https://github.com/cucumber/query/pull/87)) + ## [13.6.0] - 2025-08-11 ### Changed From 79fa021750c9fcdb2ace6260c46181f1dd4f43ee Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Mon, 21 Jul 2025 16:51:29 +0200 Subject: [PATCH 3/4] Update CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b559015..56e89c4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] ### Changed -- Java increase dependency messages to at least v28.1 ([#87](https://github.com/cucumber/query/pull/87)) +- [Java] increase dependency messages to at least v28.1 ([#87](https://github.com/cucumber/query/pull/87)) ## [13.6.0] - 2025-08-11 From 7fd59373e22418cd0bd397bc5bed1150fd9518e4 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Fri, 15 Aug 2025 01:35:07 +0200 Subject: [PATCH 4/4] Simplify --- java/src/main/java/io/cucumber/query/Query.java | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/java/src/main/java/io/cucumber/query/Query.java b/java/src/main/java/io/cucumber/query/Query.java index c9135599..aff9957c 100644 --- a/java/src/main/java/io/cucumber/query/Query.java +++ b/java/src/main/java/io/cucumber/query/Query.java @@ -32,9 +32,7 @@ import java.time.Duration; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; -import java.util.Comparator; import java.util.EnumMap; import java.util.HashMap; import java.util.LinkedHashMap; @@ -45,7 +43,6 @@ import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Supplier; -import java.util.stream.Collectors; import static java.util.Collections.emptyList; import static java.util.Comparator.comparing; @@ -70,9 +67,6 @@ * @see Cucumber Messages - Message Overview */ public final class Query { - private static final Map ZEROES_BY_TEST_STEP_RESULT_STATUSES = Arrays.stream(TestStepResultStatus.values()) - .collect(Collectors.toMap(identity(), (s) -> 0L)); - private static final Comparator testStepResultComparator = comparing(TestStepResult::getStatus, new TestStepResultStatusComparator()); private final Map testCaseStartedById = new LinkedHashMap<>(); private final Map testCaseFinishedByTestCaseStartedId = new HashMap<>(); private final Map> testStepsFinishedByTestCaseStartedId = new HashMap<>(); @@ -91,7 +85,10 @@ public final class Query { private TestRunFinished testRunFinished; public Map countMostSevereTestStepResultStatus() { - EnumMap results = new EnumMap<>(ZEROES_BY_TEST_STEP_RESULT_STATUSES); + EnumMap results = new EnumMap<>(TestStepResultStatus.class); + for (TestStepResultStatus value : TestStepResultStatus.values()) { + results.put(value, 0L); + } results.putAll(findAllTestCaseStarted().stream() .map(this::findMostSevereTestStepResultBy) .filter(Optional::isPresent) @@ -172,7 +169,7 @@ public Optional findMostSevereTestStepResultBy(TestCaseStarted t return findTestStepsFinishedBy(testCaseStarted) .stream() .map(TestStepFinished::getTestStepResult) - .max(testStepResultComparator); + .max(comparing(TestStepResult::getStatus, new TestStepResultStatusComparator())); } public String findNameOf(GherkinDocument element, NamingStrategy namingStrategy) {