package org.pentaho.di.core.database;

import static org.junit.Assert.*;

import org.junit.Test;
import org.pentaho.di.core.KettleEnvironment;

/**
 * Tests the value returned from org.pentaho.di.core.database.DatabaseInterface.getSelectCountStatement
 * for the database the interface is fronting.
 * 
 * As this release, Hive uses the following to select the number of rows:
 * 
 *   SELECT COUNT(1) FROM ....
 *   
 * All other databases use:
 * 
 *   SELECT COUNT(*) FROM ....
 */
public class SelectCountTests {
    
    /** 
     * 
     */
    private final static String NonHiveSelect = "select count(*) from ";
    private final static String HiveSelect = "select count(1) from ";
    private final static String TableName = "NON_EXISTANT";
    
    public static final String h2DatabaseXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<connection>" +
            "<name>H2</name>" +
            "<server>127.0.0.1</server>" +
            "<type>H2</type>" +
            "<access>Native</access>" + 
            "<database>mem:db</database>" +
            "<port></port>" +
            "<username>sa</username>" +
            "<password></password>" +
          "</connection>";
          
    public static final String OracleDatabaseXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<connection>" +
            "<name>Oracle</name>" +
            "<server>127.0.0.1</server>" +
            "<type>Oracle</type>" +
            "<access>Native</access>" + 
            "<database>test</database>" +
            "<port>1024</port>" +
            "<username>scott</username>" +
            "<password>tiger</password>" +
          "</connection>";
          
    public static final String MySQLDatabaseXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<connection>" +
            "<name>MySQL</name>" +
            "<server>127.0.0.1</server>" +
            "<type>MySQL</type>" +
            "<access></access>" + 
            "<database>test</database>" +
            "<port>3306</port>" +
            "<username>sa</username>" +
            "<password></password>" +
          "</connection>";

    public static final String HiveDatabaseXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
          "<connection>" +
            "<name>Hadoop Hive</name>" +
            "<server>127.0.0.1</server>" +
            "<type>Hadoop Hive</type>" +
            "<access></access>" + 
            "<database>default</database>" +
            "<port>10000</port>" +
            "<username>sean</username>" +
            "<password>sean</password>" +
          "</connection>"; 

    @Test
    public void testH2Database() {
        try {
            KettleEnvironment.init();
            String expectedSQL = NonHiveSelect+TableName;
            DatabaseMeta databaseMeta = new DatabaseMeta(h2DatabaseXML);
            String sql= databaseMeta.getDatabaseInterface().getSelectCountStatement(TableName);
            assertTrue(sql.equalsIgnoreCase(expectedSQL));
        }
        catch (Exception e ) {
            e.printStackTrace();
        }
    }

    @Test
    public void testOracleDatabase() {
        try {
            KettleEnvironment.init();
            String expectedSQL = NonHiveSelect+TableName;
            DatabaseMeta databaseMeta = new DatabaseMeta(OracleDatabaseXML);
            String sql= databaseMeta.getDatabaseInterface().getSelectCountStatement(TableName);
            assertTrue(sql.equalsIgnoreCase(expectedSQL));
        }
        catch (Exception e ) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void testMySQLDatabase() {
        try {
            KettleEnvironment.init();
            String expectedSQL = NonHiveSelect+TableName;
            DatabaseMeta databaseMeta = new DatabaseMeta(MySQLDatabaseXML);
            String sql= databaseMeta.getDatabaseInterface().getSelectCountStatement(TableName);
            assertTrue(sql.equalsIgnoreCase(expectedSQL));
            }
            catch (Exception e ) {
                e.printStackTrace();
        }
    }
    
    @Test
    public void testHiveDatabase() throws Exception {
        try {
            KettleEnvironment.init();
            String expectedSQL = HiveSelect+TableName;
            DatabaseMeta databaseMeta = new DatabaseMeta(HiveDatabaseXML);
            String sql= databaseMeta.getDatabaseInterface().getSelectCountStatement(TableName);
            assertTrue(sql.equalsIgnoreCase(expectedSQL));
        }
        catch (Exception e ) {
                e.printStackTrace();
        }
    }

}
@@ -642,79 +642,8 @@
 %22;%0D%0A
-    private final static String HiveSelect = %22select count(1) from %22;%0D%0A
     
@@ -695,24 +695,24 @@
 EXISTANT%22;%0D%0A
+
     %0D%0A    pu
@@ -2139,496 +2139,8 @@
 %0D%0A%0D%0A
-    public static final String HiveDatabaseXML = %22%3C?xml version=%5C%221.0%5C%22 encoding=%5C%22UTF-8%5C%22?%3E%22 +%0D%0A          %22%3Cconnection%3E%22 +%0D%0A            %22%3Cname%3EHadoop Hive%3C/name%3E%22 +%0D%0A            %22%3Cserver%3E127.0.0.1%3C/server%3E%22 +%0D%0A            %22%3Ctype%3EHadoop Hive%3C/type%3E%22 +%0D%0A            %22%3Caccess%3E%3C/access%3E%22 + %0D%0A            %22%3Cdatabase%3Edefault%3C/database%3E%22 +%0D%0A            %22%3Cport%3E10000%3C/port%3E%22 +%0D%0A            %22%3Cusername%3Esean%3C/username%3E%22 +%0D%0A            %22%3Cpassword%3Esean%3C/password%3E%22 +%0D%0A          %22%3C/connection%3E%22; %0D%0A%0D%0A
     
@@ -3624,528 +3624,12 @@
  %7D%0D%0A
+
     %7D%0D%0A
-    %0D%0A    @Test%0D%0A    public void testHiveDatabase() throws Exception %7B%0D%0A        try %7B%0D%0A            KettleEnvironment.init();%0D%0A            String expectedSQL = HiveSelect+TableName;%0D%0A            DatabaseMeta databaseMeta = new DatabaseMeta(HiveDatabaseXML);%0D%0A            String sql= databaseMeta.getDatabaseInterface().getSelectCountStatement(TableName);%0D%0A            assertTrue(sql.equalsIgnoreCase(expectedSQL));%0D%0A        %7D%0D%0A        catch (Exception e ) %7B%0D%0A                e.printStackTrace();%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A
 %7D

Move PDI Hive Dialect into hadoop-ee and add license checks. Removed testing of select count(1) from a hive table.