KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > scenario > tools > mock > MockConnectionManager


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Mathieu Peltier.
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.cjdbc.scenario.tools.mock;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.ResultSet JavaDoc;
29 import java.sql.SQLException JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.util.Hashtable JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.objectweb.cjdbc.common.sql.schema.DatabaseColumn;
35 import org.objectweb.cjdbc.common.sql.schema.DatabaseTable;
36 import org.objectweb.cjdbc.controller.connection.AbstractConnectionManager;
37 import org.objectweb.cjdbc.scenario.tools.databases.AbstractDatabase;
38
39 import com.mockobjects.sql.MockConnection2;
40 import com.mockobjects.sql.MockMultiRowResultSet;
41
42 /**
43  * Mock connection manager.
44  *
45  * @author <a HREF="mailto:Mathieu.Peltier@inrialpes.fr">Mathieu Peltier </a>
46  * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager
47  */

48 public class MockConnectionManager extends AbstractConnectionManager
49 {
50   /** Fake connection. */
51   private MockConnection2 connection;
52
53   /** <code>true</code> if the connections have been initialized. */
54   private boolean isInitialized;
55
56   /**
57    * Creates a new <code>MockConnectionManager</code> instance.
58    *
59    * @param database <code>AbstractDatabase</code> instance.
60    */

61   public MockConnectionManager(AbstractDatabase database)
62   {
63     super("", "", "", "", null, null);
64     isInitialized = false;
65     connection = new MockConnection2();
66     connection.setupMetaData(new MockDatabaseMetaData(database));
67   }
68
69   /**
70    * @see java.lang.Object#clone()
71    */

72   protected Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
73   {
74     return null;
75   }
76
77   /**
78    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#finalizeConnections()
79    */

80   public void finalizeConnections() throws SQLException JavaDoc
81   {
82     isInitialized = false;
83   }
84
85   /**
86    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getConnection()
87    */

88   public Connection JavaDoc getConnection()
89   {
90     return connection;
91   }
92
93   /**
94    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#initializeConnections()
95    */

96   public void initializeConnections() throws SQLException JavaDoc
97   {
98     isInitialized = true;
99   }
100
101   /**
102    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#isInitialized()
103    */

104   public boolean isInitialized()
105   {
106     return isInitialized;
107   }
108
109   /**
110    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#releaseConnection(java.sql.Connection)
111    */

112   public void releaseConnection(Connection JavaDoc connection)
113   {
114   }
115
116   /**
117    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#deleteConnection(java.sql.Connection)
118    */

119   public void deleteConnection(Connection JavaDoc connection)
120   {
121   }
122
123   /**
124    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getCurrentNumberOfConnections()
125    */

126   public int getCurrentNumberOfConnections()
127   {
128     return 0;
129   }
130
131   /**
132    * @see org.objectweb.cjdbc.controller.connection.AbstractConnectionManager#getXmlImpl()
133    */

134   public String JavaDoc getXmlImpl()
135   {
136     return null;
137   }
138
139   /**
140    * Mock database meta data.
141    */

142   public class MockDatabaseMetaData
143       extends
144         com.mockobjects.sql.MockDatabaseMetaData
145   {
146     /** Database columns <code>ResultSet</code>. */
147     private MockMultiRowResultSet tables;
148
149     /**
150      * Hashtable of database columns <code>ResultSet</code> hashed by the
151      * table name.
152      */

153     private Hashtable JavaDoc columnsHashtable;
154
155     /**
156      * Hashtable of primary keys <code>ResultSet</code> hashed by the table
157      * name.
158      */

159     private Hashtable JavaDoc primaryKeysHashtable;
160
161     /**
162      * Creates a new <code>MockDatabaseMetaData</code> instance.
163      *
164      * @param database <code>AbstractDatabase</code> instance.
165      */

166     public MockDatabaseMetaData(AbstractDatabase database)
167     {
168       ArrayList JavaDoc array1, array2, array3;
169       MockMultiRowResultSet resultSet;
170       DatabaseTable table;
171       DatabaseColumn column;
172       Object JavaDoc[][] expected;
173       Iterator JavaDoc it1, it2;
174
175       // Set expected tables, columns and primaryKeys ResultSet
176
tables = new MockMultiRowResultSet();
177       columnsHashtable = new Hashtable JavaDoc();
178       primaryKeysHashtable = new Hashtable JavaDoc();
179       array1 = new ArrayList JavaDoc();
180       it1 = database.getSchema().getTables().iterator();
181       while (it1.hasNext())
182       {
183         table = (DatabaseTable) it1.next();
184         array1.add(new Object JavaDoc[]{null, null, table.getName(), null});
185
186         // Set expected columns and primaryKeys ResultSet for the table
187
// table.getName()
188
array2 = new ArrayList JavaDoc();
189         array3 = new ArrayList JavaDoc();
190         it2 = table.getColumns().iterator();
191         while (it2.hasNext())
192         {
193           column = (DatabaseColumn) it2.next();
194           array2.add(new Object JavaDoc[]{null, null, table.getName(),
195               column.getName(),
196               new Short JavaDoc(new Integer JavaDoc(column.getType()).shortValue())});
197
198           if (column.isUnique())
199             array3.add(new Object JavaDoc[]{null, null, table.getName(),
200                 column.getName()});
201
202         }
203
204         // Transform array2 to Object[][]
205
expected = new Object JavaDoc[array2.size()][5];
206         for (int i = 0; i < array2.size(); i++)
207         {
208           expected[i] = (Object JavaDoc[]) array2.get(i);
209         }
210         // Set columns ResultSet
211
resultSet = new MockMultiRowResultSet();
212         resultSet.setupRows(expected);
213         columnsHashtable.put(table.getName(), resultSet);
214
215         // Transform array3 to Object[][]
216
expected = new Object JavaDoc[array3.size()][5];
217         for (int i = 0; i < array3.size(); i++)
218         {
219           expected[i] = (Object JavaDoc[]) array3.get(i);
220         }
221         // Set primaryKeys ResultSet
222
resultSet = new MockMultiRowResultSet();
223         resultSet.setupRows(expected);
224         primaryKeysHashtable.put(table.getName(), resultSet);
225
226       }
227
228       // Transform array1 to Object[][]
229
expected = new Object JavaDoc[array1.size()][4];
230       for (int i = 0; i < array1.size(); i++)
231       {
232         expected[i] = (Object JavaDoc[]) array1.get(i);
233       }
234       // Set tables ResultSet
235
tables.setupRows(expected);
236     }
237
238     /**
239      * Miminal implementation for <code>DatabaseBackendMetaDataTest</code>.
240      *
241      * @see java.sql.DatabaseMetaData#getColumns(java.lang.String,
242      * java.lang.String, java.lang.String, java.lang.String)
243      */

244     public ResultSet JavaDoc getColumns(String JavaDoc catalog, String JavaDoc schemaPattern,
245         String JavaDoc tableNamePattern, String JavaDoc columnNamePattern) throws SQLException JavaDoc
246     {
247       if (columnNamePattern.equals("%"))
248         return (ResultSet JavaDoc) columnsHashtable.get(tableNamePattern);
249       else
250         return null;
251     }
252
253     /**
254      * Miminal implementation for <code>DatabaseBackendMetaDataTest</code>.
255      *
256      * @see java.sql.DatabaseMetaData#getPrimaryKeys(java.lang.String,
257      * java.lang.String, java.lang.String)
258      */

259     public ResultSet JavaDoc getPrimaryKeys(String JavaDoc catalog, String JavaDoc schema, String JavaDoc table)
260         throws SQLException JavaDoc
261     {
262       return (ResultSet JavaDoc) primaryKeysHashtable.get(table);
263     }
264
265     /**
266      * Miminal implementation for <code>DatabaseBackendMetaDataTest</code>.
267      *
268      * @see java.sql.DatabaseMetaData#getTables(java.lang.String,
269      * java.lang.String, java.lang.String, java.lang.String[])
270      */

271     public ResultSet JavaDoc getTables(String JavaDoc catalog, String JavaDoc schemaPattern,
272         String JavaDoc tableNamePattern, String JavaDoc[] types) throws SQLException JavaDoc
273     {
274       if (tableNamePattern.equals("%"))
275         return tables;
276       else
277         return null;
278     }
279   }
280 }
281
Popular Tags