private void invalidEmployee(File fileInvalid) throws Exception { JAXBException exception = null; toggleDriversGroupOnOff(); /* Marshal w/ validation - doesn't pass (we want to check that). */ try { marshallerValidOn.marshal(employeeInvalid, fileInvalid); } catch (JAXBException e) { exception = e; } assertNotNull(exception); assertEquals( String.valueOf(BeanValidationException.CONSTRAINT_VIOLATION), exception.getErrorCode()); if (DEBUG) System.out.println(exception.getMessage()); checkValidationMessages(marshallerValidOn.getConstraintViolations(), violationMessages); /* Marshal w/o validation - creates file for the next part of the test. */ marshallerValidOff.marshal(employeeInvalid, fileInvalid); Set<ConstraintViolationWrapper<Object>> marshalCV = marshallerValidOff.getConstraintViolations(); assertTrue(marshalCV.isEmpty()); /* Unmarshal w/ validation - doesn't pass (we want to check that). */ exception = null; try { unmarshallerValidOn.unmarshal(fileInvalid); } catch (JAXBException e) { exception = e; } assertNotNull(exception); assertEquals( String.valueOf(BeanValidationException.CONSTRAINT_VIOLATION), exception.getErrorCode()); if (DEBUG) System.out.println(exception.getMessage()); checkValidationMessages(unmarshallerValidOn.getConstraintViolations(), violationMessages); /* Unmarshal w/ validation AND no groups - doesn't pass (we want to check that). */ toggleDriversGroupOnOff(); exception = null; try { unmarshallerValidOn.unmarshal(fileInvalid); } catch (JAXBException e) { exception = e; } assertNotNull(exception); assertEquals( String.valueOf(BeanValidationException.CONSTRAINT_VIOLATION), exception.getErrorCode()); if (DEBUG) System.out.println(exception.getMessage()); checkValidationMessages( unmarshallerValidOn.getConstraintViolations(), violationMessagesWithoutGroup); toggleDriversGroupOnOff(); /* Unmarshal w/o validation - testing that invalid objects are correctly unmarshalled when validation is NONE. */ Employee employeeUnm = (Employee) unmarshallerValidOff.unmarshal(fileInvalid); assertTrue(unmarshallerValidOff.getConstraintViolations().isEmpty()); /* Final check that the validation feature did not affect original behavior of JAXB. */ assertEquals(employeeInvalid, employeeUnm); }
private void validEmployee(File file) throws Exception { toggleDriversGroupOnOff(); marshallerValidOn.marshal(employeeValid, file); assertTrue(marshallerValidOn.getConstraintViolations().isEmpty()); Employee employeeUnm = (Employee) unmarshallerValidOn.unmarshal(file); assertTrue(unmarshallerValidOn.getConstraintViolations().isEmpty()); assertEquals(employeeValid, employeeUnm); }