@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 singleTestingEntityData() { Assert.assertEquals(0, em.createQuery("select t from TestingEntity t").getResultList().size()); this.createDbDataUtil().buildImport() .from("/datasets/singleTestingEntityData.xml") .executeInsert(); Assert.assertEquals(1, em.createQuery("select t from TestingEntity t").getResultList().size()); this.createDbDataUtil().buildAssert() .expected(JuUrl.resource().relativeTo(BaseDbTest.class).get("BaseDbTest_singleTestingEntityData.xml")) .assertEqualsTable("TestingEntity", "id"); } }
ImportBuilder fullData = du.buildImport().from("/ju-testing/data/default-fullData.xml"); if (emptyTables) { fullData.executeDeleteAll(); } else { fullData.executeCleanInsert(); du.buildImport().from("/ju-testing/data/default-fullData-dataTypes.xml").executeUpdate();
.resourceToUrl(dataSetInfo.getAnnotation().value(), this.dataSetConfigInfo.getResourceDirectory()); du.buildImport() .from(resourceUrl) .executeCleanInsert(); URL resourceUrl = this.resourceToUrl(insertResource, this.dataSetConfigInfo.getResourceDirectory()); du.buildImport() .from(resourceUrl) .executeInsert();
/** * 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(); } }
@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()); }
/** * Returns a new ImportBuilder to import data from XML resources into the DB. * @return ImportBuilder instance */ public ImportBuilder buildImport() { return new ImportBuilder(this); }
/** * 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(); }
/** * Imports DB data from the specified XML. * <p> * Only one 'from' is possible per import. * @param resourcePath Resource path, either absolute or relative to the current class * @return ImportBuilder */ public ImportBuilder from(String resourcePath) { URL url = JuUrl.resource().relativeTo(ReflectUtils.getCallingClass()).get(resourcePath); return from(url); }
/** * 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()); }