View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2002-2004, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  
22  package org.dbunit.operation;
23  
24  import org.dbunit.AbstractDatabaseIT;
25  import org.dbunit.database.DatabaseConfig;
26  import org.dbunit.database.MockDatabaseConnection;
27  import org.dbunit.database.statement.MockBatchStatement;
28  import org.dbunit.database.statement.MockStatementFactory;
29  import org.dbunit.dataset.Column;
30  import org.dbunit.dataset.DefaultDataSet;
31  import org.dbunit.dataset.DefaultTable;
32  import org.dbunit.dataset.DefaultTableMetaData;
33  import org.dbunit.dataset.IDataSet;
34  import org.dbunit.dataset.ITable;
35  import org.dbunit.dataset.ITableMetaData;
36  import org.dbunit.dataset.LowerCaseDataSet;
37  import org.dbunit.dataset.NoPrimaryKeyException;
38  import org.dbunit.dataset.datatype.DataType;
39  import org.dbunit.dataset.xml.XmlDataSet;
40  import org.dbunit.testutil.TestUtils;
41  
42  import java.io.File;
43  import java.io.FileReader;
44  import java.io.Reader;
45  
46  /**
47   * @author Manuel Laflamme
48   * @version $Revision$
49   * @since Feb 19, 2002
50   */
51  public class DeleteOperationIT extends AbstractDatabaseIT
52  {
53      public DeleteOperationIT(String s)
54      {
55          super(s);
56      }
57  
58      public void testMockExecute() throws Exception
59      {
60          String schemaName = "schema";
61          String tableName1 = "table1";
62          String tableName2 = "table2";
63          String[] expected = {
64              "delete from schema.table2 where c2 = 1234 and c1 = 'toto'",
65              "delete from schema.table2 where c2 = 123.45 and c1 = 'qwerty'",
66              "delete from schema.table1 where c2 = 1234 and c1 = 'toto'",
67              "delete from schema.table1 where c2 = 123.45 and c1 = 'qwerty'",
68          };
69  
70          Column[] columns = new Column[]{
71              new Column("c1", DataType.VARCHAR),
72              new Column("c2", DataType.NUMERIC),
73              new Column("c3", DataType.BOOLEAN),
74          };
75          String[] primaryKeys = {"c2", "c1"};
76  
77          DefaultTable table1 = new DefaultTable(new DefaultTableMetaData(
78                  tableName1, columns, primaryKeys));
79          table1.addRow(new Object[]{"qwerty", new Double("123.45"), "true"});
80          table1.addRow(new Object[]{"toto", "1234", Boolean.FALSE});
81          DefaultTable table2 = new DefaultTable(new DefaultTableMetaData(
82                  tableName2, columns, primaryKeys));
83          table2.addTableRows(table1);
84          IDataSet dataSet = new DefaultDataSet(table1, table2);
85  
86          // setup mock objects
87          MockBatchStatement statement = new MockBatchStatement();
88          statement.addExpectedBatchStrings(expected);
89          statement.setExpectedExecuteBatchCalls(2);
90          statement.setExpectedClearBatchCalls(2);
91          statement.setExpectedCloseCalls(2);
92  
93          MockStatementFactory factory = new MockStatementFactory();
94          factory.setExpectedCreatePreparedStatementCalls(2);
95          factory.setupStatement(statement);
96  
97          MockDatabaseConnection connection = new MockDatabaseConnection();
98          connection.setupDataSet(dataSet);
99          connection.setupSchema(schemaName);
100         connection.setupStatementFactory(factory);
101         connection.setExpectedCloseCalls(0);
102 
103         // execute operation
104         new DeleteOperation().execute(connection, dataSet);
105 
106         statement.verify();
107         factory.verify();
108         connection.verify();
109     }
110 
111     public void testExecuteWithEscapedNames() throws Exception
112     {
113         String schemaName = "schema";
114         String tableName = "table";
115         String[] expected = {
116             "delete from [schema].[table] where [c2] = 123.45 and [c1] = 'qwerty'",
117             "delete from [schema].[table] where [c2] = 1234 and [c1] = 'toto'",
118         };
119 
120         Column[] columns = new Column[]{
121             new Column("c1", DataType.VARCHAR),
122             new Column("c2", DataType.NUMERIC),
123             new Column("c3", DataType.BOOLEAN),
124         };
125         String[] primaryKeys = {"c2", "c1"};
126 
127         DefaultTable table = new DefaultTable(new DefaultTableMetaData(
128                 tableName, columns, primaryKeys));
129         table.addRow(new Object[]{"toto", "1234", Boolean.FALSE});
130         table.addRow(new Object[]{"qwerty", new Double("123.45"), "true"});
131         IDataSet dataSet = new DefaultDataSet(table);
132 
133         // setup mock objects
134         MockBatchStatement statement = new MockBatchStatement();
135         statement.addExpectedBatchStrings(expected);
136         statement.setExpectedExecuteBatchCalls(1);
137         statement.setExpectedClearBatchCalls(1);
138         statement.setExpectedCloseCalls(1);
139 
140         MockStatementFactory factory = new MockStatementFactory();
141         factory.setExpectedCreatePreparedStatementCalls(1);
142         factory.setupStatement(statement);
143 
144         MockDatabaseConnection connection = new MockDatabaseConnection();
145         connection.setupDataSet(dataSet);
146         connection.setupSchema(schemaName);
147         connection.setupStatementFactory(factory);
148         connection.setExpectedCloseCalls(0);
149 
150         // execute operation
151         connection.getConfig().setProperty(
152                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "[?]");
153         new DeleteOperation().execute(connection, dataSet);
154 
155         statement.verify();
156         factory.verify();
157         connection.verify();
158     }
159 
160     public void testExecuteWithEmptyTable() throws Exception
161     {
162         Column[] columns = {new Column("c1", DataType.VARCHAR)};
163         ITable table = new DefaultTable(new DefaultTableMetaData(
164                 "name", columns, columns));
165         IDataSet dataSet = new DefaultDataSet(table);
166 
167         // setup mock objects
168         MockStatementFactory factory = new MockStatementFactory();
169         factory.setExpectedCreatePreparedStatementCalls(0);
170 
171         MockDatabaseConnection connection = new MockDatabaseConnection();
172         connection.setupDataSet(dataSet);
173         connection.setupStatementFactory(factory);
174         connection.setExpectedCloseCalls(0);
175 
176         // execute operation
177         new DeleteOperation().execute(connection, dataSet);
178 
179         factory.verify();
180         connection.verify();
181     }
182 
183     public void testExecuteAndNoPrimaryKey() throws Exception
184     {
185         IDataSet dataSet = _connection.createDataSet();
186         ITableMetaData metaData = dataSet.getTableMetaData("TEST_TABLE");
187         try
188         {
189             new DeleteOperation().getOperationData(
190                     metaData, null, _connection);
191             fail("Should throw a NoPrimaryKeyException");
192         }
193         catch (NoPrimaryKeyException e)
194         {
195         }
196     }
197 
198     public void testExecute() throws Exception
199     {
200         Reader in = new FileReader(
201                 TestUtils.getFile("xml/deleteOperationTest.xml"));
202         IDataSet dataSet = new XmlDataSet(in);
203 
204         testExecute(dataSet);
205 
206     }
207 
208     public void testExecuteCaseInsensitive() throws Exception
209     {
210         Reader in = new FileReader(
211                 TestUtils.getFile("xml/deleteOperationTest.xml"));
212         IDataSet dataSet = new XmlDataSet(in);
213 
214         testExecute(new LowerCaseDataSet(dataSet));
215     }
216 
217     private void testExecute(IDataSet dataSet) throws Exception
218     {
219         String tableName = "PK_TABLE";
220         String columnName = "PK0";
221 
222         // verify table before
223         ITable tableBefore = createOrderedTable(tableName, columnName);
224         assertEquals("row count before", 3, tableBefore.getRowCount());
225         assertEquals("before", "0", tableBefore.getValue(0, columnName).toString());
226         assertEquals("before", "1", tableBefore.getValue(1, columnName).toString());
227         assertEquals("before", "2", tableBefore.getValue(2, columnName).toString());
228 
229         DatabaseOperation.DELETE.execute(_connection, dataSet);
230 
231         ITable tableAfter = createOrderedTable(tableName, columnName);
232         assertEquals("row count after", 2, tableAfter.getRowCount());
233         assertEquals("after", "0", tableAfter.getValue(0, columnName).toString());
234         assertEquals("after", "2", tableAfter.getValue(1, columnName).toString());
235     }
236 }
237 
238 
239 
240 
241 
242 
243 
244