package com.inmobi.grill.driver.jdbc;

/*
 * #%L
 * Grill Driver for JDBC
 * %%
 * Copyright (C) 2014 Inmobi
 * %%
 * 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.
 * #L%
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.hadoop.conf.Configuration;
import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestDataSourceConnectionProvider {
  public static final Logger LOG = Logger.getLogger(TestDataSourceConnectionProvider.class);

  @Test
  public void testGetConnectionHSQL() throws Exception {
    final Configuration conf = new Configuration();
    conf.set(JDBCDriverConfConstants.JDBC_DRIVER_CLASS, "org.hsqldb.jdbc.JDBCDriver");
    conf.set(JDBCDriverConfConstants.JDBC_DB_URI, "jdbc:hsqldb:mem:mymemdb");
    conf.set(JDBCDriverConfConstants.JDBC_USER, "SA");
    conf.set(JDBCDriverConfConstants.JDBC_PASSWORD, "");
    final DataSourceConnectionProvider cp = new DataSourceConnectionProvider();

    int numThreads = 50;
    Thread threads[] = new Thread[numThreads];
    final AtomicInteger passed = new AtomicInteger(0);
    final Semaphore sem = new Semaphore(1);

    for (int i = 0; i < numThreads; i++) {
      final int thid = i;
      threads[thid] = new Thread(new Runnable() {
        @Override
        public void run() {
          Connection conn = null;
          Statement st = null;
          List<Integer> expected;
          List<Integer> actual;
          try {
            conn = cp.getConnection(conf);
            conn.setAutoCommit(false);

            sem.acquire();
            try {
              st = conn.createStatement();
              st.execute("CREATE TABLE TEST_" + thid + "(ID INT)");
              st.close();
              conn.commit();
            } finally {
              sem.release();
            }

            PreparedStatement ps = conn.prepareStatement("INSERT INTO TEST_" + thid + " VALUES(?)");
            expected = new ArrayList<Integer>();
            for (int j = 0; j < 10; j++) {
              ps.setInt(1, j);
              ps.executeUpdate();
              expected.add(j);
            }
            ps.close();
            conn.commit();

            st = conn.createStatement();
            ResultSet rs = st.executeQuery("SELECT * FROM TEST_" + thid);
            actual = new ArrayList<Integer>();
            while (rs.next()) {
              actual.add(rs.getInt(1));
            }
            Assert.assertEquals(actual, expected);
            System.out.println("Test:" + thid + " passs");
            passed.incrementAndGet();
          } catch (SQLException e) {
            LOG.error("Error in test:" + thid, e);
          } catch (InterruptedException e1) {
            LOG.error("Interrupted:" + thid + " : " + e1.getMessage());
          } finally {
            try {
              st.close();
              conn.close();
            } catch (SQLException e) {
              e.printStackTrace();
            }
          }
        }
      });
      threads[thid].start();
    }

    for (Thread t : threads) {
      t.join();
    }
    cp.close();
    Assert.assertEquals(passed.get(), numThreads);
  }
}

@@ -2139,482 +2139,169 @@
     
-List%3CInteger%3E expected;%0A          List%3CInteger%3E actual;%0A          try %7B%0A            conn = cp.getConnection(conf);%0A            conn.setAutoCommit(false);%0A%0A            sem.acquire();%0A            try %7B%0A              st = conn.createStatement();%0A              st.execute(%22CREATE TABLE TEST_%22 + thid + %22(ID INT)%22);%0A              st.close();%0A              conn.commit();%0A            %7D finally %7B%0A              sem.release();%0A            %7D%0A%0A            PreparedStatement ps
+try %7B%0A            conn = cp.getConnection(conf);%0A            Assert.assertNotNull(conn);%0A            // Make sure the connection is usable%0A            st
  = conn.
 prep
@@ -2300,14 +2300,13 @@
 onn.
-prepar
+creat
 eSta
@@ -2316,930 +2316,424 @@
 ent(
-%22INSERT INTO TEST_%22 + thid + %22 VALUES(?)%22);%0A            expected = new ArrayList%3CInteger%3E();%0A            for (int j = 0; j %3C 10; j++) %7B%0A              ps.setInt(1, j);%0A              ps.executeUpdate();%0A              expected.add(j);%0A            %7D%0A            ps.close();%0A            conn.commit();%0A%0A            st = conn.createStatement();%0A            ResultSet rs = st.executeQuery(%22SELECT * FROM TEST_%22 + thid);%0A            actual = new ArrayList%3CInteger%3E();%0A            while (rs.next()) %7B%0A              actual.add(rs.getInt(1));%0A            %7D%0A            Assert.assertEquals(actual, expected);%0A            System.out.println(%22Test:%22 + thid + %22 passs%22);%0A            passed.incrementAndGet();%0A          %7D catch (SQLException e) %7B%0A            LOG.error(%22Error in test:%22 + thid, e);%0A          %7D catch (InterruptedException e1) %7B%0A            LOG.error(%22Interrupted:%22 + thid + %22 : %22 + e1.getMessage());%0A          %7D finally
+);%0A            Assert.assertNotNull(st);%0A            passed.incrementAndGet();%0A          %7D catch (SQLException e) %7B%0A            LOG.error(%22error getting connection to db!%22);%0A          %7D finally %7B%0A            if (st != null) %7B%0A              try %7B%0A                st.close();%0A              %7D catch (SQLException e) %7B%0A                e.printStackTrace();%0A              %7D%0A            %7D%0A            if (conn != null)
  %7B%0A
+  
     
@@ -2752,32 +2752,8 @@
 %7B%0A  
-            st.close();%0A
     
@@ -2768,32 +2768,34 @@
   conn.close();%0A
+  
             %7D ca
@@ -2823,32 +2823,34 @@
 %7B%0A              
+  
 e.printStackTrac
@@ -2846,32 +2846,48 @@
 ntStackTrace();%0A
+              %7D%0A
             %7D%0A  

Simplify data source test to only test getConnection method