mirror of
https://github.com/apache/struts.git
synced 2026-08-11 01:27:14 +00:00
WW-4505 Add plugin to support bean validation
Add bean validation test with hibernate validator on an model driven action
This commit is contained in:
@@ -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>
|
||||
+109
@@ -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("jogep@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("jogep@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"));
|
||||
}
|
||||
}
|
||||
+29
@@ -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 {
|
||||
}
|
||||
}
|
||||
+38
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+40
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+67
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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>
|
||||
Reference in New Issue
Block a user