/** * 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(); } }
@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")); }
@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")); }
/** * 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_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")); }
@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 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 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()); }
/** * 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); }
/** * 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()); }