/** * Initializes the DataVerifier. Needs to be called from the testing * framework before the verify method is invoked. * @param em EntityManager instance of the current persistence context * @param doc XML Document of an (optional) data export. If no data was exported, the * document will be null */ public final void init(EntityManager em, Document doc) { this.em = em; this.emUtil = new JuEmUtil(em); if (doc != null) this.xg = new XPathGetter(doc); }
/** * Write the DB data to an XML file. * @param file Path to the file */ public void writeToXmlFile(final Path file) { this.writeToXml().writeToXmlFile(file); }
/** * Writes the DB data to a String. * @return Xml Document instance */ public String writeToXmlString() { return this.writeToXml().getXmlString(); }
public void xPathGetter_canEvaluateXPath2() throws Exception { Document doc = XmlUtils.loadXml(JuUrl.resource().relativeTo(XmlUtilsTest.class).get("xPathGetter.xml")); XPathGetter xg = new XPathGetter(doc); // The name() function is an XPath 2.0 function String name = xg.getSingle("root/a1/name()"); Assert.assertEquals("a1", name); }
@Test public void canBuildXml_withNamespaces() { Document doc = XmlUtils.buildXml("test", "ns1", "http://inftec.ch/ns1") .addNamespace("ns2", "http://inftec.ch/ns2") .addChild("child") .endChild() .addChild("child", "ns2") .addChild("child", "ns1") .endChild() .endChild() .getDocument(); TestUtils.assertEqualsXmlResource("XmlUtilsTest_canBuildXml_withNamespaces.xml", doc); }
@Test public void canCreateXml_fromJaxbObject_usingFormattedOutput() { String xmlString = XmlUtils.marshaller().formattedOutput(true).marshalToString(this.getMichael()); TestUtils.assertEqualsResource("XmlUtilsTest_canCreateXml_fromJaxbObject_usingFormattedOutput.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 canMarshal_invalidObject_whenNotUsingSchema() { XmlUtils.marshaller().marshalToString(this.getMichael(100)); }
@Test public void canConvert_GregorianCalendar_toDate() { Date testDate = getTestDateUtc_2000_1_2_mignidht(); XMLGregorianCalendar gc = XmlUtils.asXMLGregorianCalendar(testDate); Date dConv = XmlUtils.asDate(gc); Assert.assertEquals(testDate, dConv); }
@Test public void canMarshall_object_withMultipleNamespaces() { String xml = XmlUtils.marshaller() .marshalToString(this.getComplexRoot()); TestUtils.assertEqualsXmlResource("XmlUtilsJaxbTest_canMarshall_object_withMultipleNamespaces.xml", xml); }
@Test public void canGetNodeNames() throws Exception { XPathGetter xg = XmlUtils.loadXmlAsXPathGetter(JuUrl.resource().relativeTo(XPathGetterTest.class).get("simple.xml")); List<String> nodeNames = xg.getNodeNames("root/element/*"); TestUtils.assertCollectionEquals(nodeNames, "childElement", "childElement", "textElement"); } }
/** * Writes the DB data to an (in-memory) XML Document. * @return Xml Document instance */ public Document writeToXmlDocument() { return this.writeToXml().getDocument(); }
@Test public void canConvertDate_toGregorianCalendar() { XMLGregorianCalendar gc = XmlUtils.asXMLGregorianCalendar(getTestDateUtc_2000_1_2_mignidht()); Assert.assertEquals("2000-01-02T00:00:00.000Z", gc.toXMLFormat()); }
@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 the XPathGetter on a XML without namespaces. */ @Test public void xPathGetter() throws Exception { this.xPathGetter("xPathGetter.xml"); }
@Test public void canLookForSpecialCharacters_inXPath() throws Exception { Document doc1 = XmlUtils.loadXml(JuUrl.resource().relativeTo(XmlUtilsTest.class).get("simpleSpecialChars.xml")); XPathGetter xg = new XPathGetter(doc1); Assert.assertEquals("", xg.getSingle("//attrElement[@textQuot='And ;']/textQuot")); }
@Test public void canCompare_xmlDocument_toResource() { Document doc = XmlUtils.buildXml("root").addChild("child").endChild().getDocument(); TestUtils.assertEqualsXmlResource("TestUtilsTest_canCompare_xmlDocument_toResource.xml", doc); }
@Test public void canCreateXml_fromJaxbObject() { String xmlString = XmlUtils.marshaller().marshalToString(this.getMichael()); TestUtils.assertEqualsResource("XmlUtilsTest_canCreateXml_fromJaxbObject.xml", xmlString); }
/** * Test the XPathGetter on a XML with namespace. */ @Test public void xPathGetter_namespace() throws Exception { this.xPathGetter("xPathGetter_namespace.xml"); }
@Test public void canHandleSpecialCharacters_inAttributes() throws Exception { Document doc1 = XmlUtils.loadXml(JuUrl.resource().relativeTo(XmlUtilsTest.class).get("simpleSpecialChars.xml")); XPathGetter xg = new XPathGetter(doc1); Assert.assertEquals("This is a little attr-text: äöü°+\"*ç%&/()=?`è!éà£><;:_,.-", xg.getSingle("//attrElement/@textFull")); }