📄 updateoperationtest.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.util.ArrayList;
import java.util.List;
/**
* @author Manuel Laflamme
* @version $Revision: 1.22 $
* @since Feb 19, 2002
*/
public class UpdateOperationTest extends AbstractDatabaseTest
{
public UpdateOperationTest(String s)
{
super(s);
}
////////////////////////////////////////////////////////////////////////////
//
protected IDataSet getDataSet() throws Exception
{
IDataSet dataSet = super.getDataSet();
DatabaseEnvironment environment = DatabaseEnvironment.getInstance();
if (environment.support(TestFeature.BLOB))
{
dataSet = new CompositeDataSet(
new FlatXmlDataSet(new File("src/xml/blobInsertTest.xml")),
dataSet);
}
if (environment.support(TestFeature.CLOB))
{
dataSet = new CompositeDataSet(
new FlatXmlDataSet(new File("src/xml/clobInsertTest.xml")),
dataSet);
}
return dataSet;
}
////////////////////////////////////////////////////////////////////////////
//
public void testMockExecute() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'",
"update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'",
};
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", "false", "0"});
valueList.add(new Object[]{"qwerty", new Double("123.45"), null, "0"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.VARCHAR),
new Column("c4", DataType.NUMERIC),
};
String[] primaryKeys = {"c4", "c1"};
ITable table = new DefaultTable(new DefaultTableMetaData(
tableName, columns, primaryKeys), 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 UpdateOperation().execute(connection, dataSet);
statement.verify();
factory.verify();
connection.verify();
}
public void testExecuteWithEscapedName() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"update [schema].[table] set [c2] = 1234, [c3] = 'false' where [c4] = 0 and [c1] = 'toto'",
"update [schema].[table] set [c2] = 123.45, [c3] = NULL where [c4] = 0 and [c1] = 'qwerty'",
};
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", "false", "0"});
valueList.add(new Object[]{"qwerty", new Double("123.45"), null, "0"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.VARCHAR),
new Column("c4", DataType.NUMERIC),
};
String[] primaryKeys = {"c4", "c1"};
ITable table = new DefaultTable(new DefaultTableMetaData(
tableName, columns, primaryKeys), 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 UpdateOperation().execute(connection, dataSet);
statement.verify();
factory.verify();
connection.verify();
}
public void testExecuteWithDuplicateTables() throws Exception
{
String schemaName = "schema";
String tableName = "table";
String[] expected = {
"update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'",
"update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'",
"update schema.table set c2 = 1234, c3 = 'false' where c4 = 0 and c1 = 'toto'",
"update schema.table set c2 = 123.45, c3 = NULL where c4 = 0 and c1 = 'qwerty'",
};
List valueList = new ArrayList();
valueList.add(new Object[]{"toto", "1234", "false", "0"});
valueList.add(new Object[]{"qwerty", new Double("123.45"), null, "0"});
Column[] columns = new Column[]{
new Column("c1", DataType.VARCHAR),
new Column("c2", DataType.NUMERIC),
new Column("c3", DataType.VARCHAR),
new Column("c4", DataType.NUMERIC),
};
String[] primaryKeys = {"c4", "c1"};
ITable table = new DefaultTable(new DefaultTableMetaData(
tableName, columns, primaryKeys), valueList);
IDataSet dataSet = new DefaultDataSet(new ITable[]{table, table});
// setup mock objects
MockBatchStatement statement = new MockBatchStatement();
statement.addExpectedBatchStrings(expected);
statement.setExpectedExecuteBatchCalls(2);
statement.setExpectedClearBatchCalls(2);
statement.setExpectedCloseCalls(2);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -