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  package org.dbunit.operation;
22  
23  import static org.assertj.core.api.Assertions.assertThat;
24  
25  import org.dbunit.AbstractDatabaseIT;
26  import org.dbunit.Assertion;
27  import org.dbunit.dataset.Column;
28  import org.dbunit.dataset.DefaultDataSet;
29  import org.dbunit.dataset.DefaultTable;
30  import org.dbunit.dataset.ITable;
31  import org.dbunit.dataset.LowerCaseDataSet;
32  import org.dbunit.dataset.SortedTable;
33  import org.dbunit.dataset.datatype.DataType;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Integration tests for {@link DatabaseOperation#CLEAN_INSERT}.
38   *
39   * @since 3.2.0
40   */
41  class CleanInsertOperationIT extends AbstractDatabaseIT
42  {
43      private static final String TABLE_NAME = "TEST_TABLE";
44  
45      private static final Column[] COLUMNS = new Column[]{
46              new Column("COLUMN0", DataType.VARCHAR),
47              new Column("COLUMN1", DataType.VARCHAR),
48              new Column("COLUMN2", DataType.VARCHAR),
49              new Column("COLUMN3", DataType.VARCHAR)
50      };
51  
52      @Test
53      void testExecute_withExistingRows_deletesAllBeforeInserting() throws Exception
54      {
55          assertThat(_connection.getRowCount(TABLE_NAME)).as("count before.").isEqualTo(6);
56  
57          final DefaultTable newData = new DefaultTable(TABLE_NAME, COLUMNS);
58          newData.addRow(new Object[]{"new0", "v0", "v0", "v0"});
59          newData.addRow(new Object[]{"new1", "v1", "v1", "v1"});
60  
61          DatabaseOperation.CLEAN_INSERT.execute(_connection, new DefaultDataSet(newData));
62  
63          assertThat(_connection.getRowCount(TABLE_NAME)).as("count after.").isEqualTo(2);
64      }
65  
66      @Test
67      void testExecute_withLowerCaseDataSet_deletesAndInsertsSuccessfully() throws Exception
68      {
69          final DefaultTable data = new DefaultTable(TABLE_NAME, COLUMNS);
70          data.addRow(new Object[]{"r0", "v0", "v0", "v0"});
71          data.addRow(new Object[]{"r1", "v1", "v1", "v1"});
72          final LowerCaseDataSet lowerDataSet = new LowerCaseDataSet(new DefaultDataSet(data));
73  
74          DatabaseOperation.CLEAN_INSERT.execute(_connection, lowerDataSet);
75  
76          assertThat(_connection.getRowCount(TABLE_NAME)).as("count after.").isEqualTo(2);
77      }
78  
79      @Test
80      void testExecute_withXmlDataSet_replacesAllExistingRowsWithNewData() throws Exception
81      {
82          final DefaultTable initialData = new DefaultTable(TABLE_NAME, COLUMNS);
83          initialData.addRow(new Object[]{"initial0", "v0", "v0", "v0"});
84          DatabaseOperation.INSERT.execute(_connection,
85                  new DefaultDataSet(initialData));
86  
87          final DefaultTable newData = new DefaultTable(TABLE_NAME, COLUMNS);
88          newData.addRow(new Object[]{"replaced0", "n0", "n0", "n0"});
89          newData.addRow(new Object[]{"replaced1", "n1", "n1", "n1"});
90          newData.addRow(new Object[]{"replaced2", "n2", "n2", "n2"});
91  
92          DatabaseOperation.CLEAN_INSERT.execute(_connection, new DefaultDataSet(newData));
93  
94          final ITable actual = _connection.createDataSet().getTable(TABLE_NAME);
95          final SortedTable sortedExpected =
96                  new SortedTable(newData, new String[]{"COLUMN0"});
97          final SortedTable sortedActual =
98                  new SortedTable(actual, new String[]{"COLUMN0"});
99          Assertion.assertEquals(sortedExpected, sortedActual);
100     }
101 
102     @Test
103     void testExecute_withEmptyReplacementDataset_deletesAllExistingRows() throws Exception
104     {
105         assertThat(_connection.getRowCount(TABLE_NAME)).as("count before.").isEqualTo(6);
106 
107         final DefaultTable emptyData = new DefaultTable(TABLE_NAME, COLUMNS);
108 
109         DatabaseOperation.CLEAN_INSERT.execute(_connection, new DefaultDataSet(emptyData));
110 
111         assertThat(_connection.getRowCount(TABLE_NAME)).as("count after.").isZero();
112     }
113 }