BeanValidator { public <T> void validate(T bean) throws ValidationException { validateOnly(bean); } private  BeanValidator(); static BeanValidator getInstance(); void validate(T bean); void validateOnly(T bean, String ... fieldNames); void validateWithout(T bean, String ... fieldNames);  }
@Test public void testSizeMap() { SizeBean bean = new SizeBean(); try { validator.validate(bean); } catch (ValidationException ex) { fail("SizeBean should be valid."); } bean.map.put("r", "b"); bean.map.put("t", "b"); bean.map.put("x", "b"); try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.map.clear(); try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.map = null; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testAssertTrueFalse() { BooleanBeanChild bean = new BooleanBeanChild(); bean.trueValue = true; bean.falseValue = false; try { validator.validate(bean); } catch (ValidationException ex) { fail("BooleanBean should be valid."); } bean.trueValue = false; bean.falseValue = false; try { validator.validate(bean); fail("BooleanBean should be invalid."); } catch (ValidationException ex) { } bean.trueValue = true; bean.falseValue = true; try { validator.validate(bean); fail("BooleanBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testDecimalMinMax() { DecimalBean bean = new DecimalBean(); bean.minValue = 2; bean.maxValue = 5; try { validator.validate(bean); } catch (ValidationException ex) { fail("DecimalBean should be valid."); } bean.minValue = 0; bean.maxValue = 5; try { validator.validate(bean); fail("DecimalBean should be invalid."); } catch (ValidationException ex) { } bean.minValue = 1; bean.maxValue = 11; try { validator.validate(bean); fail("DecimalBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testMinMax() { IntegerBean bean = new IntegerBean(); bean.minValue = 1; bean.maxValue = 9; try { validator.validate(bean); } catch (ValidationException ex) { fail("IntegerBean should be valid."); } bean.minValue = -1; bean.maxValue = 5; try { validator.validate(bean); fail("IntegerBean should be invalid."); } catch (ValidationException ex) { } bean.minValue = 1; bean.maxValue = 11; try { validator.validate(bean); fail("IntegerBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testNullNotNull() { ObjectBean bean = new ObjectBean(); bean.notNullObject = new Object(); bean.nullObject = null; try { validator.validate(bean); } catch (ValidationException ex) { fail("ObjectBean should be valid."); } bean.notNullObject = null; bean.nullObject = null; try { validator.validate(bean); fail("ObjectBean should be invalid."); } catch (ValidationException ex) { } bean.notNullObject = new Object(); bean.nullObject = new Object(); try { validator.validate(bean); fail("ObjectBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testPattern() { EmailBean bean = new EmailBean(); bean.email = "test@web.de"; try { validator.validate(bean); } catch (ValidationException ex) { fail("EmailBean should be valid."); } bean.email = "abcdefg"; try { validator.validate(bean); fail("EmailBean should be invalid."); } catch (ValidationException ex) { } bean.email = ""; try { validator.validate(bean); fail("EmailBean should be invalid."); } catch (ValidationException ex) { } bean.email = null; try { validator.validate(bean); fail("EmailBean should be invalid."); } catch (ValidationException ex) { } bean.email = "hello@world"; try { validator.validate(bean); fail("EmailBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testSizeString() { SizeBean bean = new SizeBean(); try { validator.validate(bean); } catch (ValidationException ex) { fail("SizeBean should be valid."); } bean.text = "h"; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.text = null; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.text = ""; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.text = "hello world"; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testSizeCollection() { SizeBean bean = new SizeBean(); bean.list.add("b"); try { validator.validate(bean); } catch (ValidationException ex) { fail("SizeBean should be valid."); } bean.list.add("b"); try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.list.clear(); try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.list = null; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } }
@Test public void testSizeArray() { SizeBean bean = new SizeBean(); try { validator.validate(bean); } catch (ValidationException ex) { fail("SizeBean should be valid."); } bean.array = new String[]{}; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.array = new String[]{"a"}; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.array = new String[]{"a", "b", "c", "d", "e"}; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } bean.array = null; try { validator.validate(bean); fail("SizeBean should be invalid."); } catch (ValidationException ex) { } }