KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > database > MockDatabaseConnection


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.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 JavaDoc;
30 import java.sql.SQLException JavaDoc;
31
32 /**
33  * @author Manuel Laflamme
34  * @version $Revision: 1.10 $
35  * @since Mar 16, 2002
36  */

37 public class MockDatabaseConnection implements IDatabaseConnection, Verifiable
38 {
39     private ExpectationCounter _closeCalls =
40             new ExpectationCounter("MockDatabaseConnection.close");;
41
42     private Connection JavaDoc _connection;
43     private String JavaDoc _schema;
44     private IDataSet _dataSet;
45 // private IStatementFactory _statementFactory;
46
private DatabaseConfig _databaseConfig = new DatabaseConfig();
47
48     public void setupSchema(String JavaDoc schema)
49     {
50         _schema = schema;
51     }
52
53     public void setupConnection(Connection JavaDoc connection)
54     {
55         _connection = connection;
56     }
57
58     public void setupDataSet(IDataSet dataSet)
59     {
60         _dataSet = dataSet;
61     }
62
63     public void setupDataSet(ITable table)
64     {
65         _dataSet = new DefaultDataSet(table);
66     }
67
68     public void setupDataSet(ITable[] tables)
69     {
70         _dataSet = new DefaultDataSet(tables);
71     }
72
73     public void setupStatementFactory(IStatementFactory statementFactory)
74     {
75         _databaseConfig.setProperty(DatabaseConfig.PROPERTY_STATEMENT_FACTORY, statementFactory);
76     }
77
78 // public void setupEscapePattern(String escapePattern)
79
// {
80
// _databaseConfig.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern);
81
// }
82
//
83
public void setExpectedCloseCalls(int callsCount)
84     {
85         _closeCalls.setExpected(callsCount);
86     }
87
88     ///////////////////////////////////////////////////////////////////////////
89
// Verifiable interface
90

91     public void verify()
92     {
93         _closeCalls.verify();
94     }
95
96     ///////////////////////////////////////////////////////////////////////////
97
// IDatabaseConnection interface
98

99     public Connection JavaDoc getConnection() throws SQLException JavaDoc
100     {
101         return _connection;
102     }
103
104     public String JavaDoc getSchema()
105     {
106         return _schema;
107     }
108
109     public void close() throws SQLException JavaDoc
110     {
111         _closeCalls.inc();
112     }
113
114     public IDataSet createDataSet() throws SQLException JavaDoc
115     {
116         return _dataSet;
117     }
118
119     public IDataSet createDataSet(String JavaDoc[] tableNames) throws SQLException JavaDoc
120     {
121         return new FilteredDataSet(tableNames, createDataSet());
122     }
123
124     public ITable createQueryTable(String JavaDoc resultName, String JavaDoc sql)
125             throws DataSetException, SQLException JavaDoc
126     {
127         throw new UnsupportedOperationException JavaDoc();
128     }
129
130     public int getRowCount(String JavaDoc tableName) throws SQLException JavaDoc
131     {
132         throw new UnsupportedOperationException JavaDoc();
133     }
134
135     public int getRowCount(String JavaDoc tableName, String JavaDoc whereClause) throws SQLException JavaDoc
136     {
137         throw new UnsupportedOperationException JavaDoc();
138     }
139
140     public IStatementFactory getStatementFactory()
141     {
142         return (IStatementFactory)_databaseConfig.getProperty(
143                 DatabaseConfig.PROPERTY_STATEMENT_FACTORY);
144     }
145
146     public DatabaseConfig getConfig()
147     {
148         return _databaseConfig;
149     }
150 }
151
152
153
154
155
156
Popular Tags