From dbde75221e7b0445bf9006559cc9b128f810311c Mon Sep 17 00:00:00 2001 From: Johannes Geppert Date: Wed, 27 May 2015 20:07:08 +0200 Subject: [PATCH] Add test cases for ModelDriven action, standard actions and FieldMatch validator --- .../BeanValidationInterceptorTest.java | 34 +++++++- .../beanvalidation/actions/FieldAction.java | 39 +++++++++ .../actions/FieldMatchAction.java | 80 +++++++++++++++++++ .../test/resources/bean-validation-test.xml | 8 ++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldAction.java create mode 100644 plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldMatchAction.java diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.java index 0c40c48bb..11aa13575 100644 --- a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.java +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.java @@ -24,8 +24,11 @@ import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.ValidationAware; import com.opensymphony.xwork2.XWorkTestCase; import com.opensymphony.xwork2.config.providers.XmlConfigurationProvider; +import org.apache.struts.beanvalidation.actions.FieldAction; +import org.apache.struts.beanvalidation.actions.FieldMatchAction; import org.apache.struts.beanvalidation.actions.ModelDrivenAction; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -96,11 +99,40 @@ public class BeanValidationInterceptorTest extends XWorkTestCase { baseActionProxy.execute(); Map> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); - System.out.println(fieldErrors); + assertNotNull(fieldErrors); assertEquals(0, fieldErrors.size()); } + public void testFieldAction() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "fieldAction", null, null); + FieldAction action = (FieldAction) baseActionProxy.getAction(); + action.setTest(" "); + baseActionProxy.execute(); + + Map> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + + assertNotNull(fieldErrors); + assertEquals(1, fieldErrors.size()); + assertTrue(fieldErrors.get("test").size() > 0); + } + + public void testFieldMatchAction() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "fieldMatchAction", null, null); + FieldMatchAction action = (FieldMatchAction) baseActionProxy.getAction(); + action.setPassword("pass1"); + action.setConfirmPassword("pass2"); + action.setEmail("test1@mail.org"); + action.setConfirmEmail("test2@mail.org"); + baseActionProxy.execute(); + + Collection actionErrors = ((ValidationAware) baseActionProxy.getAction()).getActionErrors(); + System.out.println(actionErrors); + + assertNotNull(actionErrors); + assertEquals(2, actionErrors.size()); + } + @Override protected void setUp() throws Exception { super.setUp(); diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldAction.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldAction.java new file mode 100644 index 000000000..9d95be2b1 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldAction.java @@ -0,0 +1,39 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.beanvalidation.actions; + +import com.opensymphony.xwork2.ActionSupport; +import org.hibernate.validator.constraints.NotBlank; + +public class FieldAction extends ActionSupport { + + @NotBlank(message = "canNotBeBlank") + private String test; + + public String getTest() { + return test; + } + + public void setTest(String test) { + this.test = test; + } + +} diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldMatchAction.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldMatchAction.java new file mode 100644 index 000000000..a4d452082 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/FieldMatchAction.java @@ -0,0 +1,80 @@ +/* + * $Id$ + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts.beanvalidation.actions; + +import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts.beanvalidation.constraints.FieldMatch; +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotBlank; + +@FieldMatch.List({ + @FieldMatch(first = "password", second = "confirmPassword", message = "The password fields must match"), + @FieldMatch(first = "email", second = "confirmEmail", message = "The email fields must match") +}) +public class FieldMatchAction extends ActionSupport { + + + @NotBlank + private String password; + + @NotBlank + private String confirmPassword; + + @NotBlank + @Email + private String email; + + @NotBlank + @Email + private String confirmEmail; + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getConfirmPassword() { + return confirmPassword; + } + + public void setConfirmPassword(String confirmPassword) { + this.confirmPassword = confirmPassword; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getConfirmEmail() { + return confirmEmail; + } + + public void setConfirmEmail(String confirmEmail) { + this.confirmEmail = confirmEmail; + } +} diff --git a/plugins/bean-validation/src/test/resources/bean-validation-test.xml b/plugins/bean-validation/src/test/resources/bean-validation-test.xml index fece87380..3e616aa82 100644 --- a/plugins/bean-validation/src/test/resources/bean-validation-test.xml +++ b/plugins/bean-validation/src/test/resources/bean-validation-test.xml @@ -26,6 +26,14 @@ + + + + + + + +