/*
 * Copyright (C) 2013 Brett Wooldridge
 *
 * 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 com.zaxxer.hikari.mocks;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.PrintWriter;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.logging.Logger;

import javax.sql.DataSource;

import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

/**
 *
 * @author Brett Wooldridge
 */
public class MockDataSource implements DataSource
{
    public Connection getConnection() throws SQLException
    {
        return createMockConnection();
    }

    public Connection getConnection(String username, String password) throws SQLException
    {
        return getConnection();
    }

    public PrintWriter getLogWriter() throws SQLException
    {
        return null;
    }

    public void setLogWriter(PrintWriter out) throws SQLException
    {
    }

    public void setLoginTimeout(int seconds) throws SQLException
    {
    }

    public int getLoginTimeout() throws SQLException
    {
        return 0;
    }

    public Logger getParentLogger() throws SQLFeatureNotSupportedException
    {
        return null;
    }

    public <T> T unwrap(Class<T> iface) throws SQLException
    {
        return null;
    }

    public boolean isWrapperFor(Class<?> iface) throws SQLException
    {
        return false;
    }

    public static Connection createMockConnection() throws SQLException {
        // Setup mock connection
        final Connection mockConnection = mock(Connection.class);

        // Autocommit is always true by default
        when(mockConnection.getAutoCommit()).thenReturn(true);

        // Handle Connection.createStatement()
        Statement statement = mock(Statement.class);
        when(mockConnection.createStatement()).thenReturn(statement);
        when(mockConnection.createStatement(anyInt(), anyInt())).thenReturn(statement);
        when(mockConnection.createStatement(anyInt(), anyInt(), anyInt())).thenReturn(statement);

        // Handle Connection.prepareStatement()
        PreparedStatement mockPreparedStatement = mock(PreparedStatement.class);
        when(mockConnection.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
        when(mockConnection.prepareStatement(anyString(), anyInt())).thenReturn(mockPreparedStatement);
        when(mockConnection.prepareStatement(anyString(), (int[]) anyObject())).thenReturn(mockPreparedStatement);
        when(mockConnection.prepareStatement(anyString(), (String[]) anyObject())).thenReturn(mockPreparedStatement);
        when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);
        when(mockConnection.prepareStatement(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockPreparedStatement);

        ResultSet mockResultSet = mock(ResultSet.class);
        when(mockPreparedStatement.executeQuery()).thenReturn(mockResultSet);
        when(mockResultSet.getString(anyInt())).thenReturn("aString");

        // Handle Connection.prepareCall()
        CallableStatement mockCallableStatement = mock(CallableStatement.class);
        when(mockConnection.prepareCall(anyString())).thenReturn(mockCallableStatement);
        when(mockConnection.prepareCall(anyString(), anyInt(), anyInt())).thenReturn(mockCallableStatement);
        when(mockConnection.prepareCall(anyString(), anyInt(), anyInt(), anyInt())).thenReturn(mockCallableStatement);

        // Handle Connection.close()
        doAnswer(new Answer<Void>() {
            public Void answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        }).doThrow(new SQLException("Connection is already closed")).when(mockConnection).close();

        // Handle Connection.commit()
        doAnswer(new Answer<Void>() {
            public Void answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        }).doThrow(new SQLException("Transaction already commited")).when(mockConnection).commit();

        // Handle Connection.rollback()
        doAnswer(new Answer<Void>() {
            public Void answer(InvocationOnMock invocation) throws Throwable {
                return null;
            }
        }).doThrow(new SQLException("Transaction already rolledback")).when(mockConnection).rollback();

        return mockConnection;
    }
}

@@ -3703,32 +3703,283 @@
 paredStatement);
+%0A        doAnswer(new Answer%3CVoid%3E() %7B%0A            public Void answer(InvocationOnMock invocation) throws Throwable%0A            %7B%0A                return null;%0A            %7D%0A        %7D).doNothing().when(mockPreparedStatement).setInt(anyInt(), anyInt());
 %0A%0A        Result
@@ -4169,16 +4169,69 @@
 tring%22);
+%0A        when(mockResultSet.next()).thenReturn(true);
 %0A%0A      
@@ -4703,16 +4703,18 @@
 close()%0A
+//
         
@@ -4735,32 +4735,34 @@
 nswer%3CVoid%3E() %7B%0A
+//
             publ
@@ -4816,32 +4816,34 @@
 ows Throwable %7B%0A
+//
                 
@@ -4847,32 +4847,34 @@
    return null;%0A
+//
             %7D%0A  
@@ -4863,32 +4863,34 @@
 //            %7D%0A
+//
         %7D).doThr
@@ -5007,24 +5007,26 @@
 on.commit()%0A
+//
         doAn
@@ -5043,32 +5043,34 @@
 nswer%3CVoid%3E() %7B%0A
+//
             publ
@@ -5124,32 +5124,34 @@
 ows Throwable %7B%0A
+//
                 
@@ -5155,32 +5155,34 @@
    return null;%0A
+//
             %7D%0A  
@@ -5171,32 +5171,34 @@
 //            %7D%0A
+//
         %7D).doThr
@@ -5322,16 +5322,18 @@
 lback()%0A
+//
         
@@ -5354,32 +5354,34 @@
 nswer%3CVoid%3E() %7B%0A
+//
             publ
@@ -5443,16 +5443,18 @@
 wable %7B%0A
+//
         
@@ -5466,32 +5466,34 @@
    return null;%0A
+//
             %7D%0A  
@@ -5482,32 +5482,34 @@
 //            %7D%0A
+//
         %7D).doThr

Fix up the mock datasource