📄 insertoperationtest.java
字号:
/*
*
* The DbUnit Database Testing Framework
* Copyright (C)2002-2004, DbUnit.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.dbunit.operation;
import org.dbunit.AbstractDatabaseTest;
import org.dbunit.Assertion;
import org.dbunit.DatabaseEnvironment;
import org.dbunit.TestFeature;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.database.MockDatabaseConnection;
import org.dbunit.database.statement.MockBatchStatement;
import org.dbunit.database.statement.MockStatementFactory;
import org.dbunit.dataset.*;
import org.dbunit.dataset.datatype.DataType;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* @author Manuel Laflamme
* @version $Revision: 1.23 $
* @since Feb 19, 2002
*/
public class InsertOperationTest extends AbstractDatabaseTest
{
public InsertOperationTest(String s)
{
super(s);
}
public void testMockExecute() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
"insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
};
// setup table
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", Boolean.FALSE});
valueList.add(new Object[]{"qwerty", new Double("123.45"), "true"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.BOOLEAN),
};
DefaultTable table = new DefaultTable(tableName, columns, valueList);
IDataSet dataSet = new DefaultDataSet(table);
// setup mock objects
MockBatchStatement statement = new MockBatchStatement();
statement.addExpectedBatchStrings(expected);
statement.setExpectedExecuteBatchCalls(1);
statement.setExpectedClearBatchCalls(1);
statement.setExpectedCloseCalls(1);
MockStatementFactory factory = new MockStatementFactory();
factory.setExpectedCreatePreparedStatementCalls(1);
factory.setupStatement(statement);
MockDatabaseConnection connection = new MockDatabaseConnection();
connection.setupDataSet(dataSet);
connection.setupSchema(schemaName);
connection.setupStatementFactory(factory);
connection.setExpectedCloseCalls(0);
// execute operation
new InsertOperation().execute(connection, dataSet);
statement.verify();
factory.verify();
connection.verify();
}
public void testExecuteIgnoreNull() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
"insert into schema.table (c2, c3) values (123.45, 'true')",
"insert into schema.table (c1, c2, c3) values ('qwerty1', 1, 'true')",
"insert into schema.table (c1, c2, c3) values ('qwerty2', 2, 'false')",
"insert into schema.table (c3) values ('false')",
};
// setup table
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", Boolean.FALSE});
valueList.add(new Object[]{null, new Double("123.45"), "true"});
valueList.add(new Object[]{"qwerty1", "1", Boolean.TRUE});
valueList.add(new Object[]{"qwerty2", "2", Boolean.FALSE});
valueList.add(new Object[]{null, null, Boolean.FALSE});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.BOOLEAN),
};
DefaultTable table = new DefaultTable(tableName, columns, valueList);
IDataSet dataSet = new DefaultDataSet(table);
// setup mock objects
MockBatchStatement statement = new MockBatchStatement();
statement.addExpectedBatchStrings(expected);
statement.setExpectedExecuteBatchCalls(4);
statement.setExpectedClearBatchCalls(4);
statement.setExpectedCloseCalls(4);
MockStatementFactory factory = new MockStatementFactory();
factory.setExpectedCreatePreparedStatementCalls(4);
factory.setupStatement(statement);
MockDatabaseConnection connection = new MockDatabaseConnection();
connection.setupDataSet(dataSet);
connection.setupSchema(schemaName);
connection.setupStatementFactory(factory);
connection.setExpectedCloseCalls(0);
// execute operation
new InsertOperation().execute(connection, dataSet);
statement.verify();
factory.verify();
connection.verify();
}
public void testExecuteWithEscapedNames() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('toto', 1234, 'false')",
"insert into 'schema'.'table' ('c1', 'c2', 'c3') values ('qwerty', 123.45, 'true')",
};
// setup table
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", Boolean.FALSE});
valueList.add(new Object[]{"qwerty", new Double("123.45"), "true"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.BOOLEAN),
};
DefaultTable table = new DefaultTable(tableName, columns, valueList);
IDataSet dataSet = new DefaultDataSet(table);
// setup mock objects
MockBatchStatement statement = new MockBatchStatement();
statement.addExpectedBatchStrings(expected);
statement.setExpectedExecuteBatchCalls(1);
statement.setExpectedClearBatchCalls(1);
statement.setExpectedCloseCalls(1);
MockStatementFactory factory = new MockStatementFactory();
factory.setExpectedCreatePreparedStatementCalls(1);
factory.setupStatement(statement);
MockDatabaseConnection connection = new MockDatabaseConnection();
connection.setupDataSet(dataSet);
connection.setupSchema(schemaName);
connection.setupStatementFactory(factory);
connection.setExpectedCloseCalls(0);
// execute operation
connection.getConfig().setProperty(
DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "'?'");
new InsertOperation().execute(connection, dataSet);
statement.verify();
factory.verify();
connection.verify();
}
public void testExecuteWithDuplicateTables() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
"insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
"insert into schema.table (c1, c2, c3) values ('toto', 1234, 'false')",
"insert into schema.table (c1, c2, c3) values ('qwerty', 123.45, 'true')",
};
// setup table
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", Boolean.FALSE});
valueList.add(new Object[]{"qwerty", new Double("123.45"), "true"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.BOOLEAN),
};
DefaultTable table = new DefaultTable(tableName, columns, valueList);
IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table});
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -