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); }
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."); }
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."); }