package com.rgi.common.util.jdbc;

import com.mockrunner.mock.jdbc.MockConnection;
import org.junit.Test;

import java.io.File;
import java.nio.file.FileSystems;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Random;

import static org.junit.Assert.fail;



/**
 *
 * Created by justin.rhee on 10/29/2015.
 */
public class JdbcUtilityTest {

    private static final String TEST_TABLE_NAME = "tiles";

    private final Random randomGenerator = new Random();



    @Test(expected = IllegalArgumentException.class)
    public void selectOneNullConectionTest() throws SQLException
    {
        final boolean result = JdbcUtility.selectOne(null,
                "SELECT COUNT(*) FROM sqlite_master WHERE (type = 'table' OR type = 'view') AND name = ? LIMIT 1;",
                preparedStatement -> preparedStatement.setString(1, "tiles"),
                resultSet -> resultSet.getInt(1)) > 0;
        fail("selectOne should have thrown an IllegalArgumentException for a null Connection.");
    }


    @Test(expected = IllegalArgumentException.class)
    public void selectOneNullStringTest() throws Exception
    {
        final File testFile = this.getRandomFile(4);
        testFile.createNewFile();

        final Connection con = new MockConnection();
        final boolean result = JdbcUtility.selectOne(con,
                null,
                preparedStatement -> preparedStatement.setString(1, "tiles"),
                resultSet -> resultSet.getInt(1)) > 0;
        fail("selectOne should have thrown an IllegalArgumentException for a null or empty String.");
    }

    private File getRandomFile(final int length)
    {
        File testFile;

        do
        {
            testFile = new File(String.format(FileSystems.getDefault().getPath(this.getRandomString(length)) + ".gpkg"));
        }
        while (testFile.exists());

        return testFile;
    }

    private String getRandomString(final int length)
    {
        final String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        final char[] text = new char[length];
        for (int i = 0; i < length; i++)
        {
            text[i] = characters.charAt(this.randomGenerator.nextInt(characters.length()));
        }
        return new String(text);
    }

    private Connection getConnection(final String filePath) throws Exception
    {
        Class.forName("org.sqlite.JDBC"); // Register the driver

        return DriverManager.getConnection("jdbc:sqlite:" + filePath);
    }


}

@@ -214,16 +214,43 @@
 anager;%0A
+import java.sql.ResultSet;%0A
 import j
@@ -297,16 +297,59 @@
 andom;%0A%0A
+import static org.junit.Assert.assertTrue;%0A
 import s
@@ -582,18 +582,16 @@
 dom();%0A%0A
-%0A%0A
     @Tes
@@ -691,36 +691,32 @@
 ows SQLException
-%0A   
  %7B%0A        final
@@ -1218,36 +1218,32 @@
 throws Exception
-%0A   
  %7B%0A        final
@@ -1688,32 +1688,1544 @@
 .%22);%0A    %7D%0A%0A    
+@SuppressWarnings(%22ConstantConditions%22)%0A    @Test(expected = IllegalArgumentException.class)%0A    public void selectOneNullResultSetFunctionTest() throws Exception %7B%0A        final File testFile = this.getRandomFile(2);%0A        testFile.createNewFile();%0A%0A        final Connection con = new MockConnection();%0A%0A        final String str = %22SELECT COUNT(*) FROM sqlite_master WHERE (type = 'table' OR type = 'view') AND name = ? LIMIT 1;%22;%0A%0A        final boolean result = JdbcUtility.selectOne(con, str,%0A                preparedStatement -%3E preparedStatement.setString(1, %22tiles%22),%0A                null);%0A        fail(%22selectOne should have thrown an IllegalArgumentException for a null resultMapper.%22);%0A    %7D%0A%0A%0A    //TODO fix the tests for preparedStatement section%0A//    @SuppressWarnings(%22ConstantConditions%22)%0A//    @Test%0A//    public void selectOnePreparedStatementTest() throws Exception %7B%0A//        final File testFile = this.getRandomFile(6);%0A//        testFile.createNewFile();%0A//%0A//        final Connection con = new MockConnection();%0A//%0A//        final String str = %22SELECT COUNT(*) FROM sqlite_master WHERE (type = 'table' OR type = 'view') AND name = ? LIMIT 1;%22;%0A//%0A//        final boolean result = JdbcUtility.selectOne(con, str,%0A//                preparedStatement -%3E preparedStatement.setString(1, %22tiles%22),%0A//                resultSet -%3E resultSet.getInt(1)) %3E 0;%0A//%0A//        fail(%22selectOne should have thrown an IllegalArgumentException for a null resultMapper.%22);%0A//%0A//    %7D%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A%0A    
 private File get
@@ -3339,22 +3339,8 @@
 File
-(String.format
 (Fil
@@ -3409,17 +3409,16 @@
 %22.gpkg%22)
-)
 ;%0A      
@@ -3881,234 +3881,6 @@
  %7D%0A%0A
-    private Connection getConnection(final String filePath) throws Exception%0A    %7B%0A        Class.forName(%22org.sqlite.JDBC%22); // Register the driver%0A%0A        return DriverManager.getConnection(%22jdbc:sqlite:%22 + filePath);%0A    %7D%0A%0A%0A
 %7D%0A

create ResultSetFunction test