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.database.search;
22  
23  import static org.assertj.core.api.Assertions.assertThat;
24  
25  import java.sql.SQLException;
26  import java.util.Set;
27  
28  import org.dbunit.AbstractDatabaseIT;
29  import org.dbunit.DdlExecutor;
30  import org.dbunit.database.IDatabaseConnection;
31  import org.dbunit.testutil.TestUtils;
32  import org.dbunit.util.CollectionsHelper;
33  import org.dbunit.util.search.DepthFirstSearch;
34  import org.dbunit.util.search.ISearchCallback;
35  import org.junit.jupiter.api.AfterEach;
36  import org.junit.jupiter.api.BeforeEach;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * @author Felipe Leme (dbunit@felipeal.net)
41   * @version $Revision$
42   * @since Aug 28, 2005
43   */
44  public abstract class AbstractMetaDataBasedSearchCallbackTestCase
45          extends AbstractDatabaseIT
46  {
47  
48      private String sqlFile;
49  
50      public void setSqlFile(final String sqlFile)
51      {
52          this.sqlFile = sqlFile;
53      }
54  
55      @BeforeEach
56      public void setUpConnectionWithFile() throws Exception
57      {
58          this.sqlFile = getSqlFile();
59          DdlExecutor.dropTables(_connection.getConnection(), getTestTableNames());
60          DdlExecutor.executeDdlFile(TestUtils.getFile("sql/" + this.sqlFile),
61                  _connection.getConnection(), false);
62      }
63  
64      @AfterEach
65      protected void tearDownTables() throws Exception
66      {
67          DdlExecutor.dropTables(_connection.getConnection(), getTestTableNames());
68          refreshConnection();
69      }
70  
71      /**
72       * Replaces {@code _connection} with a fresh connection after test tables
73       * have been dropped, so that {@code AbstractDatabaseIT.tearDown()} uses an
74       * up-to-date dataset cache that does not include the dropped tables.
75       *
76       * @throws Exception
77       */
78      protected void refreshConnection() throws Exception
79      {
80          _connection.close();
81          _connection = getDatabaseTester().getConnection();
82          setUpDatabaseConfig(_connection.getConfig());
83      }
84  
85      protected abstract String getSqlFile();
86  
87      protected abstract String[] getTestTableNames();
88  
89      protected IDatabaseConnection getConnection()
90      {
91          return _connection;
92      }
93  
94      protected abstract String[][] getInput();
95  
96      protected abstract String[][] getExpectedOutput();
97  
98      protected abstract AbstractMetaDataBasedSearchCallback getCallback(
99              IDatabaseConnection connection2);
100 
101     @Test
102     void testSearch_withAllDefinedInputCombinations_returnsExpectedOutputForEach() throws Exception
103     {
104         final IDatabaseConnection connection = getConnection();
105         final boolean storesLowerCase = connection.getConnection().getMetaData()
106                 .storesLowerCaseIdentifiers();
107 
108         final String[][] allInput = getInput();
109         final String[][] allExpectedOutput = getExpectedOutput();
110         final ISearchCallback callback = getCallback(connection);
111         for (int i = 0; i < allInput.length; i++)
112         {
113             final String[] input = allInput[i];
114             final String[] normalizedInput;
115             if (storesLowerCase)
116             {
117                 normalizedInput = new String[input.length];
118                 for (int j = 0; j < input.length; j++)
119                 {
120                     normalizedInput[j] = input[j].toLowerCase();
121                 }
122             }
123             else
124             {
125                 normalizedInput = input;
126             }
127             final String[] expectedOutput = allExpectedOutput[i];
128             final DepthFirstSearch search = new DepthFirstSearch();
129             final Set result = search.search(normalizedInput, callback);
130             final String[] actualOutput =
131                     CollectionsHelper.setToStrings(result);
132             assertThat(actualOutput).as("output didn't match for i=" + i + ".")
133                     .usingElementComparator(String.CASE_INSENSITIVE_ORDER)
134                     .containsExactly(expectedOutput);
135         }
136     }
137 
138 }