From 8dc97e152f769330ac28748834e24cd53111aede Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Tue, 13 Sep 2022 15:37:49 -0400 Subject: [PATCH] Fix `SpansAssert` generics inference Without this fix the new test in the `TracerAssertTests` would have to use a casting like: ``` SpansAssert.assertThat((Collection) (Collection) simpleSpans) ``` --- .../tracing/test/simple/SpansAssert.java | 15 +++++++++++---- .../tracing/test/simple/TracerAssertTests.java | 16 +++++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SpansAssert.java b/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SpansAssert.java index 29469117..e262a2d0 100644 --- a/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SpansAssert.java +++ b/micrometer-tracing-tests/micrometer-tracing-test/src/main/java/io/micrometer/tracing/test/simple/SpansAssert.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 VMware, Inc. + * Copyright 2021-2022 VMware, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,6 +31,7 @@ * {@link SpansAssert#assertThat(Collection)} or {@link SpansAssert#then(Collection)}. * * @author Marcin Grzejszczak + * @author Artem Bilan * @since 1.0.0 */ public class SpansAssert extends CollectionAssert { @@ -39,7 +40,7 @@ public class SpansAssert extends CollectionAssert { * Creates a new instance of {@link SpansAssert}. * @param actual actual object to assert */ - protected SpansAssert(Collection actual) { + protected SpansAssert(Collection actual) { super(actual); } @@ -48,7 +49,7 @@ protected SpansAssert(Collection actual) { * @param actual span to assert against * @return span collection assertions */ - public static SpansAssert assertThat(Collection actual) { + public static SpansAssert assertThat(Collection actual) { return new SpansAssert(actual); } @@ -57,7 +58,7 @@ public static SpansAssert assertThat(Collection actual) { * @param actual span to assert against * @return span collection assertions */ - public static SpansAssert then(Collection actual) { + public static SpansAssert then(Collection actual) { return new SpansAssert(actual); } @@ -125,12 +126,14 @@ public SpansAssert hasASpanWithName(String name) { * @throws AssertionError if the span assertion is not met * @since 1.0.0 */ + @SuppressWarnings("rawtypes") public SpansAssert hasASpanWithName(String name, Consumer spanConsumer) { isNotEmpty(); atLeastOneSpanPassesTheAssertion(name, spanConsumer); return this; } + @SuppressWarnings("rawtypes") private void atLeastOneSpanPassesTheAssertion(String name, Consumer spanConsumer) { FinishedSpan finishedSpan = this.actual.stream().filter(f -> name.equals(f.getName())).filter(f -> { try { @@ -165,12 +168,14 @@ private void atLeastOneSpanPassesTheAssertion(String name, Consumer * @throws AssertionError if the span assertion is not met * @since 1.0.0 */ + @SuppressWarnings("rawtypes") public SpansAssert hasASpanWithNameIgnoreCase(String name, Consumer spanConsumer) { isNotEmpty(); atLeastOneSpanPassesTheAssertionIgnoreCase(name, spanConsumer); return this; } + @SuppressWarnings("rawtypes") private void atLeastOneSpanPassesTheAssertionIgnoreCase(String name, Consumer spanConsumer) { FinishedSpan finishedSpan = this.actual.stream().filter(f -> name.equalsIgnoreCase(f.getName())).filter(f -> { try { @@ -248,6 +253,7 @@ public SpansAssert hasASpanWithNameIgnoreCase(String name) { * assertion is not successful * @since 1.0.0 */ + @SuppressWarnings("rawtypes") public SpansAssert forAllSpansWithNameEqualTo(String name, Consumer spanConsumer) { isNotEmpty(); hasASpanWithName(name); @@ -273,6 +279,7 @@ public SpansAssert forAllSpansWithNameEqualTo(String name, Consumer * the additional assertion is not successful * @since 1.0.0 */ + @SuppressWarnings("rawtypes") public SpansAssert forAllSpansWithNameEqualToIgnoreCase(String name, Consumer spanConsumer) { isNotEmpty(); hasASpanWithNameIgnoreCase(name); diff --git a/micrometer-tracing-tests/micrometer-tracing-test/src/test/java/io/micrometer/tracing/test/simple/TracerAssertTests.java b/micrometer-tracing-tests/micrometer-tracing-test/src/test/java/io/micrometer/tracing/test/simple/TracerAssertTests.java index a4116bda..a52789ff 100644 --- a/micrometer-tracing-tests/micrometer-tracing-test/src/test/java/io/micrometer/tracing/test/simple/TracerAssertTests.java +++ b/micrometer-tracing-tests/micrometer-tracing-test/src/test/java/io/micrometer/tracing/test/simple/TracerAssertTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 VMware, Inc. + * Copyright 2021-2022 VMware, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -79,4 +79,18 @@ void should_throw_exception_when_size_reported_spans_is_invalid() { thenThrownBy(() -> assertThat(simpleTracer).reportedSpans().hasSize(2)).isInstanceOf(AssertionError.class); } + @Test + void should_satisfy_reported_spans() { + SimpleTracer simpleTracer = new SimpleTracer(); + + simpleTracer.nextSpan().name("foo").start().end(); + + assertThat(simpleTracer) + .reportedSpans() + .hasSize(1) + .satisfies(simpleSpans -> + SpansAssert.assertThat(simpleSpans) + .hasASpanWithName("foo")); + } + }