@Before public void setUp() throws Exception { preferredValidatorFactory = Validation.buildDefaultValidatorFactory(); JAXBContext ctx = JAXBContextFactory.createContext(EMPLOYEE, null); marshallerValidOn = (JAXBMarshaller) ctx.createMarshaller(); marshallerValidOn.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshallerValidOff = (JAXBMarshaller) ctx.createMarshaller(); /* tests setting the property through marshaller */ marshallerValidOff.setProperty( MarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE); marshallerValidOff.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); JAXBContext ctxValidationOff = JAXBContextFactory.createContext( EMPLOYEE, new HashMap<String, Object>() { { put(JAXBContextProperties.BEAN_VALIDATION_MODE, BeanValidationMode.NONE); put(JAXBContextProperties.BEAN_VALIDATION_FACTORY, preferredValidatorFactory); } }); unmarshallerValidOn = (JAXBUnmarshaller) ctxValidationOff.createUnmarshaller(); /* tests setting the property through unmarshaller */ unmarshallerValidOn.setProperty( UnmarshallerProperties.BEAN_VALIDATION_MODE, BeanValidationMode.CALLBACK); unmarshallerValidOff = (JAXBUnmarshaller) ctxValidationOff.createUnmarshaller(); }
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 switchToJson() throws PropertyException { marshallerValidOn.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json"); marshallerValidOn.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true); unmarshallerValidOn.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json"); unmarshallerValidOn.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true); marshallerValidOff.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json"); marshallerValidOff.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true); unmarshallerValidOff.setProperty(JAXBContextProperties.MEDIA_TYPE, "application/json"); unmarshallerValidOff.setProperty(JAXBContextProperties.JSON_INCLUDE_ROOT, true); }
private void toggleDriversGroupOnOff() throws PropertyException { if (toggle ^= true) { marshallerValidOn.setProperty( MarshallerProperties.BEAN_VALIDATION_GROUPS, new Class[] {Default.class, Drivers.class}); unmarshallerValidOn.setProperty( MarshallerProperties.BEAN_VALIDATION_GROUPS, new Class[] {Default.class, Drivers.class}); } else { marshallerValidOn.setProperty(MarshallerProperties.BEAN_VALIDATION_GROUPS, new Class[0]); unmarshallerValidOn.setProperty(MarshallerProperties.BEAN_VALIDATION_GROUPS, new Class[0]); } }
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); }