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 static org.assertj.core.api.Assertions.assertThat;
25  
26  import org.dbunit.AbstractDatabaseIT;
27  import org.dbunit.dataset.AbstractDataSetTest;
28  import org.dbunit.dataset.DataSetUtils;
29  import org.dbunit.dataset.DefaultDataSet;
30  import org.dbunit.dataset.EmptyTableDataSet;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.ITable;
33  import org.dbunit.dataset.LowerCaseDataSet;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * @author Manuel Laflamme
39   * @author Eric Pugh TODO Refactor all the references to
40   *         AbstractDataSetTest.removeExtraTestTables() to something better.
41   * @version $Revision$
42   * @since Feb 18, 2002
43   */
44  class DeleteAllOperationIT extends AbstractDatabaseIT
45  {
46  
47      @Override
48      @BeforeEach
49      protected void setUp() throws Exception
50      {
51          super.setUp();
52  
53          DatabaseOperation.CLEAN_INSERT.execute(_connection,
54                  getEnvironment().getInitDataSet());
55      }
56  
57      protected DatabaseOperation getDeleteAllOperation()
58      {
59          return new DeleteAllOperation();
60      }
61  
62      protected String getExpectedStament(final String tableName)
63      {
64          return "delete from " + tableName;
65      }
66  
67      @Test
68      void testExecute_withPopulatedDatabase_deletesAllRows() throws Exception
69      {
70          final IDataSet databaseDataSet = _connection.createDataSet();
71          final IDataSet dataSet =
72                  AbstractDataSetTest.removeExtraTestTables(databaseDataSet);
73  
74          testExecute(dataSet);
75      }
76  
77      @Test
78      void testExecute_withEmptyTableDataSet_deletesAllRows() throws Exception
79      {
80          final IDataSet databaseDataSet = _connection.createDataSet();
81          final IDataSet dataSet =
82                  AbstractDataSetTest.removeExtraTestTables(databaseDataSet);
83  
84          testExecute(new EmptyTableDataSet(dataSet));
85      }
86  
87      @Test
88      void testExecute_withLowerCaseDataSet_deletesAllRows() throws Exception
89      {
90          final IDataSet dataSet = AbstractDataSetTest
91                  .removeExtraTestTables(_connection.createDataSet());
92  
93          testExecute(new LowerCaseDataSet(dataSet));
94      }
95  
96      /*
97       * The AbstractDataSetTest.removeExtraTestTables() is required when you run
98       * on something besides hypersone (like mssql or oracle) to deal with the
99       * extra tables that may not have data.
100      *
101      * Need something like getDefaultTables or something that is totally cross
102      * dbms.
103      */
104     private void testExecute(final IDataSet dataSet) throws Exception
105     {
106         // dataSet = dataSet);
107         final ITable[] tablesBefore = DataSetUtils.getTables(AbstractDataSetTest
108                 .removeExtraTestTables(_connection.createDataSet()));
109         getDeleteAllOperation().execute(_connection, dataSet);
110         final ITable[] tablesAfter = DataSetUtils.getTables(AbstractDataSetTest
111                 .removeExtraTestTables(_connection.createDataSet()));
112 
113         assertThat(tablesBefore).as("table count > 0").hasSizeGreaterThan(0);
114         assertThat(tablesAfter).as("table count").hasSameSizeAs(tablesBefore);
115         for (int i = 0; i < tablesBefore.length; i++)
116         {
117             final ITable table = tablesBefore[i];
118             final String name = table.getTableMetaData().getTableName();
119 
120             if (!name.toUpperCase().startsWith("EMPTY"))
121             {
122                 assertThat(table.getRowCount()).as(name + " before")
123                         .isPositive();
124             }
125         }
126 
127         for (int i = 0; i < tablesAfter.length; i++)
128         {
129             final ITable table = tablesAfter[i];
130             final String name = table.getTableMetaData().getTableName();
131             assertThat(table.getRowCount()).as(name + " after " + i).isZero();
132         }
133     }
134 
135     @Test
136     void testExecute_withEmptyDataset_completesWithoutError() throws Exception
137     {
138         getDeleteAllOperation().execute(_connection,
139                 new DefaultDataSet(new ITable[0]));
140     }
141 }