Skip to content

Commit

Permalink
Rename MethodValidationTests and polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jun 12, 2023
1 parent 082784e commit 38abee0
Showing 1 changed file with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -50,32 +51,36 @@
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Tests for proxy-based method validation via {@link MethodValidationInterceptor}
* and/or {@link MethodValidationPostProcessor}.
*
* @author Juergen Hoeller
* @author Rossen Stoyanchevß
*/
public class MethodValidationTests {
public class MethodValidationProxyTests {

@Test
@SuppressWarnings("unchecked")
public void testMethodValidationInterceptor() {
MyValidBean bean = new MyValidBean();
ProxyFactory proxyFactory = new ProxyFactory(bean);
proxyFactory.addAdvice(new MethodValidationInterceptor());
proxyFactory.addAdvisor(new AsyncAnnotationAdvisor());
doTestProxyValidation((MyValidInterface<String>) proxyFactory.getProxy());
ProxyFactory factory = new ProxyFactory(bean);
factory.addAdvice(new MethodValidationInterceptor());
factory.addAdvisor(new AsyncAnnotationAdvisor());
doTestProxyValidation((MyValidInterface<String>) factory.getProxy());
}

@Test
@SuppressWarnings("unchecked")
public void testMethodValidationPostProcessor() {
StaticApplicationContext ac = new StaticApplicationContext();
ac.registerSingleton("mvpp", MethodValidationPostProcessor.class);
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("mvpp", MethodValidationPostProcessor.class);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("beforeExistingAdvisors", false);
ac.registerSingleton("aapp", AsyncAnnotationBeanPostProcessor.class, pvs);
ac.registerSingleton("bean", MyValidBean.class);
ac.refresh();
doTestProxyValidation(ac.getBean("bean", MyValidInterface.class));
ac.close();
context.registerSingleton("aapp", AsyncAnnotationBeanPostProcessor.class, pvs);
context.registerSingleton("bean", MyValidBean.class);
context.refresh();
doTestProxyValidation(context.getBean("bean", MyValidInterface.class));
context.close();
}

@Test // gh-29782
Expand All @@ -90,46 +95,41 @@ public void testMethodValidationPostProcessorForInterfaceOnlyProxy() {
context.close();
}

@SuppressWarnings("DataFlowIssue")
private void doTestProxyValidation(MyValidInterface<String> proxy) {
assertThat(proxy.myValidMethod("value", 5)).isNotNull();
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myValidMethod("value", 15));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myValidMethod(null, 5));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myValidMethod("value", 0));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myValidMethod("value", 15));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myValidMethod(null, 5));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myValidMethod("value", 0));
proxy.myValidAsyncMethod("value", 5);
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myValidAsyncMethod("value", 15));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myValidAsyncMethod(null, 5));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myValidAsyncMethod("value", 15));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myValidAsyncMethod(null, 5));
assertThat(proxy.myGenericMethod("myValue")).isEqualTo("myValue");
assertThatExceptionOfType(ValidationException.class).isThrownBy(() ->
proxy.myGenericMethod(null));
assertThatExceptionOfType(ValidationException.class).isThrownBy(() -> proxy.myGenericMethod(null));
}

@Test
public void testLazyValidatorForMethodValidation() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ApplicationContext context = new AnnotationConfigApplicationContext(
LazyMethodValidationConfig.class, CustomValidatorBean.class,
MyValidBean.class, MyValidFactoryBean.class);
ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
context.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
}

@Test
public void testLazyValidatorForMethodValidationWithProxyTargetClass() {
@SuppressWarnings("resource")
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(
ApplicationContext context = new AnnotationConfigApplicationContext(
LazyMethodValidationConfigWithProxyTargetClass.class, CustomValidatorBean.class,
MyValidBean.class, MyValidFactoryBean.class);
ctx.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
context.getBeansOfType(MyValidInterface.class).values().forEach(bean -> bean.myValidMethod("value", 5));
}


@MyStereotype
public static class MyValidBean implements MyValidInterface<String> {

@SuppressWarnings("DataFlowIssue")
@NotNull
@Override
public Object myValidMethod(String arg1, int arg2) {
return (arg2 == 0 ? null : "value");
Expand Down Expand Up @@ -159,6 +159,8 @@ public Class<?> getObjectType() {
return String.class;
}

@SuppressWarnings("DataFlowIssue")
@NotNull
@Override
public Object myValidMethod(String arg1, int arg2) {
return (arg2 == 0 ? null : "value");
Expand All @@ -178,10 +180,12 @@ public String myGenericMethod(String value) {
@MyStereotype
public interface MyValidInterface<T> {

@NotNull Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2);
@NotNull
Object myValidMethod(@NotNull(groups = MyGroup.class) String arg1, @Max(10) int arg2);

@MyValid
@Async void myValidAsyncMethod(@NotNull(groups = OtherGroup.class) String arg1, @Max(10) int arg2);
@Async
void myValidAsyncMethod(@NotNull(groups = OtherGroup.class) String arg1, @Max(10) int arg2);

T myGenericMethod(@NotNull T value);
}
Expand Down

0 comments on commit 38abee0

Please sign in to comment.