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 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   * @author Manuel Laflamme
40   * @version $Revision$
41   * @since Mar 16, 2002
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      // private IStatementFactory _statementFactory;
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      // public void setupEscapePattern(String escapePattern)
88      // {
89      // _databaseConfig.setProperty(DatabaseConfig.PROPERTY_ESCAPE_PATTERN,
90      // escapePattern);
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     // IDatabaseConnection interface
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 }