| 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 static org.mockito.ArgumentMatchers.anyInt; |
| 20 |
import static org.mockito.ArgumentMatchers.anyString; |
| 21 |
import static org.mockito.ArgumentMatchers.any; |
| 22 |
import static org.mockito.Mockito.doAnswer; |
| 23 |
import static org.mockito.Mockito.mock; |
| 24 |
import static org.mockito.Mockito.when; |
| 25 |
|
| 26 |
import java.io.PrintWriter; |
| 27 |
import java.sql.CallableStatement; |
| 28 |
import java.sql.Connection; |
| 29 |
import java.sql.PreparedStatement; |
| 30 |
import java.sql.ResultSet; |
| 31 |
import java.sql.SQLException; |
| 32 |
import java.sql.SQLFeatureNotSupportedException; |
| 33 |
import java.sql.Statement; |
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
import javax.sql.DataSource; |
| 37 |
|
| 38 |
import org.mockito.invocation.InvocationOnMock; |
| 39 |
import org.mockito.stubbing.Answer; |
| 40 |
|
| 41 |
/** |
| 42 |
* |
| 43 |
* @author Brett Wooldridge |
| 44 |
*/ |
| 45 |
public class MockDataSource implements DataSource |
| 46 |
{ |
| 47 |
@Override |
| 48 |
public Connection getConnection() throws SQLException |
| 49 |
{ |
| 50 |
return createMockConnection(); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Connection getConnection(String username, String password) throws SQLException |
| 55 |
{ |
| 56 |
return getConnection(); |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public PrintWriter getLogWriter() throws SQLException |
| 61 |
{ |
| 62 |
return null; |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public void setLogWriter(PrintWriter out) throws SQLException |
| 67 |
{ |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public void setLoginTimeout(int seconds) throws SQLException |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public int getLoginTimeout() throws SQLException |
| 77 |
{ |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
public Logger getParentLogger() throws SQLFeatureNotSupportedException |
| 82 |
{ |
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public <T> T unwrap(Class<T> iface) throws SQLException |
| 88 |
{ |
| 89 |
return null; |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public boolean isWrapperFor(Class<?> iface) throws SQLException |
| 94 |
{ |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
public static Connection createMockConnection() throws SQLException { |
| 99 |
// Setup mock connection |
| 100 |
final Connection mockConnection = mock(Connection.class); |
| 101 |
|
| 102 |
// Autocommit is always true by default |
| 103 |
when(mockConnection.getAutoCommit()).thenReturn(true); |
| 104 |
|
| 105 |
// Handle Connection.createStatement() |
| 106 |
Statement statement = mock(Statement.class); |
| 107 |
when(mockConnection.createStatement()).thenReturn(statement); |
| 108 |
when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement); |
| 109 |
when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement); |
| 110 |
when(mockConnection.isValid(anyInt())).thenReturn(true); |
| 111 |
|
| 112 |
// Handle Connection.prepareStatement() |
| 113 |
PreparedStatement mockPreparedStatement = mock(PreparedStatement.class); |
| 114 |
when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement); |
| 115 |
when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement); |
| 116 |
when(mockConnection.prepareStatement(anyString(), any(int[].class))).thenReturn(mockPreparedStatement); |
| 117 |
when(mockConnection.prepareStatement(anyString(), any(String[].class))).thenReturn(mockPreparedStatement); |
| 118 |
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement); |
| 119 |
when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement); |
| 120 |
doAnswer(new Answer<Void>() { |
| 121 |
@Override |
| 122 |
public Void answer(InvocationOnMock invocation) throws Throwable |
| 123 |
{ |
| 124 |
return null; |
| 125 |
} |
| 126 |
}).doNothing().when(mockPreparedStatement).setInt(anyInt(), anyInt()); |
| 127 |
|
| 128 |
ResultSet mockResultSet = mock(ResultSet.class); |
| 129 |
when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet); |
| 130 |
when(mockResultSet.getString(anyInt())).thenReturn("aString"); |
| 131 |
when(mockResultSet.next()).thenReturn(true); |
| 132 |
|
| 133 |
// Handle Connection.prepareCall() |
| 134 |
CallableStatement mockCallableStatement = mock(CallableStatement.class); |
| 135 |
when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement); |
| 136 |
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement); |
| 137 |
when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement); |
| 138 |
|
| 139 |
// Handle Connection.close() |
| 140 |
// doAnswer(new Answer<Void>() { |
| 141 |
// public Void answer(InvocationOnMock invocation) throws Throwable { |
| 142 |
// return null; |
| 143 |
// } |
| 144 |
// }).doThrow(new SQLException("Connection is already closed")).when(mockConnection).close(); |
| 145 |
|
| 146 |
// Handle Connection.commit() |
| 147 |
// doAnswer(new Answer<Void>() { |
| 148 |
// public Void answer(InvocationOnMock invocation) throws Throwable { |
| 149 |
// return null; |
| 150 |
// } |
| 151 |
// }).doThrow(new SQLException("Transaction already commited")).when(mockConnection).commit(); |
| 152 |
|
| 153 |
// Handle Connection.rollback() |
| 154 |
// doAnswer(new Answer<Void>() { |
| 155 |
// public Void answer(InvocationOnMock invocation) throws Throwable { |
| 156 |
// return null; |
| 157 |
// } |
| 158 |
// }).doThrow(new SQLException("Transaction already rolledback")).when(mockConnection).rollback(); |
| 159 |
|
| 160 |
return mockConnection; |
| 161 |
} |
| 162 |
} |