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 java.util.HashMap;
19  import java.util.Map;
20  
21  import org.seasar.cubby.action.Action;
22  import org.seasar.cubby.action.ActionErrors;
23  import org.seasar.extension.unit.S2TestCase;
24  
25  public class UserValidationRuleTest extends S2TestCase {
26  
27  	public MockAction action;
28  
29  	public ValidationProcessor validationProcessor;
30  
31  	@Override
32  	protected void setUp() throws Exception {
33  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
34  	}
35  
36  	public void testUserValidation() {
37  		ActionErrors errors = action.getErrors();
38  		Object form = action;
39  		ValidationRules rules = action.rules;
40  		Map<String, Object[]> params = new HashMap<String, Object[]>();
41  
42  		validationProcessor.process(errors, params, form, rules);
43  		assertTrue(errors.isEmpty());
44  
45  		action.value2 = "ng";
46  		validationProcessor.process(errors, params, form, rules);
47  		assertFalse(errors.isEmpty());
48  	}
49  
50  	public static class MockAction extends Action {
51  
52  		public String value1;
53  
54  		public String value2;
55  
56  		public ValidationRules rules = new DefaultValidationRules() {
57  			@Override
58  			public void initialize() {
59  				add(new UserValidationRule());
60  			}
61  		};
62  
63  		class UserValidationRule implements ValidationRule {
64  
65  			public void apply(Map<String, Object[]> params, Object form,
66  					ActionErrors errors) {
67  				if ("ng".equals(value1) || "ng".equals(value2)) {
68  					errors.add("validation fail", "value1", "value2");
69  				}
70  			}
71  
72  		}
73  	}
74  }