diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java index e69de29..9ee305b 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/BeelineWithHS2ConnectionFileTestBase.java @@ -0,0 +1,214 @@ +/** + * 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.hive.beeline.hs2connection; + +import static org.junit.Assert.assertFalse; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.sql.DriverManager; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.apache.hive.beeline.BeeLine; +import org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser; +import org.apache.hive.beeline.hs2connection.HiveSiteHS2ConnectionFileParser; +import org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser; +import org.apache.hive.jdbc.miniHS2.MiniHS2; +import org.apache.hive.service.cli.CLIServiceClient; +import org.apache.hive.service.cli.HiveSQLException; +import org.apache.hive.service.cli.OperationHandle; +import org.apache.hive.service.cli.RowSet; +import org.apache.hive.service.cli.SessionHandle; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; + +public abstract class BeelineWithHS2ConnectionFileTestBase { + protected MiniHS2 miniHS2; + protected HiveConf hiveConf = new HiveConf(); + protected final String tableName = "testBeelineTable"; + protected String dataFileDir = hiveConf.get("test.data.files"); + protected static final String LOCALHOST_KEY_STORE_NAME = "keystore.jks"; + protected static final String TRUST_STORE_NAME = "truststore.jks"; + protected static final String KEY_STORE_TRUST_STORE_PASSWORD = "HiveJdbc"; + protected static final String HS2_HTTP_MODE = "http"; + protected static final String HS2_HTTP_ENDPOINT = "cliservice"; + private final String fileLocation = + System.getProperty("java.io.tmpdir") + "testHs2ConnectionConfig.xml"; + protected static final String JAVA_TRUST_STORE_PROP = "javax.net.ssl.trustStore"; + protected static final String JAVA_TRUST_STORE_PASS_PROP = "javax.net.ssl.trustStorePassword"; + + protected Map confOverlay = new HashMap<>(); + + protected class TestBeeLine extends BeeLine { + UserHS2ConnectionFileParser testHs2ConfigFileManager; + ByteArrayOutputStream os; + + TestBeeLine(List defaultHS2ConnectionFiles) { + testHs2ConfigFileManager = new UserHS2ConnectionFileParser(defaultHS2ConnectionFiles); + os = new ByteArrayOutputStream(); + PrintStream beelineOutputStream = new PrintStream(os); + setOutputStream(beelineOutputStream); + setErrorStream(beelineOutputStream); + } + + TestBeeLine() { + testHs2ConfigFileManager = new UserHS2ConnectionFileParser(null); + os = new ByteArrayOutputStream(); + PrintStream beelineOutputStream = new PrintStream(os); + setOutputStream(beelineOutputStream); + setErrorStream(beelineOutputStream); + } + + public String getOutput() throws UnsupportedEncodingException { + return os.toString("UTF8"); + } + + @Override + public HS2ConnectionFileParser getUserHS2ConnFileParser() { + return testHs2ConfigFileManager; + } + + @Override + public HS2ConnectionFileParser getHiveSiteHS2ConnectionFileParser() { + HiveSiteHS2ConnectionFileParser ret = new HiveSiteHS2ConnectionFileParser(); + ret.setHiveConf(miniHS2.getHiveConf()); + return ret; + } + } + + /* + * Wrapper class to write a HS2ConnectionConfig file + */ + protected class Hs2ConnectionXmlConfigFileWriter { + private final PrintWriter writer; + private final File file; + private final Configuration conf; + + protected Hs2ConnectionXmlConfigFileWriter() throws IOException { + file = new File(fileLocation); + conf = new Configuration(false); + try { + if (file.exists()) { + file.delete(); + } + file.createNewFile(); + writer = new PrintWriter(file.getAbsolutePath(), "UTF-8"); + } finally { + file.deleteOnExit(); + } + } + + protected void writeProperty(String key, String value) { + conf.set(key, value); + } + + protected String path() { + return file.getAbsolutePath(); + } + + protected void close() throws IOException { + try { + conf.writeXml(writer); + } finally { + writer.close(); + } + } + } + + @BeforeClass + public static void beforeTest() throws Exception { + Class.forName(MiniHS2.getJdbcDriverName()); + } + + @Before + public void before() throws Exception { + DriverManager.setLoginTimeout(0); + if (!System.getProperty("test.data.files", "").isEmpty()) { + dataFileDir = System.getProperty("test.data.files"); + } + dataFileDir = dataFileDir.replace('\\', '/').replace("c:", ""); + hiveConf = new HiveConf(); + miniHS2 = getNewMiniHS2(); + confOverlay = new HashMap(); + confOverlay.put(ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, "false"); + confOverlay.put(ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, "binary"); + } + + protected MiniHS2 getNewMiniHS2() throws Exception { + return new MiniHS2(hiveConf); + } + + @After + public void tearDown() throws Exception { + if (miniHS2 != null && miniHS2.isStarted()) { + miniHS2.stop(); + } + miniHS2 = null; + System.clearProperty(JAVA_TRUST_STORE_PROP); + System.clearProperty(JAVA_TRUST_STORE_PASS_PROP); + } + + protected void createTable() throws HiveSQLException { + CLIServiceClient serviceClient = miniHS2.getServiceClient(); + SessionHandle sessHandle = serviceClient.openSession("foo", "bar"); + serviceClient.executeStatement(sessHandle, "DROP TABLE IF EXISTS " + tableName, confOverlay); + serviceClient.executeStatement(sessHandle, "CREATE TABLE " + tableName + " (id INT)", + confOverlay); + OperationHandle opHandle = + serviceClient.executeStatement(sessHandle, "SHOW TABLES", confOverlay); + RowSet rowSet = serviceClient.fetchResults(opHandle); + assertFalse(rowSet.numRows() == 0); + } + + protected String testBeeLineConnection(String path, String[] beelineArgs, + String expectedOutput) throws IOException { + TestBeeLine beeLine = null; + try { + if(path != null) { + List testLocations = new ArrayList<>(); + testLocations.add(path); + beeLine = new TestBeeLine(testLocations); + } else { + beeLine = new TestBeeLine(); + } + beeLine.begin(beelineArgs, null); + String output = beeLine.getOutput(); + System.out.println(output); + Assert.assertNotNull(output); + Assert.assertTrue("Output " + output + " does not contain " + expectedOutput, + output.toLowerCase().contains(expectedOutput.toLowerCase())); + return output; + } finally { + if (beeLine != null) { + beeLine.close(); + } + } + } +} diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineConnectionUsingHiveSite.java itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineConnectionUsingHiveSite.java index fe77667..fee38fb 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineConnectionUsingHiveSite.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineConnectionUsingHiveSite.java @@ -24,7 +24,7 @@ import org.apache.hive.jdbc.miniHS2.MiniHS2; import org.junit.Test; -public class TestBeelineConnectionUsingHiveSite extends TestBeelineWithHS2ConnectionFile { +public class TestBeelineConnectionUsingHiveSite extends BeelineWithHS2ConnectionFileTestBase { @Test public void testBeelineConnectionHttp() throws Exception { setupHs2(); diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithHS2ConnectionFile.java itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithHS2ConnectionFile.java deleted file mode 100644 index 32e9afd..0000000 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithHS2ConnectionFile.java +++ /dev/null @@ -1,214 +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.hive.beeline.hs2connection; - -import static org.junit.Assert.assertFalse; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.sql.DriverManager; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.hadoop.conf.Configuration; -import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; -import org.apache.hive.beeline.BeeLine; -import org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser; -import org.apache.hive.beeline.hs2connection.HiveSiteHS2ConnectionFileParser; -import org.apache.hive.beeline.hs2connection.UserHS2ConnectionFileParser; -import org.apache.hive.jdbc.miniHS2.MiniHS2; -import org.apache.hive.service.cli.CLIServiceClient; -import org.apache.hive.service.cli.HiveSQLException; -import org.apache.hive.service.cli.OperationHandle; -import org.apache.hive.service.cli.RowSet; -import org.apache.hive.service.cli.SessionHandle; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; - -public abstract class TestBeelineWithHS2ConnectionFile { - protected MiniHS2 miniHS2; - protected HiveConf hiveConf = new HiveConf(); - protected final String tableName = "testBeelineTable"; - protected String dataFileDir = hiveConf.get("test.data.files"); - protected static final String LOCALHOST_KEY_STORE_NAME = "keystore.jks"; - protected static final String TRUST_STORE_NAME = "truststore.jks"; - protected static final String KEY_STORE_TRUST_STORE_PASSWORD = "HiveJdbc"; - protected static final String HS2_HTTP_MODE = "http"; - protected static final String HS2_HTTP_ENDPOINT = "cliservice"; - private final String fileLocation = - System.getProperty("java.io.tmpdir") + "testHs2ConnectionConfig.xml"; - protected static final String JAVA_TRUST_STORE_PROP = "javax.net.ssl.trustStore"; - protected static final String JAVA_TRUST_STORE_PASS_PROP = "javax.net.ssl.trustStorePassword"; - - protected Map confOverlay = new HashMap<>(); - - protected class TestBeeLine extends BeeLine { - UserHS2ConnectionFileParser testHs2ConfigFileManager; - ByteArrayOutputStream os; - - TestBeeLine(List defaultHS2ConnectionFiles) { - testHs2ConfigFileManager = new UserHS2ConnectionFileParser(defaultHS2ConnectionFiles); - os = new ByteArrayOutputStream(); - PrintStream beelineOutputStream = new PrintStream(os); - setOutputStream(beelineOutputStream); - setErrorStream(beelineOutputStream); - } - - TestBeeLine() { - testHs2ConfigFileManager = new UserHS2ConnectionFileParser(null); - os = new ByteArrayOutputStream(); - PrintStream beelineOutputStream = new PrintStream(os); - setOutputStream(beelineOutputStream); - setErrorStream(beelineOutputStream); - } - - public String getOutput() throws UnsupportedEncodingException { - return os.toString("UTF8"); - } - - @Override - public HS2ConnectionFileParser getUserHS2ConnFileParser() { - return testHs2ConfigFileManager; - } - - @Override - public HS2ConnectionFileParser getHiveSiteHS2ConnectionFileParser() { - HiveSiteHS2ConnectionFileParser ret = new HiveSiteHS2ConnectionFileParser(); - ret.setHiveConf(miniHS2.getHiveConf()); - return ret; - } - } - - /* - * Wrapper class to write a HS2ConnectionConfig file - */ - protected class Hs2ConnectionXmlConfigFileWriter { - private final PrintWriter writer; - private final File file; - private final Configuration conf; - - protected Hs2ConnectionXmlConfigFileWriter() throws IOException { - file = new File(fileLocation); - conf = new Configuration(false); - try { - if (file.exists()) { - file.delete(); - } - file.createNewFile(); - writer = new PrintWriter(file.getAbsolutePath(), "UTF-8"); - } finally { - file.deleteOnExit(); - } - } - - protected void writeProperty(String key, String value) { - conf.set(key, value); - } - - protected String path() { - return file.getAbsolutePath(); - } - - protected void close() throws IOException { - try { - conf.writeXml(writer); - } finally { - writer.close(); - } - } - } - - @BeforeClass - public static void beforeTest() throws Exception { - Class.forName(MiniHS2.getJdbcDriverName()); - } - - @Before - public void before() throws Exception { - DriverManager.setLoginTimeout(0); - if (!System.getProperty("test.data.files", "").isEmpty()) { - dataFileDir = System.getProperty("test.data.files"); - } - dataFileDir = dataFileDir.replace('\\', '/').replace("c:", ""); - hiveConf = new HiveConf(); - miniHS2 = getNewMiniHS2(); - confOverlay = new HashMap(); - confOverlay.put(ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, "false"); - confOverlay.put(ConfVars.HIVE_SERVER2_TRANSPORT_MODE.varname, "binary"); - } - - protected MiniHS2 getNewMiniHS2() throws Exception { - return new MiniHS2(hiveConf); - } - - @After - public void tearDown() throws Exception { - if (miniHS2 != null && miniHS2.isStarted()) { - miniHS2.stop(); - } - miniHS2 = null; - System.clearProperty(JAVA_TRUST_STORE_PROP); - System.clearProperty(JAVA_TRUST_STORE_PASS_PROP); - } - - protected void createTable() throws HiveSQLException { - CLIServiceClient serviceClient = miniHS2.getServiceClient(); - SessionHandle sessHandle = serviceClient.openSession("foo", "bar"); - serviceClient.executeStatement(sessHandle, "DROP TABLE IF EXISTS " + tableName, confOverlay); - serviceClient.executeStatement(sessHandle, "CREATE TABLE " + tableName + " (id INT)", - confOverlay); - OperationHandle opHandle = - serviceClient.executeStatement(sessHandle, "SHOW TABLES", confOverlay); - RowSet rowSet = serviceClient.fetchResults(opHandle); - assertFalse(rowSet.numRows() == 0); - } - - protected String testBeeLineConnection(String path, String[] beelineArgs, - String expectedOutput) throws IOException { - TestBeeLine beeLine = null; - try { - if(path != null) { - List testLocations = new ArrayList<>(); - testLocations.add(path); - beeLine = new TestBeeLine(testLocations); - } else { - beeLine = new TestBeeLine(); - } - beeLine.begin(beelineArgs, null); - String output = beeLine.getOutput(); - System.out.println(output); - Assert.assertNotNull(output); - Assert.assertTrue("Output " + output + " does not contain " + expectedOutput, - output.toLowerCase().contains(expectedOutput.toLowerCase())); - return output; - } finally { - if (beeLine != null) { - beeLine.close(); - } - } - } -} diff --git itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithUserHs2ConnectionFile.java itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithUserHs2ConnectionFile.java index 0f86186..f0e20b1 100644 --- itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithUserHs2ConnectionFile.java +++ itests/hive-unit/src/test/java/org/apache/hive/beeline/hs2connection/TestBeelineWithUserHs2ConnectionFile.java @@ -24,7 +24,7 @@ import org.apache.hive.beeline.hs2connection.HS2ConnectionFileParser; import org.junit.Test; -public class TestBeelineWithUserHs2ConnectionFile extends TestBeelineWithHS2ConnectionFile { +public class TestBeelineWithUserHs2ConnectionFile extends BeelineWithHS2ConnectionFileTestBase { @Test public void testBeelineConnectionHttp() throws Exception {