mirror of
https://github.com/apache/struts.git
synced 2026-08-06 15:17:00 +00:00
Add test cases for ModelDriven action, standard actions and FieldMatch validator
This commit is contained in:
+33
-1
@@ -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<String, List<String>> 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<String, List<String>> 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<String> actionErrors = ((ValidationAware) baseActionProxy.getAction()).getActionErrors();
|
||||
System.out.println(actionErrors);
|
||||
|
||||
assertNotNull(actionErrors);
|
||||
assertEquals(2, actionErrors.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setUp() throws Exception {
|
||||
super.setUp();
|
||||
|
||||
+39
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,14 @@
|
||||
<interceptor-ref name="beanValidation"/>
|
||||
<result type="void"/>
|
||||
</action>
|
||||
<action name="fieldAction" class="org.apache.struts.beanvalidation.actions.FieldAction">
|
||||
<interceptor-ref name="beanValidation"/>
|
||||
<result type="void"/>
|
||||
</action>
|
||||
<action name="fieldMatchAction" class="org.apache.struts.beanvalidation.actions.FieldMatchAction">
|
||||
<interceptor-ref name="beanValidation"/>
|
||||
<result type="void"/>
|
||||
</action>
|
||||
|
||||
</package>
|
||||
</xwork>
|
||||
|
||||
Reference in New Issue
Block a user