/*
* $Header: /cvsroot/jvalidate/jvalidate-framework/jvalidate/src/test/java/nl/knowlogy/validation/validators/ValidatorsTest.java,v 1.6 2007/06/20 16:24:56 roberthofstra Exp $
* $Revision: 1.6 $
* $Date: 2007/06/20 16:24:56 $
*
*
* Created on Oct 6, 2004
*
* All right reserved(c) 2004, Knowlogy
*
* Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
* All Knowlogy brand and product names are trademarks or registered trademarks
* of Knowlogy in the Netherlands and other countries.
*
* No part of this publication may be reproduced, transmitted, stored in a retrieval system,
* or translated into any human or computer language, in any form, or by any means, electronic,
* mechanical, magnetic, optical, chemical, manual, or otherwise,
* without the prior written permission of the copyright owner, Knowlogy.
*
*/
package nl.knowlogy.validation.validators;
import junit.framework.TestCase;
import nl.knowlogy.validation.ClassValidatorImpl;
import nl.knowlogy.validation.Messages;
import nl.knowlogy.validation.MessagesImpl;
import nl.knowlogy.validation.Message;
import nl.knowlogy.validation.PropertyValidation;
/**
*
* ValidatorsTest
*
* Note that these test do not demonstrate normal usage of the validators.
*
* @author Robert
*/
public class ValidatorsTest extends TestCase {
class DummyClassValidator extends ClassValidatorImpl{
public DummyClassValidator(Class clazz){
super(clazz);
}
}
public void testIsRequiredValidator(){
IsRequiredValidator isRequiredValidator = new IsRequiredValidator("test");
isRequiredValidator.setErrorCode("missingrequired");
checkError(isRequiredValidator, null, "test", "missingrequired", null);
checkNoError(isRequiredValidator, new Long(3),"test");
}
public void testMaxLengthForIntegerValue(){
MaxLengthValidator maxLengthValidator = new MaxLengthValidator("test",new Long(2),"invalidlength");
Integer integer = new Integer(888);
checkError(maxLengthValidator, integer, "test", "invalidlength", new Object[]{new Long(2), new Integer(3)});
checkNoError(maxLengthValidator, new Integer(88), "test");
checkNoError(maxLengthValidator, null, "test");
}
public void testMaxLengthForStringValue(){
MaxLengthValidator maxLengthValidator = new MaxLengthValidator("test", new Long(2),"invalidlength");
checkError(maxLengthValidator, "abc", "test", "invalidlength", new Object[]{new Long(2), new Integer(3)});
checkNoError(maxLengthValidator, "ab", "test");
}
private void checkError(PropertyValidation validator,Object value, String propertyName, String errorCode, Object[] messageArgs){
Messages errors = new MessagesImpl();
validator.doValidatePropertyValue(null, value, errors);
Message invalidProperty = errors.getMessage(null,propertyName);
assertNotNull(invalidProperty);
assertEquals(invalidProperty.getMessageCode(),errorCode);
checkMessageArgs(messageArgs, invalidProperty.getMessageArgs());
}
private void checkMessageArgs(Object[] expected, Object[] actual){
if (expected != null && actual == null){
fail("actual is null, expected not");
}
if (expected == null && actual != null){
fail("expected is null, actual not");
}
if (expected != null && actual != null){
if(expected.length != actual.length){
fail("expected size is different from actual size, expected lenght [" + expected.length +
"], actual length [" + actual.length + "]");
}
for(int i = 0; i < expected.length; i++){
assertEquals( expected[i], actual[i]);
}
}
}
private void checkNoError(PropertyValidation validator,Object value, String propertyName){
Messages errors = new MessagesImpl();
validator.doValidatePropertyValue(null, value, errors);
Message invalidProperty = errors.getMessage(null,propertyName);
assertNull(invalidProperty);
}
}
|