001 package com.mockrunner.mock.jdbc; 002 003 import java.io.PrintWriter; 004 import java.sql.Connection; 005 import java.sql.SQLException; 006 007 import javax.sql.DataSource; 008 009 /** 010 * Mock implementation of <code>DataSource</code>. 011 */ 012 public class MockDataSource implements DataSource 013 { 014 private Connection connection = null; 015 private int loginTimeout = 0; 016 private PrintWriter logWriter = null; 017 018 /** 019 * Set up the connection. 020 * @param connection the connection 021 */ 022 public void setupConnection(Connection connection) 023 { 024 this.connection = connection; 025 } 026 027 /** 028 * Returns the {@link com.mockrunner.mock.jdbc.MockConnection}. 029 * If the underlying connection is not an instance of 030 * {@link com.mockrunner.mock.jdbc.MockConnection}, 031 * <code>null</code> is returned. 032 * @return the {@link com.mockrunner.mock.jdbc.MockConnection} 033 */ 034 public MockConnection getMockConnection() 035 { 036 if(connection instanceof MockConnection) 037 { 038 return (MockConnection)connection; 039 } 040 return null; 041 } 042 043 public int getLoginTimeout() throws SQLException 044 { 045 return loginTimeout; 046 } 047 048 public void setLoginTimeout(int seconds) throws SQLException 049 { 050 loginTimeout = seconds; 051 } 052 053 public PrintWriter getLogWriter() throws SQLException 054 { 055 return logWriter; 056 } 057 058 public void setLogWriter(PrintWriter out) throws SQLException 059 { 060 logWriter = out; 061 } 062 063 public Connection getConnection() throws SQLException 064 { 065 return connection; 066 } 067 068 public Connection getConnection(String username, String password) throws SQLException 069 { 070 return connection; 071 } 072 073 public boolean isWrapperFor(Class iface) throws SQLException 074 { 075 return false; 076 } 077 078 public Object unwrap(Class iface) throws SQLException 079 { 080 throw new SQLException("No object found for " + iface); 081 } 082 }