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); }
public void testFailOnSecondErrorFile() throws Exception { unmarshaller.setEventHandler(new CustomErrorValidationEventHandler()); File file = new File(DOUBLE_ERROR_XML); try { unmarshaller.setSchema(this.schema); unmarshaller.unmarshal(file); } catch (UnmarshalException ex) { assertTrue(true); return; } catch (UnsupportedOperationException uoe) { // XDK does not support setSchema, so just pass in this case assertTrue(true); return; } fail("No Exceptions thrown."); }
public void testFailOnSecondErrorInputStream() throws Exception { unmarshaller.setEventHandler(new CustomErrorValidationEventHandler()); InputStream stream = ClassLoader.getSystemResourceAsStream(DOUBLE_ERROR_XML); try { unmarshaller.setSchema(this.schema); unmarshaller.unmarshal(stream); } catch (UnmarshalException ex) { assertTrue(true); return; } catch (UnsupportedOperationException uoe) { // XDK does not support setSchema, so just pass in this case assertTrue(true); return; } fail("No Exceptions thrown."); }
@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(); }
public void testFailOnSecondErrorXMLStreamReaderWithType() throws Exception { InputStream stream = ClassLoader.getSystemResourceAsStream(DOUBLE_ERROR_XML); XMLStreamReader xmlStreamReader = createXMLStreamReader(stream); if (null == xmlStreamReader) { return; } try { unmarshaller.setSchema(this.schema); unmarshaller.setEventHandler(new CustomErrorValidationEventHandler()); unmarshaller.unmarshal(xmlStreamReader, (Type) Employee.class); } catch (UnmarshalException ex) { assertTrue(true); return; } catch (UnsupportedOperationException uoe) { // XDK does not support setSchema, so just pass in this case assertTrue(true); return; } fail("No Exceptions thrown."); }
public void testFailOnSecondErrorNodeWithClass() throws Exception { unmarshaller.setEventHandler(new CustomErrorValidationEventHandler()); InputStream stream = ClassLoader.getSystemResourceAsStream(DOUBLE_ERROR_XML); XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform(); XMLParser xmlParser = xmlPlatform.newXMLParser(); xmlParser.setNamespaceAware(true); Node node = xmlParser.parse(stream); try { unmarshaller.setSchema(this.schema); unmarshaller.unmarshal(node, Employee.class); } catch (UnmarshalException ex) { assertTrue(true); return; } catch (UnsupportedOperationException uoe) { // XDK does not support setSchema, so just pass in this case assertTrue(true); return; } fail("No Exceptions thrown."); }
public void testFailOnSecondErrorSAXSourceWithType() throws Exception { unmarshaller.setEventHandler(new CustomErrorValidationEventHandler()); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setNamespaceAware(true); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); InputStream stream = ClassLoader.getSystemResourceAsStream(DOUBLE_ERROR_XML); Source source = new SAXSource(xr, new InputSource(stream)); try { unmarshaller.setSchema(this.schema); unmarshaller.unmarshal(source, (Type) Employee.class); } catch (UnmarshalException ex) { assertTrue(true); return; } catch (UnsupportedOperationException uoe) { // XDK does not support setSchema, so just pass in this case assertTrue(true); return; } fail("No Exceptions thrown."); }
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); }