Migrate ParameterizedClassTest to java
I was looking at some of the annotation utilities and maybe wanted to
make some changes but noticed the tests. This is a start...
DELETE
instrumentation-annotations-support/src/test/groovy/io/opentelemetry/instrumentation/api/annotation/support/ParameterizedClassTest.groovy
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.instrumentation.api.annotation.support
-
-import org.hamcrest.Description
-import org.hamcrest.Matcher
-import org.hamcrest.TypeSafeMatcher
-import spock.lang.Shared
-import spock.lang.Specification
-
-import java.lang.reflect.Type
-import java.lang.reflect.TypeVariable
-
-import static org.hamcrest.Matchers.arrayContainingInAnyOrder
-import static spock.util.matcher.HamcrestSupport.expect
-
-class ParameterizedClassTest extends Specification {
- @Shared
- def stringListType = TestFields.getField("stringListField").getGenericType()
-
- @Shared
- def stringArrayListType = TestFields.getField("stringArrayListField").getGenericType()
-
- def "reflects Class"() {
- when:
- def underTest = ParameterizedClass.of(ArrayList)
-
- then:
- underTest.getRawClass() == ArrayList
- underTest.getActualTypeArguments()[0] instanceof TypeVariable
- }
-
- def "reflects ParameterizedType"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType)
-
- then:
- underTest.getRawClass() == ArrayList
- underTest.getActualTypeArguments() == [String] as Type[]
- }
-
- def "gets parameterized superclass with mapped type arguments"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).getParameterizedSuperclass()
-
- then:
- underTest.getRawClass() == AbstractList
- underTest.getActualTypeArguments() == [String] as Type[]
- }
-
- def "gets parameterized interfaces with mapped type arguments"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).getParameterizedInterfaces()
-
- then:
- expect underTest, arrayContainingInAnyOrder(
- matchesParameterizedClass(List, [String] as Type[]),
- matchesParameterizedClass(Cloneable, [] as Type[]),
- matchesParameterizedClass(RandomAccess, [] as Type[]),
- matchesParameterizedClass(Serializable, [] as Type[]),
- )
- }
-
- def "finds parameterized interface with mapped type arguments"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(List)
-
- then:
- underTest.isPresent()
- underTest.get().getRawClass() == List
- underTest.get().getActualTypeArguments() == [String] as Type[]
- }
-
- def "finds parameterized interface of superclass with mapped type arguments"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(Collection)
-
- then:
- underTest.isPresent()
- underTest.get().getRawClass() == Collection
- underTest.get().getActualTypeArguments() == [String] as Type[]
- }
-
- def "does not find parameterized superclass that type does not extend"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(C)
-
- then:
- !underTest.isPresent()
- }
-
- def "does not find parameterized interface that type does not implement"() {
- when:
- def underTest = ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(I)
-
- then:
- !underTest.isPresent()
- }
-
- interface I {}
-
- abstract class C {}
-
- static class TestFields {
- public static List<String> stringListField
- public static ArrayList<String> stringArrayListField
- }
-
- static Matcher<ParameterizedClass> matchesParameterizedClass(Class<?> rawClass, Type[] typeArguments) {
- return new TypeSafeMatcher<ParameterizedClass>() {
- @Override
- boolean matchesSafely(ParameterizedClass item) {
- item != null && item.getRawClass() == rawClass && Arrays.equals(item.getActualTypeArguments(), typeArguments)
- }
-
- @Override
- void describeTo(Description description) {
- description
- .appendText("a ParameterizedClass with raw type ")
- .appendValue(rawClass)
- .appendText(" and type parameters ")
- .appendValue(typeArguments)
- }
- }
- }
-}

