All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.snowflake.client.jdbcapi.BaseJDBCTest Maven / Gradle / Ivy

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.snowflake.client.jdbcapi;

import net.snowflake.client.AbstractDriverIT;
import net.snowflake.client.jdbc.SnowflakeDriver;
import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.sql.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import static org.junit.Assert.*;
/**
 *
 * @author hyu
 */
public class BaseJDBCTest extends AbstractDriverIT
{
  private static Logger logger = Logger.getLogger(BaseJDBCTest.class.getName());

  BaseJDBCTest(){
    loadConfig();
    try {
      Class.forName(JDBCTestConfig.DRIVER_CLASS_NAME);
    }catch(Exception e){
      logger.log(Level.SEVERE, "Cannot find Driver", e);
    }
  }

  public static Connection getConnection() throws SQLException{
    return getConnection(true, false, 0, null);
  }

  public static Connection getConnection(Properties properties) throws SQLException{
    return getConnection(true, false, 0, properties);
  }

  public int getSizeOfResultSet(ResultSet rs) throws SQLException{
    int count = 0; 
    while(rs.next()){
      count++;
    }
    return count;
  }
  
  public void loadConfig(){
    Properties config = new Properties();
    
    try {
      String root = getSFProjectRootString();
      String configFileName = root + "/Client/src/test/java/net/snowflake/client/jdbcapi/config.ini";
      config.load(new FileInputStream(configFileName));
      
      JDBCTestConfig.DRIVER_CLASS_NAME = checkAndReadConfigProperty("DRIVER_CLASS_NAME", config);
      JDBCTestConfig.DATABASE = checkAndReadConfigProperty("DATABASE", config);
      JDBCTestConfig.SCHEMA = checkAndReadConfigProperty("SCHEMA", config);
      JDBCTestConfig.CONNECTION_STR = checkAndReadConfigProperty("CONNECTION_STR", config);
      JDBCTestConfig.USER = checkAndReadConfigProperty("USER", config);
      JDBCTestConfig.PWD = checkAndReadConfigProperty("PWD", config);
      JDBCTestConfig.ACCOUNT = checkAndReadConfigProperty("ACCOUNT", config);
      JDBCTestConfig.SSL = checkAndReadConfigProperty("SSL", config);
      
    }catch(Exception e){
      e.printStackTrace();
      System.exit(1);
    }     
  }
  
  private String checkAndReadConfigProperty(String configKey, Properties config){
    String propValue;
    propValue = config.getProperty(configKey);
    
    if((propValue == null) || (propValue.trim().length() <= 0)){
      System.out.println("Expected option " + configKey + " is not found!");
      System.exit(1);
    }
    
    return propValue;
  }
  
  public String getSFProjectRootString() throws Exception{
    URL location =
        BaseJDBCTest.class.getProtectionDomain().getCodeSource().getLocation();
    String testDir = URLDecoder.decode(location.getPath(), "UTF-8");
    System.out.println(testDir);
    String sfProjectRoot =
        testDir.substring(0, testDir.indexOf("Client"));
    return sfProjectRoot;
  }

  public void compareTwoLists(ArrayList expected, ArrayList actual){
    assertEquals(expected.size(), actual.size());
    for (int i=0; i> fetchResultSet(ResultSet rs, boolean printResult) throws SQLException{
    ArrayList> result = new ArrayList>();
    int columnCount = rs.getMetaData().getColumnCount();
    while(rs.next()){
      ArrayList row = new ArrayList<>();
      for (int i=1; i<= columnCount; i++) {
        row.add(rs.getString(i));
        if (printResult){ 
          System.out.print(rs.getString(i));
          System.out.print(", ");
        }
      }
      result.add(row);
      if (printResult){
        System.out.println("\n");
      }
    }
    return result;
  }

  public ArrayList fetchColumnInResultSet(ResultSet rs, int columnIndex) throws  SQLException{
    ArrayList result = new ArrayList<>();
    while(rs.next()){
      result.add(rs.getString(columnIndex));
    }
    return result;
  }

  public ArrayList getInfoViaSQLCmd(String sqlCmd) throws SQLException{
    Connection con = getConnection();
    Statement st = con.createStatement();
    ArrayList result = new ArrayList<>();
    ResultSet rs = st.executeQuery(sqlCmd);
    while(rs.next()){
      result.add(rs.getString(1));
    } 
    return result;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy