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.impl;
17  
18  import java.util.HashMap;
19  import java.util.Map;
20  
21  import org.seasar.cubby.action.ActionErrors;
22  import org.seasar.cubby.action.impl.ActionErrorsImpl;
23  import org.seasar.cubby.validator.DefaultValidationRules;
24  import org.seasar.cubby.validator.FieldValidationRule;
25  import org.seasar.cubby.validator.ValidationRule;
26  import org.seasar.cubby.validator.validators.RequiredValidator;
27  import org.seasar.extension.unit.S2TestCase;
28  
29  public class ValidationProcessorImplTest extends S2TestCase {
30  
31  	public ValidationProcessorImpl processor;
32  	Map<String, Object[]> params;
33  	ActionErrors errors = new ActionErrorsImpl();
34  	DefaultValidationRules validators = new DefaultValidationRules();
35  
36  	@Override
37  	protected void setUp() throws Exception {
38  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
39  
40  		params = new HashMap<String, Object[]>();
41  		validators = new DefaultValidationRules();
42  		validators.add("prop1", new RequiredValidator());
43  		validators.add("prop2", new RequiredValidator());
44  	}
45  
46  	public void testProcessValidation() {
47  		params.put("prop2", new Object[] { "prop2 value" });
48  		boolean success = processor.process(errors, params,
49  				new Sample1Form(), validators);
50  		assertFalse(success);
51  	}
52  
53  	public void testValidateAction() {
54  		params.put("prop2", new Object[] { "prop2 value" });
55  		for (ValidationRule rule : validators.getRules()) {
56  			rule.apply(params, new Sample1Form(), errors);
57  		}
58  		assertEquals(1, errors.getFields().get("prop1").size());
59  		assertEquals("prop1は必須です。", errors.getFields().get("prop1").get(0));
60  		assertEquals(0, errors.getFields().get("prop2").size());
61  	}
62  
63  	public void testValidate() {
64  		params.put("prop2", new Object[] { "prop2 value" });
65  		Object form = new Object();
66  		FieldValidationRule[] rules = {
67  				new FieldValidationRule("prop1", new RequiredValidator()),
68  				new FieldValidationRule("prop2", new RequiredValidator()), };
69  		for (final ValidationRule rule : rules) {
70  			rule.apply(params, form, errors);
71  		}
72  		assertEquals(1, errors.getFields().get("prop1").size());
73  		assertEquals("prop1は必須です。", errors.getFields().get("prop1").get(0));
74  		assertEquals(0, errors.getFields().get("prop2").size());
75  	}
76  
77  	public static class Foo {
78  		public String value1 = "1";
79  		private String value2 = "2";
80  
81  		public String getValue2() {
82  			return value2;
83  		}
84  	}
85  
86  //	public static class SampleAction extends Action {
87  //
88  //		@Validation(rules = "validation", errorPage = "error.jsp")
89  //		public String test() {
90  //			return null;
91  //		}
92  //	}
93  
94  	static class Sample1Form {
95  		private String prop1;
96  		private Integer prop2;
97  
98  		public String getProp1() {
99  			return prop1;
100 		}
101 
102 		public void setProp1(String prop1) {
103 			this.prop1 = prop1;
104 		}
105 
106 		public Integer getProp2() {
107 			return prop2;
108 		}
109 
110 		public void setProp2(Integer prop2) {
111 			this.prop2 = prop2;
112 		}
113 
114 	}
115 
116 	static class Sample2Form {
117 	}
118 
119 }