Skip to content

Commit

Permalink
test: GH-132: Add test for SpringPayloadAnnotationTypeExtractor
Browse files Browse the repository at this point in the history
Add log message to indicate mismatch java versions

Co-Authored-By: 93372330+sam0r040@users.noreply.github.com
  • Loading branch information
timonback authored and sam0r040 committed Jan 20, 2023
1 parent 2b49e7f commit 0e43c29
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ static Class<?> getPayloadParameterClass(Method method, Class<?>[] parameterType
// Resolve generic type for batch listeners
if (parameterClass == List.class) {
Type type = ((ParameterizedTypeImpl) method.getGenericParameterTypes()[parameterPayloadIndex]).getActualTypeArguments()[0];
return Class.forName(type.getTypeName());
try {
return Class.forName(type.getTypeName());
} catch (IllegalAccessError error) {
// remove, when the used java version has been upgraded to support java modules
log.info("Springwolf was not compiled for the Java version you are using. More info: https://github.com/springwolf/springwolf-core/issues/132");
}
}
} catch (Exception ex) {
log.info("Found payload type List<?>, but was unable to extract generic data type", ex);
Expand Down Expand Up @@ -75,4 +80,4 @@ static int getPayloadAnnotatedParameterIndex(Annotation[][] parameterAnnotations

return -1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.github.stavshamir.springwolf.asyncapi.scanners.channels.annotation;


import org.junit.Test;
import org.springframework.messaging.handler.annotation.Payload;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.junit.Assert.assertEquals;

public class SpringPayloadAnnotationTypeExtractorTest {

@Test
public void getPayloadType() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithString", String.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

assertEquals(String.class, result);
}

@Test
public void getPayloadTypeWithPayloadAnnotation() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithStringAndPayloadAnnotation", String.class, Integer.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

assertEquals(String.class, result);
}

@Test
public void getPayloadTypeWithListOfStrings() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithListOfStrings", List.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

assertEquals(String.class, result);
}

@Test
public void getPayloadTypeWithListOfInterfaces() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithListOfGenericClasses", List.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

// Unable to resolve optional<String>, fallback to root type list
assertEquals(List.class, result);
}

@Test
public void getPayloadTypeWithInterface() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithGenericClass", Optional.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

assertEquals(Optional.class, result);
}

@Test
public void getPayloadTypeWithListOfStringExtends() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithListOfStringExtends", List.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

// Unable to resolve optional<String>, fallback to root type list
assertEquals(List.class, result);
}

@Test
public void getPayloadTypeWithCustomType() throws NoSuchMethodException {
Method m = TestClass.class.getDeclaredMethod("consumeWithCustomType", TestClass.MyType.class);

Class<?> result = SpringPayloadAnnotationTypeExtractor.getPayloadType(m);

assertEquals(TestClass.MyType.class, result);
}

public static class TestClass {
public void consumeWithStringAndPayloadAnnotation(@Payload String value, Integer value2) {}
public void consumeWithString(String value) {}
public void consumeWithGenericClass(Optional<String> value) {}
public void consumeWithListOfStrings(List<String> value) {}
public void consumeWithListOfGenericClasses(List<Optional<String>> value) {}
public void consumeWithListOfStringExtends(List<? extends String> value) {}
public void consumeWithCustomType(MyType value) {}

public static class MyType extends ArrayList<String> { }
}
}

0 comments on commit 0e43c29

Please sign in to comment.