Search in sources :

Example 11 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method test02Statistics.

@Test
public void test02Statistics() throws Exception {
    Level old = setLoggingLevelForTest(Level.FATAL);
    String[] updateValues = new String[TEST_COLUMNS.length + 1];
    updateValues[0] = updateValues[updateValues.length - 1] = getNextId();
    updateValues[2] = updateValues[4] = "required string";
    String[] insertValues = (String[]) ArrayUtils.subarray(updateValues, 0, updateValues.length - 1);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = getWriter(out);
    writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
    writer.writeRecord(new String[] { CsvConstants.CHANNEL, TestConstants.TEST_CHANNEL_ID });
    String nextBatchId = getNextBatchId();
    writer.writeRecord(new String[] { CsvConstants.BATCH, nextBatchId });
    writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
    // Update becomes fallback insert
    writer.write(CsvConstants.UPDATE);
    writer.writeRecord(updateValues, true);
    // Insert becomes fallback update
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    // Insert becomes fallback update
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    // Clean insert
    insertValues[0] = getNextId();
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    // Delete affects no rows
    writer.writeRecord(new String[] { CsvConstants.DELETE, getNextId() }, true);
    writer.writeRecord(new String[] { CsvConstants.DELETE, getNextId() }, true);
    writer.writeRecord(new String[] { CsvConstants.DELETE, getNextId() }, true);
    // Failing statement
    insertValues[0] = getNextId();
    insertValues[5] = "i am an invalid date";
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    writer.writeRecord(new String[] { CsvConstants.COMMIT, nextBatchId });
    writer.close();
    load(out);
    IncomingBatch batch = getIncomingBatchService().findIncomingBatch(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID);
    assertNotNull(batch);
    assertEquals(batch.getStatus(), IncomingBatch.Status.ER, "Wrong status. " + printDatabase());
    assertEquals(batch.getFailedRowNumber(), 8l, "Wrong failed row number. " + batch.getSqlMessage() + ". " + printDatabase());
    assertEquals(batch.getByteCount(), 483l, "Wrong byte count. " + printDatabase());
    assertEquals(batch.getStatementCount(), 8l, "Wrong statement count. " + printDatabase());
    assertEquals(batch.getFallbackInsertCount(), 1l, "Wrong fallback insert count. " + printDatabase());
    assertEquals(batch.getFallbackUpdateCount(), 2l, "Wrong fallback update count. " + printDatabase());
    assertEquals(batch.getMissingDeleteCount(), 3l, "Wrong missing delete count. " + printDatabase());
    setLoggingLevelForTest(old);
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) Level(org.apache.log4j.Level) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) Test(org.junit.Test)

Example 12 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method test08ErrorWhileParsing.

@Test
public void test08ErrorWhileParsing() throws Exception {
    Level old = setLoggingLevelForTest(Level.OFF);
    String[] values = { getNextId(), "should not reach database", "string not null", "char", "char not null", "2007-01-02", "2007-02-03 04:05:06.000", "0", "47", "67.89", "0.474" };
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = getWriter(out);
    writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
    writer.writeRecord(new String[] { CsvConstants.CHANNEL, TestConstants.TEST_CHANNEL_ID });
    String nextBatchId = getNextBatchId();
    writer.write("UnknownTokenOutsideBatch");
    writer.writeRecord(new String[] { CsvConstants.BATCH, nextBatchId });
    writer.writeRecord(new String[] { CsvConstants.TABLE, TEST_TABLE });
    writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(values, true);
    writer.writeRecord(new String[] { CsvConstants.COMMIT, nextBatchId });
    writer.close();
    load(out);
    assertEquals(findIncomingBatchStatus(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID), null, "Wrong status");
    IncomingBatch batch = getIncomingBatchService().findIncomingBatch(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID);
    assertNull(batch);
    setLoggingLevelForTest(old);
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) Level(org.apache.log4j.Level) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) Test(org.junit.Test)

Example 13 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method getWriter.

protected CsvWriter getWriter(OutputStream out) {
    CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',');
    writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
    return writer;
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter)

Example 14 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class CsvUtils method escapeCsvData.

/**
     * This escapes backslashes but doesn't wrap the data in a text qualifier.
     */
public static String escapeCsvData(String data) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',');
    writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
    try {
        writer.write(data);
        writer.close();
        out.close();
    } catch (IOException e) {
    }
    return out.toString();
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

CsvWriter (org.jumpmind.symmetric.csv.CsvWriter)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Test (org.junit.Test)8 Level (org.apache.log4j.Level)7 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)7 OutputStreamWriter (java.io.OutputStreamWriter)5 IOException (java.io.IOException)3 IoException (org.jumpmind.exception.IoException)2 SQLException (java.sql.SQLException)1 Column (org.jumpmind.db.model.Column)1 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)1 ConflictNodeGroupLink (org.jumpmind.symmetric.service.impl.DataLoaderService.ConflictNodeGroupLink)1