@Test public void testBeanValidationSwitchedOffByAnnotation() { PmWithoutBeanValidation pm = new PmWithoutBeanValidation(pmConversation); pm.beanPm.setPmBean(new MyBean("hi", 2, 1)); PmValidationApi.validateSubTree(pm); Assert.assertEquals(0, pmConversation.getPmMessages().size()); }
@Test public void testBeanValidationBasedFieldValidationNotSwitchedOffByAnnotation() { PmWithoutBeanValidation pm = new PmWithoutBeanValidation(pmConversation); pm.beanPm.setPmBean(new MyBean("abcdefgh", null, null)); PmValidationApi.validateSubTree(pm); Assert.assertEquals(3, pmConversation.getPmMessages().size()); }
/** * Validates the PM tree having the given root instance. Fails if the subtree has validated * successfully or doesn't contain exactly the expected messages. * * @param rootPm The root of the PM tree to validate. * @param minSeverity The minimum severity to consider. * @param expectedMessagesInSubtree The expected messages in rootPm or its PM subtree. */ public static void validateNotSuccessful( PmObject rootPm, Severity minSeverity, String... expectedMessagesInSubtree) { if (PmValidationApi.validateSubTree(rootPm)) { Assert.fail("Unexpected successful validation of " + rootPm.getPmRelativeName()); } assertMessageText(rootPm, minSeverity, expectedMessagesInSubtree); }
@Test public void testAttributeValidationForEmptyBean() { myBeanPm.setPmBean(new MyBean(null, null, null)); Assert.assertEquals( "The @Size.max constraint is read from the bean restriction.", 4, myBeanPm.s.getMaxLen()); PmValidationApi.validate(myBeanPm); assertTrue(myBeanPm.s.isPmValid()); assertFalse(myBeanPm.i.isPmValid()); assertFalse(myBeanPm.j.isPmValid()); }
/** * Validates the PM tree having the given root instance. Fails if the subtree has any messages * after the validation call. * * @param rootPm The root of the PM tree to validate. */ public static void validateSuccessful(PmObject rootPm) { if (!PmValidationApi.validateSubTree(rootPm)) { List<String> msgStrings = new ArrayList<String>(); for (PmMessage m : PmMessageApi.getPmTreeMessages(rootPm, Severity.INFO)) { msgStrings.add(m.toString()); } Assert.fail( "Unexpected messages after validation of " + rootPm.getPmRelativeName() + ":\n" + StringUtils.join(msgStrings.toArray(), "\n\t")); } }
@Test public void testValidationOfBeanTriggeredByPmBean() { myBeanPm.setPmBean(new MyBean("hi", 2, 1)); assertFalse(PmValidationApi.validate(myBeanPm)); }