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.cubby.action.impl.ActionErrorsImpl;
24  import org.seasar.cubby.validator.validators.ArrayMaxSizeValidator;
25  import org.seasar.cubby.validator.validators.RequiredValidator;
26  import org.seasar.extension.unit.S2TestCase;
27  
28  public class FieldValidationRuleTest extends S2TestCase {
29  
30  	public ActionErrors errors = new ActionErrorsImpl();
31  
32  	@Override
33  	protected void setUp() throws Exception {
34  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
35  	}
36  
37  	public void testApply1() throws Exception {
38  		Map<String, Object[]> params = new HashMap<String, Object[]>();
39  		params.put("name", new Object[] { "aa" });
40  
41  		ValidationRule rule = new FieldValidationRule("name",
42  				new RequiredValidator(), new ArrayMaxSizeValidator(1));
43  		rule.apply(params, null, errors);
44  		assertTrue(errors.isEmpty());
45  	}
46  
47  	public void testApply2() throws Exception {
48  		Map<String, Object[]> params = new HashMap<String, Object[]>();
49  		params.put("name", new Object[] { "aa", "bb" });
50  
51  		ValidationRule rule = new FieldValidationRule("name",
52  				new RequiredValidator(), new ArrayMaxSizeValidator(1));
53  		rule.apply(params, null, errors);
54  		assertFalse(errors.isEmpty());
55  		assertFalse(errors.getFields().get("name").isEmpty());
56  		assertTrue(errors.getIndexedFields().get("name").get(0).isEmpty());
57  	}
58  
59  	public void testApply3() throws Exception {
60  		Map<String, Object[]> params = new HashMap<String, Object[]>();
61  
62  		ValidationRule rule = new FieldValidationRule("name",
63  				new RequiredValidator(), new ArrayMaxSizeValidator(1));
64  		rule.apply(params, null, errors);
65  		assertFalse(errors.isEmpty());
66  		assertFalse(errors.getFields().get("name").isEmpty());
67  		assertEquals(1, errors.getIndexedFields().get("name").get(0).size());
68  	}
69  
70  	public static class MockAction extends Action {
71  		public String name;
72  	}
73  
74  }