001: /*
002: * $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 $
003: * $Revision: 1.6 $
004: * $Date: 2007/06/20 16:24:56 $
005: *
006: *
007: * Created on Oct 6, 2004
008: *
009: * All right reserved(c) 2004, Knowlogy
010: *
011: * Copyright 2004 - 2005 Knowlogy, the Netherlands. All rights reserved.
012: * All Knowlogy brand and product names are trademarks or registered trademarks
013: * of Knowlogy in the Netherlands and other countries.
014: *
015: * No part of this publication may be reproduced, transmitted, stored in a retrieval system,
016: * or translated into any human or computer language, in any form, or by any means, electronic,
017: * mechanical, magnetic, optical, chemical, manual, or otherwise,
018: * without the prior written permission of the copyright owner, Knowlogy.
019: *
020: */
021: package nl.knowlogy.validation.validators;
022:
023: import junit.framework.TestCase;
024: import nl.knowlogy.validation.ClassValidatorImpl;
025: import nl.knowlogy.validation.Messages;
026: import nl.knowlogy.validation.MessagesImpl;
027: import nl.knowlogy.validation.Message;
028: import nl.knowlogy.validation.PropertyValidation;
029:
030: /**
031: *
032: * ValidatorsTest
033: *
034: * Note that these test do not demonstrate normal usage of the validators.
035: *
036: * @author Robert
037: */
038: public class ValidatorsTest extends TestCase {
039:
040: class DummyClassValidator extends ClassValidatorImpl {
041:
042: public DummyClassValidator(Class clazz) {
043: super (clazz);
044: }
045: }
046:
047: public void testIsRequiredValidator() {
048:
049: IsRequiredValidator isRequiredValidator = new IsRequiredValidator(
050: "test");
051: isRequiredValidator.setErrorCode("missingrequired");
052:
053: checkError(isRequiredValidator, null, "test",
054: "missingrequired", null);
055: checkNoError(isRequiredValidator, new Long(3), "test");
056: }
057:
058: public void testMaxLengthForIntegerValue() {
059:
060: MaxLengthValidator maxLengthValidator = new MaxLengthValidator(
061: "test", new Long(2), "invalidlength");
062: Integer integer = new Integer(888);
063: checkError(maxLengthValidator, integer, "test",
064: "invalidlength", new Object[] { new Long(2),
065: new Integer(3) });
066: checkNoError(maxLengthValidator, new Integer(88), "test");
067: checkNoError(maxLengthValidator, null, "test");
068:
069: }
070:
071: public void testMaxLengthForStringValue() {
072:
073: MaxLengthValidator maxLengthValidator = new MaxLengthValidator(
074: "test", new Long(2), "invalidlength");
075:
076: checkError(maxLengthValidator, "abc", "test", "invalidlength",
077: new Object[] { new Long(2), new Integer(3) });
078:
079: checkNoError(maxLengthValidator, "ab", "test");
080:
081: }
082:
083: private void checkError(PropertyValidation validator, Object value,
084: String propertyName, String errorCode, Object[] messageArgs) {
085: Messages errors = new MessagesImpl();
086: validator.doValidatePropertyValue(null, value, errors);
087:
088: Message invalidProperty = errors.getMessage(null, propertyName);
089: assertNotNull(invalidProperty);
090: assertEquals(invalidProperty.getMessageCode(), errorCode);
091: checkMessageArgs(messageArgs, invalidProperty.getMessageArgs());
092: }
093:
094: private void checkMessageArgs(Object[] expected, Object[] actual) {
095: if (expected != null && actual == null) {
096: fail("actual is null, expected not");
097: }
098: if (expected == null && actual != null) {
099: fail("expected is null, actual not");
100: }
101: if (expected != null && actual != null) {
102: if (expected.length != actual.length) {
103: fail("expected size is different from actual size, expected lenght ["
104: + expected.length
105: + "], actual length ["
106: + actual.length + "]");
107: }
108: for (int i = 0; i < expected.length; i++) {
109: assertEquals(expected[i], actual[i]);
110: }
111: }
112:
113: }
114:
115: private void checkNoError(PropertyValidation validator,
116: Object value, String propertyName) {
117: Messages errors = new MessagesImpl();
118: validator.doValidatePropertyValue(null, value, errors);
119:
120: Message invalidProperty = errors.getMessage(null, propertyName);
121: assertNull(invalidProperty);
122: }
123: }
|