Introduce failing test for SPR-16652

This commit introduces tests for looking up annotations on parameters
in constructors for nested and inner classes via Spring's
MethodParameter abstraction.

The test for an inner class is currently disabled since it fails on
JDK 8. See JIRA issue for details.

Issue: SPR-16652
This commit is contained in:
Sam Brannen
2018-03-28 17:42:55 +02:00
parent d95bbb6b1b
commit 4b9e3a9211

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,9 +16,15 @@
package org.springframework.core;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -26,6 +32,7 @@ import static org.junit.Assert.*;
/**
* @author Arjen Poutsma
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class MethodParameterTests {
@@ -98,9 +105,48 @@ public class MethodParameterTests {
new MethodParameter(method, 2);
}
@Test
public void annotatedConstructorParameterInStaticNestedClass() throws Exception {
Constructor<?> constructor = NestedClass.class.getDeclaredConstructor(String.class);
MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 0);
assertEquals(String.class, methodParameter.getParameterType());
assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class));
assertNotNull(methodParameter.getParameterAnnotation(Param.class));
}
@Test
@Ignore("Disabled until SPR-16652 is resolved")
public void annotatedConstructorParameterInInnerClass() throws Exception {
Constructor<?> constructor = InnerClass.class.getDeclaredConstructor(getClass(), String.class);
MethodParameter methodParameter = MethodParameter.forExecutable(constructor, 1);
assertEquals(String.class, methodParameter.getParameterType());
assertNull(methodParameter.getParameterAnnotation(Override.class));
// The following assertion currently fails if this test class is compiled using JDK 8.
assertNotNull("Failed to find @Param annotation", methodParameter.getParameterAnnotation(Param.class));
}
public int method(String p1, long p2) {
return 42;
}
@SuppressWarnings("unused")
private static class NestedClass {
NestedClass(@Param String s) {
}
}
@SuppressWarnings("unused")
private class InnerClass {
InnerClass(@Param String s) {
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
private @interface Param {
}
}