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