1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
41
42
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
73
74
75
76
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 }