Unit test of JValidator; Clean code of JValidator (#3723)
This commit is contained in:
parent
6e4ff91dfc
commit
c1be6c6160
|
|
@ -94,7 +94,7 @@ public class JValidator implements Validator {
|
|||
factory = Validation.buildDefaultValidatorFactory();
|
||||
}
|
||||
this.validator = factory.getValidator();
|
||||
this.methodClassMap = new ConcurrentHashMap<String, Class>();
|
||||
this.methodClassMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
|
||||
private static boolean isPrimitives(Class<?> cls) {
|
||||
|
|
@ -117,7 +117,7 @@ public class JValidator implements Validator {
|
|||
String parameterClassName = generateMethodParameterClassName(clazz, method);
|
||||
Class<?> parameterClass;
|
||||
try {
|
||||
parameterClass = (Class<?>) Class.forName(parameterClassName, true, clazz.getClassLoader());
|
||||
parameterClass = Class.forName(parameterClassName, true, clazz.getClassLoader());
|
||||
} catch (ClassNotFoundException e) {
|
||||
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
|
||||
CtClass ctClass = pool.makeClass(parameterClassName);
|
||||
|
|
@ -243,14 +243,14 @@ public class JValidator implements Validator {
|
|||
|
||||
@Override
|
||||
public void validate(String methodName, Class<?>[] parameterTypes, Object[] arguments) throws Exception {
|
||||
List<Class<?>> groups = new ArrayList<Class<?>>();
|
||||
List<Class<?>> groups = new ArrayList<>();
|
||||
Class<?> methodClass = methodClass(methodName);
|
||||
if (methodClass != null) {
|
||||
groups.add(methodClass);
|
||||
}
|
||||
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>();
|
||||
Set<ConstraintViolation<?>> violations = new HashSet<>();
|
||||
Method method = clazz.getMethod(methodName, parameterTypes);
|
||||
Class<?>[] methodClasses = null;
|
||||
Class<?>[] methodClasses;
|
||||
if (method.isAnnotationPresent(MethodValidated.class)){
|
||||
methodClasses = method.getAnnotation(MethodValidated.class).value();
|
||||
groups.addAll(Arrays.asList(methodClasses));
|
||||
|
|
@ -260,7 +260,7 @@ public class JValidator implements Validator {
|
|||
groups.add(1, clazz);
|
||||
|
||||
// convert list to array
|
||||
Class<?>[] classgroups = groups.toArray(new Class[0]);
|
||||
Class<?>[] classgroups = groups.toArray(new Class[groups.size()]);
|
||||
|
||||
Object parameterBean = getMethodParameterBean(clazz, method, arguments);
|
||||
if (parameterBean != null) {
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ import org.junit.jupiter.api.Assertions;
|
|||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class JValidatorTest {
|
||||
@Test
|
||||
|
|
@ -56,4 +60,27 @@ public class JValidatorTest {
|
|||
JValidator jValidator = new JValidator(url);
|
||||
jValidator.validate("someMethod2", new Class<?>[]{ValidationParameter.class}, new Object[]{new ValidationParameter("NotBeNull")});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItWithArrayArg() throws Exception {
|
||||
URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
|
||||
JValidator jValidator = new JValidator(url);
|
||||
jValidator.validate("someMethod3", new Class<?>[]{ValidationParameter[].class}, new Object[]{new ValidationParameter[]{new ValidationParameter("parameter")}});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItWithCollectionArg() throws Exception {
|
||||
URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
|
||||
JValidator jValidator = new JValidator(url);
|
||||
jValidator.validate("someMethod4", new Class<?>[]{List.class}, new Object[]{Arrays.asList("parameter")});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testItWithMapArg() throws Exception {
|
||||
URL url = URL.valueOf("test://test:11/org.apache.dubbo.validation.support.jvalidation.mock.JValidatorTestTarget");
|
||||
JValidator jValidator = new JValidator(url);
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("key", "value");
|
||||
jValidator.validate("someMethod5", new Class<?>[]{Map.class}, new Object[]{map});
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,8 @@ package org.apache.dubbo.validation.support.jvalidation.mock;
|
|||
import org.apache.dubbo.validation.MethodValidated;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface JValidatorTestTarget {
|
||||
@MethodValidated
|
||||
|
|
@ -27,6 +29,12 @@ public interface JValidatorTestTarget {
|
|||
@MethodValidated(Test2.class)
|
||||
public void someMethod2(@NotNull ValidationParameter validationParameter);
|
||||
|
||||
public void someMethod3(ValidationParameter[] parameters);
|
||||
|
||||
public void someMethod4(List<String> strings);
|
||||
|
||||
public void someMethod5(Map<String, String> map);
|
||||
|
||||
@interface Test2 {
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue