/*
* 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.TestingPrestoServer;
import org.testng.annotations.AfterMethod;
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.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import static java.lang.String.format;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
public class TestDriver
{
private TestingPrestoServer server;
@BeforeMethod
public void setup()
throws Exception
{
server = new TestingPrestoServer();
}
@AfterMethod
public void teardown()
{
closeQuietly(server);
}
@Test
public void testDriverManager()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet tableTypes = connection.getMetaData().getTableTypes()) {
assertRowCount(tableTypes, 1);
}
try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery("SELECT 123 x, 'foo' y")) {
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 2);
assertEquals(metadata.getColumnLabel(1), "x");
assertEquals(metadata.getColumnType(1), Types.BIGINT);
assertEquals(metadata.getColumnLabel(2), "y");
assertEquals(metadata.getColumnType(2), Types.LONGNVARCHAR);
assertTrue(rs.next());
assertEquals(rs.getLong(1), 123);
assertEquals(rs.getLong("x"), 123);
assertEquals(rs.getString(2), "foo");
assertEquals(rs.getString("y"), "foo");
assertFalse(rs.next());
}
}
}
}
@Test
public void testGetCatalogs()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getCatalogs()) {
assertRowCount(rs, 2);
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 1);
assertEquals(metadata.getColumnLabel(1), "TABLE_CAT");
assertEquals(metadata.getColumnType(1), Types.LONGNVARCHAR);
}
}
}
@Test
public void testGetSchemas()
throws Exception
{
try (Connection connection = createConnection()) {
try (ResultSet rs = connection.getMetaData().getSchemas()) {
assertRowCount(rs, 2);
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 2);
assertEquals(metadata.getColumnLabel(1), "TABLE_SCHEM");
assertEquals(metadata.getColumnType(1), Types.LONGNVARCHAR);
assertEquals(metadata.getColumnLabel(2), "TABLE_CATALOG");
assertEquals(metadata.getColumnType(2), Types.LONGNVARCHAR);
}
}
}
@Test
public void testExecute()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet rs = statement.getResultSet();
assertTrue(rs.next());
assertEquals(rs.getLong(1), 123);
assertEquals(rs.getLong("x"), 123);
assertEquals(rs.getString(2), "foo");
assertEquals(rs.getString("y"), "foo");
assertFalse(rs.next());
}
}
}
@Test
public void testGetUpdateCount()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
assertEquals(statement.getUpdateCount(), -1);
}
}
}
@Test
public void testResultSetClose()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet result = statement.getResultSet();
assertFalse(result.isClosed());
result.close();
assertTrue(result.isClosed());
}
}
}
@Test
public void testGetResultSet()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
ResultSet result = statement.getResultSet();
assertNotNull(result);
assertFalse(result.isClosed());
statement.getMoreResults();
assertTrue(result.isClosed());
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
result = statement.getResultSet();
assertNotNull(result);
assertFalse(result.isClosed());
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
assertFalse(statement.getMoreResults(Statement.CLOSE_CURRENT_RESULT));
}
}
}
@Test(expectedExceptions = SQLFeatureNotSupportedException.class, expectedExceptionsMessageRegExp = "Multiple open results not supported")
public void testGetMoreResultsException()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
assertTrue(statement.execute("SELECT 123 x, 'foo' y"));
statement.getMoreResults(Statement.KEEP_CURRENT_RESULT);
}
}
}
@Test
public void testConnectionStringWithCatalogAndSchema()
throws Exception
{
String prefix = format("jdbc:presto://%s", server.getAddress());
Connection connection;
connection = DriverManager.getConnection(prefix + "/a/b/", "test", null);
assertEquals(connection.getCatalog(), "a");
assertEquals(connection.getSchema(), "b");
connection = DriverManager.getConnection(prefix + "/a/b", "test", null);
assertEquals(connection.getCatalog(), "a");
assertEquals(connection.getSchema(), "b");
connection = DriverManager.getConnection(prefix + "/a/", "test", null);
assertEquals(connection.getCatalog(), "a");
assertEquals(connection.getSchema(), "default");
connection = DriverManager.getConnection(prefix + "/a", "test", null);
assertEquals(connection.getCatalog(), "a");
assertEquals(connection.getSchema(), "default");
connection = DriverManager.getConnection(prefix + "/", "test", null);
assertEquals(connection.getCatalog(), "default");
assertEquals(connection.getSchema(), "default");
connection = DriverManager.getConnection(prefix + "", "test", null);
assertEquals(connection.getCatalog(), "default");
assertEquals(connection.getSchema(), "default");
}
@Test
public void testConnectionWithCatalogAndSchema()
throws Exception
{
try (Connection connection = createConnection("default", "information_schema")) {
try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery("" +
"SELECT table_catalog, table_schema " +
"FROM tables " +
"WHERE table_schema = 'sys' AND table_name = 'node'")) {
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 2);
assertEquals(metadata.getColumnLabel(1), "table_catalog");
assertEquals(metadata.getColumnLabel(2), "table_schema");
assertTrue(rs.next());
assertEquals(rs.getString("table_catalog"), "default");
}
}
}
}
@Test
public void testConnectionWithCatalog()
throws Exception
{
try (Connection connection = createConnection("default")) {
try (Statement statement = connection.createStatement()) {
try (ResultSet rs = statement.executeQuery("" +
"SELECT table_catalog, table_schema " +
"FROM information_schema.tables " +
"WHERE table_schema = 'sys' AND table_name = 'node'")) {
ResultSetMetaData metadata = rs.getMetaData();
assertEquals(metadata.getColumnCount(), 2);
assertEquals(metadata.getColumnLabel(1), "table_catalog");
assertEquals(metadata.getColumnLabel(2), "table_schema");
assertTrue(rs.next());
assertEquals(rs.getString("table_catalog"), "default");
}
}
}
}
@Test
public void testConnectionResourceHandling()
throws Exception
{
List<Connection> connections = new ArrayList<>();
for (int i = 0; i < 100; i++) {
Connection connection = createConnection();
connections.add(connection);
try (Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT 123")) {
assertTrue(rs.next());
}
}
for (Connection connection : connections) {
connection.close();
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = ".* does not exist")
public void testBadQuery()
throws Exception
{
try (Connection connection = createConnection()) {
try (Statement statement = connection.createStatement()) {
try (ResultSet ignored = statement.executeQuery("SELECT * FROM bad_table")) {
fail("expected exception");
}
}
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = "Username property \\(user\\) must be set")
public void testUserIsRequired()
throws Exception
{
try (Connection ignored = DriverManager.getConnection("jdbc:presto://test.invalid/")) {
fail("expected exception");
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = "Invalid path segments in URL: .*")
public void testBadUrlExtraPathSegments()
throws Exception
{
String url = format("jdbc:presto://%s/hive/default/bad_string", server.getAddress());
try (Connection ignored = DriverManager.getConnection(url, "test", null)) {
fail("expected exception");
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = "Catalog name is empty: .*")
public void testBadUrlMissingCatalog()
throws Exception
{
String url = format("jdbc:presto://%s//default", server.getAddress());
try (Connection ignored = DriverManager.getConnection(url, "test", null)) {
fail("expected exception");
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = "Catalog name is empty: .*")
public void testBadUrlEndsInSlashes()
throws Exception
{
String url = format("jdbc:presto://%s//", server.getAddress());
try (Connection ignored = DriverManager.getConnection(url, "test", null)) {
fail("expected exception");
}
}
@Test(expectedExceptions = SQLException.class, expectedExceptionsMessageRegExp = "Schema name is empty: .*")
public void testBadUrlMissingSchema()
throws Exception
{
String url = format("jdbc:presto://%s/a//", server.getAddress());
try (Connection ignored = DriverManager.getConnection(url, "test", null)) {
fail("expected exception");
}
}
private Connection createConnection()
throws SQLException
{
String url = format("jdbc:presto://%s", server.getAddress());
return DriverManager.getConnection(url, "test", null);
}
private Connection createConnection(String catalog)
throws SQLException
{
String url = format("jdbc:presto://%s/%s", server.getAddress(), catalog);
return DriverManager.getConnection(url, "test", null);
}
private Connection createConnection(String catalog, String schema)
throws SQLException
{
String url = format("jdbc:presto://%s/%s/%s", server.getAddress(), catalog, schema);
return DriverManager.getConnection(url, "test", null);
}
private static void assertRowCount(ResultSet rs, int expected)
throws SQLException
{
int actual = 0;
while (rs.next()) {
actual++;
}
assertEquals(actual, expected);
}
static void closeQuietly(AutoCloseable closeable)
{
try {
closeable.close();
}
catch (Exception ignored) {
}
}
}