1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.validator;
18  
19  import static org.easymock.EasyMock.createMock;
20  import static org.easymock.EasyMock.expect;
21  import static org.easymock.EasyMock.replay;
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.fail;
24  import static org.seasar.cubby.CubbyConstants.ATTR_PARAMS;
25  
26  import java.lang.reflect.Method;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  import org.junit.After;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.seasar.cubby.action.Action;
36  import org.seasar.cubby.action.ActionErrors;
37  import org.seasar.cubby.action.ActionResult;
38  import org.seasar.cubby.action.Validation;
39  import org.seasar.cubby.mock.MockActionErrors;
40  import org.seasar.cubby.plugin.PluginRegistry;
41  import org.seasar.cubby.plugins.BinderPlugin;
42  import org.seasar.cubby.spi.BeanDescProvider;
43  import org.seasar.cubby.spi.beans.impl.DefaultBeanDescProvider;
44  
45  public class UserValidationRuleTest {
46  
47  	private final PluginRegistry pluginRegistry = PluginRegistry.getInstance();
48  
49  	// private ValidationProcessorImpl validationProcessor = new
50  	// ValidationProcessorImpl();
51  
52  	private MockAction action = new MockAction();
53  
54  	@Before
55  	public void setup() {
56  		BinderPlugin binderPlugin = new BinderPlugin();
57  		binderPlugin.bind(BeanDescProvider.class).toInstance(
58  				new DefaultBeanDescProvider());
59  		pluginRegistry.register(binderPlugin);
60  	}
61  
62  	@After
63  	public void teardown() {
64  		pluginRegistry.clear();
65  	}
66  
67  	@Test
68  	public void userValidation() throws Exception {
69  		Method method = MockAction.class.getMethod("dummy");
70  
71  		Map<String, Object[]> params = new HashMap<String, Object[]>();
72  		HttpServletRequest request = createMock(HttpServletRequest.class);
73  		expect(request.getAttribute(ATTR_PARAMS)).andReturn(params).anyTimes();
74  		replay(request);
75  
76  		ActionErrors actionErrors = new MockActionErrors();
77  
78  		Validation validation = ValidationUtils.getValidation(method);
79  		ValidationRules rules = ValidationUtils.getValidationRules(action,
80  				validation.rules());
81  
82  		action.value2 = "ng";
83  		try {
84  			rules.validate(params, action, actionErrors);
85  			fail();
86  		} catch (ValidationException e) {
87  			assertEquals(1, actionErrors.getAll().size());
88  			assertEquals("validation failed", actionErrors.getAll().get(0));
89  		}
90  	}
91  
92  	public static class MockAction extends Action {
93  
94  		private String value1;
95  
96  		private String value2;
97  
98  		private ValidationRules rules = new DefaultValidationRules() {
99  			@Override
100 			public void initialize() {
101 				add(DATA_CONSTRAINT, new UserValidationRule());
102 			}
103 		};
104 
105 		public String getValue1() {
106 			return value1;
107 		}
108 
109 		public String getValue2() {
110 			return value2;
111 		}
112 
113 		public ValidationRules getRules() {
114 			return rules;
115 		}
116 
117 		class UserValidationRule implements ValidationRule {
118 
119 			public void apply(Map<String, Object[]> params, Object form,
120 					ActionErrors errors) {
121 				if ("ng".equals(value1) || "ng".equals(value2)) {
122 					errors.add("validation failed", "value1", "value2");
123 				}
124 			}
125 
126 		}
127 
128 		@Validation(rules = "rules")
129 		public ActionResult dummy() {
130 			return null;
131 		}
132 
133 	}
134 }