ACCUMULO-2742 offset history command by one The history entries returned by the history command are 0-indexed, while the history expansion is 1-indexed. We need to offset the index when we print it so that users can accurately use event expansion.
Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/1f7dd2d5 Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/1f7dd2d5 Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/1f7dd2d5 Branch: refs/heads/master Commit: 1f7dd2d53ffebb5bac629f61caffa70c50526730 Parents: 0aa4758 Author: Mike Drob <md...@cloudera.com> Authored: Mon Apr 28 15:14:23 2014 -0400 Committer: Mike Drob <md...@cloudera.com> Committed: Mon Apr 28 23:20:35 2014 -0400 ---------------------------------------------------------------------- .../util/shell/commands/HistoryCommand.java | 31 +++---- .../util/shell/command/HistoryCommandTest.java | 90 ++++++++++++++++++++ 2 files changed, 102 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/accumulo/blob/1f7dd2d5/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java index 9531d90..d6068ba 100644 --- a/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java +++ b/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java @@ -18,7 +18,6 @@ package org.apache.accumulo.core.util.shell.commands; import java.io.IOException; import java.util.Iterator; -import java.util.ListIterator; import jline.console.history.History.Entry; @@ -27,39 +26,33 @@ import org.apache.accumulo.core.util.shell.Shell.Command; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; -import org.apache.commons.collections.iterators.AbstractIteratorDecorator; + +import com.google.common.base.Function; +import com.google.common.collect.Iterators; public class HistoryCommand extends Command { private Option clearHist; private Option disablePaginationOpt; - @SuppressWarnings("unchecked") @Override public int execute(final String fullCommand, final CommandLine cl, final Shell shellState) throws IOException { if (cl.hasOption(clearHist.getOpt())) { shellState.getReader().getHistory().clear(); } else { - ListIterator<Entry> it = shellState.getReader().getHistory().entries(); - shellState.printLines(new HistoryLineIterator(it), !cl.hasOption(disablePaginationOpt.getOpt())); + Iterator<Entry> source = shellState.getReader().getHistory().entries(); + Iterator<String> historyIterator = Iterators.transform(source, new Function<Entry,String>() { + @Override + public String apply(Entry input) { + return String.format("%d: %s", input.index() + 1, input.value()); + } + }); + + shellState.printLines(historyIterator, !cl.hasOption(disablePaginationOpt.getOpt())); } return 0; } - /** - * Decorator that converts an Iterator<History.Entry> to an Iterator<String>. - */ - private static class HistoryLineIterator extends AbstractIteratorDecorator { - public HistoryLineIterator(Iterator<Entry> iterator) { - super(iterator); - } - - @Override - public String next() { - return super.next().toString(); - } - } - @Override public String description() { return ("generates a list of commands previously executed"); http://git-wip-us.apache.org/repos/asf/accumulo/blob/1f7dd2d5/core/src/test/java/org/apache/accumulo/core/util/shell/command/HistoryCommandTest.java ---------------------------------------------------------------------- diff --git a/core/src/test/java/org/apache/accumulo/core/util/shell/command/HistoryCommandTest.java b/core/src/test/java/org/apache/accumulo/core/util/shell/command/HistoryCommandTest.java new file mode 100644 index 0000000..4d379cc --- /dev/null +++ b/core/src/test/java/org/apache/accumulo/core/util/shell/command/HistoryCommandTest.java @@ -0,0 +1,90 @@ +/* + * 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.accumulo.core.util.shell.command; + +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import jline.console.ConsoleReader; +import jline.console.history.History; +import jline.console.history.MemoryHistory; + +import org.apache.accumulo.core.util.shell.Shell; +import org.apache.accumulo.core.util.shell.commands.HistoryCommand; +import org.apache.commons.cli.CommandLine; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +public class HistoryCommandTest { + + HistoryCommand command; + CommandLine cl; + + ByteArrayOutputStream baos; + ConsoleReader reader; + Shell shell; + + @Before + public void setUp() throws Exception { + command = new HistoryCommand(); + command.getOptions(); // Make sure everything is initialized + + cl = createMock(CommandLine.class); + expect(cl.hasOption("c")).andReturn(false); + expect(cl.hasOption("np")).andReturn(true); + replay(cl); + + History history = new MemoryHistory(); + history.add("foo"); + history.add("bar"); + + baos = new ByteArrayOutputStream(); + + String input = String.format("!1%n"); // Construct a platform dependent new-line + reader = new ConsoleReader(new ByteArrayInputStream(input.getBytes()), baos); + reader.setHistory(history); + + shell = new Shell(reader, null); + } + + @Test + public void testCorrectNumbering() throws IOException { + command.execute("", cl, shell); + reader.flush(); + + assertTrue(baos.toString().contains("2: bar")); + } + + @Test + public void testEventExpansion() throws IOException { + // If we use an unsupported terminal, then history expansion doesn't work because JLine can't do magic buffer manipulations. + // This has been observed to be the case on certain versions of Eclipse. However, mvn is usually fine. + Assume.assumeTrue(reader.getTerminal().isSupported()); + + reader.readLine(); + + assertTrue(baos.toString().trim().endsWith("foo")); + } + +}