/** * Tests the data import from an XML file. */ @Test public void importDataFromXml() { Assert.assertEquals(1, em.createQuery("Select t from TestingEntity t").getResultList().size()); this.createDbDataUtil().buildImport() .from(JuUrl.resource().relativeTo(DbDataUtilTest.class).get("DbDataUtilsTest_importDataFromXml.xml")) .executeCleanInsert(); Assert.assertEquals(2, em.createQuery("Select t from TestingEntity t").getResultList().size()); }
@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()); }
@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()); }
fullData.executeDeleteAll(); } else { fullData.executeCleanInsert();
du.buildImport() .from(resourceUrl) .executeCleanInsert();
/** * Executes a DbUnit dataset import through a JDBC connection. * @param conn JDBC connection * @param dataSetUrl DataSet URL * @param cleanInsert If true, a clean insert is performed. Otherwise, an insert is performed. */ public static void executeInsert(Connection conn, URL dataSetUrl, boolean cleanInsert) { ImportBuilder ib = new DbDataUtil(conn).buildImport().from(dataSetUrl); if (cleanInsert) { ib.executeCleanInsert(); } else { ib.executeInsert(); } }
/** * Shortcut to execute a clean import from a dataset resource file. * @param resourcePath Path to dataset resource */ public void cleanImport(String resourcePath) { this.buildImport().from(resourcePath).executeCleanInsert(); }