WW-4505 Add plugin to support bean validation Add bean validation test with hibernate validator on an model driven action
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/7fe36288 Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/7fe36288 Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/7fe36288 Branch: refs/heads/master Commit: 7fe362884bbfe877445576654e7df579657e0163 Parents: 95da4e3 Author: Johannes Geppert <joh...@gmail.com> Authored: Tue May 26 21:33:49 2015 +0200 Committer: Johannes Geppert <joh...@gmail.com> Committed: Tue May 26 21:33:49 2015 +0200 ---------------------------------------------------------------------- plugins/bean-validation/pom.xml | 13 +++ .../BeanValidationInterceptorTest.java | 109 +++++++++++++++++++ .../struts/beanvalidation/VoidResult.java | 29 +++++ .../actions/ModelDrivenAction.java | 38 +++++++ .../struts/beanvalidation/models/Address.java | 40 +++++++ .../struts/beanvalidation/models/Person.java | 67 ++++++++++++ .../src/test/resources/bean-validation-test.xml | 31 ++++++ 7 files changed, 327 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/pom.xml ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/pom.xml b/plugins/bean-validation/pom.xml index a7a02f0..0a06794 100644 --- a/plugins/bean-validation/pom.xml +++ b/plugins/bean-validation/pom.xml @@ -52,6 +52,19 @@ <version>1.9.2</version> </dependency> + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-validator</artifactId> + <version>5.1.3.Final</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.glassfish</groupId> + <artifactId>javax.el</artifactId> + <version>3.0.0</version> + <scope>test</scope> + </dependency> + </dependencies> </project> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.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 new file mode 100644 index 0000000..0c40c48 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/BeanValidationInterceptorTest.java @@ -0,0 +1,109 @@ +/* + * $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; + +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.ModelDrivenAction; + +import java.util.List; +import java.util.Map; + +public class BeanValidationInterceptorTest extends XWorkTestCase { + + + public void testModelDrivenAction() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null); + ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction(); + action.getModel().setName(null); + action.getModel().setEmail(null); + action.getModel().getAddress().setStreet(null); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + + assertNotNull(fieldErrors); + assertEquals(3, fieldErrors.size()); + assertTrue(fieldErrors.get("name").size() > 0); + assertEquals(fieldErrors.get("name").get(0), "nameNotNull"); + assertTrue(fieldErrors.get("email").size() > 0); + assertEquals(fieldErrors.get("email").get(0), "emailNotNull"); + assertTrue(fieldErrors.get("address.street").size() > 0); + assertEquals(fieldErrors.get("address.street").get(0), "streetNotNull"); + } + + + public void testModelDrivenActionEmailField() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null); + ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction(); + action.getModel().setName("name"); + action.getModel().setEmail("notamail"); + action.getModel().getAddress().setStreet("street"); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + + assertNotNull(fieldErrors); + assertEquals(1, fieldErrors.size()); + assertTrue(fieldErrors.get("email").size() > 0); + assertEquals(fieldErrors.get("email").get(0), "emailNotValid"); + } + + public void testModelDrivenActionSize() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null); + ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction(); + action.getModel().setName("j"); + action.getModel().setEmail("jo...@apache.org"); + action.getModel().getAddress().setStreet("st"); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + System.out.println(fieldErrors); + assertNotNull(fieldErrors); + assertEquals(2, fieldErrors.size()); + assertTrue(fieldErrors.get("name").size() > 0); + assertEquals(fieldErrors.get("name").get(0), "nameSize"); + assertTrue(fieldErrors.get("address.street").size() > 0); + assertEquals(fieldErrors.get("address.street").get(0), "streetSize"); + } + + public void testModelDrivenActionSuccess() throws Exception { + ActionProxy baseActionProxy = actionProxyFactory.createActionProxy("bean-validation", "modelDrivenAction", null, null); + ModelDrivenAction action = (ModelDrivenAction) baseActionProxy.getAction(); + action.getModel().setName("name"); + action.getModel().setEmail("jo...@apache.org"); + action.getModel().getAddress().setStreet("street"); + baseActionProxy.execute(); + + Map<String, List<String>> fieldErrors = ((ValidationAware) baseActionProxy.getAction()).getFieldErrors(); + System.out.println(fieldErrors); + assertNotNull(fieldErrors); + assertEquals(0, fieldErrors.size()); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + loadConfigurationProviders(new XmlConfigurationProvider("bean-validation-test.xml")); + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/VoidResult.java ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/VoidResult.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/VoidResult.java new file mode 100644 index 0000000..3a5e955 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/VoidResult.java @@ -0,0 +1,29 @@ +/* + * $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; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.Result; + +public class VoidResult implements Result { + public void execute(ActionInvocation invocation) throws Exception { + } +} http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/ModelDrivenAction.java ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/ModelDrivenAction.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/ModelDrivenAction.java new file mode 100644 index 0000000..a51c6b5 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/actions/ModelDrivenAction.java @@ -0,0 +1,38 @@ +/* + * $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 com.opensymphony.xwork2.ModelDriven; +import org.apache.struts.beanvalidation.models.Person; + +import javax.validation.Valid; + +public class ModelDrivenAction extends ActionSupport implements ModelDriven<Person> { + + @Valid + private Person model = new Person(); + + public Person getModel() { + return model; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Address.java ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Address.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Address.java new file mode 100644 index 0000000..a94f2e3 --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Address.java @@ -0,0 +1,40 @@ +/* + * $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.models; + +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +public class Address { + + @NotNull(message = "streetNotNull") + @Size(min = 3, max = 64, message = "streetSize") + private String street; + + public void setStreet(String street) { + this.street = street; + } + + public String getStreet() { + return street; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Person.java ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Person.java b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Person.java new file mode 100644 index 0000000..5e7a88e --- /dev/null +++ b/plugins/bean-validation/src/test/java/org/apache/struts/beanvalidation/models/Person.java @@ -0,0 +1,67 @@ +package org.apache.struts.beanvalidation.models; + +import org.hibernate.validator.constraints.Email; + +import javax.validation.Valid; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +/* + * $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. + */ + +public class Person { + + @NotNull(message = "nameNotNull") + @Size(min = 2, max = 64, message = "nameSize") + private String name; + + @NotNull(message = "emailNotNull") + @Email(message = "emailNotValid") + private String email; + + @Valid + private Address address = new Address(); + + public void setEmail(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setAddress(Address address) { + this.address = address; + } + + public Address getAddress() { + return address; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/7fe36288/plugins/bean-validation/src/test/resources/bean-validation-test.xml ---------------------------------------------------------------------- diff --git a/plugins/bean-validation/src/test/resources/bean-validation-test.xml b/plugins/bean-validation/src/test/resources/bean-validation-test.xml new file mode 100644 index 0000000..fece873 --- /dev/null +++ b/plugins/bean-validation/src/test/resources/bean-validation-test.xml @@ -0,0 +1,31 @@ +<!DOCTYPE xwork PUBLIC + "-//Apache Struts//XWork 2.0//EN" + "http://struts.apache.org/dtds/xwork-2.0.dtd" + > + +<xwork> + <constant name="struts.beanValidation.providerClass" value="org.hibernate.validator.HibernateValidator"/> + <constant name="struts.beanValidation.ignoreXMLConfiguration" value="false"/> + <constant name="struts.beanValidation.convertMessageToUtf" value="false"/> + <constant name="struts.beanValidation.convertMessageFromEncoding" value="ISO-8859-1"/> + + <bean type="org.apache.struts.beanvalidation.validation.interceptor.BeanValidationManager" + class="org.apache.struts.beanvalidation.validation.interceptor.DefaultBeanValidationManager" + scope="singleton"/> + + <package namespace="bean-validation" name="bean-validation-test" extends=""> + <result-types> + <result-type name="void" class="org.apache.struts.beanvalidation.VoidResult"/> + </result-types> + <interceptors> + <interceptor name="beanValidation" + class="org.apache.struts.beanvalidation.validation.interceptor.BeanValidationInterceptor"/> + </interceptors> + + <action name="modelDrivenAction" class="org.apache.struts.beanvalidation.actions.ModelDrivenAction"> + <interceptor-ref name="beanValidation"/> + <result type="void"/> + </action> + + </package> +</xwork>