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  
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   * @author Manuel Laflamme
35   * @version $Revision$
36   * @since Mar 16, 2002
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  //    private IStatementFactory _statementFactory;
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  //    public void setupEscapePattern(String escapePattern)
80  //    {
81  //        _databaseConfig.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN, escapePattern);
82  //    }
83  //
84      public void setExpectedCloseCalls(int callsCount)
85      {
86          _closeCalls.setExpected(callsCount);
87      }
88  
89      ///////////////////////////////////////////////////////////////////////////
90      // Verifiable interface
91  
92      public void verify()
93      {
94          _closeCalls.verify();
95      }
96  
97      ///////////////////////////////////////////////////////////////////////////
98      // IDatabaseConnection interface
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