下面列出了org.springframework.core.MethodParameter#forExecutable ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void resolvesJoinContainerWithRegularJoin() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod_joinContainerWithRegularJoin"), 0);
FakeWebRequest req = new FakeWebRequest();
QueryContext queryCtx = new WebRequestQueryContext(req);
req.setParameterValues("path1", "value1");
Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);
assertThat(innerSpecs(resolved))
.hasSize(2)
.contains(new Like<Object>(queryCtx, "path1", new String[] { "value1" }))
.contains(new Conjunction<Object>(
new net.kaczmarzyk.spring.data.jpa.domain.Join<Object>(queryCtx, "join1", "alias1", JoinType.INNER, true),
new net.kaczmarzyk.spring.data.jpa.domain.Join<Object>(queryCtx, "join2", "alias2", JoinType.LEFT, false)));
}
protected static void validateStreamListenerMessageHandler(Method method) {
int methodArgumentsLength = method.getParameterTypes().length;
if (methodArgumentsLength > 1) {
int numAnnotatedMethodParameters = 0;
int numPayloadAnnotations = 0;
for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) {
MethodParameter methodParameter = MethodParameter.forExecutable(method,
parameterIndex);
if (methodParameter.hasParameterAnnotations()) {
numAnnotatedMethodParameters++;
}
if (methodParameter.hasParameterAnnotation(Payload.class)) {
numPayloadAnnotations++;
}
}
if (numPayloadAnnotations > 0) {
Assert.isTrue(
methodArgumentsLength == numAnnotatedMethodParameters
&& numPayloadAnnotations <= 1,
StreamListenerErrorMessages.AMBIGUOUS_MESSAGE_HANDLER_METHOD_ARGUMENTS);
}
}
}
@Test
public void resolvesWrapperOfSimpleSpecsAndOrs() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });
when(req.getParameterValues("path2")).thenReturn(new String[] { "value2" });
when(req.getParameterValues("path3")).thenReturn(new String[] { "value3" });
when(req.getParameterValues("path4")).thenReturn(new String[] { "value4" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> result = resolver.buildSpecification(ctx, param.getParameterAnnotation(Conjunction.class));
Specification<Object> or = new Disjunction<>(new Like<>(ctx.queryContext(), "path3", "value3"),
new Like<>(ctx.queryContext(), "path4", "value4"));
assertThat(result).isEqualTo(new net.kaczmarzyk.spring.data.jpa.domain.Conjunction<>(or, new Like<>(ctx.queryContext(), "path1", "value1"),
new Like<>(ctx.queryContext(), "path2", "value2")));
}
@Test
public void resolvesJoinFetchForSimpleSpec() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });
Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);
assertThat(innerSpecs(resolved))
.hasSize(2)
.contains(new Like<Object>(queryCtx, "path1", new String[] { "value1" }))
.contains(new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1", "fetch2" }, JoinType.LEFT));
}
/**
* Resolve the prepared arguments stored in the given bean definition.
*/
private Object[] resolvePreparedArguments(String beanName, RootBeanDefinition mbd, BeanWrapper bw,
Executable executable, Object[] argsToResolve, boolean fallback) {
TypeConverter customConverter = this.beanFactory.getCustomTypeConverter();
TypeConverter converter = (customConverter != null ? customConverter : bw);
BeanDefinitionValueResolver valueResolver =
new BeanDefinitionValueResolver(this.beanFactory, beanName, mbd, converter);
Class<?>[] paramTypes = executable.getParameterTypes();
Object[] resolvedArgs = new Object[argsToResolve.length];
for (int argIndex = 0; argIndex < argsToResolve.length; argIndex++) {
Object argValue = argsToResolve[argIndex];
MethodParameter methodParam = MethodParameter.forExecutable(executable, argIndex);
GenericTypeResolver.resolveParameterType(methodParam, executable.getDeclaringClass());
if (argValue instanceof AutowiredArgumentMarker) {
argValue = resolveAutowiredArgument(methodParam, beanName, null, converter, fallback);
}
else if (argValue instanceof BeanMetadataElement) {
argValue = valueResolver.resolveValueIfNecessary("constructor argument", argValue);
}
else if (argValue instanceof String) {
argValue = this.beanFactory.evaluateBeanDefinitionString((String) argValue, mbd);
}
Class<?> paramType = paramTypes[argIndex];
try {
resolvedArgs[argIndex] = converter.convertIfNecessary(argValue, paramType, methodParam);
}
catch (TypeMismatchException ex) {
throw new UnsatisfiedDependencyException(
mbd.getResourceDescription(), beanName, new InjectionPoint(methodParam),
"Could not convert argument value of type [" + ObjectUtils.nullSafeClassName(argValue) +
"] to required type [" + paramType.getName() + "]: " + ex.getMessage());
}
}
return resolvedArgs;
}
@Test
public void resolvesJoinFetchEvenIfOtherSpecificationIsNotPresent() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
Specification<?> resolved = (Specification<?>) resolver.resolveArgument(param, null, req, null);
assertThat(resolved)
.isEqualTo(new net.kaczmarzyk.spring.data.jpa.domain.JoinFetch<Object>(new String[] { "fetch1", "fetch2" }, JoinType.LEFT));
}
@Test
public void buildsTheSpecUsingMultiValueWebParameterTheSameAsPathAndParamSeparator() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod6"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("thePath")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2", "val3", "val4", "val5", "val6", "val7" }, converter));
}
@Test
public void skipsEmptyWebParameterValues() {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod3"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("theParameter")).thenReturn(new String[] { "value1", "" });
Specification<Object> resolved = resolver.buildSpecification(new WebRequestProcessingContext(param, req), param.getParameterAnnotation(Spec.class));
assertThat(resolved).isEqualTo(new EqualEnum<>(queryCtx, "thePath", new String[] { "value1" }));
}
@Test
public void resolves5ArgsSpec() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("methodWith5argSpec"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
SpecWith5ArgConstructor resolved = (SpecWith5ArgConstructor) resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved.path).isEqualTo("thePath");
assertThat(resolved.args).isEqualTo(new String[] { "theValue" });
assertThat(resolved.converter).isEqualTo(Converter.withDateFormat("yyyyMMdd", OnTypeMismatch.EXCEPTION, null));
assertThat(resolved.config).isEqualTo(new String[] { "yyyyMMdd" });
}
@Test
public void resolves3ArgsSpec() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("methodWith3argSpec"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
SpecWith3ArgConstructor resolved = (SpecWith3ArgConstructor) resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved.path).isEqualTo("thePath");
assertThat(resolved.args).isEqualTo(new String[] { "theValue" });
}
private boolean checkDeclarativeMethod(Method method,
String methodAnnotatedInboundName, String methodAnnotatedOutboundName) {
int methodArgumentsLength = method.getParameterTypes().length;
for (int parameterIndex = 0; parameterIndex < methodArgumentsLength; parameterIndex++) {
MethodParameter methodParameter = MethodParameter.forExecutable(method,
parameterIndex);
if (methodParameter.hasParameterAnnotation(Input.class)) {
String inboundName = (String) AnnotationUtils.getValue(
methodParameter.getParameterAnnotation(Input.class));
Assert.isTrue(StringUtils.hasText(inboundName),
StreamListenerErrorMessages.INVALID_INBOUND_NAME);
Assert.isTrue(
isDeclarativeMethodParameter(inboundName, methodParameter),
StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
return true;
}
else if (methodParameter.hasParameterAnnotation(Output.class)) {
String outboundName = (String) AnnotationUtils.getValue(
methodParameter.getParameterAnnotation(Output.class));
Assert.isTrue(StringUtils.hasText(outboundName),
StreamListenerErrorMessages.INVALID_OUTBOUND_NAME);
Assert.isTrue(
isDeclarativeMethodParameter(outboundName, methodParameter),
StreamListenerErrorMessages.INVALID_DECLARATIVE_METHOD_PARAMETERS);
return true;
}
else if (StringUtils.hasText(methodAnnotatedOutboundName)) {
return isDeclarativeMethodParameter(methodAnnotatedOutboundName,
methodParameter);
}
else if (StringUtils.hasText(methodAnnotatedInboundName)) {
return isDeclarativeMethodParameter(methodAnnotatedInboundName,
methodParameter);
}
}
return false;
}
@Test
public void buildsTheSpecUsingMultiValueWebParameterAndParamSeparator() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod8"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("theParameter")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2", "val3", "val4", "val5", "val6", "val7" }, converter));
}
@Test
public void buildsTheSpecUsingMultiValueWebParameterWithoutParamSeparator() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod7"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("theParameter")).thenReturn(new String[] {"val1", "val2,val3,val4", "val5,val6", "val7"});
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isEqualTo(new In<>(queryCtx, "thePath", new String[] { "val1", "val2,val3,val4", "val5,val6", "val7" }, converter));
}
@Test
public void buildsTheSpecUsingCustomMultiValueWebParameterName() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod3"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
QueryContext queryCtx = new WebRequestQueryContext(req);
when(req.getParameterValues("theParameter")).thenReturn(new String[] { "theValue", "theValue2" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isEqualTo(new EqualEnum<>(queryCtx, "thePath", new String[] { "theValue", "theValue2" }));
}
@Test
public void resolvesZeroArgSpecificatinEvenWithoutAnyWebParameters() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethodWithZeroArgSpec"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isInstanceOf(IsNull.class);
}
@Test
public void returnsNullIfTheWebParameterIsMissing_customParameterName() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved).isNull();
}
@Test
public void doesNotWrapWithEmptyResultSpecWhenSpecificationDoesntRequireDataConversion() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethodWithSpecWithoutDataConversion"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("thePath")).thenReturn(new String[]{ "theValue" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> resolved = resolver.buildSpecification(ctx, param.getParameterAnnotation(Spec.class));
assertThat(resolved)
.isNotInstanceOf(EmptyResultOnTypeMismatch.class)
.isInstanceOf(SpecWithoutDataConversion.class);
}
@Test
public void skipsMissingInnerSpec() throws Exception {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("path1")).thenReturn(new String[] { "value1" });
WebRequestProcessingContext ctx = new WebRequestProcessingContext(param, req);
Specification<?> result = resolver.buildSpecification(ctx, param.getParameterAnnotation(Or.class));
assertThat(result).isEqualTo(new Disjunction<>(new Like<>(ctx.queryContext(), "path1", "value1")));
}
@Test
public void returnsNullIfAtLeastOneEmptyWebParameter_customParameterName() {
MethodParameter param = MethodParameter.forExecutable(testMethod("testMethod2"), 0);
NativeWebRequest req = mock(NativeWebRequest.class);
when(req.getParameterValues("thePath")).thenReturn(new String[] { "theValue", "theValue2", "" });
assertThat(resolver.buildSpecification(new WebRequestProcessingContext(param, req), param.getParameterAnnotation(Spec.class))).isNull();;
}
private MethodParameter testMethodParameter(String methodName, Class<?> controllerClass) {
return MethodParameter.forExecutable(testMethod(methodName, controllerClass, Specification.class), 0);
}