Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test of JValidator; Clean code of JValidator #3723

Merged
merged 1 commit into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public JValidator(URL url) {
factory = Validation.buildDefaultValidatorFactory();
}
this.validator = factory.getValidator();
this.methodClassMap = new ConcurrentHashMap<String, Class>();
this.methodClassMap = new ConcurrentHashMap<>();
}

private static boolean isPrimitives(Class<?> cls) {
Expand All @@ -117,7 +117,7 @@ private static Object getMethodParameterBean(Class<?> clazz, Method method, Obje
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);
Expand Down Expand Up @@ -243,14 +243,14 @@ else if (memberValue instanceof ArrayMemberValue) {

@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));
Expand All @@ -260,7 +260,7 @@ public void validate(String methodName, Class<?>[] parameterTypes, Object[] argu
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
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
Expand Down Expand Up @@ -56,4 +60,27 @@ public void testItWhenItMeetsConstraint() throws Exception {
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});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.dubbo.validation.MethodValidated;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

public interface JValidatorTestTarget {
@MethodValidated
Expand All @@ -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 {
}

Expand Down