/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 * 
 * Please see distribution for license.
 */
package com.opengamma.util.test;

import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.fail;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import org.hibernate.mapping.Table;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.opengamma.OpenGammaRuntimeException;

/**
 * Test DbTool.
 */
@Test
public class DbToolTest {

  private static final String TEST_TABLE = "db_tool_unit_test_table";
  
  private DbTool _tool;
  private String _dbHost;
  private String _user;
  private String _password;

  @BeforeMethod
  public void setUp() throws Exception {
    Properties props = new Properties();
    props.load(getClass().getResourceAsStream("/tests.properties"));

    _dbHost = props.getProperty("jdbc.url");
    _user = props.getProperty("jdbc.username");
    _password = props.getProperty("jdbc.password");

    _tool = new DbTool(_dbHost, _user, _password);
    _tool.initialize();
  }

  public void testDrop() throws Exception {

    _tool.createTestSchema();
    createTestTable();
    _tool.dropTestSchema();

    // The table should no longer be there
    try {
      _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), "SELECT * FROM " + TEST_TABLE);
      fail();
    } catch (OpenGammaRuntimeException e) {
      // Ok - no table should be there!
    }
  }

  public void testClear() throws Exception {

    _tool.createTestSchema();
    createTestTable();
    _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), "INSERT INTO " + TEST_TABLE + " (test_column) VALUES ('test')");

    _tool.clearTestTables();

    Connection connection = getConnection();
    Statement statement = connection.createStatement();
    ResultSet rs = statement
        .executeQuery("SELECT COUNT(*) FROM " + TEST_TABLE);
    if (rs.next()) {
      int count = rs.getInt(1);
      assertEquals(0, count);
    } else {
      fail();
    }

    rs.close();
    statement.close();
    connection.close();

    _tool.dropTestSchema();
  }

  private Connection getConnection() throws SQLException {
    return DriverManager.getConnection(_dbHost + "/" + _tool.getTestCatalog(),
        _user, _password);
  }

  private void createTestTable() throws SQLException {
    Table table = new Table(TEST_TABLE);
    try {
      String dropSql = table.sqlDropString(_tool.getHibernateDialect(), null, _tool
          .getTestSchema());
      _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), dropSql);
    } catch (OpenGammaRuntimeException e) {
      // It might not exist, that's OK
    }
    String createSql = "CREATE TABLE "
        + table.getQualifiedName(_tool.getHibernateDialect(), null, _tool
            .getTestSchema()) + " (test_column char(50))";
    _tool.executeSql(_tool.getTestCatalog(), _tool.getTestSchema(), createSql);
  }

}

@@ -727,18 +727,16 @@
 table%22;%0A
-  
 %0A  priva
@@ -755,86 +755,8 @@
 ool;
-%0A  private String _dbHost;%0A  private String _user;%0A  private String _password;
 %0A%0A  
@@ -833,20 +833,20 @@
 props = 
-new 
+Test
 Properti
@@ -851,67 +851,17 @@
 ties
-();%0A    props.load(getClass().getResourceAsStream(%22/tests.p
+.getTestP
 rope
@@ -869,19 +869,23 @@
 ties
-%22)
+(
 );%0A
-%0A
     
-_
+String 
 dbHo
@@ -920,25 +920,31 @@
 .url%22);%0A    
-_
+String 
 user = props
@@ -978,17 +978,23 @@
 %22);%0A    
-_
+String 
 password
@@ -1028,25 +1028,24 @@
 password%22);%0A
-%0A
     _tool = 
@@ -1059,17 +1059,16 @@
 ool(
-_
 dbHost, 
 _use
@@ -1063,24 +1063,22 @@
 dbHost, 
-_
 user, 
-_
 password
@@ -1145,33 +1145,32 @@
 ows Exception %7B%0A
-%0A
     _tool.create
@@ -1544,17 +1544,16 @@
 ption %7B%0A
-%0A
     _too
@@ -2268,15 +2268,40 @@
 ion(
-_db
+%0A        _tool.getDbServer
 Host
+()
  + %22
@@ -2332,33 +2332,45 @@
 g(),
-%0A        _u
+ _tool.getU
 ser
+()
 , _
-p
+tool.getP
 assword
+()
 );%0A 

Move DbToolTest to be based on TestProperties