diff --git a/plugins/bean-validation/pom.xml b/plugins/bean-validation/pom.xml index a7a02f028..0a0679446 100644 --- a/plugins/bean-validation/pom.xml +++ b/plugins/bean-validation/pom.xml @@ -52,6 +52,19 @@ 1.9.2 + + org.hibernate + hibernate-validator + 5.1.3.Final + test + + + org.glassfish + javax.el + 3.0.0 + test + + \ No newline at end of file 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 000000000..0c40c48bb --- /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> 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> 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("jogep@apache.org"); + action.getModel().getAddress().setStreet("st"); + baseActionProxy.execute(); + + Map> 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("jogep@apache.org"); + action.getModel().getAddress().setStreet("street"); + baseActionProxy.execute(); + + Map> 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")); + } +} 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 000000000..3a5e95586 --- /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 { + } +} 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 000000000..a51c6b5f1 --- /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 { + + @Valid + private Person model = new Person(); + + public Person getModel() { + return model; + } + +} 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 000000000..a94f2e31e --- /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; + } + +} 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 000000000..5e7a88e9e --- /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; + } + +} 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 000000000..fece87380 --- /dev/null +++ b/plugins/bean-validation/src/test/resources/bean-validation-test.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + +