ovalvalidatortest.java.svn-base

来自「google的开源项目」· SVN-BASE 代码 · 共 94 行

SVN-BASE
94
字号
package com.google.code.rsser.web.validator;import java.util.HashMap;import junit.framework.Assert;import org.junit.Before;import org.junit.Test;import org.springframework.validation.Errors;import org.springframework.validation.MapBindingResult;import org.springframework.validation.ValidationUtils;import net.sf.oval.Validator;import net.sf.oval.constraint.Length;public class OvalValidatorTest {	class MyClass {		@Length(min = 5, max = 10, profiles = { "one" })		protected int a;				@Length(min = 5, max = 10, profiles = { "two" })		protected int b;		public int getA() {			return a;		}		public void setA(int a) {			this.a = a;		}		public int getB() {			return b;		}		public void setB(int b) {			this.b = b;		}	}		private OvalValidator ovalValidator;	private Validator validator;		@Before	public void setUp() throws Exception {		ovalValidator = new OvalValidator();		validator = new Validator();				ovalValidator.setValidator(validator);		ovalValidator.afterPropertiesSet();	}	@Test	public void testA() throws Exception {		MyClass obj = new MyClass();				obj.setA(11);		obj.setB(11);				Assert.assertEquals(1, getValidationErrorCount(obj, "one"));	}	@Test	public void testB() throws Exception {		MyClass obj = new MyClass();				obj.setA(11);		obj.setB(11);				Assert.assertEquals(1, getValidationErrorCount(obj, "two"));	}		@Test	public void testBoth() throws Exception {		MyClass obj = new MyClass();				obj.setA(11);		obj.setB(11);				Assert.assertEquals(2, getValidationErrorCount(obj, null));	}		@SuppressWarnings("unchecked")	protected int getValidationErrorCount(Object obj, String profile) {		ovalValidator.setProfile(profile);				Errors errors = new MapBindingResult(new HashMap(), "test");				ValidationUtils.invokeValidator(ovalValidator, obj, errors);				return errors.getErrorCount();	}}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?