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.assertj.core.api.Assertions.assertThat;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import java.util.Arrays;
28  import java.util.LinkedList;
29  import java.util.List;
30  import java.util.stream.Collectors;
31  
32  import org.dbunit.database.AmbiguousTableNameException;
33  import org.junit.jupiter.api.Test;
34  
35  /**
36   * @author Manuel Laflamme
37   * @version $Revision$
38   * @since Feb 22, 2002
39   */
40  public abstract class AbstractDataSetTest extends AbstractTest
41  {
42  
43      protected int[] getExpectedDuplicateRows()
44      {
45          return new int[] {1, 0, 2};
46      }
47  
48      /**
49       * This method exclude BLOB_TABLE and CLOB_TABLE from the specified dataset
50       * because BLOB and CLOB are not supported by all database vendor. It also
51       * excludes tables with Identity columns (MSSQL) because they are specific
52       * to MSSQL. TODO : should be refactored into the various
53       * DatabaseEnvironments!
54       */
55      public static IDataSet removeExtraTestTables(final IDataSet dataSet)
56              throws Exception
57      {
58          String[] names = dataSet.getTableNames();
59  
60          // exclude BLOB_TABLE and CLOB_TABLE from test since not supported by
61          // all database vendor
62          final List<String> nameList = new LinkedList<>(Arrays.asList(names));
63          nameList.remove("BLOB_TABLE");
64          nameList.remove("CLOB_TABLE");
65          nameList.remove("SDO_GEOMETRY_TABLE");
66          nameList.remove("XML_TYPE_TABLE");
67          nameList.remove("DBUNIT.BLOB_TABLE");
68          nameList.remove("DBUNIT.CLOB_TABLE");
69          nameList.remove("DBUNIT.SDO_GEOMETRY");
70          nameList.remove("DBUNIT.XML_TYPE_TABLE");
71          /*
72           * this table shows up on MSSQLServer. It is a user table for storing
73           * diagram information that really should be considered a system table.
74           */
75          nameList.remove("DBUNIT.dtproperties");
76          nameList.remove("dtproperties");
77          /*
78           * these noted in mcr.microsoft.com/mssql/server:2019-latest
79           */
80          nameList.remove("MSreplication_options");
81          final List<String> removeList = nameList.stream()
82                  .filter(t -> t.startsWith("spt")).collect(Collectors.toList());
83          nameList.removeAll(removeList);
84          /*
85           * These tables are created specifically for testing identity columns on
86           * MSSQL server. They should be ignored on other platforms.
87           */
88          nameList.remove("DBUNIT.IDENTITY_TABLE");
89          nameList.remove("IDENTITY_TABLE");
90          nameList.remove("DBUNIT.TEST_IDENTITY_NOT_PK");
91          nameList.remove("TEST_IDENTITY_NOT_PK");
92  
93          names = nameList.toArray(new String[0]);
94  
95          return new FilteredDataSet(names, dataSet);
96      }
97  
98      protected abstract IDataSet createDataSet() throws Exception;
99  
100     protected abstract IDataSet createDuplicateDataSet() throws Exception;
101 
102     /**
103      * Create a dataset with duplicate tables having different char case in name
104      * 
105      * @return
106      */
107     protected abstract IDataSet createMultipleCaseDuplicateDataSet()
108             throws Exception;
109 
110     protected void assertEqualsTableName(final String mesage,
111             final String expected, final String actual)
112     {
113         assertThat(actual).as(mesage).isEqualTo(expected);
114     }
115 
116     @Test
117     void testGetTableNames() throws Exception
118     {
119         final String[] expected = getExpectedNames();
120         assertContainsIgnoreCase("minimal names subset",
121                 super.getExpectedNames(), expected);
122 
123         final IDataSet dataSet = createDataSet();
124         final String[] names = dataSet.getTableNames();
125 
126         assertThat(names).as("table count").hasSize(expected.length);
127         for (int i = 0; i < expected.length; i++)
128         {
129             assertEqualsTableName("name " + i, expected[i], names[i]);
130         }
131     }
132 
133     @Test
134     void testGetTableNamesDefensiveCopy() throws Exception
135     {
136         final IDataSet dataSet = createDataSet();
137         assertThat(dataSet.getTableNames()).as("Should not be same intance")
138                 .isNotSameAs(dataSet.getTableNames());
139     }
140 
141     @Test
142     void testGetTable() throws Exception
143     {
144         final String[] expected = getExpectedNames();
145 
146         final IDataSet dataSet = createDataSet();
147         for (int i = 0; i < expected.length; i++)
148         {
149             final ITable table = dataSet.getTable(expected[i]);
150             assertEqualsTableName("name " + i, expected[i],
151                     table.getTableMetaData().getTableName());
152         }
153     }
154 
155     @Test
156     void testGetUnknownTable() throws Exception
157     {
158         final IDataSet dataSet = createDataSet();
159         assertThrows(NoSuchTableException.class,
160                 () -> dataSet.getTable("UNKNOWN_TABLE"),
161                 "Should throw a NoSuchTableException");
162 
163     }
164 
165     @Test
166     void testGetTableMetaData() throws Exception
167     {
168         final String[] expected = getExpectedNames();
169 
170         final IDataSet dataSet = createDataSet();
171         for (int i = 0; i < expected.length; i++)
172         {
173             final ITableMetaData metaData =
174                     dataSet.getTableMetaData(expected[i]);
175             assertEqualsTableName("name " + i, expected[i],
176                     metaData.getTableName());
177         }
178     }
179 
180     @Test
181     void testGetUnknownTableMetaData() throws Exception
182     {
183         final IDataSet dataSet = createDataSet();
184         assertThrows(NoSuchTableException.class,
185                 () -> dataSet.getTableMetaData("UNKNOWN_TABLE"),
186                 "Should throw a NoSuchTableException");
187     }
188 
189     @Test
190     void testGetTables() throws Exception
191     {
192         final String[] expected = getExpectedNames();
193         assertContainsIgnoreCase("minimal names subset",
194                 super.getExpectedNames(), expected);
195 
196         final IDataSet dataSet = createDataSet();
197         final ITable[] tables = dataSet.getTables();
198         assertThat(tables).as("table count").hasSize(expected.length);
199         for (int i = 0; i < expected.length; i++)
200         {
201             assertEqualsTableName("name " + i, expected[i],
202                     tables[i].getTableMetaData().getTableName());
203         }
204     }
205 
206     @Test
207     public void testGetTablesDefensiveCopy() throws Exception
208     {
209         final IDataSet dataSet = createDataSet();
210         assertThat(dataSet.getTables()).as("Should not be same instance")
211                 .isNotSameAs(dataSet.getTables());
212     }
213 
214     @Test
215     public void testCreateDuplicateDataSet() throws Exception
216     {
217         assertThrows(AmbiguousTableNameException.class, () ->
218         /* IDataSet dataSet = */createDuplicateDataSet(),
219                 "Should throw AmbiguousTableNameException in creation phase");
220 
221     }
222 
223     @Test
224     public void testCreateMultipleCaseDuplicateDataSet() throws Exception
225     {
226         assertThrows(AmbiguousTableNameException.class, () ->
227         /* IDataSet dataSet = */createMultipleCaseDuplicateDataSet(),
228                 "Should throw AmbiguousTableNameException in creation phase");
229     }
230 
231     @Test
232     public void testGetCaseInsensitiveTable() throws Exception
233     {
234         final String[] expectedNames = getExpectedLowerNames();
235 
236         final IDataSet dataSet = createDataSet();
237         for (int i = 0; i < expectedNames.length; i++)
238         {
239             final String expected = expectedNames[i];
240             final ITable table = dataSet.getTable(expected);
241             final String actual = table.getTableMetaData().getTableName();
242 
243             if (!expected.equalsIgnoreCase(actual))
244             {
245                 assertThat(actual).as("name " + i).isEqualTo(expected);
246             }
247         }
248     }
249 
250     @Test
251     public void testGetCaseInsensitiveTableMetaData() throws Exception
252     {
253         final String[] expectedNames = getExpectedLowerNames();
254         final IDataSet dataSet = createDataSet();
255 
256         for (int i = 0; i < expectedNames.length; i++)
257         {
258             final String expected = expectedNames[i];
259             final ITableMetaData metaData = dataSet.getTableMetaData(expected);
260             final String actual = metaData.getTableName();
261 
262             if (!expected.equalsIgnoreCase(actual))
263             {
264                 assertThat(actual).as("name " + i).isEqualTo(expected);
265             }
266         }
267     }
268 
269     @Test
270     void testIterator() throws Exception
271     {
272         final String[] expected = getExpectedNames();
273         assertContainsIgnoreCase("minimal names subset",
274                 super.getExpectedNames(), expected);
275 
276         int i = 0;
277         final ITableIterator iterator = createDataSet().iterator();
278         while (iterator.next())
279         {
280             assertEqualsTableName("name " + i, expected[i],
281                     iterator.getTableMetaData().getTableName());
282             i++;
283         }
284         assertThat(i).as("table count").isEqualTo(expected.length);
285     }
286 
287     @Test
288     void testReverseIterator() throws Exception
289     {
290         final String[] expected =
291                 DataSetUtils.reverseStringArray(getExpectedNames());
292         assertContainsIgnoreCase("minimal names subset",
293                 super.getExpectedNames(), expected);
294 
295         int i = 0;
296         final ITableIterator iterator = createDataSet().reverseIterator();
297         while (iterator.next())
298         {
299             assertEqualsTableName("name " + i, expected[i],
300                     iterator.getTableMetaData().getTableName());
301             i++;
302         }
303         assertThat(i).as("table count").isEqualTo(expected.length);
304     }
305 
306     protected ITable[] createDuplicateTables(final boolean multipleCase)
307             throws AmbiguousTableNameException
308     {
309         final ITable table1 = new DefaultTable("DUPLICATE_TABLE");
310         final ITable table2 = new DefaultTable("EMPTY_TABLE");
311         ITable table3;
312         if (!multipleCase)
313         {
314             table3 = new DefaultTable("DUPLICATE_TABLE");
315         } else
316         {
317             table3 = new DefaultTable("duplicate_TABLE");
318         }
319         final ITable[] tables = new ITable[] {table1, table2, table3};
320         return tables;
321     }
322 }