Support for reactive types in Conventions

Issue: SPR-14915
This commit is contained in:
Rossen Stoyanchev
2017-04-18 16:45:45 -04:00
parent 71966b0777
commit 5c502b87ca
2 changed files with 125 additions and 2 deletions

View File

@@ -16,14 +16,23 @@
package org.springframework.core;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import io.reactivex.Observable;
import io.reactivex.Single;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.tests.sample.objects.TestObject;
import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertEquals;
@@ -41,8 +50,15 @@ public class ConventionsTests {
@Test
public void simpleObject() {
assertEquals("Incorrect singular variable name",
"testObject", Conventions.getVariableName(new TestObject()));
assertEquals("Incorrect singular variable name", "testObject",
Conventions.getVariableNameForParameter(getMethodParameter(TestObject.class)));
assertEquals("Incorrect singular variable name", "testObject",
Conventions.getVariableNameForReturnType(getMethodForReturnType(TestObject.class)));
}
@Test
@@ -53,8 +69,15 @@ public class ConventionsTests {
@Test
public void list() {
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableName(Collections.singletonList(new TestObject())));
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableNameForParameter(getMethodParameter(List.class)));
assertEquals("Incorrect plural List form", "testObjectList",
Conventions.getVariableNameForReturnType(getMethodForReturnType(List.class)));
}
@Test
@@ -65,8 +88,47 @@ public class ConventionsTests {
@Test
public void set() {
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableName(Collections.singleton(new TestObject())));
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableNameForParameter(getMethodParameter(Set.class)));
assertEquals("Incorrect plural Set form", "testObjectList",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Set.class)));
}
@Test
public void reactiveParameters() throws Exception {
assertEquals("testObjectMono",
Conventions.getVariableNameForParameter(getMethodParameter(Mono.class)));
assertEquals("testObjectFlux",
Conventions.getVariableNameForParameter(getMethodParameter(Flux.class)));
assertEquals("testObjectSingle",
Conventions.getVariableNameForParameter(getMethodParameter(Single.class)));
assertEquals("testObjectObservable",
Conventions.getVariableNameForParameter(getMethodParameter(Observable.class)));
}
@Test
public void reactiveReturnTypes() throws Exception {
assertEquals("testObjectMono",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Mono.class)));
assertEquals("testObjectFlux",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Flux.class)));
assertEquals("testObjectSingle",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Single.class)));
assertEquals("testObjectObservable",
Conventions.getVariableNameForReturnType(getMethodForReturnType(Observable.class)));
}
@Test
@@ -84,4 +146,48 @@ public class ConventionsTests {
assertEquals(desiredResult, Conventions.getQualifiedAttributeName(cls, baseName));
}
private static MethodParameter getMethodParameter(Class<?> parameterType) {
Method method = ClassUtils.getMethod(TestBean.class, "handle", (Class<?>[]) null);
for (int i=0; i < method.getParameterCount(); i++) {
if (parameterType.equals(method.getParameterTypes()[i])) {
return new MethodParameter(method, i);
}
}
throw new IllegalArgumentException("Parameter type not found: " + parameterType);
}
private static Method getMethodForReturnType(Class<?> returnType) {
return Arrays.stream(TestBean.class.getMethods())
.filter(method -> method.getReturnType().equals(returnType))
.findFirst()
.orElseThrow(() ->
new IllegalArgumentException("Unique return type not found: " + returnType));
}
@SuppressWarnings("unused")
private static class TestBean {
public void handle(TestObject to,
List<TestObject> toList, Set<TestObject> toSet,
Mono<TestObject> toMono, Flux<TestObject> toFlux,
Single<TestObject> toSingle, Observable<TestObject> toObservable) { }
public TestObject handleTo() { return null; }
public List<TestObject> handleToList() { return null; }
public Set<TestObject> handleToSet() { return null; }
public Mono<TestObject> handleToMono() { return null; }
public Flux<TestObject> handleToFlux() { return null; }
public Single<TestObject> handleToSingle() { return null; }
public Observable<TestObject> handleToObservable() { return null; }
}
}