Repository: calcite-avatica Updated Branches: refs/heads/master 01ec8834e -> 557f15cc7
http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/dd65a2b1/server/src/test/java/org/apache/calcite/avatica/remote/AvaticaServersForTest.java ---------------------------------------------------------------------- diff --git a/server/src/test/java/org/apache/calcite/avatica/remote/AvaticaServersForTest.java b/server/src/test/java/org/apache/calcite/avatica/remote/AvaticaServersForTest.java new file mode 100644 index 0000000..8e5de3a --- /dev/null +++ b/server/src/test/java/org/apache/calcite/avatica/remote/AvaticaServersForTest.java @@ -0,0 +1,141 @@ +/* + * 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.remote; + +import org.apache.calcite.avatica.ConnectionSpec; +import org.apache.calcite.avatica.Meta; +import org.apache.calcite.avatica.jdbc.JdbcMeta; +import org.apache.calcite.avatica.remote.Driver.Serialization; +import org.apache.calcite.avatica.server.AvaticaJsonHandler; +import org.apache.calcite.avatica.server.AvaticaProtobufHandler; +import org.apache.calcite.avatica.server.HttpServer; +import org.apache.calcite.avatica.server.Main; +import org.apache.calcite.avatica.server.Main.HandlerFactory; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Utility class which encapsulates the setup required to write Avatica tests that run against + * servers using each serialization approach. + */ +public class AvaticaServersForTest { + private static final ConnectionSpec CONNECTION_SPEC = ConnectionSpec.HSQLDB; + private static final String[] SERVER_ARGS = { FullyRemoteJdbcMetaFactory.class.getName() }; + + private final Map<Serialization, HttpServer> serversBySerialization; + + public AvaticaServersForTest() { + serversBySerialization = new HashMap<>(); + } + + /** + * Starts an Avatica server for each serialization type. + */ + public void startServers() throws Exception { + // Bind to '0' to pluck an ephemeral port instead of expecting a certain one to be free + final HttpServer jsonServer = Main.start(SERVER_ARGS, 0, new HandlerFactory() { + @Override public AvaticaJsonHandler createHandler(Service service) { + return new AvaticaJsonHandler(service); + } + }); + serversBySerialization.put(Serialization.JSON, jsonServer); + + final HttpServer protobufServer = Main.start(SERVER_ARGS, 0, new HandlerFactory() { + @Override public AvaticaProtobufHandler createHandler(Service service) { + return new AvaticaProtobufHandler(service); + } + }); + serversBySerialization.put(Serialization.PROTOBUF, protobufServer); + } + + /** + * Stops the servers currently running. + * + * @throws Exception If there is an error stopping a server + */ + public void stopServers() throws Exception { + Iterator<Entry<Serialization, HttpServer>> servers = + serversBySerialization.entrySet().iterator(); + while (servers.hasNext()) { + try { + servers.next().getValue().stop(); + } finally { + // Still remove it if we failed to stop it + servers.remove(); + } + } + } + + /** + * Computes an array of parameters to support JUnit's parameterized tests. The Object array + * actually contains a {@link Serialization} and the {@link HttpServer} instance in that order. + * + * @return A list of arrays of Serialization and HttpServer pairs. + */ + public List<Object[]> getJUnitParameters() { + List<Object[]> params = new ArrayList<>(serversBySerialization.size()); + + for (Entry<Serialization, HttpServer> servers : serversBySerialization.entrySet()) { + params.add(new Object[]{ servers.getKey(), servers.getValue() }); + } + + return params; + } + + /** + * Computes the JDBC url for the Avatica server running on localhost, bound to the given port, + * and using the given serialization. + * + * @param port The port the Avatica server is listening on. + * @param serialization The serialization the Avatica server is using. + * @return A JDBC server to the local Avatica server. + */ + public String getJdbcUrl(int port, Serialization serialization) { + return "jdbc:avatica:remote:url=http://localhost:" + port + ";serialization=" + + serialization.name(); + } + + /** Factory that provides a {@link JdbcMeta}. */ + public static class FullyRemoteJdbcMetaFactory implements Meta.Factory { + + private static JdbcMeta instance = null; + + static JdbcMeta getInstance() { + if (instance == null) { + try { + instance = new JdbcMeta(CONNECTION_SPEC.url, CONNECTION_SPEC.username, + CONNECTION_SPEC.password); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + return instance; + } + + @Override public Meta create(List<String> args) { + return getInstance(); + } + } +} + +// End AvaticaServersForTest.java http://git-wip-us.apache.org/repos/asf/calcite-avatica/blob/dd65a2b1/server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java ---------------------------------------------------------------------- diff --git a/server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java b/server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java index ebd3c76..4cfcc1b 100644 --- a/server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java +++ b/server/src/test/java/org/apache/calcite/avatica/remote/RemoteMetaTest.java @@ -26,13 +26,10 @@ import org.apache.calcite.avatica.ConnectionSpec; import org.apache.calcite.avatica.Meta; import org.apache.calcite.avatica.Meta.DatabaseProperty; import org.apache.calcite.avatica.jdbc.JdbcMeta; +import org.apache.calcite.avatica.remote.AvaticaServersForTest.FullyRemoteJdbcMetaFactory; import org.apache.calcite.avatica.remote.Service.ErrorResponse; import org.apache.calcite.avatica.remote.Service.Response; -import org.apache.calcite.avatica.server.AvaticaJsonHandler; -import org.apache.calcite.avatica.server.AvaticaProtobufHandler; import org.apache.calcite.avatica.server.HttpServer; -import org.apache.calcite.avatica.server.Main; -import org.apache.calcite.avatica.server.Main.HandlerFactory; import org.apache.calcite.avatica.util.ArrayImpl; import org.apache.calcite.avatica.util.FilteredConstants; @@ -62,7 +59,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -86,58 +82,30 @@ import static org.junit.Assert.fail; /** Tests covering {@link RemoteMeta}. */ @RunWith(Parameterized.class) public class RemoteMetaTest { + private static final AvaticaServersForTest SERVERS = new AvaticaServersForTest(); private static final Random RANDOM = new Random(); - private static final ConnectionSpec CONNECTION_SPEC = ConnectionSpec.HSQLDB; - - // Keep a reference to the servers we start to clean them up after - private static final List<HttpServer> ACTIVE_SERVERS = new ArrayList<>(); private final HttpServer server; private final String url; private final int port; private final Driver.Serialization serialization; - @Parameters + @Parameters(name = "{0}") public static List<Object[]> parameters() throws Exception { - List<Object[]> params = new ArrayList<>(); - - final String[] mainArgs = { FullyRemoteJdbcMetaFactory.class.getName() }; - - // Bind to '0' to pluck an ephemeral port instead of expecting a certain one to be free - - final HttpServer jsonServer = Main.start(mainArgs, 0, new HandlerFactory() { - @Override public AvaticaJsonHandler createHandler(Service service) { - return new AvaticaJsonHandler(service); - } - }); - params.add(new Object[] {jsonServer, Driver.Serialization.JSON}); - ACTIVE_SERVERS.add(jsonServer); - - final HttpServer protobufServer = Main.start(mainArgs, 0, new HandlerFactory() { - @Override public AvaticaProtobufHandler createHandler(Service service) { - return new AvaticaProtobufHandler(service); - } - }); - params.add(new Object[] {protobufServer, Driver.Serialization.PROTOBUF}); - - ACTIVE_SERVERS.add(protobufServer); - - return params; + SERVERS.startServers(); + return SERVERS.getJUnitParameters(); } - public RemoteMetaTest(HttpServer server, Driver.Serialization serialization) { + public RemoteMetaTest(Driver.Serialization serialization, HttpServer server) { this.server = server; this.port = this.server.getPort(); this.serialization = serialization; - url = "jdbc:avatica:remote:url=http://localhost:" + port + ";serialization=" - + serialization.name(); + this.url = SERVERS.getJdbcUrl(port, serialization); } @AfterClass public static void afterClass() throws Exception { - for (HttpServer server : ACTIVE_SERVERS) { - if (server != null) { - server.stop(); - } + if (null != SERVERS) { + SERVERS.stopServers(); } } @@ -747,28 +715,6 @@ public class RemoteMetaTest { assertEquals(props, originalProps); } } - - /** Factory that provides a {@link JdbcMeta}. */ - public static class FullyRemoteJdbcMetaFactory implements Meta.Factory { - - private static JdbcMeta instance = null; - - private static JdbcMeta getInstance() { - if (instance == null) { - try { - instance = new JdbcMeta(CONNECTION_SPEC.url, CONNECTION_SPEC.username, - CONNECTION_SPEC.password); - } catch (SQLException e) { - throw new RuntimeException(e); - } - } - return instance; - } - - @Override public Meta create(List<String> args) { - return getInstance(); - } - } } // End RemoteMetaTest.java
