001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestValidationRuleFormat.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import java.text.SimpleDateFormat;
011: import junit.framework.TestCase;
012:
013: public class TestValidationRuleFormat extends TestCase {
014: public TestValidationRuleFormat(String name) {
015: super (name);
016: }
017:
018: public void testInstantiation() {
019: Bean bean = new Bean("30/01/2004");
020: ValidationRule rule = new ValidationRuleFormat("property",
021: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
022: assertNotNull(rule);
023: }
024:
025: public void testValid() {
026: Bean bean = new Bean("30/01/2004");
027: ValidationRule rule = new ValidationRuleFormat("property",
028: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
029: assertTrue(rule.validate());
030: }
031:
032: public void testValidArray() {
033: Bean bean = new Bean(
034: new String[] { "30/01/2004", "01/03/2006" });
035: ValidationRule rule = new ValidationRuleFormat("arrayProperty",
036: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
037: assertTrue(rule.validate());
038: }
039:
040: public void testInvalid() {
041: Bean bean = new Bean("3/01/2004");
042: ValidationRule rule = new ValidationRuleFormat("property",
043: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
044: assertFalse(rule.validate());
045: }
046:
047: public void testInvalidArray() {
048: Bean bean = new Bean(new String[] { "30/01/2004", "1/10/2006",
049: "17/06/2006" });
050: ValidationRule rule = new ValidationRuleFormat("arrayProperty",
051: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
052: assertFalse(rule.validate());
053: }
054:
055: public void testUnknownProperty() {
056: Bean bean = new Bean("30/01/2004");
057: ValidationRule rule = new ValidationRuleFormat(
058: "unknown_property", new SimpleDateFormat("dd/MM/yyyy"))
059: .setBean(bean);
060: assertTrue(rule.validate());
061: }
062:
063: public void testGetError() {
064: Bean bean = new Bean("82/01/2004");
065: ValidationRule rule = new ValidationRuleFormat("property",
066: new SimpleDateFormat("dd/MM/yyyy")).setBean(bean);
067: ValidationError error = rule.getError();
068: assertEquals(ValidationError.IDENTIFIER_INVALID, error
069: .getIdentifier());
070: assertEquals("property", error.getSubject());
071: assertEquals(rule.getSubject(), error.getSubject());
072: }
073:
074: public class Bean {
075: private String mProperty = null;
076: private String[] mArrayProperty = null;
077:
078: public Bean(String property) {
079: mProperty = property;
080: }
081:
082: public Bean(String[] arrayProperty) {
083: mArrayProperty = arrayProperty;
084: }
085:
086: public void setArrayProperty(String[] arrayProperty) {
087: mArrayProperty = arrayProperty;
088: }
089:
090: public String[] getArrayProperty() {
091: return mArrayProperty;
092: }
093:
094: public void setProperty(String property) {
095: mProperty = property;
096: }
097:
098: public String getProperty() {
099: return mProperty;
100: }
101: }
102: }
|