@Test public void canNotMarshal_invalidObject_whenUsingSchema() { exception.expect(JuRuntimeException.class); exception.expectCause(Is.isA(MarshalException.class)); XmlUtils.marshaller() .schema(JuUrl.resourceRelativeTo("player.xsd", this.getClass())) .marshalToString(this.getMichael(100)); }
@Test public void canNotUnmarshal_invalidXml_whenUsingSchema() throws Exception { exception.expect(JuRuntimeException.class); exception.expectCause(Is.isA(UnmarshalException.class)); String xmlString = new IOUtil().loadTextFromUrl(JuUrl.resourceRelativeTo("XmlUtilsTest_michael_invalid.xml", this.getClass())); XmlUtils.marshaller() .schema(JuUrl.resourceRelativeTo("player.xsd", this.getClass())) .unmarshal(xmlString, Player.class); }
@Test public void canMarshall_object_withMultipleNamespaces() { String xml = XmlUtils.marshaller() .marshalToString(this.getComplexRoot()); TestUtils.assertEqualsXmlResource("XmlUtilsJaxbTest_canMarshall_object_withMultipleNamespaces.xml", xml); }
@Test public void canCreate_object_fromXml() throws Exception { String xmlString = new IOUtil().loadTextFromUrl(JuUrl.resourceRelativeTo("XmlUtilsTest_canCreateXml_fromJaxbObject.xml", XmlUtilsJaxbTest.class)); Object o = XmlUtils.marshaller().unmarshalRaw(xmlString, Player.class); this.assertMichael((Player) o); }
@Test public void canMarshal_invalidObject_whenNotUsingSchema() { XmlUtils.marshaller().marshalToString(this.getMichael(100)); }
@Ignore("Cannot set prefix mapper on all platforms...") @Test public void canMarshall_object_withMultipleNamespaces_andCustomDefaultPrefix() { String xml = XmlUtils.marshaller() .setNamespacePrefix(null, "urn:inftec.ch/ju/util/xml/ns/ref") .marshalToString(this.getComplexRoot()); TestUtils.assertEqualsXmlResource("XmlUtilsJaxbTest_canMarshall_object_withMultipleNamespaces_andCustomDefaultPrefix.xml", xml); }
@Test public void canCreate_typedOject_fromXml() throws Exception { String xmlString = new IOUtil().loadTextFromUrl(JuUrl.resourceRelativeTo("XmlUtilsTest_canCreateXml_fromJaxbObject.xml", XmlUtilsJaxbTest.class)); Player p = XmlUtils.marshaller().unmarshal(xmlString, Player.class); this.assertMichael(p); }
@Test public void canCreateXml_fromJaxbObject_usingFormattedOutput() { String xmlString = XmlUtils.marshaller().formattedOutput(true).marshalToString(this.getMichael()); TestUtils.assertEqualsResource("XmlUtilsTest_canCreateXml_fromJaxbObject_usingFormattedOutput.xml", xmlString); }
@Test public void canUnmarshal_invalidXml_whenNotUsingSchema() throws Exception { String xmlString = new IOUtil().loadTextFromUrl(JuUrl.resourceRelativeTo("XmlUtilsTest_michael_invalid.xml", this.getClass())); XmlUtils.marshaller().unmarshal(xmlString, Player.class); }
@Test public void canCreateXml_fromJaxbObject() { String xmlString = XmlUtils.marshaller().marshalToString(this.getMichael()); TestUtils.assertEqualsResource("XmlUtilsTest_canCreateXml_fromJaxbObject.xml", xmlString); }
@Ignore("Cannot set prefix mapper on all platforms...") @Test public void canMarshall_object_withMultipleNamespaces_andCustomPrefixes() { String xml = XmlUtils.marshaller() .setNamespacePrefix("m", "urn:inftec.ch/ju/util/xml/ns/main") .setNamespacePrefix("r", "urn:inftec.ch/ju/util/xml/ns/ref") .marshalToString(this.getComplexRoot()); TestUtils.assertEqualsXmlResource("XmlUtilsJaxbTest_canMarshall_object_withMultipleNamespaces_andCustomPrefixes.xml", xml); }
@Test public void canNot_createObject_fromXml_withoutNamespace() throws Exception { exception.expect(JuRuntimeException.class); exception.expectCause(Is.isA(UnmarshalException.class)); String xmlString = new IOUtil().loadTextFromUrl(JuUrl.resourceRelativeTo("XmlUtilsTest_michael_noNamespace.xml", XmlUtilsJaxbTest.class)); Object o = XmlUtils.marshaller().unmarshalRaw(xmlString, Player.class); this.assertMichael((Player) o); }