/*
 * Copyright 2017 Adaptris Ltd.
 * 
 * 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.adaptris.core.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.Properties;

import org.junit.Test;

import com.adaptris.security.exc.PasswordException;

// This is all just a bit of fakery to get 100% (ha ha).
public class JdbcUtilTest extends JdbcUtil {


  @Test
  public void testClose_AutoCloseable() throws Exception {
    closeQuietly((AutoCloseable) null);
    AutoCloseable mock = mock(AutoCloseable.class);
    closeQuietly(null, mock);
    doThrow(new IOException("Expected")).when(mock).close();
    closeQuietly(mock);
  }

  @Test
  public void testRollbackSavepointConnection() throws Exception {
    rollback(null, null);
    Savepoint savepoint = mock(Savepoint.class);
    Connection c = mock(Connection.class);
    when(c.getAutoCommit()).thenReturn(true);
    rollback(savepoint, c);
    when(c.getAutoCommit()).thenReturn(false);
    rollback(null, c);
    rollback(savepoint, c);
    doThrow(new SQLException("Expected")).when(c).rollback(any(Savepoint.class));
    rollback(savepoint, c);
  }

  @Test
  public void testRollbackConnection() throws Exception {
    rollback(null);
    Connection c = mock(Connection.class);
    when(c.getAutoCommit()).thenReturn(true);
    rollback(c);
    when(c.getAutoCommit()).thenReturn(false);
    rollback(c);
    doThrow(new SQLException("Expected")).when(c).rollback();
    rollback(c);
  }

  @Test
  public void testCommit() throws Exception {
    commit(null);
    Connection c = mock(Connection.class);
    when(c.getAutoCommit()).thenReturn(true);
    commit(c);
    when(c.getAutoCommit()).thenReturn(false);
    commit(c);
    try {
      doThrow(new SQLException("Expected")).when(c).commit();
      commit(c);
      fail();
    } catch (SQLException expected) {

    }
  }

  @Test
  public void testCreateSavepoint() throws Exception {
    assertNull(createSavepoint(null));
    Connection c = mock(Connection.class);
    Savepoint savepoint = mock(Savepoint.class);

    when(c.getAutoCommit()).thenReturn(true);
    assertNull(createSavepoint(c));

    when(c.getAutoCommit()).thenReturn(false);
    when(c.setSavepoint()).thenReturn(savepoint);
    assertNotNull(createSavepoint(c));

    try {
      doThrow(new SQLException("Expected")).when(c).setSavepoint();
      createSavepoint(c);
      fail();
    } catch (SQLException expected) {

    }
  }

  @Test(expected = PasswordException.class)
  public void testMergeProperties() throws Exception {
    Properties p = new Properties();
    mergeConnectionProperties(p, "user", "password");
    assertEquals("user", p.getProperty("user"));
    assertEquals("password", p.getProperty("password"));
    p = new Properties();
    mergeConnectionProperties(p, "", "");
    assertEquals(0, p.size());
    mergeConnectionProperties(p, "", "ALTPW:ABCDEFGGHJ");
  }
}

@@ -836,16 +836,105 @@
 rs.any;%0A
+import static org.mockito.Matchers.anyInt;%0Aimport static org.mockito.Matchers.anyString;%0A
 import s
@@ -968,16 +968,16 @@
 oThrow;%0A
-
 import s
@@ -1101,24 +1101,86 @@
 Connection;%0A
+import java.sql.ResultSet;%0Aimport java.sql.ResultSetMetaData;%0A
 import java.
@@ -1220,24 +1220,51 @@
 .Savepoint;%0A
+import java.sql.Statement;%0A
 import java.
@@ -1525,24 +1525,24 @@
 Exception %7B%0A
-
     closeQui
@@ -1560,16 +1560,18 @@
 loseable
+%5B%5D
 ) null);
@@ -3993,11 +3993,824 @@
 J%22);%0A  %7D
+%0A%0A  @Test%0A  public void testTestConnection() throws Exception %7B%0A    Connection c = mock(Connection.class);%0A    Statement s = mock(Statement.class);%0A    ResultSet rs = mock(ResultSet.class);%0A    ResultSetMetaData rsm = mock(ResultSetMetaData.class);%0A    when(c.createStatement()).thenReturn(s);%0A    when(s.executeQuery(anyString())).thenReturn(rs);%0A    when(rs.next()).thenReturn(false, true);%0A    when(rs.getObject(anyInt())).thenReturn(%22columnData%22);%0A    when(rs.getMetaData()).thenReturn(rsm);%0A    when(rsm.getColumnName(anyInt())).thenReturn(%22columnName%22);%0A    when(rsm.getColumnCount()).thenReturn(1);%0A    %0A    testConnection(c, null, false); // won't fail because of the first isEmptyCheck%0A    testConnection(c, %22hello%22, true);%0A    testConnection(c, %22hello%22, true);%0A    testConnection(c, %22hello%22, false);%0A  %7D
 %0A%7D%0A

add for coverage.