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: TestValidationRuleNotEmpty.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.site;
009:
010: import junit.framework.TestCase;
011:
012: public class TestValidationRuleNotEmpty extends TestCase {
013: public TestValidationRuleNotEmpty(String name) {
014: super (name);
015: }
016:
017: public void testInstantiation() {
018: Bean bean = new Bean("not empty");
019: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
020: "property").setBean(bean);
021: assertNotNull(rule);
022: }
023:
024: public void testValid() {
025: Bean bean = new Bean("not empty");
026: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
027: "property").setBean(bean);
028: assertTrue(rule.validate());
029: }
030:
031: public void testValidArray() {
032: Bean bean = new Bean(new String[] { "not empty",
033: "not empty either" });
034: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
035: "arrayProperty").setBean(bean);
036: assertTrue(rule.validate());
037: }
038:
039: public void testInvalid() {
040: Bean bean = new Bean("");
041: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
042: "property").setBean(bean);
043: assertFalse(rule.validate());
044: }
045:
046: public void testInvalidArray() {
047: Bean bean = new Bean(new String[] { "not empty", "" });
048: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
049: "arrayProperty").setBean(bean);
050: assertFalse(rule.validate());
051: }
052:
053: public void testInvalidTrim() {
054: Bean bean = new Bean(" ");
055: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
056: "property").setBean(bean);
057: assertFalse(rule.validate());
058: }
059:
060: public void testUnknownProperty() {
061: Bean bean = new Bean("not empty");
062: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
063: "unknown_property").setBean(bean);
064: assertTrue(rule.validate());
065: }
066:
067: public void testGetError() {
068: Bean bean = new Bean("");
069: ValidationRuleNotEmpty rule = new ValidationRuleNotEmpty(
070: "property").setBean(bean);
071: ValidationError error = rule.getError();
072: assertEquals(ValidationError.IDENTIFIER_MANDATORY, error
073: .getIdentifier());
074: assertEquals("property", error.getSubject());
075: assertEquals(rule.getSubject(), error.getSubject());
076: }
077:
078: public class Bean {
079: private String mProperty = null;
080: private String[] mArrayProperty = null;
081:
082: public Bean(String property) {
083: mProperty = property;
084: }
085:
086: public Bean(String[] arrayProperty) {
087: mArrayProperty = arrayProperty;
088: }
089:
090: public void setArrayProperty(String[] arrayProperty) {
091: mArrayProperty = arrayProperty;
092: }
093:
094: public String[] getArrayProperty() {
095: return mArrayProperty;
096: }
097:
098: public void setProperty(String property) {
099: mProperty = property;
100: }
101:
102: public String getProperty() {
103: return mProperty;
104: }
105: }
106: }
|