Search in sources :

Example 1 with DbImport

use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.

the class DbExportImportTest method importUniqueKeywordTable.

@Test
public void importUniqueKeywordTable() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    DbImport dbImport = new DbImport(engine.getDatabasePlatform());
    dbImport.setFormat(DbImport.Format.XML);
    dbImport.setDropIfExists(true);
    dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
    dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
    IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
    Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
    Table table = testTables.findTable("test_db_import_unique", false);
    Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
    DbImport importCsv = new DbImport(engine.getDatabasePlatform());
    importCsv.setFormat(DbImport.Format.SQL);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-good.sql"));
    Assert.assertEquals(5, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
    try {
        importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
        Assert.fail("Expected a sql exception");
    } catch (SqlException ex) {
    }
    Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
    importCsv.setCommitRate(1);
    importCsv.setForceImport(true);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-unique-bad-line-2.sql"));
    Assert.assertEquals(4, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) SqlException(org.jumpmind.db.sql.SqlException) DbImport(org.jumpmind.symmetric.io.data.DbImport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 2 with DbImport

use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.

the class DbExportImportTest method exportUniqueKeywordSqliteTable.

@Test
public void exportUniqueKeywordSqliteTable() throws Exception {
    String dbName = engine.getDatabasePlatform().getName();
    if (dbName.equals(DatabaseNamesConstants.SQLITE)) {
        ISymmetricEngine engine = getSymmetricEngine();
        DbImport dbImport = new DbImport(engine.getDatabasePlatform());
        dbImport.setFormat(DbImport.Format.XML);
        dbImport.setDropIfExists(true);
        dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
        dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
        IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
        Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
        Table table = testTables.findTable("test_db_import_unique", false);
        Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
        Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
        final int RECORD_COUNT = 100;
        DbFill fill = new DbFill(platform);
        fill.setRecordCount(RECORD_COUNT);
        fill.fillTables(table.getName());
        Assert.assertEquals(RECORD_COUNT, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
        DbExport export = new DbExport(platform);
        export.setFormat(Format.XML);
        export.setNoCreateInfo(false);
        export.setNoData(true);
        String xmlOutput = export.exportTables(new String[] { table.getName() });
        dbImport.importTables(xmlOutput);
        table = testTables.findTable("test_db_import_unique", false);
        Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
        Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
    }
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) DbImport(org.jumpmind.symmetric.io.data.DbImport) DbFill(org.jumpmind.symmetric.io.data.DbFill) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 3 with DbImport

use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.

the class DbExportImportTest method importSymXmlData.

@Test
public void importSymXmlData() throws Exception {
    final String FILE = "/test-dbimport-1-sym_xml-1.xml";
    ISymmetricEngine engine = getSymmetricEngine();
    recreateImportTable();
    assertCountDbImportTableRecords(0);
    DbImport importCsv = new DbImport(engine.getDatabasePlatform());
    importCsv.setFormat(DbImport.Format.SYM_XML);
    importCsv.importTables(getClass().getResourceAsStream(FILE));
    assertCountDbImportTableRecords(2);
    try {
        importCsv.importTables(getClass().getResourceAsStream(FILE));
        Assert.fail("Expected a sql exception");
    } catch (ConflictException ex) {
    }
    assertCountDbImportTableRecords(2);
    recreateImportTable();
    importCsv.setReplaceRows(true);
    importCsv.importTables(getClass().getResourceAsStream(FILE));
    assertCountDbImportTableRecords(2);
}
Also used : ConflictException(org.jumpmind.symmetric.io.data.writer.ConflictException) DbImport(org.jumpmind.symmetric.io.data.DbImport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 4 with DbImport

use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.

the class DbExportImportTest method importSqlData.

@Test
public void importSqlData() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    recreateImportTable();
    assertCountDbImportTableRecords(0);
    DbImport importCsv = new DbImport(engine.getDatabasePlatform());
    importCsv.setFormat(DbImport.Format.SQL);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-1-good.sql"));
    assertCountDbImportTableRecords(5);
    recreateImportTable();
    assertCountDbImportTableRecords(0);
    try {
        importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-1-bad-line-2.sql"));
        Assert.fail("Expected a sql exception");
    } catch (SqlException ex) {
    }
    assertCountDbImportTableRecords(0);
    importCsv.setCommitRate(1);
    importCsv.setForceImport(true);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-1-bad-line-2.sql"));
    assertCountDbImportTableRecords(4);
}
Also used : SqlException(org.jumpmind.db.sql.SqlException) DbImport(org.jumpmind.symmetric.io.data.DbImport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 5 with DbImport

use of org.jumpmind.symmetric.io.data.DbImport in project symmetric-ds by JumpMind.

the class DbExportImportTest method exportThenImportCsv.

@Test
public void exportThenImportCsv() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
    Database testTables = platform.readDatabaseFromXml("/test-dbimport.xml", true);
    Table table = testTables.findTable("test_db_import_1", false);
    recreateImportTable();
    final int RECORD_COUNT = 100;
    DbFill fill = new DbFill(platform);
    fill.setRecordCount(RECORD_COUNT);
    fill.fillTables(table.getName());
    DbExport export = new DbExport(platform);
    export.setFormat(Format.CSV);
    export.setNoCreateInfo(true);
    export.setNoData(false);
    String csvOutput = export.exportTables(new String[] { table.getName() });
    logger.info(csvOutput);
    ISqlTemplate sqlTemplate = platform.getSqlTemplate();
    List<Row> rowsBeforeImport = sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID);
    recreateImportTable();
    DbImport importCsv = new DbImport(platform);
    importCsv.setFormat(DbImport.Format.CSV);
    importCsv.importTables(csvOutput, table.getName());
    Assert.assertEquals(RECORD_COUNT, sqlTemplate.queryForInt("select count(*) from " + table.getName()));
    compareRows(table, rowsBeforeImport, sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID));
// TODO test error
// TODO test replace
// TODO test ignore
// TODO test force
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) Row(org.jumpmind.db.sql.Row) DbImport(org.jumpmind.symmetric.io.data.DbImport) DbFill(org.jumpmind.symmetric.io.data.DbFill) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Aggregations

DbImport (org.jumpmind.symmetric.io.data.DbImport)11 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)9 Test (org.junit.Test)9 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)6 Table (org.jumpmind.db.model.Table)5 Database (org.jumpmind.db.model.Database)4 DbExport (org.jumpmind.symmetric.io.data.DbExport)4 File (java.io.File)2 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)2 Row (org.jumpmind.db.sql.Row)2 SqlException (org.jumpmind.db.sql.SqlException)2 DbFill (org.jumpmind.symmetric.io.data.DbFill)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 Column (org.jumpmind.db.model.Column)1 ConflictException (org.jumpmind.symmetric.io.data.writer.ConflictException)1 IDatabaseWriterFilter (org.jumpmind.symmetric.io.data.writer.IDatabaseWriterFilter)1