    1   /*
    2    * Copyright 2004-2009 the Seasar Foundation and the Others.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
    13   * either express or implied. See the License for the specific language
    14   * governing permissions and limitations under the License.
    15   */
    16  package org.seasar.cubby.validator;
    17
    18  import static org.junit.Assert.assertEquals;
    19  import static org.junit.Assert.assertFalse;
    20  import static org.junit.Assert.assertTrue;
    21  import static org.seasar.cubby.validator.DefaultValidationRules.DATA_CONSTRAINT;
    22  import static org.seasar.cubby.validator.DefaultValidationRules.DATA_TYPE;
    23
    24  import java.util.Collection;
    25  import java.util.Iterator;
    26  import java.util.Map;
    27
    28  import org.junit.Test;
    29  import org.seasar.cubby.action.ActionErrors;
    30  import org.seasar.cubby.action.ActionResult;
    31  import org.seasar.cubby.action.Forward;
    32  import org.seasar.cubby.action.Redirect;
    33  import org.seasar.cubby.validator.validators.EqualsValidator;
    34  import org.seasar.cubby.validator.validators.MaxLengthValidator;
    35  import org.seasar.cubby.validator.validators.NumberValidator;
    36  import org.seasar.cubby.validator.validators.RangeValidator;
    37  import org.seasar.cubby.validator.validators.RequiredValidator;
    38
    39  public class DefaultValidationRulesTest {
    40
    41  	@Test
    42  	public void addAndGetRules1() throws Exception {
    43  		ValidationRules rules = new DefaultValidationRules() {
    44  			@Override
    45  			protected void initialize() {
    46  				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    47  			}
    48  		};
    49  		assertEquals(1, rules.getPhaseValidationRules(DATA_TYPE).size());
    50  		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
    51  	}
    52
    53  	@Test
    54  	public void addAndGetRules2() throws Exception {
    55  		ValidationRules rules = new DefaultValidationRules() {
    56  			@Override
    57  			protected void initialize() {
    58  				add(DATA_CONSTRAINT, new ValidationRule() {
    59  					public void apply(Map<String, Object[]> params,
    60  							Object form, ActionErrors errors)
    61  							throws ValidationException {
    62  						if ("2".equals(params.get("foo"))) {
    63  							if (params.get("bar") == null
    64  									|| "".equals(params.get("bar"))) {
    65  								throw new ValidationException("message");
    66  							}
    67  						}
    68  					}
    69  				});
    70  			}
    71  		};
    72  		assertEquals(0, rules.getPhaseValidationRules(DATA_TYPE).size());
    73  		assertEquals(1, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
    74  	}
    75
    76  	@Test
    77  	public void initialize() throws Exception {
    78  		ValidationRules rules = new DefaultValidationRules() {
    79  			public void initialize() {
    80  				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    81  				add("age", new NumberValidator(), new RangeValidator(0, 10));
    82  			}
    83  		};
    84  		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
    85  		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
    86  	}
    87
    88  	@Test
    89  	public void constractor1() throws Exception {
    90  		ValidationRules rules = new DefaultValidationRules() {
    91  			public void initialize() {
    92  				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    93  				add("age", new NumberValidator(), new RangeValidator(0, 10));
    94  			}
    95  		};
    96  		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
    97  		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
    98
    99  		Iterator<ValidationRule> iter = rules
    100 				.getPhaseValidationRules(DATA_TYPE).iterator();
    101 		FieldValidationRule rule = (FieldValidationRule) iter.next();
    102 		assertEquals("name", rule.getFieldName());
    103 		assertEquals("name", rule.getFieldNameKey());
    104 		rule = (FieldValidationRule) iter.next();
    105 		assertEquals("age", rule.getFieldName());
    106 		assertEquals("age", rule.getFieldNameKey());
    107 	}
    108
    109 	@Test
    110 	public void constractor2() throws Exception {
    111 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
    112 			public void initialize() {
    113 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    114 				add("age", new NumberValidator(), new RangeValidator(0, 10));
    115 			}
    116 		};
    117 		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
    118 		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
    119
    120 		Iterator<ValidationRule> iter = rules
    121 				.getPhaseValidationRules(DATA_TYPE).iterator();
    122 		FieldValidationRule rule = (FieldValidationRule) iter.next();
    123 		assertEquals("name", rule.getFieldName());
    124 		assertEquals("userProfile.name", rule.getFieldNameKey());
    125 		rule = (FieldValidationRule) iter.next();
    126 		assertEquals("age", rule.getFieldName());
    127 		assertEquals("userProfile.age", rule.getFieldNameKey());
    128 	}
    129
    130 	@Test
    131 	public void fail() {
    132 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
    133 			public void initialize() {
    134 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    135 				add("age", new NumberValidator(), new RangeValidator(0, 10));
    136 			}
    137 		};
    138 		ActionResult result = rules.fail("error.jsp");
    139 		assertTrue(result instanceof Forward);
    140 		Forward forward = (Forward) result;
    141 		assertEquals("error.jsp", forward.getPath("UTF-8"));
    142 	}
    143
    144 	@Test
    145 	public void failOverride() {
    146 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
    147 			public void initialize() {
    148 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
    149 				add("age", new NumberValidator(), new RangeValidator(0, 10));
    150 			}
    151
    152 			public ActionResult fail(String errorPage) {
    153 				return new Redirect(errorPage);
    154 			}
    155 		};
    156 		ActionResult result = rules.fail("error.jsp");
    157 		assertTrue(result instanceof Redirect);
    158 		Redirect redirect = (Redirect) result;
    159 		assertEquals("error.jsp", redirect.getPath("UTF-8"));
    160 	}
    161
    162 	@Test
    163 	public void validationPhasePriority() {
    164
    165 		ValidationRules validationRules = new DefaultValidationRules() {
    166
    167 			@Override
    168 			protected void initialize() {
    169 			}
    170
    171 		};
    172
    173 		Iterator<ValidationPhase> iterator = validationRules
    174 				.getValidationPhases().iterator();
    175 		ValidationPhase first = iterator.next();
    176 		ValidationPhase second = iterator.next();
    177 		assertFalse(iterator.hasNext());
    178
    179 		assertEquals(DATA_TYPE, first);
    180 		assertEquals(DATA_CONSTRAINT, second);
    181 	}
    182
    183 	@Test
    184 	public void addAll() {
    185 		final ValidationRules base = new DefaultValidationRules() {
    186 			@Override
    187 			protected void initialize() {
    188 				add("param1", new RequiredValidator());
    189 				add("param2", new EqualsValidator("a"));
    190 				add(DATA_CONSTRAINT, new ValidationRule() {
    191
    192 					public void apply(Map<String, Object[]> params,
    193 							Object form, ActionErrors errors)
    194 							throws ValidationException {
    195 					}
    196
    197 				});
    198 			}
    199 		};
    200
    201 		final ValidationRules rules = new DefaultValidationRules() {
    202 			@Override
    203 			protected void initialize() {
    204 				addAll(base);
    205 			}
    206 		};
    207
    208 		Collection<ValidationRule> dataTypeRules = rules
    209 				.getPhaseValidationRules(DATA_TYPE);
    210 		Collection<ValidationRule> dataConstraintRules = rules
    211 				.getPhaseValidationRules(DATA_CONSTRAINT);
    212
    213 		assertEquals(2, dataTypeRules.size());
    214 		assertEquals(1, dataConstraintRules.size());
    215 	}
    216
    217 }