/** * Tests the data export function writing DB data to an XML file. */ @Test public void writeToXmlFile() { File file = null; try { // Whole table to file String fileName = "writeToXmlFile_team.xml"; file = new File(fileName); if (file.exists()) file.delete(); this.createDbDataUtil().buildExport() .addTable("Team", null) .writeToXmlFile(fileName); Assert.assertTrue(file.exists()); } finally { if (file != null && file.exists()) file.delete(); } }
.addTablesByDataSet(tablesDataSestUrl, true) .writeToXml();
@Test public void xmlExport_canApply_casedTableNames() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); // Export table with camel case DbDataUtil du = new DbDataUtil(this.em); Document doc = du.buildExport() .addTable("TESTINGENTITY") .setTableNamesCasingByDataSet("/ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity.xml") .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")); }
/** * Writes the DB data to an XML Output Converter that can convert or write it to different * output formats without having to re-execute the whole DbUnit export. * @return */ public XmlOutputConverter writeToXml() { final DataHolder<XmlOutputConverter> doc = new DataHolder<>(); this.doWork(new DataSetWork() { @Override public void execute(IDataSet dataSet) { try { XmlOutputConverter xmlConv = new XmlOutputConverter(); ExportBuilder.writeToXml(dataSet, xmlConv.getOutputStream()); doc.setValue(xmlConv); } catch (Exception ex) { throw new JuDbException("Couldn't write DB data to XML Output Converter", ex); } } }); return doc.getValue(); }
/** * 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); } }
/** * Adds the data of the specified table, ordering by the specified columns. * @param tableName Table names * @param orderColumns List of columns to order by * @return ExportBuilder to allow for chaining */ public ExportBuilder addTableSorted(String tableName, String... orderColumns) { if (orderColumns.length == 0) { return this.addTable(tableName); } else { XString xs = new XString(); xs.addFormatted("SELECT * FROM %s ORDER BY ", tableName); for (String orderColumn : orderColumns) { xs.assertText("ORDER BY ", ", "); xs.addText(orderColumn); } return this.addTable(tableName, xs.toString()); } }
@Test public void xmlExport_copesWithLowerCaseTable_andUsesUpperCaseColumnNames() { // MySQL is case sensitive JuAssumeUtils.dbIsNot(this.emUtil, DbType.MYSQL); DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); // Export table with camel case 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")); }
@Test public void canExportTables_basedOnDatasetXml() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); // Export table with camel case DbDataUtil du = new DbDataUtil(this.em); Document doc = du.buildExport() .addTablesByDataSet(JuUrl.resource("ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity.xml"), false) .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")); }
@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 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")); }
@Test public void xmlExport_copesWithUpperCaseTable_andUsesUpperCaseColumnNames() { // MySQL is case sensitive JuAssumeUtils.dbIsNot(this.emUtil, DbType.MYSQL); DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); // Export table with camel case 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")); }
@Test public void xmlExport_copesWithCamelCaseTable_andUsesUpperCaseColumnNames() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); TestingEntity te = new TestingEntity(); te.setName("Export Test"); this.em.persist(te); // Export table with camel case 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")); }
@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()); }
/** * Adds the specific table to the builder, exporting the table data. * <p> * The data will be sorted by the tables primary key column. * <p> * Note: This works only if the DbDataUtil was initialized with an emUtil instance. * @param tableName Table name * @return ExportBuilder to allow for chaining */ public ExportBuilder addTableSorted(String tableName) { AssertUtil.assertNotNull( "Sorting by primary key only works with DbDataUtils that were initialized with an JuConnUtil instance" , this.dbDataUtil.connUtil); return this.addTableSorted(tableName, this.getPrimaryKeyColumns(tableName)); }
@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")); }
/** * Returns a new ExportBuilder to configure and execute DB data exports. * @return ExportBuilder instance */ public ExportBuilder buildExport() { return new ExportBuilder(this); }
/** * Write the DB data to an XML file. * <p> * File name will be resolved relatively to the current working directory. * @param fileName File name * @deprecated Use writeToXmlFile(Path) instead */ @Deprecated public void writeToXmlFile(String fileName) { this.writeToXmlFile(Paths.get(fileName)); }
@Override public void execute(IDatabaseConnection conn) { try { IDataSet dbDataSet = dbDataUtil.buildExport() .addTablesByDataSet(dataSetUrl, true) .createDataSet(conn); Assertion.assertEquals(flatXmlDataSet, dbDataSet); } catch (Exception ex) { throw new JuDbException("Couldn't assert DB data", ex); } }
/** * Adds the specific table to the builder, exporting the table data. * @param tableName Table name * @return ExportBuilder to allow for chaining */ public ExportBuilder addTable(String tableName) { return this.addTable(tableName, null); }