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 java.io.Reader;
27  import java.nio.charset.StandardCharsets;
28  import java.nio.file.Files;
29  
30  import org.dbunit.AbstractDatabaseIT;
31  import org.dbunit.dataset.IDataSet;
32  import org.dbunit.dataset.ITable;
33  import org.dbunit.dataset.xml.XmlDataSet;
34  import org.dbunit.testutil.TestUtils;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * @author Manuel Laflamme
39   * @version $Revision$
40   * @since Feb 19, 2002
41   */
42  class CompositeOperationIT extends AbstractDatabaseIT
43  {
44  
45      @Test
46      void testExecute_withDeleteAllAndInsertOperations_resultMatchesExpected() throws Exception
47      {
48          final String tableName = "PK_TABLE";
49          final String columnName = "PK0";
50          final IDataSet xmlDataSet;
51          try (Reader in = Files.newBufferedReader(
52                  TestUtils.getFile("xml/compositeOperationTest.xml").toPath(), StandardCharsets.UTF_8))
53          {
54              xmlDataSet = new XmlDataSet(in);
55          }
56  
57          // verify table before
58          final ITable tableBefore = createOrderedTable(tableName, columnName);
59          assertThat(tableBefore.getRowCount()).as("row count before")
60                  .isEqualTo(3);
61          assertThat(tableBefore.getValue(0, columnName)).as("before")
62                  .hasToString("0");
63          assertThat(tableBefore.getValue(1, columnName)).as("before")
64                  .hasToString("1");
65          assertThat(tableBefore.getValue(2, columnName)).as("before")
66                  .hasToString("2");
67  
68          final DatabaseOperation operation = new CompositeOperation(
69                  DatabaseOperation.DELETE_ALL, DatabaseOperation.INSERT);
70          operation.execute(_connection, xmlDataSet);
71  
72          final ITable tableAfter = createOrderedTable(tableName, columnName);
73          assertThat(tableAfter.getRowCount()).as("row count after").isEqualTo(2);
74          assertThat(tableAfter.getValue(0, columnName)).as("after")
75                  .hasToString("1");
76          assertThat(tableAfter.getValue(1, columnName)).as("after")
77                  .hasToString("3");
78      }
79  
80  }