From 4d941b597d9df77597ec571ab89d2c6891089474 Mon Sep 17 00:00:00 2001 From: snuyanzin Date: Mon, 6 May 2019 22:34:26 +0200 Subject: [PATCH] [JLINE3-376] Add a test indicating ArrayIndexOutOfBoundException, adaptrs fix based on review comments --- .../java/org/jline/builtins/Commands.java | 10 +-- .../java/org/jline/builtins/CommandsTest.java | 67 +++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 builtins/src/test/java/org/jline/builtins/CommandsTest.java diff --git a/builtins/src/main/java/org/jline/builtins/Commands.java b/builtins/src/main/java/org/jline/builtins/Commands.java index cdbaaed34..6f9a65727 100644 --- a/builtins/src/main/java/org/jline/builtins/Commands.java +++ b/builtins/src/main/java/org/jline/builtins/Commands.java @@ -226,8 +226,8 @@ public static void history(LineReader reader, PrintStream out, PrintStream err, } int firstId = opt.args().size() > argId ? parseInteger(opt.args().get(argId++)) : -17; int lastId = opt.args().size() > argId ? parseInteger(opt.args().get(argId++)) : -1; - firstId = historyId(firstId, history.size() - 1); - lastId = historyId(lastId, history.size() - 1); + firstId = historyId(firstId, history.first(), history.last()); + lastId = historyId(lastId, history.first(), history.last()); if (firstId > lastId) { throw new IllegalArgumentException(); } @@ -275,13 +275,13 @@ public static void history(LineReader reader, PrintStream out, PrintStream err, } } - private static int historyId(int id, int maxId) { + private static int historyId(int id, int minId, int maxId) { int out = id; if (id < 0) { out = maxId + id + 1; } - if (out < 0) { - out = 0; + if (out < minId) { + out = minId; } else if (out > maxId) { out = maxId; } diff --git a/builtins/src/test/java/org/jline/builtins/CommandsTest.java b/builtins/src/test/java/org/jline/builtins/CommandsTest.java new file mode 100644 index 000000000..13424d5ba --- /dev/null +++ b/builtins/src/test/java/org/jline/builtins/CommandsTest.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2002-2018, the original author or authors. + * + * This software is distributable under the BSD license. See the terms of the + * BSD license in the documentation provided with this software. + * + * https://opensource.org/licenses/BSD-3-Clause + */ +package org.jline.builtins; + +import org.jline.reader.History; +import org.jline.reader.LineReader; +import org.jline.reader.LineReaderBuilder; +import org.jline.reader.impl.history.DefaultHistory; +import org.jline.terminal.Size; +import org.jline.terminal.Terminal; +import org.jline.terminal.TerminalBuilder; +import org.junit.Test; + +import java.io.*; +import java.nio.file.Files; + +import static org.junit.Assert.assertEquals; + +public class CommandsTest { + @Test + public void testHistoryForFileWithMoreHistoryRecordsThanAtHistoryFileSize() { + try { + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + final File tmpHistoryFile = Files.createTempFile("tmpHistory", "temp").toFile(); + tmpHistoryFile.deleteOnExit(); + try (BufferedWriter bw = + new BufferedWriter( + new OutputStreamWriter( + new FileOutputStream(tmpHistoryFile)))) { + bw.write("1536743099591:SELECT \\n CURRENT_TIMESTAMP \\n as \\n c1;\n" + + "1536743104551:SELECT \\n 'asd' as \"sdf\", 4 \\n \\n as \\n c2;\\n\n" + + "1536743104551:SELECT \\n 'asd' \\n as \\n c2;\\n\n" + + "1536743104551:!/ 2\n" + + "1536743104551:SELECT \\n 2123 \\n as \\n c2 from dual;\\n\n" + + "1536743107526:!history\n" + + "1536743115431:SELECT \\n 2 \\n as \\n c2;\n" + + "1536743115431:SELECT \\n '213' \\n as \\n c1;\n" + + "1536743115431:!/ 8\n"); + bw.flush(); + } + Terminal terminal = TerminalBuilder.builder().streams(System.in, System.out).build(); + terminal.setSize(new Size(50, 30)); + final History historyFromFile = new DefaultHistory(); + final LineReaderBuilder lineReaderBuilder = LineReaderBuilder.builder() + .terminal(terminal) + .variable(LineReader.HISTORY_FILE, tmpHistoryFile.getAbsolutePath()) + .history(historyFromFile); + final LineReader lineReader = lineReaderBuilder.build(); + historyFromFile.attach(lineReader); + final int maxLines = 3; + lineReader.setVariable(LineReader.HISTORY_FILE_SIZE, maxLines); + lineReader.getHistory().save(); + PrintStream out = new PrintStream(os, false); + Commands.history(lineReader, out, out, new String[] {"-d"}); + assertEquals(maxLines + 1, + os.toString("UTF8").split("\\s+\\d{2}:\\d{2}:\\d{2}\\s+").length); + } catch (Exception e) { + throw new RuntimeException("Test failed", e); + } + } +} \ No newline at end of file