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.dataset;
23  
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.io.OutputStream;
27  import java.nio.file.Files;
28  import java.nio.file.Paths;
29  
30  import org.dbunit.AbstractDatabaseIT;
31  import org.dbunit.DdlExecutor;
32  import org.dbunit.database.QueryDataSet;
33  import org.dbunit.dataset.datatype.DataType;
34  import org.dbunit.dataset.xml.FlatXmlDataSet;
35  import org.dbunit.testutil.TestUtils;
36  import org.junit.jupiter.api.AfterEach;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Test Case for issue #1721870
42   *
43   * @author Sebastien Le Callonnec
44   * @version $Revision$
45   * @since Mar 11, 2008
46   */
47  class CompositeDataSetIterationIT extends AbstractDatabaseIT
48  {
49  
50      private final String sqlFile = "hypersonic_simple_dataset.sql";
51  
52      @BeforeEach
53      protected void setUpTables() throws Exception
54      {
55          DdlExecutor.dropTables(_connection.getConnection(), "A", "B", "C");
56          DdlExecutor.executeDdlFile(TestUtils.getFile("sql/" + sqlFile),
57                  _connection.getConnection(), false);
58      }
59  
60      @AfterEach
61      protected void tearDownTables() throws Exception
62      {
63          DdlExecutor.dropTables(_connection.getConnection(), "A", "B", "C");
64          refreshConnection();
65      }
66  
67      /**
68       * Replaces {@code _connection} with a fresh connection after test tables
69       * have been dropped, so that {@code AbstractDatabaseIT.tearDown()} uses an
70       * up-to-date dataset that does not include the dropped tables.
71       *
72       * @throws Exception
73       */
74      private void refreshConnection() throws Exception
75      {
76          _connection.close();
77          _connection = getDatabaseTester().getConnection();
78          setUpDatabaseConfig(_connection.getConfig());
79      }
80  
81      @Test
82      void testCompositeIteration_withQueryAndDefaultDataSets_writesAllTablesToFile() throws Exception
83      {
84  
85          // 1. QueryDataSet
86          final QueryDataSet queryDataSet = new QueryDataSet(_connection);
87          queryDataSet.addTable("B", "select * from B");
88          queryDataSet.addTable("C", "select * from C");
89  
90          // 2. Hard-coded data set
91          final DefaultDataSet plainDataSet = new DefaultDataSet();
92  
93          final Column id = new Column("id", DataType.DOUBLE);
94          final Column name = new Column("name", DataType.VARCHAR);
95  
96          final Column[] cols = {id, name};
97  
98          final DefaultTable aTable = new DefaultTable("D", cols);
99          final Object[] row1 = {Long.valueOf(1), "D1"};
100         final Object[] row2 = {Long.valueOf(2), "D2"};
101 
102         aTable.addRow(row1);
103         aTable.addRow(row2);
104 
105         plainDataSet.addTable(aTable);
106 
107         // 3. Composite
108         final CompositeDataSet compositeDataSet =
109                 new CompositeDataSet(queryDataSet, plainDataSet);
110 
111         // 4. Write
112         try (OutputStream out = Files.newOutputStream(Paths.get("target/full.xml")))
113         {
114             FlatXmlDataSet.write(compositeDataSet, out);
115         }
116         catch (final Exception e)
117         {
118             fail(e.getMessage());
119         }
120     }
121 }