1   /*
2    * Copyright 2004-2008 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.seasar.cubby.validator.DefaultValidationRules.*;
19  
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.Map;
23  
24  import org.seasar.cubby.action.ActionErrors;
25  import org.seasar.cubby.action.ActionResult;
26  import org.seasar.cubby.action.Forward;
27  import org.seasar.cubby.action.Redirect;
28  import org.seasar.cubby.validator.validators.EqualsValidator;
29  import org.seasar.cubby.validator.validators.MaxLengthValidator;
30  import org.seasar.cubby.validator.validators.NumberValidator;
31  import org.seasar.cubby.validator.validators.RangeValidator;
32  import org.seasar.cubby.validator.validators.RequiredValidator;
33  import org.seasar.extension.unit.S2TestCase;
34  
35  public class DefaultValidationRulesTest extends S2TestCase {
36  
37  	@Override
38  	protected void setUp() throws Exception {
39  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
40  	}
41  
42  	public void testAddAndGetRules1() 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  	public void testAddAndGetRules2() throws Exception {
54  		ValidationRules rules = new DefaultValidationRules() {
55  			@Override
56  			protected void initialize() {
57  				add(DATA_CONSTRAINT, new ValidationRule() {
58  					public void apply(Map<String, Object[]> params,
59  							Object form, ActionErrors errors)
60  							throws ValidationException {
61  						if ("2".equals(params.get("foo"))) {
62  							if (params.get("bar") == null
63  									|| "".equals(params.get("bar"))) {
64  								throw new ValidationException("message");
65  							}
66  						}
67  					}
68  				});
69  			}
70  		};
71  		assertEquals(0, rules.getPhaseValidationRules(DATA_TYPE).size());
72  		assertEquals(1, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
73  	}
74  
75  	public void testInitialize() throws Exception {
76  		ValidationRules rules = new DefaultValidationRules() {
77  			public void initialize() {
78  				add("name", new RequiredValidator(), new MaxLengthValidator(10));
79  				add("age", new NumberValidator(), new RangeValidator(0, 10));
80  			}
81  		};
82  		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
83  		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
84  	}
85  
86  	public void testConstractor1() throws Exception {
87  		ValidationRules rules = new DefaultValidationRules() {
88  			public void initialize() {
89  				add("name", new RequiredValidator(), new MaxLengthValidator(10));
90  				add("age", new NumberValidator(), new RangeValidator(0, 10));
91  			}
92  		};
93  		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
94  		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
95  
96  		Iterator<ValidationRule> iter = rules
97  				.getPhaseValidationRules(DATA_TYPE).iterator();
98  		FieldValidationRule rule = (FieldValidationRule) iter.next();
99  		assertEquals("name", rule.getFieldName());
100 		assertEquals("name", rule.getFieldNameKey());
101 		rule = (FieldValidationRule) iter.next();
102 		assertEquals("age", rule.getFieldName());
103 		assertEquals("age", rule.getFieldNameKey());
104 	}
105 
106 	public void testConstractor2() throws Exception {
107 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
108 			public void initialize() {
109 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
110 				add("age", new NumberValidator(), new RangeValidator(0, 10));
111 			}
112 		};
113 		assertEquals(2, rules.getPhaseValidationRules(DATA_TYPE).size());
114 		assertEquals(0, rules.getPhaseValidationRules(DATA_CONSTRAINT).size());
115 
116 		Iterator<ValidationRule> iter = rules
117 				.getPhaseValidationRules(DATA_TYPE).iterator();
118 		FieldValidationRule rule = (FieldValidationRule) iter.next();
119 		assertEquals("name", rule.getFieldName());
120 		assertEquals("userProfile.name", rule.getFieldNameKey());
121 		rule = (FieldValidationRule) iter.next();
122 		assertEquals("age", rule.getFieldName());
123 		assertEquals("userProfile.age", rule.getFieldNameKey());
124 	}
125 
126 	public void testFail() {
127 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
128 			public void initialize() {
129 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
130 				add("age", new NumberValidator(), new RangeValidator(0, 10));
131 			}
132 		};
133 		ActionResult result = rules.fail("error.jsp");
134 		assertTrue(result instanceof Forward);
135 		Forward forward = (Forward) result;
136 		assertEquals("error.jsp", forward.getPath("UTF-8"));
137 	}
138 
139 	public void testFailOverride() {
140 		ValidationRules rules = new DefaultValidationRules("userProfile.") {
141 			public void initialize() {
142 				add("name", new RequiredValidator(), new MaxLengthValidator(10));
143 				add("age", new NumberValidator(), new RangeValidator(0, 10));
144 			}
145 
146 			public ActionResult fail(String errorPage) {
147 				return new Redirect(errorPage);
148 			}
149 		};
150 		ActionResult result = rules.fail("error.jsp");
151 		assertTrue(result instanceof Redirect);
152 		Redirect redirect = (Redirect) result;
153 		assertEquals("error.jsp", redirect.getPath("UTF-8"));
154 	}
155 
156 	public void testValidationPhasePriority() {
157 
158 		ValidationRules validationRules = new DefaultValidationRules() {
159 
160 			@Override
161 			protected void initialize() {
162 			}
163 
164 		};
165 
166 		Iterator<ValidationPhase> iterator = validationRules
167 				.getValidationPhases().iterator();
168 		ValidationPhase first = iterator.next();
169 		ValidationPhase second = iterator.next();
170 		assertFalse(iterator.hasNext());
171 
172 		assertEquals(DATA_TYPE, first);
173 		assertEquals(DATA_CONSTRAINT, second);
174 	}
175 
176 	public void testAddAll() {
177 		final ValidationRules base = new DefaultValidationRules() {
178 			@Override
179 			protected void initialize() {
180 				add("param1", new RequiredValidator());
181 				add("param2", new EqualsValidator("a"));
182 				add(DATA_CONSTRAINT, new ValidationRule() {
183 
184 					public void apply(Map<String, Object[]> params,
185 							Object form, ActionErrors errors)
186 							throws ValidationException {
187 					}
188 
189 				});
190 			}
191 		};
192 
193 		final ValidationRules rules = new DefaultValidationRules() {
194 			@Override
195 			protected void initialize() {
196 				addAll(base);
197 			}
198 		};
199 
200 		Collection<ValidationRule> dataTypeRules = rules
201 				.getPhaseValidationRules(DATA_TYPE);
202 		Collection<ValidationRule> dataConstraintRules = rules
203 				.getPhaseValidationRules(DATA_CONSTRAINT);
204 
205 		assertEquals(2, dataTypeRules.size());
206 		assertEquals(1, dataConstraintRules.size());
207 	}
208 
209 }