forked from p81075629/datagear
[dataexchange]完善CSV导入单元测试用例
This commit is contained in:
parent
0b3028d8cf
commit
fe35a5b19a
|
@ -351,10 +351,19 @@ public abstract class AbstractTextDevotedDataImporter<T extends Import> extends
|
|||
|
||||
case Types.TIMESTAMP:
|
||||
|
||||
// XXX 这种处理方式会丢失纳秒数据,待以后版本升级至jdk1.8库时采用java.time可解决
|
||||
java.util.Date tsdv = setParameterContext.getTimestampFormatter().parse(parameterValue);
|
||||
java.sql.Timestamp tsv = new Timestamp(tsdv.getTime());
|
||||
st.setTimestamp(parameterIndex, tsv);
|
||||
// 如果是默认格式,则直接使用Timestamp.valueOf,这样可以避免丢失纳秒精度
|
||||
if (DataFormat.DEFAULT_TIMESTAMP_FORMAT.equals(dataFormat.getTimestampFormat()))
|
||||
{
|
||||
java.sql.Timestamp tsv = Timestamp.valueOf(parameterValue);
|
||||
st.setTimestamp(parameterIndex, tsv);
|
||||
}
|
||||
else
|
||||
{
|
||||
// XXX 这种处理方式会丢失纳秒数据,待以后版本升级至jdk1.8库时采用java.time可解决
|
||||
java.util.Date tsdv = setParameterContext.getTimestampFormatter().parse(parameterValue);
|
||||
java.sql.Timestamp tsv = new Timestamp(tsdv.getTime());
|
||||
st.setTimestamp(parameterIndex, tsv);
|
||||
}
|
||||
break;
|
||||
|
||||
case Types.CLOB:
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
package org.datagear.dataexchange.support;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
|
@ -17,7 +19,9 @@ import org.datagear.dbinfo.DevotedDatabaseInfoResolver;
|
|||
import org.datagear.dbinfo.GenericDatabaseInfoResolver;
|
||||
import org.datagear.dbinfo.WildcardDevotedDatabaseInfoResolver;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
/**
|
||||
* {@linkplain CsvDataImporter}单元测试类。
|
||||
|
@ -29,6 +33,9 @@ public class CsvDataImporterTest extends DataexchangeTestSupport
|
|||
{
|
||||
public static final String TABLE_NAME = "T_DATAEXCHANGE";
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
protected DatabaseInfoResolver databaseInfoResolver;
|
||||
|
||||
private CsvDataImporter csvDataImporter;
|
||||
|
@ -45,10 +52,37 @@ public class CsvDataImporterTest extends DataexchangeTestSupport
|
|||
}
|
||||
|
||||
@Test
|
||||
public void imptTest() throws Exception
|
||||
public void imptTest_ignoreInexistentColumn_false() throws Exception
|
||||
{
|
||||
Reader reader = IOUtil.getReader(CsvDataImporterTest.class.getClassLoader()
|
||||
.getResourceAsStream("org/datagear/dataexchange/support/CsvDataImporterTest.csv"), "UTF-8");
|
||||
Reader reader = IOUtil.getReader(getResourceInputStream("CsvDataImporterTest_ignoreInexistentColumn.csv"),
|
||||
"UTF-8");
|
||||
DataFormat dataFormat = new DataFormat();
|
||||
|
||||
Connection cn = getConnection();
|
||||
|
||||
CsvImport impt = new CsvImport(cn, true, null, reader, dataFormat, false, TABLE_NAME);
|
||||
|
||||
try
|
||||
{
|
||||
clearTable(cn, TABLE_NAME);
|
||||
|
||||
this.thrown.expect(ColumnNotFoundException.class);
|
||||
this.thrown.expectMessage("Column [INEXISTENT_COLUMN] not found");
|
||||
|
||||
this.csvDataImporter.impt(impt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
IOUtil.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imptTest_ignoreInexistentColumn_true() throws Exception
|
||||
{
|
||||
Reader reader = IOUtil.getReader(getResourceInputStream("CsvDataImporterTest_ignoreInexistentColumn.csv"),
|
||||
"UTF-8");
|
||||
DataFormat dataFormat = new DataFormat();
|
||||
|
||||
Connection cn = getConnection();
|
||||
|
@ -68,6 +102,65 @@ public class CsvDataImporterTest extends DataexchangeTestSupport
|
|||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
IOUtil.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imptTest_abortOnError_false() throws Exception
|
||||
{
|
||||
Reader reader = IOUtil.getReader(getResourceInputStream("CsvDataImporterTest_abortOnError.csv"), "UTF-8");
|
||||
DataFormat dataFormat = new DataFormat();
|
||||
|
||||
Connection cn = getConnection();
|
||||
|
||||
CsvImport impt = new CsvImport(cn, false, null, reader, dataFormat, true, TABLE_NAME);
|
||||
|
||||
try
|
||||
{
|
||||
clearTable(cn, TABLE_NAME);
|
||||
|
||||
this.csvDataImporter.impt(impt);
|
||||
|
||||
int count = getCount(cn, TABLE_NAME);
|
||||
|
||||
Assert.assertEquals(2, count);
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
IOUtil.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void imptTest_abortOnError_true() throws Exception
|
||||
{
|
||||
Reader reader = IOUtil.getReader(getResourceInputStream("CsvDataImporterTest_abortOnError.csv"), "UTF-8");
|
||||
DataFormat dataFormat = new DataFormat();
|
||||
|
||||
Connection cn = getConnection();
|
||||
|
||||
CsvImport impt = new CsvImport(cn, true, null, reader, dataFormat, true, TABLE_NAME);
|
||||
|
||||
try
|
||||
{
|
||||
clearTable(cn, TABLE_NAME);
|
||||
|
||||
this.thrown.expect(IllegalSourceValueException.class);
|
||||
|
||||
this.csvDataImporter.impt(impt);
|
||||
}
|
||||
finally
|
||||
{
|
||||
JdbcUtil.closeConnection(cn);
|
||||
IOUtil.close(reader);
|
||||
}
|
||||
}
|
||||
|
||||
protected InputStream getResourceInputStream(String resourceName) throws IOException
|
||||
{
|
||||
return CsvDataImporterTest.class.getClassLoader()
|
||||
.getResourceAsStream("org/datagear/dataexchange/support/" + resourceName);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
ID,NAME
|
||||
1,NAME-1
|
||||
2,NAME-2
|
||||
3,NAME-3
|
|
|
@ -0,0 +1,4 @@
|
|||
ID,NAME
|
||||
1,NAME-1
|
||||
NOT-NUMBER,NAME-2
|
||||
3,NAME-3
|
|
|
@ -0,0 +1,4 @@
|
|||
ID,NAME,INEXISTENT_COLUMN
|
||||
1,NAME-1,A
|
||||
2,NAME-2,B
|
||||
3,NAME-3,C
|
|
Loading…
Reference in New Issue