| 1 |
/* |
| 2 |
* Copyright (C) 2013 Brett Wooldridge |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.zaxxer.hikari.mocks; |
| 18 |
|
| 19 |
import com.zaxxer.hikari.util.UtilityElf; |
| 20 |
|
| 21 |
import java.io.PrintWriter; |
| 22 |
import java.sql.Connection; |
| 23 |
import java.sql.SQLException; |
| 24 |
import java.sql.SQLFeatureNotSupportedException; |
| 25 |
import java.util.logging.Logger; |
| 26 |
|
| 27 |
import javax.sql.DataSource; |
| 28 |
|
| 29 |
/** |
| 30 |
* |
| 31 |
* @author Brett Wooldridge |
| 32 |
*/ |
| 33 |
public class StubDataSource implements DataSource |
| 34 |
{ |
| 35 |
private String user; |
| 36 |
private String password; |
| 37 |
private PrintWriter logWriter; |
| 38 |
private SQLException throwException; |
| 39 |
private long connectionAcquistionTime = 0; |
| 40 |
private int loginTimeout; |
| 41 |
|
| 42 |
public String getUser() |
| 43 |
{ |
| 44 |
return user; |
| 45 |
} |
| 46 |
|
| 47 |
public void setUser(String user) |
| 48 |
{ |
| 49 |
this.user = user; |
| 50 |
} |
| 51 |
|
| 52 |
public String getPassword() |
| 53 |
{ |
| 54 |
return password; |
| 55 |
} |
| 56 |
|
| 57 |
public void setPassword(String password) |
| 58 |
{ |
| 59 |
this.password = password; |
| 60 |
} |
| 61 |
|
| 62 |
public void setURL(String url) |
| 63 |
{ |
| 64 |
// we don't care |
| 65 |
} |
| 66 |
|
| 67 |
/** {@inheritDoc} */ |
| 68 |
@Override |
| 69 |
public PrintWriter getLogWriter() throws SQLException |
| 70 |
{ |
| 71 |
return logWriter; |
| 72 |
} |
| 73 |
|
| 74 |
/** {@inheritDoc} */ |
| 75 |
@Override |
| 76 |
public void setLogWriter(PrintWriter out) throws SQLException |
| 77 |
{ |
| 78 |
this.logWriter = out; |
| 79 |
} |
| 80 |
|
| 81 |
/** {@inheritDoc} */ |
| 82 |
@Override |
| 83 |
public void setLoginTimeout(int seconds) throws SQLException |
| 84 |
{ |
| 85 |
this.loginTimeout = seconds; |
| 86 |
} |
| 87 |
|
| 88 |
/** {@inheritDoc} */ |
| 89 |
@Override |
| 90 |
public int getLoginTimeout() throws SQLException |
| 91 |
{ |
| 92 |
return loginTimeout; |
| 93 |
} |
| 94 |
|
| 95 |
/** {@inheritDoc} */ |
| 96 |
public Logger getParentLogger() throws SQLFeatureNotSupportedException |
| 97 |
{ |
| 98 |
return null; |
| 99 |
} |
| 100 |
|
| 101 |
/** {@inheritDoc} */ |
| 102 |
@SuppressWarnings("unchecked") |
| 103 |
@Override |
| 104 |
public <T> T unwrap(Class<T> iface) throws SQLException |
| 105 |
{ |
| 106 |
if (iface.isInstance(this)) { |
| 107 |
return (T) this; |
| 108 |
} |
| 109 |
|
| 110 |
throw new SQLException("Wrapped DataSource is not an instance of " + iface); |
| 111 |
} |
| 112 |
|
| 113 |
/** {@inheritDoc} */ |
| 114 |
@Override |
| 115 |
public boolean isWrapperFor(Class<?> iface) throws SQLException |
| 116 |
{ |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
/** {@inheritDoc} */ |
| 121 |
@Override |
| 122 |
public Connection getConnection() throws SQLException |
| 123 |
{ |
| 124 |
if (throwException != null) { |
| 125 |
throw throwException; |
| 126 |
} |
| 127 |
if (connectionAcquistionTime > 0) { |
| 128 |
UtilityElf.quietlySleep(connectionAcquistionTime); |
| 129 |
} |
| 130 |
|
| 131 |
return new StubConnection(); |
| 132 |
} |
| 133 |
|
| 134 |
/** {@inheritDoc} */ |
| 135 |
@Override |
| 136 |
public Connection getConnection(String username, String password) throws SQLException |
| 137 |
{ |
| 138 |
return new StubConnection(); |
| 139 |
} |
| 140 |
|
| 141 |
public void setThrowException(SQLException e) |
| 142 |
{ |
| 143 |
this.throwException = e; |
| 144 |
} |
| 145 |
|
| 146 |
public void setConnectionAcquistionTime(long connectionAcquisitionTime) { |
| 147 |
this.connectionAcquistionTime = connectionAcquisitionTime; |
| 148 |
} |
| 149 |
} |