Class JaxbConverter


  • public final class JaxbConverter
    extends Object
    JAXB-empowered object to XML converting utility.

    The object has to be annotated with JAXB annotations in order to be convertable. Let's consider an example JAXB-annotated class:

     import javax.xml.bind.annotation.XmlAccessType;
     import javax.xml.bind.annotation.XmlAccessorType;
     import javax.xml.bind.annotation.XmlElement;
     import javax.xml.bind.annotation.XmlRootElement;
     @XmlRootElement(name = "employee")
     @XmlAccessorType(XmlAccessType.NONE)
     public class Employee {
       @XmlElement(name = "name")
       public String getName() {
         return "John Doe";
       }
     }

    Now you want to test how it works with real data after convertion to XML (in a unit test):

     import com.jcabi.matchers.JaxbConverter;
     import com.jcabi.matchers.XhtmlMatchers;
     import org.junit.Assert;
     import org.junit.Test;
     public final class EmployeeTest {
       @Test
       public void testObjectToXmlConversion() throws Exception {
         final Object object = new Employee();
         Assert.assertThat(
           JaxbConverter.the(object),
           XhtmlMatchers.hasXPath("/employee/name[.='John Doe']")
         );
       }
     }
    Since:
    0.1
    • Method Detail

      • the

        public static Source the​(Object object,
                                 Class<?>... deps)
                          throws javax.xml.bind.JAXBException
        Convert an object to XML.

        The method will throw AssertionError if marshalling of provided object fails for some reason.

        The name of the method is motivated by xmlatchers project and their XmlMatchers.the(String) method. Looks like this name is short enough and convenient for unit tests.

        Parameters:
        object - The object to convert
        deps - Dependencies that we should take into account
        Returns:
        DOM source/document
        Throws:
        javax.xml.bind.JAXBException - If an exception occurs inside