@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); } }
@Test public void assert_canAssertTables() { DbSchemaUtil ds = new DbSchemaUtil(this.em); ds.prepareDefaultTestData(true, true, true); DbDataUtil du = new DbDataUtil(this.emUtil); du.cleanImport("/ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity_unsorted.xml"); du.buildAssert() .expected("/ch/inftec/ju/testing/db/DbDataUtilTest_testingEntity_sorted.xml") .assertEquals(); }
/** * 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(); } }
/** * Gets an instance of a DbDataUtil for the current connection. * @return DbDataUtil instance */ protected final DbDataUtil createDbDataUtil() { DbDataUtil util = new DbDataUtil(this.em); // util.setSchema(this.connectionInfo.getSchema()); if (this.dataTypeFactor != null) { util.setConfigProperty("http://www.dbunit.org/properties/datatypeFactory", this.dataTypeFactor); } return util; }
@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 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 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 canImportData_fromDatasetFile() { 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.xml"); TestingEntity te = this.em.find(TestingEntity.class, 1L); Assert.assertEquals("DbDataUtilTest", te.getName()); }
@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"); } }
/** * 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(); }
@Before public void loadTestData() { this.createDbDataUtil().cleanImport("/datasets/fullData.xml"); }
/** * Asserts that the complete DB export equals the data in an XML file. */ @Test public void assertEqualsAll() { // // Can be used to create full export XML // du.buildExport().writeToXmlFile("completeExport.xml"); this.createDbDataUtil().buildAssert() .expected(JuUrl.resource().relativeTo(DbDataUtilTest.class).get("DbDataUtilsTest_assertEqualsAll.xml")) .assertEqualsAll(); }
@Override public void execute(Connection connection) throws SQLException { URL url = JuUrl.resource("ch/inftec/ju/testing/db/DbDataUtilTest_jdbcConnectionImport.xml"); DbDataUtil.executeInsert(connection, url, true); } });
private DbDataUtil getDbDataUtil(JuEmUtil emUtil) { if (this.annoInfo.dbDataUtilConfigAnnos.size() > 0) { AnnotationInfo<DbDataUtilConfig> annoInfo = this.annoInfo.dbDataUtilConfigAnnos.get(0); Class<? extends DbDataUtilProvider> providerClass = annoInfo.getAnnotation().value(); logger.debug("Retrieving DbDataUtil from provider {} (defined in Annotation {}", providerClass, annoInfo); DbDataUtilProvider provider = ReflectUtils.newInstance(providerClass, false); DbDataUtil dbDataUtil = provider.getDbDataUtil(); if (dbDataUtil == null) { logger.warn("DbDataUtilProvider.getDbDataUtil() returned null. Creating DbDataUtil using JuEmUtil."); } else { return provider.getDbDataUtil(); } } return new DbDataUtil(emUtil); }
case ORACLE: this.setConfigProperty(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true); this.setConfigProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, dataTypeFactory); this.setConfigProperty(DatabaseConfig.PROPERTY_METADATA_HANDLER, metadataHandler);
@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 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 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()); }
/** * 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()); }
@Before public void loadTestData() { this.createDbDataUtil().cleanImport("/datasets/fullData.xml"); }