@Test public void exportTables_areSortedByPrimaryKey() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); DbDataUtil du = new DbDataUtil(this.em); du.cleanImport("/ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity_unsorted.xml"); // Export table with camel case Document doc = du.buildExport() .addTableSorted("TestingEntity") .writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); logger.debug("Exported XML\n" + XmlUtils.toString(doc, false, true)); Assert.assertEquals(3, xg.getArray("//TestingEntity").length); Assert.assertEquals("1", xg.getSingle("//TestingEntity[1]/@id")); Assert.assertEquals("2", xg.getSingle("//TestingEntity[2]/@id")); Assert.assertEquals("3", xg.getSingle("//TestingEntity[3]/@id")); }
/** * Gets an XPathGetter to verify the XML document. * <p> * We can only get an XPathGetter when an XML was exported using the @DataSetExport annotation. * <p> * This method returns an XPathGetter relative to /dataset, so we don't need to explicitly * specify the /dataset root to query the XML. * @return XPathGetter on the exported XML */ protected final XPathGetter getXg() { if (this.xg == null) { logger.warn("XML verifying is only possible when using @DataSetExport with exportType != NONE"); return null; } else { return this.xg.getGetter("/dataset"); } } }
XPathGetter xg = new XPathGetter(doc); for (XPathGetter xgConn : xg.getGetters("persistence/persistence-unit")) { String name = xgConn.getSingle("@name"); String flagString = xgConn.getSingle("properties/property[@name='ch.inftec.ju.flags']/@value"); String schemaName = xgConn.getSingle("properties/property[@name='ch.inftec.ju.schemaName']/@value");
private void xPathGetter(String resourceName) throws Exception { Document doc = XmlUtils.loadXml(JuUrl.resource().relativeTo(XmlUtilsTest.class).get(resourceName)); XPathGetter xg = new XPathGetter(doc); assertEquals("XPathGetter[node=#document,nodeValue=<null>]", xg.toString()); assertArrayEquals(xg.getArray("//a1/*/@text"), new String[] {"B2", "B2", "B3"}); assertArrayEquals(xg.getArrayLong("//*/@value"), new Long[] {1L, 2L, 2L}); assertArrayEquals(xg.getDistinctArray("//b3/*/@value"), new String[] {"2"}); assertArrayEquals(xg.getDistinctArrayLong("//b3/*/@value"), new Long[] {2L}); assertEquals(xg.getGetter("//b3").getSingle("@text"), "B3"); XPathGetter xgSubs[] = xg.getGetters("//b3/*"); assertEquals(xgSubs.length, 2); assertEquals(xgSubs[0].getSingle("@text"), "C2"); assertEquals("XPathGetter[node=c2,nodeValue=<null>]", xgSubs[0].toString()); assertEquals(xgSubs[1].getSingle("@text"), "C3"); assertEquals("XPathGetter[node=c3,nodeValue=<null>]", xgSubs[1].toString()); assertEquals(xg.getNode("root"), doc.getDocumentElement()); Node bNodes[] = xg.getNodes("//a1/*"); assertEquals(bNodes.length, 3); assertEquals(bNodes[2].getNodeName(), "b3"); assertEquals(xg.getSingle("/root/a1/@text"), "A1"); assertEquals(xg.getSingleLong("/root/a1/b2/c1/@value"), new Long(1)); assertEquals(3, xg.getCount("//a1/*")); }
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); }
/** * Tests the data export function writing DB data to an XML Document. */ @Test public void writeToDocument() { // Whole table to XML Document Document doc = this.createDbDataUtil().buildExport() .addTable("Team", null) .writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); Assert.assertEquals(2, xg.getSingleLong("count(//Team)").intValue()); }
/** * Adds the data of the tables contained in the specified data set. * <p> * It doesn't matter what kind of dataset we got, we're just extracting the table names. * @param resourceUrl * @param sortedByPrimaryKey If true, the entries will be sorted by primary key * @return ExportBuilder to allow for chaining */ public ExportBuilder addTablesByDataSet(URL resourceUrl, boolean sortedByPrimaryKey) { try { Set<String> tableNames = JuCollectionUtils.asSameOrderSet(new XPathGetter(XmlUtils.loadXml(resourceUrl)).getNodeNames("dataset/*")); for (String tableName : tableNames) { if (sortedByPrimaryKey) { this.addTableSorted(tableName); } else { this.addTable(tableName); } } return this; } catch (Exception ex) { throw new JuDbException("Couldn't add tables by dataset " + resourceUrl, ex); } }
@Test public void xPathGetter_reckognizesEmptyElements() throws Exception { Document doc = XmlUtils.loadXml(JuUrl.resource().relativeTo(XmlUtilsTest.class).get("xPathGetter_emptyElement.xml")); XPathGetter xg = new XPathGetter(doc); Assert.assertTrue(xg.isEmptyElement("//a1")); Assert.assertTrue(xg.isEmptyElement("//a5")); Assert.assertTrue(xg.isEmptyElement("//a6")); // Empty result Assert.assertFalse(xg.isEmptyElement("//a2")); Assert.assertFalse(xg.isEmptyElement("//a4")); }
/** * 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); }
@Override public void verify() throws Exception { Assert.assertEquals("DoCommit", this.getXg().getSingle("//TestingEntity/@name")); } }
@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"); } }
@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 writeToDocument_order() { // Annotation not working as it would run after class annotation which results // in an INSERT instead of a CLEAN_INSERT this.createDbDataUtil().buildImport() .from("/datasets/testingEntityUnsortedData.xml") .executeCleanInsert(); Document doc = this.createDbDataUtil().buildExport() .addTableSorted("TestingEntity", "ID") .writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); Assert.assertEquals(1, xg.getSingleLong("//TestingEntity[1]/@id").intValue()); Assert.assertEquals(2, xg.getSingleLong("//TestingEntity[2]/@id").intValue()); Assert.assertEquals(3, xg.getSingleLong("//TestingEntity[3]/@id").intValue()); }
/** * Loads the table names from the specified dataset XML resource and uses it as a template * of how to case any table name that will be exported. * <p> * Note that calling this method doesn't actually ADD a table. * @param resourcePath Resource path to dataset XML * @return ExportBuilder to allow for chaining */ public ExportBuilder setTableNamesCasingByDataSet(String resourcePath) { try { this.exportItems.setCasedTableNames(new XPathGetter(XmlUtils.loadXml( JuUrl.resource().relativeTo(DbDataUtil.class).get(resourcePath))).getNodeNames("dataset/*")); } catch (Exception ex) { throw new JuDbException("Couldn't load table names data set " + resourcePath, ex); } return this; }
@Override public void verify() throws Exception { Assert.assertEquals("TransactionTest", this.getXg().getSingle("//TestingEntity/@name")); } }
@Test public void exportTables_basedOnDatasetXml_areSortedByPrimaryKey() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); DbDataUtil du = new DbDataUtil(this.em); du.cleanImport("/ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity_unsorted.xml"); // Export table with camel case Document doc = du.buildExport() .addTablesByDataSet(JuUrl.resource("ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity.xml"), true) .writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); logger.debug("Exported XML\n" + XmlUtils.toString(doc, false, true)); Assert.assertEquals(3, xg.getArray("//TestingEntity").length); Assert.assertEquals("1", xg.getSingle("//TestingEntity[1]/@id")); Assert.assertEquals("2", xg.getSingle("//TestingEntity[2]/@id")); Assert.assertEquals("3", xg.getSingle("//TestingEntity[3]/@id")); }
@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")); }
@Test public void writeToDocument_query() { // Annotation not working as it would run after class annotation which results // in an INSERT instead of a CLEAN_INSERT this.createDbDataUtil().buildImport() .from("/datasets/testingEntityUnsortedData.xml") .executeCleanInsert(); Document doc = this.createDbDataUtil().buildExport() .addTable("TestingEntity", "SELECT * FROM TESTINGENTITY WHERE ID=2") .writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); Assert.assertEquals(1, xg.getSingleLong("count(//TestingEntity)").intValue()); Assert.assertEquals(2, xg.getSingleLong("//TestingEntity/@id").intValue()); }
@Test public void canExportData_toXmlDocument() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); DbDataUtil du = new DbDataUtil(this.em); Document doc = du.buildExport().addTable("TestingEntity").writeToXmlDocument(); XPathGetter xg = new XPathGetter(doc); logger.debug("Exported XML\n" + XmlUtils.toString(doc, false, true)); Assert.assertEquals(1, xg.getArray("//TestingEntity").length); Assert.assertEquals("Export Test", xg.getSingle("//TestingEntity/@name")); }