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  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import java.io.Reader;
28  import java.nio.charset.StandardCharsets;
29  import java.nio.file.Files;
30  
31  import org.dbunit.AbstractDatabaseIT;
32  import org.dbunit.database.DatabaseConfig;
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.xml.XmlDataSet;
39  import org.dbunit.testutil.TestUtils;
40  import org.junit.jupiter.api.Test;
41  
42  /**
43   * @author Manuel Laflamme
44   * @version $Revision$
45   * @since Feb 19, 2002
46   */
47  public class DeleteOperationIT extends AbstractDatabaseIT
48  {
49  
50      @Test
51      void testGetOperationData_withNoPrimaryKey_throwsNoPrimaryKeyException() throws Exception
52      {
53          final IDataSet dataSet = _connection.createDataSet();
54          final ITableMetaData metaData = dataSet.getTableMetaData("TEST_TABLE");
55          assertThrows(
56                  NoPrimaryKeyException.class, () -> new DeleteOperation()
57                          .getOperationData(metaData, null, _connection),
58                  "Should throw a NoPrimaryKeyException");
59      }
60  
61      @Test
62      void testExecute_withXmlDataSet_deletesMatchingRowsByPrimaryKey() throws Exception
63      {
64          final Reader in = Files.newBufferedReader(
65                  TestUtils.getFile("xml/deleteOperationTest.xml").toPath(), StandardCharsets.UTF_8);
66          final IDataSet dataSet = new XmlDataSet(in);
67  
68          testExecute(dataSet);
69  
70      }
71  
72      @Test
73      void testExecute_withLowerCaseDataSet_deletesMatchingRows() throws Exception
74      {
75          final Reader in = Files.newBufferedReader(
76                  TestUtils.getFile("xml/deleteOperationTest.xml").toPath(), StandardCharsets.UTF_8);
77          final IDataSet dataSet = new XmlDataSet(in);
78  
79          testExecute(new LowerCaseDataSet(dataSet));
80      }
81  
82      @Test
83      void testExecute_withBatchedStatements_deletesMatchingRowsByPrimaryKey() throws Exception
84      {
85          final Reader in = Files.newBufferedReader(
86                  TestUtils.getFile("xml/deleteOperationTest.xml").toPath(), StandardCharsets.UTF_8);
87          final IDataSet dataSet = new XmlDataSet(in);
88  
89          _connection.getConfig()
90                  .setFeature(DatabaseConfig.FEATURE_BATCHED_STATEMENTS, true);
91  
92          testExecute(dataSet);
93      }
94  
95      void testExecute(final IDataSet dataSet) throws Exception
96      {
97          final String tableName = "PK_TABLE";
98          final String columnName = "PK0";
99  
100         // verify table before
101         final ITable tableBefore = createOrderedTable(tableName, columnName);
102         assertThat(tableBefore.getRowCount()).as("row count before")
103                 .isEqualTo(3);
104         assertThat(tableBefore.getValue(0, columnName)).as("before")
105                 .hasToString("0");
106         assertThat(tableBefore.getValue(1, columnName)).as("before")
107                 .hasToString("1");
108         assertThat(tableBefore.getValue(2, columnName)).as("before")
109                 .hasToString("2");
110 
111         DatabaseOperation.DELETE.execute(_connection, dataSet);
112 
113         final ITable tableAfter = createOrderedTable(tableName, columnName);
114         assertThat(tableAfter.getRowCount()).as("row count after").isEqualTo(2);
115         assertThat(tableAfter.getValue(0, columnName)).as("after")
116                 .hasToString("0");
117         assertThat(tableAfter.getValue(1, columnName)).as("after")
118                 .hasToString("2");
119     }
120 }