MockDataSource.java

/***************************************************************************
   Copyright 2012 Emily Estes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
***************************************************************************/
package net.metanotion.sql;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import javax.sql.DataSource;

/** This is a "null" implementation of a JDBC DataSource. It's not really meant to be useful
other than as a way to test code with non-null instances of DataSource. It would be acceptable
to expand this to a more interesting "mock" implementation of a DataSource, but it mostly consists
of the bare minimum code to "technically" implement the {@link javax.sql.DataSource} interface.
*/
public final class MockDataSource implements DataSource {
	private static final int DEFAULT_TIMEOUT = 30;
	private int logintimeout = DEFAULT_TIMEOUT;
	private PrintWriter printwriter = null;
	@Override public Connection getConnection() { return new MockConnection(); }
	@Override public Connection getConnection(final String username, final String password) { return new MockConnection(); }
	@Override public int getLoginTimeout() { return this.logintimeout; }
	@Override public PrintWriter getLogWriter() { return this.printwriter; }
	@Override public void setLoginTimeout(final int seconds) { this.logintimeout = seconds; }
	@Override public void setLogWriter(final PrintWriter out) { this.printwriter = out; }
	@Override public boolean isWrapperFor(final Class<?> iface) { return false; }
	@Override public <T> T unwrap(final Class<T> iface) throws SQLException { throw new SQLException(); }
	@Override public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		throw new SQLFeatureNotSupportedException();
	}
}