ADD
instrumentation-annotations-support/src/test/java/io/opentelemetry/instrumentation/api/annotation/support/ParameterizedClassTest.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.instrumentation.api.annotation.support;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.fail;
+
+import java.io.Serializable;
+import java.lang.reflect.Type;
+import java.lang.reflect.TypeVariable;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Optional;
+import java.util.RandomAccess;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.junit.jupiter.api.Test;
+
+class ParameterizedClassTest {
+
+ Type stringArrayListType = getGeneric(TestFields.class, "stringArrayListField");
+
+ static Type getGeneric(Class<?> clazz, String fieldName) {
+ try {
+ return clazz.getField(fieldName).getGenericType();
+ } catch (NoSuchFieldException e) {
+ fail("No such field");
+ throw new RuntimeException("No such field", e);
+ }
+ }
+
+ @Test
+ void reflectsClass() {
+
+ ParameterizedClass underTest = ParameterizedClass.of(ArrayList.class);
+ assertThat(underTest.getRawClass()).isEqualTo(ArrayList.class);
+ assertThat(underTest.getActualTypeArguments())
+ .hasSize(1)
+ .allMatch(x -> x instanceof TypeVariable);
+ }
+
+ @Test
+ void reflectsParameterizedType() {
+ ParameterizedClass underTest = ParameterizedClass.of(stringArrayListType);
+ assertThat(underTest.getRawClass()).isEqualTo(ArrayList.class);
+ assertThat(underTest.getActualTypeArguments()).containsExactly(String.class);
+ }
+
+ @Test
+ void getsParameterizedSuperclassWithMappedtypeArguments() {
+ ParameterizedClass underTest =
+ ParameterizedClass.of(stringArrayListType).getParameterizedSuperclass();
+ assertThat(underTest.getActualTypeArguments()).containsExactly(String.class);
+ }
+
+ @Test
+ void getsParameterizedInterfacesWithMappedTypeArguments() {
+ ParameterizedClass[] underTest =
+ ParameterizedClass.of(stringArrayListType).getParameterizedInterfaces();
+ assertThat(underTest)
+ .satisfiesExactlyInAnyOrder(
+ x -> matchesParameterizedClass(List.class, new Type[] {String.class}).matches(x),
+ x -> matchesParameterizedClass(Cloneable.class, new Type[0]).matches(x),
+ x -> matchesParameterizedClass(RandomAccess.class, new Type[0]).matches(x),
+ x -> matchesParameterizedClass(Serializable.class, new Type[0]).matches(x));
+ }
+
+ @Test
+ void findsParameterizedInterfaceWithMappedTypeArguments() {
+ Optional<ParameterizedClass> underTest =
+ ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(List.class);
+ assertThat(underTest).isPresent();
+ assertThat(underTest.get().getRawClass()).isEqualTo(List.class);
+ assertThat(underTest.get().getActualTypeArguments())
+ .hasSize(1)
+ .allMatch(x -> x == String.class);
+ }
+
+ @Test
+ void findsParameterizedInterfaceOfSuperclassWithMappedTypeArguments() {
+ Optional<ParameterizedClass> underTest =
+ ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(Collection.class);
+ assertThat(underTest.get().getRawClass()).isEqualTo(Collection.class);
+ assertThat(underTest.get().getActualTypeArguments())
+ .hasSize(1)
+ .allMatch(x -> x == String.class);
+ }
+
+ @Test
+ void doesNotFindParameterizedSuperclassThatTypeDoesNotExtend() {
+ Optional<ParameterizedClass> underTest =
+ ParameterizedClass.of(stringArrayListType)
+ .findParameterizedSuperclass(AbstractTestClass.class);
+ assertThat(underTest).isNotPresent();
+ }
+
+ @Test
+ void doesNotFindParameterizedInterfaceThatTypeDoesNotImplement() {
+ Optional<ParameterizedClass> underTest =
+ ParameterizedClass.of(stringArrayListType).findParameterizedSuperclass(TestInterface.class);
+ assertThat(underTest).isNotPresent();
+ }
+
+ static class TestFields {
+ private TestFields() {}
+
+ public static List<String> stringListField;
+ public static ArrayList<String> stringArrayListField;
+ }
+
+ interface TestInterface {}
+
+ abstract static class AbstractTestClass {}
+
+ static Matcher<ParameterizedClass> matchesParameterizedClass(
+ Class<?> rawClass, Type[] typeArguments) {
+ return new TypeSafeMatcher<ParameterizedClass>() {
+ @Override
+ public boolean matchesSafely(ParameterizedClass item) {
+ return (item != null)
+ && (item.getRawClass() == rawClass)
+ && Arrays.equals(item.getActualTypeArguments(), typeArguments);
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description
+ .appendText("a ParameterizedClass with raw type ")
+ .appendValue(rawClass)
+ .appendText(" and type parameters ")
+ .appendValue(typeArguments);
+ }
+ };
+ }
+}

Migrate ParameterizedClassTest to java (#7768)

I was looking at some of the annotation utilities and maybe wanted to
make some changes but noticed the tests. This is a start...

---------

Co-authored-by: Mateusz Rzeszutek <mrzeszutek@splunk.com>