branched for 1.x releases
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/branches/common/common-validator/1.0@83926 82a268e6-3cf1-43bd-a215-b396298e98cffeature/24727
commit
d971d5c04c
@ -0,0 +1,36 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" output="target/classes" path="src/main/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="output" path="target/classes"/>
|
||||
</classpath>
|
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>common-validator</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
@ -0,0 +1,7 @@
|
||||
#Thu Oct 24 11:33:16 CEST 2013
|
||||
eclipse.preferences.version=1
|
||||
encoding//src/main/java=UTF-8
|
||||
encoding//src/main/resources=UTF-8
|
||||
encoding//src/test/java=UTF-8
|
||||
encoding//src/test/resources=UTF-8
|
||||
encoding/<project>=UTF-8
|
@ -0,0 +1,13 @@
|
||||
#Thu Oct 24 11:33:16 CEST 2013
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
@ -0,0 +1,5 @@
|
||||
#Mon Sep 09 18:07:27 CEST 2013
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
@ -0,0 +1 @@
|
||||
|
@ -0,0 +1,6 @@
|
||||
gCube System - License
|
||||
------------------------------------------------------------
|
||||
|
||||
The gCube/gCore software is licensed as Free Open Source software conveying to the EUPL (http://ec.europa.eu/idabc/eupl).
|
||||
The software and documentation is provided by its authors/distributors "as is" and no expressed or
|
||||
implied warranty is given for its use, quality or fitness for a particular case.
|
@ -0,0 +1,2 @@
|
||||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
@ -0,0 +1,39 @@
|
||||
The gCube System - ${name}
|
||||
----------------------
|
||||
|
||||
This work has been partially supported by the following European projects: DILIGENT (FP6-2003-IST-2), D4Science (FP7-INFRA-2007-1.2.2),
|
||||
D4Science-II (FP7-INFRA-2008-1.2.2), iMarine (FP7-INFRASTRUCTURES-2011-2), and EUBrazilOpenBio (FP7-ICT-2011-EU-Brazil).
|
||||
|
||||
Authors
|
||||
-------
|
||||
|
||||
* Lucio Lelii (lucio.lelii@isti.cnr.it), CNR, Italy
|
||||
* Fabio Simeoni (fabio.simeoni@fao.org), FAO of the UN, Italy
|
||||
|
||||
Version and Release Date
|
||||
------------------------
|
||||
${version}
|
||||
|
||||
Description
|
||||
-----------
|
||||
${description}
|
||||
|
||||
Download information
|
||||
--------------------
|
||||
|
||||
Source code is available from SVN:
|
||||
${scm.url}
|
||||
|
||||
Binaries can be downloaded from:
|
||||
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
Documentation is available on-line from the Projects Documentation Wiki:
|
||||
https://gcube.wiki.gcube-system.org/gcube/index.php/....
|
||||
|
||||
|
||||
Licensing
|
||||
---------
|
||||
|
||||
This software is licensed under the terms you may find in the file named "LICENSE" in this directory.
|
@ -0,0 +1,5 @@
|
||||
<ReleaseNotes>
|
||||
<Changeset component="${build.finalName}" date="2013-10-24">
|
||||
<Change>First Release</Change>
|
||||
</Changeset>
|
||||
</ReleaseNotes>
|
@ -0,0 +1,42 @@
|
||||
<assembly
|
||||
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||
<id>servicearchive</id>
|
||||
<formats>
|
||||
<format>tar.gz</format>
|
||||
</formats>
|
||||
<baseDirectory>/</baseDirectory>
|
||||
<fileSets>
|
||||
<fileSet>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<outputDirectory>/</outputDirectory>
|
||||
<useDefaultExcludes>true</useDefaultExcludes>
|
||||
<includes>
|
||||
<include>README</include>
|
||||
<include>LICENSE</include>
|
||||
<include>INSTALL</include>
|
||||
<include>MAINTAINERS</include>
|
||||
<include>changelog.xml</include>
|
||||
</includes>
|
||||
<fileMode>755</fileMode>
|
||||
<filtered>true</filtered>
|
||||
</fileSet>
|
||||
</fileSets>
|
||||
<files>
|
||||
<file>
|
||||
<source>${distroDirectory}/profile.xml</source>
|
||||
<outputDirectory>/etc</outputDirectory>
|
||||
<filtered>true</filtered>
|
||||
</file>
|
||||
<file>
|
||||
<source>target/${build.finalName}.jar</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
</file>
|
||||
<file>
|
||||
<source>${distroDirectory}/svnpath.txt</source>
|
||||
<outputDirectory>/${artifactId}</outputDirectory>
|
||||
<filtered>true</filtered>
|
||||
</file>
|
||||
</files>
|
||||
</assembly>
|
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<ID />
|
||||
<Type>Service</Type>
|
||||
<Profile>
|
||||
<Description>${description}</Description>
|
||||
<Class>Common</Class>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>1.0.0</Version>
|
||||
<Packages>
|
||||
<Software>
|
||||
<Name>${artifactId}</Name>
|
||||
<Version>${version}</Version>
|
||||
<MavenCoordinates>
|
||||
<groupId>${groupId}</groupId>
|
||||
<artifactId>${artifactId}</artifactId>
|
||||
<version>${version}</version>
|
||||
</MavenCoordinates>
|
||||
<Files>
|
||||
<File>${build.finalName}.jar</File>
|
||||
</Files>
|
||||
</Software>
|
||||
</Packages>
|
||||
</Profile>
|
||||
</Resource>
|
||||
|
@ -0,0 +1 @@
|
||||
${scm.url}
|
@ -0,0 +1,101 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<artifactId>maven-parent</artifactId>
|
||||
<groupId>org.gcube.tools</groupId>
|
||||
<version>1.0.0</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<groupId>org.gcube.core</groupId>
|
||||
<artifactId>common-validator</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>validator</name>
|
||||
|
||||
<properties>
|
||||
<distroDirectory>distro</distroDirectory>
|
||||
</properties>
|
||||
|
||||
<scm>
|
||||
<connection>scm:svn:http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</connection>
|
||||
<developerConnection>scm:svn:https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</developerConnection>
|
||||
<url>http://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/${project.artifactId}</url>
|
||||
</scm>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>1.7.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>1.0.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-resources-plugin</artifactId>
|
||||
<version>2.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>copy-profile</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>copy-resources</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<outputDirectory>target</outputDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>${distroDirectory}</directory>
|
||||
<filtering>true</filtering>
|
||||
<includes>
|
||||
<include>profile.xml</include>
|
||||
</includes>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptors>
|
||||
<descriptor>${distroDirectory}/descriptor.xml</descriptor>
|
||||
</descriptors>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>servicearchive</id>
|
||||
<phase>install</phase>
|
||||
<goals>
|
||||
<goal>single</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,70 @@
|
||||
package org.gcube.common.validator;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.gcube.common.validator.annotations.FieldValidator;
|
||||
import org.gcube.common.validator.annotations.IsValid;
|
||||
import org.gcube.common.validator.annotations.ValidityChecker;
|
||||
import org.gcube.common.validator.context.ValidatorContext;
|
||||
|
||||
|
||||
public class DefaultValidator implements Validator{
|
||||
|
||||
public List<ValidationError> validate(Object obj){
|
||||
try{
|
||||
return validateObject(obj, null);
|
||||
}catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<ValidationError> validateObject(Object obj, Object parent) throws Exception{
|
||||
List<ValidationError> errors = new ArrayList<ValidationError>();
|
||||
Class<?> _clazz = obj.getClass();
|
||||
for (Field field:Util.getAllFields(_clazz)){
|
||||
ValidatorContext context = new ValidatorContext(parent, field, obj);
|
||||
validateField(context);
|
||||
errors.addAll(context.errors());
|
||||
}
|
||||
return errors;
|
||||
}
|
||||
|
||||
private void validateField(ValidatorContext context) throws Exception{
|
||||
Field field = context.field();
|
||||
field.setAccessible(true);
|
||||
Object object = context.value();
|
||||
for (Annotation annotation:field.getAnnotations()){
|
||||
if (annotation.annotationType().isAnnotationPresent(ValidityChecker.class)){
|
||||
Class<? extends FieldValidator<?>> managedClass = ((ValidityChecker)annotation.annotationType().getAnnotation(ValidityChecker.class)).managed();
|
||||
FieldValidator<?> validator = managedClass.newInstance();
|
||||
Object toCheck = field.get(object);
|
||||
if (!validator.isValid(toCheck))
|
||||
context.addError(validator.getErrorSuffix());
|
||||
}
|
||||
}
|
||||
|
||||
if(field.isAnnotationPresent(IsValid.class) && field.get(object)!=null){
|
||||
if (field.getType().isArray() ){
|
||||
for (Object o : (Object[])field.get(object))
|
||||
context.addErrors(validateObject(o, object));
|
||||
|
||||
}else if ( field.get(object) instanceof Iterable<?>){
|
||||
for (Object o: (Iterable<?>)field.get(object)){
|
||||
context.addErrors(validateObject(o, object));
|
||||
}
|
||||
} else if (field.get(object) instanceof Map<?,?>){
|
||||
for (Entry<?, ?> entry: ((Map<?,?>)field.get(object)).entrySet())
|
||||
context.addErrors(validateObject(entry.getValue(), object));
|
||||
} else if (!field.getType().isPrimitive())
|
||||
context.addErrors(validateObject(field.get(object), object));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package org.gcube.common.validator;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class Util {
|
||||
|
||||
public static List<Field> getAllFields(Class<?> type) {
|
||||
List<Field> fields = new ArrayList<Field>();
|
||||
for (Class<?> c = type; c != null; c = c.getSuperclass())
|
||||
fields.addAll(Arrays.asList(c.getDeclaredFields()));
|
||||
return fields;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package org.gcube.common.validator;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.gcube.common.validator.context.ValidatorContext;
|
||||
|
||||
public class ValidationError {
|
||||
|
||||
|
||||
public Field field;
|
||||
public Object parent;
|
||||
public Object value;
|
||||
public String msg;
|
||||
|
||||
public ValidationError(ValidatorContext context, String msg) {
|
||||
this.field=context.field();
|
||||
this.parent=context.parent();
|
||||
this.value=context.value();
|
||||
this.msg=msg;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return field.getName().toUpperCase()+" in "+value.getClass().getSimpleName()+" "+msg;
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.gcube.common.validator;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface Validator {
|
||||
|
||||
List<ValidationError> validate(Object obj);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package org.gcube.common.validator;
|
||||
|
||||
public class ValidatorFactory {
|
||||
|
||||
private static Validator validator = new DefaultValidator();
|
||||
|
||||
|
||||
public static Validator validator() {
|
||||
return validator;
|
||||
}
|
||||
|
||||
public static void validator(Validator newValidator) {
|
||||
validator = newValidator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.gcube.common.validator.annotations;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
|
||||
public interface FieldValidator<T extends Annotation> {
|
||||
|
||||
Class<T> annotation();
|
||||
|
||||
boolean isValid(Object toValidate);
|
||||
|
||||
String getErrorSuffix();
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.gcube.common.validator.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface IsValid {
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.gcube.common.validator.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.gcube.common.validator.annotations.validators.NotEmptyValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@ValidityChecker(managed=NotEmptyValidator.class)
|
||||
public @interface NotEmpty {
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.gcube.common.validator.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.gcube.common.validator.annotations.validators.NotNullValidator;
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@ValidityChecker(managed=NotNullValidator.class)
|
||||
public @interface NotNull {
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package org.gcube.common.validator.annotations;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Target(ElementType.ANNOTATION_TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Inherited
|
||||
public @interface ValidityChecker {
|
||||
|
||||
Class<? extends FieldValidator<?>> managed();
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package org.gcube.common.validator.annotations.validators;
|
||||
|
||||
import java.util.Map;
|
||||
import org.gcube.common.validator.annotations.FieldValidator;
|
||||
import org.gcube.common.validator.annotations.NotEmpty;
|
||||
|
||||
public class NotEmptyValidator implements FieldValidator<NotEmpty>{
|
||||
|
||||
public Class<NotEmpty> annotation() {
|
||||
return NotEmpty.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Object toValidate) {
|
||||
if (toValidate == null) return false;
|
||||
if (toValidate.getClass().isArray() ){
|
||||
return ((Object[])toValidate).length>0;
|
||||
}else if ( toValidate instanceof Iterable<?>){
|
||||
return ((Iterable<?>) toValidate).iterator().hasNext();
|
||||
} else if (toValidate instanceof Map<?,?>){
|
||||
return ((Map<?,?>) toValidate).size()>0;
|
||||
} else if (toValidate instanceof String ){
|
||||
return !((String)toValidate).isEmpty();
|
||||
} else return true;
|
||||
}
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return "is empty";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package org.gcube.common.validator.annotations.validators;
|
||||
|
||||
import org.gcube.common.validator.annotations.FieldValidator;
|
||||
import org.gcube.common.validator.annotations.NotNull;
|
||||
|
||||
public class NotNullValidator implements FieldValidator<NotNull>{
|
||||
|
||||
public String getErrorSuffix() {
|
||||
return "is null";
|
||||
}
|
||||
|
||||
public Class<NotNull> annotation() {
|
||||
return NotNull.class;
|
||||
}
|
||||
|
||||
public boolean isValid(Object toValidate) {
|
||||
return toValidate!=null;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.gcube.common.validator.context;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.validator.ValidationError;
|
||||
|
||||
public class ValidatorContext {
|
||||
|
||||
private List<ValidationError> errors = new ArrayList<ValidationError>();
|
||||
|
||||
private Object parent;
|
||||
private Field field;
|
||||
private Object value;
|
||||
|
||||
public ValidatorContext(Object parent, Field field, Object value) {
|
||||
super();
|
||||
this.parent = parent;
|
||||
this.field = field;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void addError(String msg) {
|
||||
errors.add(new ValidationError(this,msg));
|
||||
}
|
||||
|
||||
public void addErrors(List<ValidationError> errorsToAdd) {
|
||||
errors.addAll(errorsToAdd);
|
||||
}
|
||||
|
||||
public List<ValidationError> errors() {
|
||||
return errors;
|
||||
}
|
||||
|
||||
public Object parent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public Field field() {
|
||||
return field;
|
||||
}
|
||||
|
||||
public Object value() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,39 @@
|
||||
package org.gcube.commons.common.validator.beans;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.validator.ValidationError;
|
||||
import org.gcube.common.validator.ValidatorFactory;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class AnnotationTest {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AnnotationTest.class);
|
||||
|
||||
@Test
|
||||
public void NotNullfail() throws Exception{
|
||||
ExampleValidation exampleValidation = new ExampleValidation();
|
||||
exampleValidation.setOffice("I43");
|
||||
List<Person> persons = new ArrayList<Person>();
|
||||
persons.add(new Person("Lucio", null, new Person[]{new Person(null, "Lelii", null)}));
|
||||
exampleValidation.setPeople(persons);
|
||||
Map<String, Person> slavePerPerson = new HashMap<String, Person>();
|
||||
//slavePerPerson.put("lucio", new Person("fabio", null, null));
|
||||
exampleValidation.setSlavePerPerson(slavePerPerson);
|
||||
Set<Person> bosses= new HashSet<Person>();
|
||||
bosses.add(new Person("", "pagano", null));
|
||||
exampleValidation.setBosses(bosses);
|
||||
List<ValidationError> errors = ValidatorFactory.validator().validate(exampleValidation);
|
||||
Assert.assertTrue(errors.size()>0);
|
||||
logger.trace(errors.toString());
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package org.gcube.commons.common.validator.beans;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.validator.annotations.IsValid;
|
||||
import org.gcube.common.validator.annotations.NotEmpty;
|
||||
import org.gcube.common.validator.annotations.NotNull;
|
||||
|
||||
public class ExampleValidation {
|
||||
|
||||
@NotNull
|
||||
private String office;
|
||||
|
||||
@NotNull @IsValid
|
||||
private List<Person> people;
|
||||
|
||||
@IsValid
|
||||
private Set<Person> bosses;
|
||||
|
||||
@IsValid @NotEmpty
|
||||
private Map<String, Person> slavePerPerson;
|
||||
|
||||
public void setOffice(String office) {
|
||||
this.office = office;
|
||||
}
|
||||
|
||||
public void setPeople(List<Person> people) {
|
||||
this.people = people;
|
||||
}
|
||||
|
||||
public void setBosses(Set<Person> bosses) {
|
||||
this.bosses = bosses;
|
||||
}
|
||||
|
||||
public void setSlavePerPerson(Map<String, Person> slavePerPerson) {
|
||||
this.slavePerPerson = slavePerPerson;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ExampleValidation [office=" + office + ", people=" + people
|
||||
+ ", bosses=" + bosses + ", slavePerPerson=" + slavePerPerson
|
||||
+ "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,11 @@
|
||||
package org.gcube.commons.common.validator.beans;
|
||||
|
||||
import org.gcube.common.validator.annotations.IsValid;
|
||||
import org.gcube.common.validator.annotations.NotEmpty;
|
||||
|
||||
public class Extension {
|
||||
|
||||
@IsValid @NotEmpty
|
||||
Person[] brothers;
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package org.gcube.commons.common.validator.beans;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.gcube.common.validator.annotations.NotEmpty;
|
||||
import org.gcube.common.validator.annotations.NotNull;
|
||||
|
||||
public class Person extends Extension{
|
||||
|
||||
public Person(String firstName, String lastName, Person[] brothers) {
|
||||
super();
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.brothers = brothers;
|
||||
}
|
||||
|
||||
@NotNull @NotEmpty
|
||||
String firstName;
|
||||
|
||||
@NotNull @NotEmpty
|
||||
String lastName;
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", brothers=" + Arrays.toString(brothers) + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0}: %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
|
||||
<logger name="org.gcube" level="TRACE" />
|
||||
|
||||
<root level="WARN">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
Loading…
Reference in New Issue