/*
 * 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.
 */
package com.facebook.presto.jdbc;

import com.facebook.presto.server.testing.TestingPrestoServer;
import io.airlift.log.Logging;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;

import static com.facebook.presto.jdbc.TestDriver.closeQuietly;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

@Test(singleThreaded = true)
public class TestJdbcResultSet
{
    private TestingPrestoServer server;

    private Connection connection;
    private Statement statement;

    @BeforeClass
    public void setupServer()
            throws Exception
    {
        Logging.initialize();
        server = new TestingPrestoServer();
    }

    @AfterClass
    public void teardownServer()
    {
        closeQuietly(server);
    }

    @SuppressWarnings("JDBCResourceOpenedButNotSafelyClosed")
    @BeforeMethod
    public void setup()
            throws Exception
    {
        connection = createConnection();
        statement = connection.createStatement();
    }

    @AfterMethod
    public void teardown()
    {
        closeQuietly(statement);
        closeQuietly(connection);
    }

    @Test
    public void testDuplicateColumnLabels()
            throws Exception
    {
        try (ResultSet rs = statement.executeQuery("SELECT 123 x, 456 x")) {
            ResultSetMetaData metadata = rs.getMetaData();
            assertEquals(metadata.getColumnCount(), 2);
            assertEquals(metadata.getColumnName(1), "x");
            assertEquals(metadata.getColumnName(2), "x");

            assertTrue(rs.next());
            assertEquals(rs.getLong(1), 123L);
            assertEquals(rs.getLong(2), 456L);
            assertEquals(rs.getLong("x"), 123L);
        }
    }

    @SuppressWarnings("UnnecessaryBoxing")
    @Test
    public void testObjectTypes()
            throws Exception
    {
        // TODO: enable these after the server is fixed
//        String sql = "SELECT 123, 0.1, true, 'hello', 1.0 / 0.0, 0.0 / 0.0";
        String sql = "SELECT 123, 0.1, true, 'hello'";
        try (ResultSet rs = statement.executeQuery(sql)) {
            ResultSetMetaData metadata = rs.getMetaData();
//            assertEquals(metadata.getColumnCount(), 6);
            assertEquals(metadata.getColumnCount(), 4);
            assertEquals(metadata.getColumnType(1), Types.BIGINT);
            assertEquals(metadata.getColumnType(2), Types.DOUBLE);
            assertEquals(metadata.getColumnType(3), Types.BOOLEAN);
            assertEquals(metadata.getColumnType(4), Types.LONGNVARCHAR);
//            assertEquals(metadata.getColumnType(5), Types.DOUBLE);
//            assertEquals(metadata.getColumnType(6), Types.DOUBLE);

            assertTrue(rs.next());
            assertEquals(rs.getObject(1), 123L);
            assertEquals(rs.getObject(2), 0.1d);
            assertEquals(rs.getObject(3), true);
            assertEquals(rs.getObject(4), "hello");
//            assertEquals(rs.getObject(5), Double.POSITIVE_INFINITY);
//            assertEquals(rs.getObject(6), Double.NaN);
        }
    }

    private Connection createConnection()
            throws SQLException
    {
        String url = format("jdbc:presto://%s", server.getAddress());
        return DriverManager.getConnection(url, "test", null);
    }
}

@@ -2796,66 +2796,8 @@
   %7B%0A
-        // TODO: enable these after the server is fixed%0A//
     
@@ -2873,63 +2873,8 @@
 0%22;%0A
-        String sql = %22SELECT 123, 0.1, true, 'hello'%22;%0A
     
@@ -2987,18 +2987,16 @@
 Data();%0A
-//
         
@@ -3047,64 +3047,8 @@
 6);%0A
-            assertEquals(metadata.getColumnCount(), 4);%0A
     
@@ -3318,18 +3318,16 @@
 RCHAR);%0A
-//
         
@@ -3385,18 +3385,16 @@
 OUBLE);%0A
-//
         
@@ -3687,18 +3687,16 @@
 ello%22);%0A
-//
         
@@ -3756,18 +3756,16 @@
 INITY);%0A
-//
         

Enable JDBC tests for NaN and infinity