diff --git beeline/src/java/org/apache/hive/beeline/BeeLine.java beeline/src/java/org/apache/hive/beeline/BeeLine.java index 1e289ca..5631b73 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -1015,13 +1015,6 @@ public ConsoleReader getConsoleReader(InputStream inputStream) throws IOExceptio } } - try { - // now set the output for the history - consoleReader.setHistory(new FileHistory(new File(getOpts().getHistoryFile()))); - } catch (Exception e) { - handleException(e); - } - if (inputStream instanceof FileInputStream || inputStream instanceof FSDataInputStream) { // from script.. no need to load history and no need of completer, either return consoleReader; @@ -1034,7 +1027,9 @@ public ConsoleReader getConsoleReader(InputStream inputStream) throws IOExceptio ((FileHistory) consoleReader.getHistory()).load(new ByteArrayInputStream(hist .toByteArray())); } else { - consoleReader.getHistory().add(hist.toString()); + for (String str : hist.toString("UTF-8").split("\n")) { + consoleReader.getHistory().add(str); + } } } } catch (Exception e) { diff --git beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java new file mode 100644 index 0000000..0e07235 --- /dev/null +++ beeline/src/test/org/apache/hive/beeline/TestBeeLineHistory.java @@ -0,0 +1,67 @@ +/** + * 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; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.PrintStream; +import java.io.PrintWriter; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * TestBeeLineHistory - executes tests of the !history command of BeeLine + */ +public class TestBeeLineHistory { + + private static final String fileName = "history"; + + @BeforeClass + public static void beforeTests() throws Exception { + PrintWriter writer = new PrintWriter(fileName); + writer.println("select 1;"); + writer.println("select 2;"); + writer.close(); + } + + @Test + public void testNumHistories() throws Exception { + String[] args = {"!history"}; + ByteArrayOutputStream os = new ByteArrayOutputStream(); + PrintStream ops = new PrintStream(os); + BeeLine beeline = new BeeLine(); + beeline.getOpts().setHistoryFile(fileName); + beeline.setOutputStream(ops); + beeline.getConsoleReader(null); + beeline.dispatch("!history"); + String output = os.toString("UTF-8"); + int numHistories = output.split("\n").length; + Assert.assertEquals(numHistories, 2); + beeline.close(); + } + + @AfterClass + public static void afterTests() throws Exception { + File file = new File(fileName); + file.delete(); + } +}