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.lang.reflect.Method;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import org.seasar.cubby.CubbyConstants;
23  import org.seasar.cubby.action.ActionResult;
24  import org.seasar.cubby.action.Forward;
25  import org.seasar.cubby.validator.ValidationException;
26  import org.seasar.extension.unit.S2TestCase;
27  import org.seasar.framework.util.ClassUtil;
28  
29  public class ValidationProcessorImplTest extends S2TestCase {
30  
31  	public ValidationProcessorImpl validationProcessor;
32  
33  	public MockAction action;
34  
35  	public Map<String, Object[]> params;
36  
37  	@Override
38  	protected void setUp() throws Exception {
39  		include(this.getClass().getName().replaceAll("\\.", "/") + ".dicon");
40  		params = new HashMap<String, Object[]>();
41  		getRequest().setAttribute(CubbyConstants.ATTR_PARAMS, params);
42  	}
43  
44  	public void testProcess1() {
45  		Method method = ClassUtil.getMethod(MockAction.class, "dummy",
46  				new Class[0]);
47  		try {
48  			validationProcessor.process(getRequest(), action, MockAction.class,
49  					method);
50  			fail();
51  		} catch (ValidationException e) {
52  			assertFalse(action.getErrors().isEmpty());
53  			assertEquals(1, action.getErrors().getFields().size());
54  			assertNotNull(action.getErrors().getFields().get("name"));
55  		}
56  	}
57  
58  	public void testProcess2() {
59  		params.put("name", new Object[] { "bob" });
60  		params.put("age", new Object[] { "bob" });
61  
62  		Method method = ClassUtil.getMethod(MockAction.class, "dummy",
63  				new Class[0]);
64  		try {
65  			validationProcessor.process(getRequest(), action, MockAction.class,
66  					method);
67  			fail();
68  		} catch (ValidationException e) {
69  			assertFalse(action.getErrors().isEmpty());
70  			assertEquals(1, action.getErrors().getFields().size());
71  			assertNotNull(action.getErrors().getFields().get("age"));
72  		}
73  	}
74  
75  	public void testProcess3() {
76  		params.put("name", new Object[] { "bob" });
77  		params.put("age", new Object[] { "5" });
78  
79  		Method method = ClassUtil.getMethod(MockAction.class, "dummy",
80  				new Class[0]);
81  		try {
82  			validationProcessor.process(getRequest(), action, MockAction.class,
83  					method);
84  		} catch (ValidationException e) {
85  			fail();
86  		}
87  	}
88  
89  	public void testHandleValidationException() {
90  		ValidationException e = new ValidationException("message", "field1");
91  		Method method = ClassUtil.getMethod(MockAction.class, "dummy",
92  				new Class[0]);
93  		ActionResult result = validationProcessor.handleValidationException(e,
94  				getRequest(), action, method);
95  		assertTrue(result instanceof Forward);
96  		Forward forward = (Forward) result;
97  		assertEquals("error.jsp", forward.getPath("UTF-8"));
98  	}
99  
100 }