001 /******************************************************************************* 002 * Portions created by Sebastian Thomschke are copyright (c) 2005-2011 Sebastian 003 * Thomschke. 004 * 005 * All Rights Reserved. This program and the accompanying materials 006 * are made available under the terms of the Eclipse Public License v1.0 007 * which accompanies this distribution, and is available at 008 * http://www.eclipse.org/legal/epl-v10.html 009 * 010 * Contributors: 011 * Sebastian Thomschke - initial implementation. 012 *******************************************************************************/ 013 package net.sf.oval; 014 015 import java.lang.reflect.Field; 016 import java.util.List; 017 018 import net.sf.oval.exception.ConstraintsViolatedException; 019 import net.sf.oval.exception.ValidationFailedException; 020 021 /** 022 * An interface implemented by Validator for easier mocking. 023 * 024 * @author Sebastian Thomschke 025 */ 026 public interface IValidator 027 { 028 029 /** 030 * validates the field and getter constrains of the given object 031 * and throws an ConstraintsViolatedException if any constraint 032 * violations are detected 033 * 034 * @param validatedObject the object to validate, cannot be null 035 * @throws ConstraintsViolatedException 036 * @throws ValidationFailedException 037 * @throws IllegalArgumentException if <code>validatedObject == null</code> 038 */ 039 void assertValid(final Object validatedObject) throws IllegalArgumentException, ValidationFailedException, 040 ConstraintsViolatedException; 041 042 /** 043 * Validates the give value against the defined field constraints and throws 044 * an ConstraintsViolatedException if any constraint violations are detected.<br> 045 * 046 * @param validatedObject the object to validate, cannot be null 047 * @param validatedField the field to validate, cannot be null 048 * @throws IllegalArgumentException if <code>validatedObject == null</code> or <code>field == null</code> 049 * @throws ConstraintsViolatedException 050 * @throws ValidationFailedException 051 */ 052 void assertValidFieldValue(final Object validatedObject, final Field validatedField, 053 final Object fieldValueToValidate) throws IllegalArgumentException, ValidationFailedException, 054 ConstraintsViolatedException; 055 056 /** 057 * validates the field and getter constrains of the given object 058 * 059 * @param validatedObject the object to validate, cannot be null 060 * @return a list with the detected constraint violations. if no violations are detected an empty list is returned 061 * @throws ValidationFailedException 062 * @throws IllegalArgumentException if <code>validatedObject == null</code> 063 */ 064 List<ConstraintViolation> validate(final Object validatedObject) throws IllegalArgumentException, 065 ValidationFailedException; 066 067 /** 068 * validates the field and getter constrains of the given object 069 * 070 * @param validatedObject the object to validate, cannot be null 071 * @param profiles constraint profiles to validate against, by default the globally enabled profiles are used that. 072 * @return a list with the detected constraint violations. if no violations are detected an empty list is returned 073 * @throws ValidationFailedException 074 * @throws IllegalArgumentException if <code>validatedObject == null</code> 075 */ 076 List<ConstraintViolation> validate(final Object validatedObject, String... profiles) 077 throws IllegalArgumentException, ValidationFailedException; 078 079 /** 080 * Validates the give value against the defined field constraints.<br> 081 * 082 * @return a list with the detected constraint violations. if no violations are detected an empty list is returned 083 * @throws IllegalArgumentException if <code>validatedObject == null</code> or <code>validatedField == null</code> 084 * @throws ValidationFailedException 085 */ 086 List<ConstraintViolation> validateFieldValue(final Object validatedObject, final Field validatedField, 087 final Object fieldValueToValidate) throws IllegalArgumentException, ValidationFailedException; 088 089 }