public static void assertTrueOrShowErrors(HumanBeing hb, ClassValidator<HumanBeing> cv, boolean shouldFail) { Exception exc = null; try { cv.assertValid(hb); } catch (InvalidStateException ise) { exc = ise; } assertTrue(showErrMsg(hb, cv),(exc == null) == shouldFail); }
private void validateInvalidPerson() { ClassValidator validator = new ClassValidator(ValidPerson.class); ValidPerson invalidPerson = new ValidPerson(); invalidPerson.setAge(1981); validator.assertValid(invalidPerson); }
@Test public void test13755() { final String errorMessage = "A term with the same value and source or accession value and source already exists"; final ClassValidator<Term> cv = new ClassValidator<Term>(Term.class); Transaction tx = null; try { // Save term tx = this.hibernateHelper.beginTransaction(); final Term term1 = new Term(); term1.setValue("13755-value-1"); final TermSource termSource1 = new TermSource(); termSource1.setName("termSource-13755-1"); term1.setSource(termSource1); term1.setAccession("13755-accession-1"); this.daoObject.save(term1); // Test validation of unique key constraint // for (Term.value, Term.source) of new Term final Term term2 = new Term(); term2.setValue("13755-value-1"); term2.setSource(termSource1); assertTrue(cv.getInvalidValues(term2)[0].getMessage().contains(errorMessage)); term2.setValue("13755-value-2"); assertEquals(0, cv.getInvalidValues(term2).length); // Test validation of unique key constraint // for (Term.accession, Term.source) of new Term final Term term3 = new Term(); term3.setAccession("13755-accession-1"); term3.setSource(termSource1); term3.setValue("13755-value-2"); assertTrue(cv.getInvalidValues(term3)[0].getMessage().contains(errorMessage)); // Test successful validation of new term term3.setAccession("13755-accession-2"); assertEquals(0, cv.getInvalidValues(term3).length); this.daoObject.save(term2); tx.commit(); // Test validation of unique key constraint // for (Term.value, Term.source) // of existing term tx = this.hibernateHelper.beginTransaction(); term2.setValue("13755-value-1"); term2.setSource(termSource1); term2.setAccession("13755-accession-100"); final MockTermHibernateProxy proxy = new MockTermHibernateProxy(term2); assertTrue(cv.getInvalidValues(proxy)[0].getMessage().contains(errorMessage)); // Test validation of unique key constraint // for (Term.accession, Term.source) // of existing term term2.setValue("13755-value-100"); term2.setSource(termSource1); term2.setAccession("13755-accession-1"); proxy.copyValues(term2); assertTrue(cv.getInvalidValues(proxy)[0].getMessage().contains(errorMessage)); // Test successful validation of existing term term2.setValue("13755-value-404"); term2.setSource(termSource1); term2.setAccession("13755-accession-404"); proxy.copyValues(term2); assertEquals(0, cv.getInvalidValues(proxy).length); tx.commit(); } catch (final Exception e) { e.printStackTrace(); } }
private static String showErrMsg(HumanBeing hb, ClassValidator<HumanBeing> cv) { return Arrays.toString(cv.getInvalidValues(hb)); }
@Test public void alpha_allow_only_alpha_by_default(){ HumanBeing hb = new HumanBeing("OnlyAlphaHereĄęłżźćłóś"); ClassValidator<HumanBeing> cv = new ClassValidator<HumanBeing>(HumanBeing.class); ValidatorTestHelper.assertTrueOrShowErrors(hb, cv, true); }
@Test public void alpha_may_contain_digits_if_specified(){ HumanBeing hb = new HumanBeing("A").setPhone("ButęHąreMałBaźżAPhoneNumberLike700800900"); ClassValidator<HumanBeing> cv = new ClassValidator<HumanBeing>(HumanBeing.class); ValidatorTestHelper.assertTrueOrShowErrors(hb, cv, true); }
@Test public void alpha_may_contain_white_spaces_if_specified(){ HumanBeing hb = new HumanBeing("AĆ").setPhone("BĄ9").setPhoneWithSpaces("Phone 48 700 800 900"); ClassValidator<HumanBeing> cv = new ClassValidator<HumanBeing>(HumanBeing.class); ValidatorTestHelper.assertTrueOrShowErrors(hb, cv, true); }
@Test public void alpha_may_contain_white_spaces_without_digits_if_specified(){ HumanBeing hb = new HumanBeing("AĆ").setPhone("B9").setPhoneWithSpaces("9 A").setNames("Sir Wright Blę Blą"); ClassValidator<HumanBeing> cv = new ClassValidator<HumanBeing>(HumanBeing.class); ValidatorTestHelper.assertTrueOrShowErrors(hb, cv, true); }
@Test public void alpha_may_be_null_by_default(){ HumanBeing hb = new HumanBeing("AĆ").setOptionalGender(null); ClassValidator<HumanBeing> cv = new ClassValidator<HumanBeing>(HumanBeing.class); ValidatorTestHelper.assertTrueOrShowErrors(hb, cv, true); }
/** * Creates a new HibernateRulesValidator with additionally a set of * properties that should not be validated. * * @param formModel The {@link ValidatingFormModel} on which validation * needs to occur * @param beanClass The class of the object this validator needs to check * @param ignoredHibernateProperties properties that should not be checked * though are */ public HibernateRulesValidator(ValidatingFormModel formModel, Class beanClass, Set<String> ignoredHibernateProperties) { this.formModel = formModel; this.beanClass = beanClass; this.hibernateValidator = new ClassValidator(beanClass, new HibernateRulesMessageInterpolator()); this.ignoredHibernateProperties = ignoredHibernateProperties; }