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.AbstractDataSetTest;
30  import org.dbunit.dataset.DataSetUtils;
31  import org.dbunit.dataset.DefaultDataSet;
32  import org.dbunit.dataset.DefaultTable;
33  import org.dbunit.dataset.EmptyTableDataSet;
34  import org.dbunit.dataset.IDataSet;
35  import org.dbunit.dataset.ITable;
36  import org.dbunit.dataset.LowerCaseDataSet;
37  
38  /**
39   * @author Manuel Laflamme
40   * @author Eric Pugh
41   * TODO Refactor all the references to AbstractDataSetTest.removeExtraTestTables() to something better.
42   * @version $Revision$
43   * @since Feb 18, 2002
44   */
45  public class DeleteAllOperationIT extends AbstractDatabaseIT
46  {
47      public DeleteAllOperationIT(String s)
48      {
49          super(s);
50      }
51      
52      protected void setUp() throws Exception
53      {
54          super.setUp();
55  
56          DatabaseOperation.CLEAN_INSERT.execute(_connection,
57                  getEnvironment().getInitDataSet());
58      }
59  
60      protected DatabaseOperation getDeleteAllOperation()
61      {
62          return new DeleteAllOperation();
63      }
64  
65      protected String getExpectedStament(String tableName)
66      {
67          return "delete from " + tableName;
68      }
69  
70      public void testMockExecute() throws Exception
71      {
72          String schemaName = "schema";
73          String tableName = "table";
74          String expected = getExpectedStament(schemaName + "." + tableName);
75  
76          IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName));
77  
78          // setup mock objects
79          MockBatchStatement statement = new MockBatchStatement();
80          statement.addExpectedBatchString(expected);
81          statement.setExpectedExecuteBatchCalls(1);
82          statement.setExpectedClearBatchCalls(1);
83          statement.setExpectedCloseCalls(1);
84  
85          MockStatementFactory factory = new MockStatementFactory();
86          factory.setExpectedCreateStatementCalls(1);
87          factory.setupStatement(statement);
88  
89          MockDatabaseConnection connection = new MockDatabaseConnection();
90          connection.setupDataSet(dataSet);
91          connection.setupSchema(schemaName);
92          connection.setupStatementFactory(factory);
93          connection.setExpectedCloseCalls(0);
94  
95          // execute operation
96          getDeleteAllOperation().execute(connection, dataSet);
97  
98          statement.verify();
99          factory.verify();
100         connection.verify();
101     }
102 
103     public void testExecuteWithEscapedNames() throws Exception
104     {
105         String schemaName = "schema";
106         String tableName = "table";
107         String expected = getExpectedStament("'" + schemaName + "'.'" + tableName +"'");
108 
109         IDataSet dataSet = new DefaultDataSet(new DefaultTable(tableName));
110 
111         // setup mock objects
112         MockBatchStatement statement = new MockBatchStatement();
113         statement.addExpectedBatchString(expected);
114         statement.setExpectedExecuteBatchCalls(1);
115         statement.setExpectedClearBatchCalls(1);
116         statement.setExpectedCloseCalls(1);
117 
118         MockStatementFactory factory = new MockStatementFactory();
119         factory.setExpectedCreateStatementCalls(1);
120         factory.setupStatement(statement);
121 
122         MockDatabaseConnection connection = new MockDatabaseConnection();
123         connection.setupDataSet(dataSet);
124         connection.setupSchema(schemaName);
125         connection.setupStatementFactory(factory);
126         connection.setExpectedCloseCalls(0);
127 
128         // execute operation
129         connection.getConfig().setProperty(
130                 DatabaseConfig.PROPERTY_ESCAPE_PATTERN, "'?'");
131         getDeleteAllOperation().execute(connection, dataSet);
132 
133         statement.verify();
134         factory.verify();
135         connection.verify();
136     }
137 
138     public void testExecute() throws Exception
139     {
140         IDataSet databaseDataSet = _connection.createDataSet();
141         IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables(
142                 databaseDataSet);
143 
144         testExecute(dataSet);
145     }
146 
147     public void testExecuteEmpty() throws Exception
148     {
149         IDataSet databaseDataSet = _connection.createDataSet();
150         IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables(
151                 databaseDataSet);
152 
153         testExecute(new EmptyTableDataSet(dataSet));
154     }
155 
156     public void testExecuteCaseInsentive() throws Exception
157     {
158         IDataSet dataSet = AbstractDataSetTest.removeExtraTestTables(
159                 _connection.createDataSet());
160 
161         testExecute(new LowerCaseDataSet(dataSet));
162     }
163 
164     /* The AbstractDataSetTest.removeExtraTestTables() is required when you
165     run on something besides hypersone (like mssql or oracle) to deal with
166     the extra tables that may not have data.
167 
168     Need something like getDefaultTables or something that is totally cross dbms.
169     */
170     private void testExecute(IDataSet dataSet) throws Exception
171     {
172         //dataSet = dataSet);
173         ITable[] tablesBefore = DataSetUtils.getTables(AbstractDataSetTest.removeExtraTestTables(_connection.createDataSet()));
174         getDeleteAllOperation().execute(_connection, dataSet);
175         ITable[] tablesAfter = DataSetUtils.getTables(AbstractDataSetTest.removeExtraTestTables(_connection.createDataSet()));
176 
177         assertTrue("table count > 0", tablesBefore.length > 0);
178         assertEquals("table count", tablesBefore.length, tablesAfter.length);
179         for (int i = 0; i < tablesBefore.length; i++)
180         {
181             ITable table = tablesBefore[i];
182             String name = table.getTableMetaData().getTableName();
183 
184             if (!name.toUpperCase().startsWith("EMPTY"))
185             {
186                 assertTrue(name + " before", table.getRowCount() > 0);
187             }
188         }
189 
190         for (int i = 0; i < tablesAfter.length; i++)
191         {
192             ITable table = tablesAfter[i];
193             String name = table.getTableMetaData().getTableName();
194             assertEquals(name + " after " + i, 0, table.getRowCount());
195         }
196     }
197 
198     public void testExecuteWithEmptyDataset() throws Exception
199     {
200         getDeleteAllOperation().execute(
201                 _connection, new DefaultDataSet(new ITable[0]));
202     }
203 }
204 
205 
206 
207 
208 
209