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 com.mockobjects.ExpectationCounter;
25 import com.mockobjects.Verifiable;
26 import org.dbunit.database.statement.IStatementFactory;
27 import org.dbunit.dataset.*;
28
29 import java.sql.Connection;
30 import java.sql.PreparedStatement;
31 import java.sql.SQLException;
32
33
34
35
36
37
38 public class MockDatabaseConnection implements IDatabaseConnection, Verifiable
39 {
40 private ExpectationCounter _closeCalls =
41 new ExpectationCounter("MockDatabaseConnection.close");;
42
43 private Connection _connection;
44 private String _schema;
45 private IDataSet _dataSet;
46
47 private DatabaseConfig _databaseConfig = new DatabaseConfig();
48
49 public void setupSchema(String schema)
50 {
51 _schema = schema;
52 }
53
54 public void setupConnection(Connection connection)
55 {
56 _connection = connection;
57 }
58
59 public void setupDataSet(IDataSet dataSet)
60 {
61 _dataSet = dataSet;
62 }
63
64 public void setupDataSet(ITable table) throws AmbiguousTableNameException
65 {
66 _dataSet = new DefaultDataSet(table);
67 }
68
69 public void setupDataSet(ITable[] tables) throws AmbiguousTableNameException
70 {
71 _dataSet = new DefaultDataSet(tables);
72 }
73
74 public void setupStatementFactory(IStatementFactory statementFactory)
75 {
76 _databaseConfig.setProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY, statementFactory);
77 }
78
79
80
81
82
83
84 public void setExpectedCloseCalls(int callsCount)
85 {
86 _closeCalls.setExpected(callsCount);
87 }
88
89
90
91
92 public void verify()
93 {
94 _closeCalls.verify();
95 }
96
97
98
99
100 public Connection getConnection() throws SQLException
101 {
102 return _connection;
103 }
104
105 public String getSchema()
106 {
107 return _schema;
108 }
109
110 public void close() throws SQLException
111 {
112 _closeCalls.inc();
113 }
114
115 public IDataSet createDataSet() throws SQLException
116 {
117 return _dataSet;
118 }
119
120 public IDataSet createDataSet(String[] tableNames) throws SQLException, AmbiguousTableNameException
121 {
122 return new FilteredDataSet(tableNames, createDataSet());
123 }
124
125 public ITable createQueryTable(String resultName, String sql)
126 throws DataSetException, SQLException
127 {
128 throw new UnsupportedOperationException();
129 }
130
131 public ITable createTable(String tableName,
132 PreparedStatement preparedStatement) throws DataSetException,
133 SQLException
134 {
135 throw new UnsupportedOperationException();
136 }
137
138 public ITable createTable(String tableName) throws DataSetException,
139 SQLException
140 {
141 throw new UnsupportedOperationException();
142 }
143
144 public int getRowCount(String tableName) throws SQLException
145 {
146 throw new UnsupportedOperationException();
147 }
148
149 public int getRowCount(String tableName, String whereClause) throws SQLException
150 {
151 throw new UnsupportedOperationException();
152 }
153
154 public IStatementFactory getStatementFactory()
155 {
156 return (IStatementFactory)_databaseConfig.getProperty(
157 DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
158 }
159
160 public DatabaseConfig getConfig()
161 {
162 return _databaseConfig;
163 }
164 }
165
166
167
168
169