1 24 25 package org.objectweb.cjdbc.scenario.tools.mock; 26 27 import java.sql.Connection ; 28 import java.sql.ResultSet ; 29 import java.sql.SQLException ; 30 import java.util.ArrayList ; 31 import java.util.Hashtable ; 32 import java.util.Iterator ; 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 48 public class MockConnectionManager extends AbstractConnectionManager 49 { 50 51 private MockConnection2 connection; 52 53 54 private boolean isInitialized; 55 56 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 72 protected Object clone() throws CloneNotSupportedException 73 { 74 return null; 75 } 76 77 80 public void finalizeConnections() throws SQLException 81 { 82 isInitialized = false; 83 } 84 85 88 public Connection getConnection() 89 { 90 return connection; 91 } 92 93 96 public void initializeConnections() throws SQLException 97 { 98 isInitialized = true; 99 } 100 101 104 public boolean isInitialized() 105 { 106 return isInitialized; 107 } 108 109 112 public void releaseConnection(Connection connection) 113 { 114 } 115 116 119 public void deleteConnection(Connection connection) 120 { 121 } 122 123 126 public int getCurrentNumberOfConnections() 127 { 128 return 0; 129 } 130 131 134 public String getXmlImpl() 135 { 136 return null; 137 } 138 139 142 public class MockDatabaseMetaData 143 extends 144 com.mockobjects.sql.MockDatabaseMetaData 145 { 146 147 private MockMultiRowResultSet tables; 148 149 153 private Hashtable columnsHashtable; 154 155 159 private Hashtable primaryKeysHashtable; 160 161 166 public MockDatabaseMetaData(AbstractDatabase database) 167 { 168 ArrayList array1, array2, array3; 169 MockMultiRowResultSet resultSet; 170 DatabaseTable table; 171 DatabaseColumn column; 172 Object [][] expected; 173 Iterator it1, it2; 174 175 tables = new MockMultiRowResultSet(); 177 columnsHashtable = new Hashtable (); 178 primaryKeysHashtable = new Hashtable (); 179 array1 = new ArrayList (); 180 it1 = database.getSchema().getTables().iterator(); 181 while (it1.hasNext()) 182 { 183 table = (DatabaseTable) it1.next(); 184 array1.add(new Object []{null, null, table.getName(), null}); 185 186 array2 = new ArrayList (); 189 array3 = new ArrayList (); 190 it2 = table.getColumns().iterator(); 191 while (it2.hasNext()) 192 { 193 column = (DatabaseColumn) it2.next(); 194 array2.add(new Object []{null, null, table.getName(), 195 column.getName(), 196 new Short (new Integer (column.getType()).shortValue())}); 197 198 if (column.isUnique()) 199 array3.add(new Object []{null, null, table.getName(), 200 column.getName()}); 201 202 } 203 204 expected = new Object [array2.size()][5]; 206 for (int i = 0; i < array2.size(); i++) 207 { 208 expected[i] = (Object []) array2.get(i); 209 } 210 resultSet = new MockMultiRowResultSet(); 212 resultSet.setupRows(expected); 213 columnsHashtable.put(table.getName(), resultSet); 214 215 expected = new Object [array3.size()][5]; 217 for (int i = 0; i < array3.size(); i++) 218 { 219 expected[i] = (Object []) array3.get(i); 220 } 221 resultSet = new MockMultiRowResultSet(); 223 resultSet.setupRows(expected); 224 primaryKeysHashtable.put(table.getName(), resultSet); 225 226 } 227 228 expected = new Object [array1.size()][4]; 230 for (int i = 0; i < array1.size(); i++) 231 { 232 expected[i] = (Object []) array1.get(i); 233 } 234 tables.setupRows(expected); 236 } 237 238 244 public ResultSet getColumns(String catalog, String schemaPattern, 245 String tableNamePattern, String columnNamePattern) throws SQLException 246 { 247 if (columnNamePattern.equals("%")) 248 return (ResultSet ) columnsHashtable.get(tableNamePattern); 249 else 250 return null; 251 } 252 253 259 public ResultSet getPrimaryKeys(String catalog, String schema, String table) 260 throws SQLException 261 { 262 return (ResultSet ) primaryKeysHashtable.get(table); 263 } 264 265 271 public ResultSet getTables(String catalog, String schemaPattern, 272 String tableNamePattern, String [] types) throws SQLException 273 { 274 if (tableNamePattern.equals("%")) 275 return tables; 276 else 277 return null; 278 } 279 } 280 } 281 | Popular Tags |