package com.rgi.common.util.jdbc;

import junit.framework.Assert;
import org.junit.Test;
import org.junit.internal.runners.statements.Fail;

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();

    private final File testFile = this.getRandomFile(5);


    @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.");
    }



    //TODO find out what to put for database connection
    @Test(expected = IllegalArgumentException.class)
    public void selectOneNullStringTest() throws Exception
    {
        final Connection con = getConnection(this.testFile.getAbsolutePath());
        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)).toString() + ".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 static Connection getConnection(final String filePath) throws Exception
    {
        Class.forName("org.sqlite.JDBC"); // Register the driver

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



}

@@ -39,104 +39,70 @@
 ort 
-junit.framework.Assert;%0Aimport org.junit.Test;%0Aimport org.junit.internal.runners.statements.Fail
+com.mockrunner.mock.jdbc.MockConnection;%0Aimport org.junit.Test
 ;%0A%0Ai
@@ -312,12 +312,17 @@
 l;%0A%0A
+%0A%0A
 /**%0A
+ *%0A
  * C
@@ -516,65 +516,8 @@
 );%0A%0A
-    private final File testFile = this.getRandomFile(5);%0A
 %0A%0A  
@@ -1055,65 +1055,8 @@
 %7D%0A%0A%0A
-%0A    //TODO find out what to put for database connection%0A
     
@@ -1161,32 +1161,120 @@
 Exception%0A    %7B%0A
+        final File testFile = this.getRandomFile(4);%0A        testFile.createNewFile();%0A%0A
         final Co
@@ -1288,19 +1288,24 @@
 n con = 
-get
+new Mock
 Connecti
@@ -1311,39 +1311,8 @@
 ion(
-this.testFile.getAbsolutePath()
 );%0A 
@@ -1844,19 +1844,8 @@
 th))
-.toString()
  + %22
@@ -2332,23 +2332,16 @@
 private 
-static 
 Connecti
@@ -2548,11 +2548,10 @@
     %7D%0A%0A%0A
-%0A
 %7D%0A

fix selectOneNullStringTest