public void marshal(Object object, XMLStreamWriter streamWriter, TypeMappingInfo type)
      throws JAXBException {
    if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
      marshal(object, streamWriter);
    } else {
      JAXBElement element = null;
      Object value = object;
      if (object instanceof JAXBElement) {
        // use the JAXBElement's properties to populate an XMLRoot
        element = (JAXBElement) object;
        value = element.getValue();
      }
      if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
        RootLevelXmlAdapter adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        if (adapter != null) {
          try {
            value = adapter.getXmlAdapter().marshal(value);
          } catch (Exception ex) {
            throw new JAXBException(XMLMarshalException.marshalException(ex));
          }
        }
      }

      value = wrapObject(value, element, type);
      marshal(value, streamWriter);
    }
  }
  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);
  }
 public void marshal(Object object, File file) throws JAXBException {
   try {
     FileOutputStream outputStream = new FileOutputStream(file);
     try {
       marshal(object, outputStream);
     } finally {
       outputStream.close();
     }
   } catch (Exception ex) {
     throw new MarshalException(ex);
   }
 }
  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);
  }