http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/main/java/org/apache/calcite/avatica/server/MetricsAwareAvaticaHandler.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/main/java/org/apache/calcite/avatica/server/MetricsAwareAvaticaHandler.java b/avatica/server/src/main/java/org/apache/calcite/avatica/server/MetricsAwareAvaticaHandler.java deleted file mode 100644 index 0914dbd..0000000 --- a/avatica/server/src/main/java/org/apache/calcite/avatica/server/MetricsAwareAvaticaHandler.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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 org.apache.calcite.avatica.server; - -import org.apache.calcite.avatica.metrics.MetricsSystem; - -/** - * An {@link AvaticaHandler} that is capable of collecting metrics. - */ -public interface MetricsAwareAvaticaHandler extends AvaticaHandler { - - /** - * General prefix for all metrics in a handler. - */ - String HANDLER_PREFIX = "Handler."; - - /** - * Name for timing requests from users - */ - String REQUEST_TIMER_NAME = HANDLER_PREFIX + "RequestTimings"; - - /** - * @return An instance of the {@link MetricsSystem} for this AvaticaHandler. - */ - MetricsSystem getMetrics(); - -} - -// End MetricsAwareAvaticaHandler.java
http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/main/java/org/apache/calcite/avatica/server/PropertyBasedSpnegoLoginService.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/main/java/org/apache/calcite/avatica/server/PropertyBasedSpnegoLoginService.java b/avatica/server/src/main/java/org/apache/calcite/avatica/server/PropertyBasedSpnegoLoginService.java deleted file mode 100644 index 9e373fb..0000000 --- a/avatica/server/src/main/java/org/apache/calcite/avatica/server/PropertyBasedSpnegoLoginService.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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 org.apache.calcite.avatica.server; - -import org.eclipse.jetty.security.SpnegoLoginService; - -import java.lang.reflect.Field; -import java.util.Objects; - -/** - * A customization of {@link SpnegoLoginService} which directly specifies the server's - * principal instead of requiring a file to exist. Known to work with Jetty-9.2.x, any other - * version would require testing/inspection to ensure the logic is still sound. - */ -public class PropertyBasedSpnegoLoginService extends SpnegoLoginService { - - private static final String TARGET_NAME_FIELD_NAME = "_targetName"; - private final String serverPrincipal; - - public PropertyBasedSpnegoLoginService(String realm, String serverPrincipal) { - super(realm); - this.serverPrincipal = Objects.requireNonNull(serverPrincipal); - } - - @Override protected void doStart() throws Exception { - // Override the parent implementation, setting _targetName to be the serverPrincipal - // without the need for a one-line file to do the same thing. - // - // AbstractLifeCycle's doStart() method does nothing, so we aren't missing any extra logic. - final Field targetNameField = SpnegoLoginService.class.getDeclaredField(TARGET_NAME_FIELD_NAME); - targetNameField.setAccessible(true); - targetNameField.set(this, serverPrincipal); - } -} - -// End PropertyBasedSpnegoLoginService.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/main/java/org/apache/calcite/avatica/server/package-info.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/main/java/org/apache/calcite/avatica/server/package-info.java b/avatica/server/src/main/java/org/apache/calcite/avatica/server/package-info.java deleted file mode 100644 index f2b8728..0000000 --- a/avatica/server/src/main/java/org/apache/calcite/avatica/server/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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. - */ - -/** - * Avatica server that listens for HTTP requests. - */ -@PackageMarker -package org.apache.calcite.avatica.server; - -import org.apache.calcite.avatica.util.PackageMarker; - -// End package-info.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/test/java/org/apache/calcite/avatica/AvaticaSpnegoTest.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/test/java/org/apache/calcite/avatica/AvaticaSpnegoTest.java b/avatica/server/src/test/java/org/apache/calcite/avatica/AvaticaSpnegoTest.java deleted file mode 100644 index 34329e2..0000000 --- a/avatica/server/src/test/java/org/apache/calcite/avatica/AvaticaSpnegoTest.java +++ /dev/null @@ -1,246 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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 org.apache.calcite.avatica; - -import org.apache.calcite.avatica.jdbc.JdbcMeta; -import org.apache.calcite.avatica.remote.Driver; -import org.apache.calcite.avatica.remote.LocalService; -import org.apache.calcite.avatica.server.HttpServer; - -import org.apache.kerby.kerberos.kerb.KrbException; -import org.apache.kerby.kerberos.kerb.client.JaasKrbUtil; -import org.apache.kerby.kerberos.kerb.client.KrbConfig; -import org.apache.kerby.kerberos.kerb.client.KrbConfigKey; -import org.apache.kerby.kerberos.kerb.server.SimpleKdcServer; - -import org.junit.AfterClass; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.File; -import java.security.PrivilegedExceptionAction; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ResultSet; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; - -import javax.security.auth.Subject; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * End to end test case for SPNEGO with Avatica. - */ -@RunWith(Parameterized.class) -@Ignore("Disabled due to [CALCITE-1183] intermittent HTTP 404 failures") -public class AvaticaSpnegoTest { - private static final Logger LOG = LoggerFactory.getLogger(AvaticaSpnegoTest.class); - - private static final ConnectionSpec CONNECTION_SPEC = ConnectionSpec.HSQLDB; - private static final List<HttpServer> SERVERS_TO_STOP = new ArrayList<>(); - - private static SimpleKdcServer kdc; - private static KrbConfig clientConfig; - private static File keytabDir; - - private static int kdcPort; - private static File clientKeytab; - private static File serverKeytab; - - private static boolean isKdcStarted = false; - - private static void setupKdc() throws Exception { - kdc = new SimpleKdcServer(); - File target = new File(System.getProperty("user.dir"), "target"); - assertTrue(target.exists()); - - File kdcDir = new File(target, AvaticaSpnegoTest.class.getSimpleName()); - if (kdcDir.exists()) { - SpnegoTestUtil.deleteRecursively(kdcDir); - } - kdcDir.mkdirs(); - kdc.setWorkDir(kdcDir); - - kdc.setKdcHost(SpnegoTestUtil.KDC_HOST); - kdcPort = SpnegoTestUtil.getFreePort(); - kdc.setAllowTcp(true); - kdc.setAllowUdp(false); - kdc.setKdcTcpPort(kdcPort); - - LOG.info("Starting KDC server at {}:{}", SpnegoTestUtil.KDC_HOST, kdcPort); - - kdc.init(); - kdc.start(); - isKdcStarted = true; - - keytabDir = new File(target, AvaticaSpnegoTest.class.getSimpleName() - + "_keytabs"); - if (keytabDir.exists()) { - SpnegoTestUtil.deleteRecursively(keytabDir); - } - keytabDir.mkdirs(); - setupServerUser(keytabDir); - - clientConfig = new KrbConfig(); - clientConfig.setString(KrbConfigKey.KDC_HOST, SpnegoTestUtil.KDC_HOST); - clientConfig.setInt(KrbConfigKey.KDC_TCP_PORT, kdcPort); - clientConfig.setString(KrbConfigKey.DEFAULT_REALM, SpnegoTestUtil.REALM); - - // Kerby sets "java.security.krb5.conf" for us! - System.setProperty("javax.security.auth.useSubjectCredsOnly", "false"); - //System.setProperty("sun.security.spnego.debug", "true"); - //System.setProperty("sun.security.krb5.debug", "true"); - } - - @AfterClass public static void stopKdc() throws Exception { - for (HttpServer server : SERVERS_TO_STOP) { - server.stop(); - } - - if (isKdcStarted) { - LOG.info("Stopping KDC on {}", kdcPort); - kdc.stop(); - } - } - - private static void setupServerUser(File keytabDir) throws KrbException { - // Create the client user - String clientPrincipal = SpnegoTestUtil.CLIENT_PRINCIPAL.substring(0, - SpnegoTestUtil.CLIENT_PRINCIPAL.indexOf('@')); - clientKeytab = new File(keytabDir, clientPrincipal.replace('/', '_') + ".keytab"); - if (clientKeytab.exists()) { - SpnegoTestUtil.deleteRecursively(clientKeytab); - } - LOG.info("Creating {} with keytab {}", clientPrincipal, clientKeytab); - SpnegoTestUtil.setupUser(kdc, clientKeytab, clientPrincipal); - - // Create the server user - String serverPrincipal = SpnegoTestUtil.SERVER_PRINCIPAL.substring(0, - SpnegoTestUtil.SERVER_PRINCIPAL.indexOf('@')); - serverKeytab = new File(keytabDir, serverPrincipal.replace('/', '_') + ".keytab"); - if (serverKeytab.exists()) { - SpnegoTestUtil.deleteRecursively(serverKeytab); - } - LOG.info("Creating {} with keytab {}", SpnegoTestUtil.SERVER_PRINCIPAL, serverKeytab); - SpnegoTestUtil.setupUser(kdc, serverKeytab, SpnegoTestUtil.SERVER_PRINCIPAL); - } - - @Parameters public static List<Object[]> parameters() throws Exception { - final ArrayList<Object[]> parameters = new ArrayList<>(); - - // Start the KDC - setupKdc(); - - // Create a LocalService around HSQLDB - final JdbcMeta jdbcMeta = new JdbcMeta(CONNECTION_SPEC.url, - CONNECTION_SPEC.username, CONNECTION_SPEC.password); - final LocalService localService = new LocalService(jdbcMeta); - - for (Driver.Serialization serialization : new Driver.Serialization[] { - Driver.Serialization.JSON, Driver.Serialization.PROTOBUF}) { - // Build and start the server - HttpServer httpServer = new HttpServer.Builder() - .withPort(0) - .withAutomaticLogin(serverKeytab) - .withSpnego(SpnegoTestUtil.SERVER_PRINCIPAL, SpnegoTestUtil.REALM) - .withHandler(localService, serialization) - .build(); - httpServer.start(); - SERVERS_TO_STOP.add(httpServer); - - final String url = "jdbc:avatica:remote:url=http://" + SpnegoTestUtil.KDC_HOST + ":" - + httpServer.getPort() + ";authentication=SPNEGO;serialization=" + serialization; - LOG.info("JDBC URL {}", url); - - parameters.add(new Object[] {url}); - } - - return parameters; - } - - private final String jdbcUrl; - - public AvaticaSpnegoTest(String jdbcUrl) { - this.jdbcUrl = Objects.requireNonNull(jdbcUrl); - } - - @Test public void testAuthenticatedClient() throws Exception { - ConnectionSpec.getDatabaseLock().lock(); - try { - final String tableName = "allowed_clients"; - // Create the subject for the client - final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(SpnegoTestUtil.CLIENT_PRINCIPAL, - clientKeytab); - - // The name of the principal - - // Run this code, logged in as the subject (the client) - Subject.doAs(clientSubject, new PrivilegedExceptionAction<Void>() { - @Override public Void run() throws Exception { - try (Connection conn = DriverManager.getConnection(jdbcUrl)) { - try (Statement stmt = conn.createStatement()) { - assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName)); - assertFalse(stmt.execute("CREATE TABLE " + tableName + "(pk integer)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(1)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(2)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(3)")); - - ResultSet results = stmt.executeQuery("SELECT count(1) FROM " + tableName); - assertTrue(results.next()); - assertEquals(3, results.getInt(1)); - } - } - return null; - } - }); - } finally { - ConnectionSpec.getDatabaseLock().unlock(); - } - } - - @Test public void testAutomaticLogin() throws Exception { - final String tableName = "automaticAllowedClients"; - // Avatica should log in for us with this info - String url = jdbcUrl + ";principal=" + SpnegoTestUtil.CLIENT_PRINCIPAL + ";keytab=" - + clientKeytab; - LOG.info("Updated JDBC url: {}", url); - try (Connection conn = DriverManager.getConnection(url); - Statement stmt = conn.createStatement()) { - assertFalse(stmt.execute("DROP TABLE IF EXISTS " + tableName)); - assertFalse(stmt.execute("CREATE TABLE " + tableName + "(pk integer)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(1)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(2)")); - assertEquals(1, stmt.executeUpdate("INSERT INTO " + tableName + " VALUES(3)")); - - ResultSet results = stmt.executeQuery("SELECT count(1) FROM " + tableName); - assertTrue(results.next()); - assertEquals(3, results.getInt(1)); - } - } -} - -// End AvaticaSpnegoTest.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/test/java/org/apache/calcite/avatica/ConnectionSpec.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/test/java/org/apache/calcite/avatica/ConnectionSpec.java b/avatica/server/src/test/java/org/apache/calcite/avatica/ConnectionSpec.java deleted file mode 100644 index ba4c5b8..0000000 --- a/avatica/server/src/test/java/org/apache/calcite/avatica/ConnectionSpec.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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 org.apache.calcite.avatica; - -import net.hydromatic.scott.data.hsqldb.ScottHsqldb; - -import java.util.concurrent.locks.ReentrantLock; - -/** Information necessary to create a JDBC connection. Specify one to run - * tests against a different database. (hsqldb is the default.) */ -public class ConnectionSpec { - public final String url; - public final String username; - public final String password; - public final String driver; - - // CALCITE-687 HSQLDB seems to fail oddly when multiple tests are run concurrently - private static final ReentrantLock HSQLDB_LOCK = new ReentrantLock(); - - public ConnectionSpec(String url, String username, String password, - String driver) { - this.url = url; - this.username = username; - this.password = password; - this.driver = driver; - } - - public static final ConnectionSpec HSQLDB = - new ConnectionSpec(ScottHsqldb.URI, ScottHsqldb.USER, - ScottHsqldb.PASSWORD, "org.hsqldb.jdbcDriver"); - - /** - * Return a lock used for controlling concurrent access to the database as it has been observed - * that concurrent access is causing problems with HSQLDB. - */ - public static ReentrantLock getDatabaseLock() { - return HSQLDB_LOCK; - } -} - -// End ConnectionSpec.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/fc7b26c8/avatica/server/src/test/java/org/apache/calcite/avatica/RemoteDriverMockTest.java ---------------------------------------------------------------------- diff --git a/avatica/server/src/test/java/org/apache/calcite/avatica/RemoteDriverMockTest.java b/avatica/server/src/test/java/org/apache/calcite/avatica/RemoteDriverMockTest.java deleted file mode 100644 index 96e524a..0000000 --- a/avatica/server/src/test/java/org/apache/calcite/avatica/RemoteDriverMockTest.java +++ /dev/null @@ -1,219 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to you 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 org.apache.calcite.avatica; - -import org.apache.calcite.avatica.remote.MockJsonService; -import org.apache.calcite.avatica.remote.MockProtobufService.MockProtobufServiceFactory; - -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; - -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.ParameterMetaData; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.ResultSetMetaData; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Callable; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; - -/** - * RemoteDriver tests that use a Mock implementation of a Connection. - */ -@RunWith(Parameterized.class) -public class RemoteDriverMockTest { - public static final String MJS = MockJsonService.Factory.class.getName(); - public static final String MPBS = MockProtobufServiceFactory.class.getName(); - - private static Connection mjs() throws SQLException { - return DriverManager.getConnection("jdbc:avatica:remote:factory=" + MJS); - } - - private static Connection mpbs() throws SQLException { - return DriverManager.getConnection("jdbc:avatica:remote:factory=" + MPBS); - } - - @Parameters - public static List<Object[]> parameters() { - List<Object[]> parameters = new ArrayList<>(); - - parameters.add(new Object[] {new Callable<Connection>() { - public Connection call() throws SQLException { - return mjs(); - } - } }); - - parameters.add(new Object[] {new Callable<Connection>() { - public Connection call() throws SQLException { - return mpbs(); - } - } }); - - return parameters; - } - - private final Callable<Connection> connectionFunctor; - - public RemoteDriverMockTest(Callable<Connection> functor) { - this.connectionFunctor = functor; - } - - private Connection getMockConnection() { - try { - return connectionFunctor.call(); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - @Test public void testRegister() throws Exception { - final Connection connection = getMockConnection(); - assertThat(connection.isClosed(), is(false)); - connection.close(); - assertThat(connection.isClosed(), is(true)); - } - - @Test public void testSchemas() throws Exception { - final Connection connection = getMockConnection(); - final ResultSet resultSet = - connection.getMetaData().getSchemas(null, null); - assertFalse(resultSet.next()); - final ResultSetMetaData metaData = resultSet.getMetaData(); - assertTrue(metaData.getColumnCount() >= 2); - assertEquals("TABLE_SCHEM", metaData.getColumnName(1)); - assertEquals("TABLE_CATALOG", metaData.getColumnName(2)); - resultSet.close(); - connection.close(); - } - - @Test public void testTables() throws Exception { - final Connection connection = getMockConnection(); - final ResultSet resultSet = - connection.getMetaData().getTables(null, null, null, new String[0]); - assertFalse(resultSet.next()); - final ResultSetMetaData metaData = resultSet.getMetaData(); - assertTrue(metaData.getColumnCount() >= 3); - assertEquals("TABLE_CAT", metaData.getColumnName(1)); - assertEquals("TABLE_SCHEM", metaData.getColumnName(2)); - assertEquals("TABLE_NAME", metaData.getColumnName(3)); - resultSet.close(); - connection.close(); - } - - @Ignore - @Test public void testNoFactory() throws Exception { - final Connection connection = - DriverManager.getConnection("jdbc:avatica:remote:"); - assertThat(connection.isClosed(), is(false)); - final ResultSet resultSet = connection.getMetaData().getSchemas(); - assertFalse(resultSet.next()); - final ResultSetMetaData metaData = resultSet.getMetaData(); - assertEquals(2, metaData.getColumnCount()); - assertEquals("TABLE_SCHEM", metaData.getColumnName(1)); - assertEquals("TABLE_CATALOG", metaData.getColumnName(2)); - resultSet.close(); - connection.close(); - assertThat(connection.isClosed(), is(true)); - } - - @Ignore - @Test public void testCatalogsMock() throws Exception { - final Connection connection = getMockConnection(); - assertThat(connection.isClosed(), is(false)); - final ResultSet resultSet = connection.getMetaData().getSchemas(); - assertFalse(resultSet.next()); - final ResultSetMetaData metaData = resultSet.getMetaData(); - assertEquals(2, metaData.getColumnCount()); - assertEquals("TABLE_SCHEM", metaData.getColumnName(1)); - assertEquals("TABLE_CATALOG", metaData.getColumnName(2)); - resultSet.close(); - connection.close(); - assertThat(connection.isClosed(), is(true)); - } - - @Ignore - @Test public void testStatementExecuteQueryMock() throws Exception { - checkStatementExecuteQuery(getMockConnection(), false); - } - - @Ignore - @Test public void testPrepareExecuteQueryMock() throws Exception { - checkStatementExecuteQuery(getMockConnection(), true); - } - - private void checkStatementExecuteQuery(Connection connection, - boolean prepare) throws SQLException { - final String sql = "select * from (\n" - + " values (1, 'a'), (null, 'b'), (3, 'c')) as t (c1, c2)"; - final Statement statement; - final ResultSet resultSet; - final ParameterMetaData parameterMetaData; - if (prepare) { - final PreparedStatement ps = connection.prepareStatement(sql); - statement = ps; - parameterMetaData = ps.getParameterMetaData(); - resultSet = ps.executeQuery(); - } else { - statement = connection.createStatement(); - parameterMetaData = null; - resultSet = statement.executeQuery(sql); - } - if (parameterMetaData != null) { - assertThat(parameterMetaData.getParameterCount(), equalTo(0)); - } - final ResultSetMetaData metaData = resultSet.getMetaData(); - assertEquals(2, metaData.getColumnCount()); - assertEquals("C1", metaData.getColumnName(1)); - assertEquals("C2", metaData.getColumnName(2)); - assertTrue(resultSet.next()); - assertTrue(resultSet.next()); - assertTrue(resultSet.next()); - assertFalse(resultSet.next()); - resultSet.close(); - statement.close(); - connection.close(); - } - - @Test public void testResultSetsFinagled() throws Exception { - // These values specified in MockJsonService - final String table = "my_table"; - final long value = 10; - - final Connection connection = getMockConnection(); - // Not an accurate ResultSet per JDBC, but close enough for testing. - ResultSet results = connection.getMetaData().getColumns(null, null, table, null); - assertTrue(results.next()); - assertEquals(table, results.getString(1)); - assertEquals(value, results.getLong(2)); - } - -} - -// End RemoteDriverMockTest.java
