1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.dbunit.database;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import java.sql.Connection;
27 import java.sql.PreparedStatement;
28 import java.sql.SQLException;
29 import java.util.Objects;
30
31 import org.dbunit.database.statement.IStatementFactory;
32 import org.dbunit.dataset.DataSetException;
33 import org.dbunit.dataset.DefaultDataSet;
34 import org.dbunit.dataset.FilteredDataSet;
35 import org.dbunit.dataset.IDataSet;
36 import org.dbunit.dataset.ITable;
37
38
39
40
41
42
43 public class MockDatabaseConnection implements IDatabaseConnection
44 {
45 private Integer _closeCalls = 0;
46 private Integer _expectedCloseCalls = 0;
47
48 private Connection _connection;
49 private String _schema;
50 private IDataSet _dataSet;
51
52 private DatabaseConfig _databaseConfig = new DatabaseConfig();
53
54 public void setupSchema(final String schema)
55 {
56 _schema = schema;
57 }
58
59 public void setupConnection(final Connection connection)
60 {
61 _connection = connection;
62 }
63
64 public void setupDataSet(final IDataSet dataSet)
65 {
66 _dataSet = dataSet;
67 }
68
69 public void setupDataSet(final ITable table)
70 throws AmbiguousTableNameException
71 {
72 _dataSet = new DefaultDataSet(table);
73 }
74
75 public void setupDataSet(final ITable[] tables)
76 throws AmbiguousTableNameException
77 {
78 _dataSet = new DefaultDataSet(tables);
79 }
80
81 public void setupStatementFactory(final IStatementFactory statementFactory)
82 {
83 _databaseConfig.setProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY,
84 statementFactory);
85 }
86
87
88
89
90
91
92
93 public void setExpectedCloseCalls(final int callsCount)
94 {
95 _expectedCloseCalls = callsCount;
96 }
97
98 public void verify()
99 {
100 if (!Objects.isNull(_expectedCloseCalls))
101 {
102 assertThat(_closeCalls).isEqualTo(_expectedCloseCalls);
103 }
104
105 }
106
107
108
109
110 @Override
111 public Connection getConnection() throws SQLException
112 {
113 return _connection;
114 }
115
116 @Override
117 public String getSchema()
118 {
119 return _schema;
120 }
121
122 @Override
123 public void close() throws SQLException
124 {
125 _closeCalls++;
126 }
127
128 @Override
129 public IDataSet createDataSet() throws SQLException
130 {
131 return _dataSet;
132 }
133
134 @Override
135 public IDataSet createDataSet(final String[] tableNames)
136 throws SQLException, AmbiguousTableNameException
137 {
138 return new FilteredDataSet(tableNames, createDataSet());
139 }
140
141 @Override
142 public ITable createQueryTable(final String resultName, final String sql)
143 throws DataSetException, SQLException
144 {
145 throw new UnsupportedOperationException();
146 }
147
148 @Override
149 public ITable createTable(final String tableName,
150 final PreparedStatement preparedStatement)
151 throws DataSetException, SQLException
152 {
153 throw new UnsupportedOperationException();
154 }
155
156 @Override
157 public ITable createTable(final String tableName)
158 throws DataSetException, SQLException
159 {
160 if (_dataSet == null)
161 {
162 throw new UnsupportedOperationException();
163 }
164 return _dataSet.getTable(tableName);
165 }
166
167 @Override
168 public int getRowCount(final String tableName) throws SQLException
169 {
170 throw new UnsupportedOperationException();
171 }
172
173 @Override
174 public int getRowCount(final String tableName, final String whereClause)
175 throws SQLException
176 {
177 throw new UnsupportedOperationException();
178 }
179
180 @Override
181 public IStatementFactory getStatementFactory()
182 {
183 return (IStatementFactory) _databaseConfig
184 .getProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
185 }
186
187 @Override
188 public DatabaseConfig getConfig()
189 {
190 return _databaseConfig;
191 }
192 }