changeset 1993:19f86f71ea64

Update to jline 2.13 Backport of http://icedtea.classpath.org/hg/thermostat/rev/88978705da03 Reviewed-by: neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019958.html PR3058
author Severin Gehwolf <sgehwolf@redhat.com>
date Wed, 29 Jun 2016 14:22:37 +0200
parents e42ee59a71fb
children 121607fc9843
files integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/CliTest.java launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java pom.xml setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/PasswordCredentialsReaderTest.java setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/UsernameCredentialsReaderTest.java
diffstat 6 files changed, 133 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/CliTest.java	Wed Jun 29 12:23:13 2016 -0400
+++ b/integration-tests/itest-run/src/test/java/com/redhat/thermostat/itest/CliTest.java	Wed Jun 29 14:22:37 2016 +0200
@@ -37,6 +37,8 @@
 package com.redhat.thermostat.itest;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -235,7 +237,29 @@
         shell.send("exit\n");
         shell.expectClose();
 
-        assertTrue(shell.getCurrentStandardErrContents().contains("!?!: event not found"));
+        assertFalse(shell.getCurrentStandardErrContents().contains("!?!: event not found"));
+        assertNoExceptions(shell.getCurrentStandardOutContents(), shell.getCurrentStandardErrContents());
+    }
+
+    @Test
+    public void testRecognizedEventsInShell() throws Exception {
+        // test '!' events
+        Spawn shell = spawnThermostat("shell");
+
+        shell.expect(SHELL_DISCONNECT_PROMPT);
+        shell.send("help\n");
+        shell.expect(SHELL_DISCONNECT_PROMPT);
+        shell.send("!hel\n");
+        shell.expect(SHELL_DISCONNECT_PROMPT);
+        shell.send("exit\n");
+        shell.expectClose();
+
+        String EXPECTED_OUTPUT_FROM_HELP = "list of commands";
+        String stdOut = shell.getCurrentStandardOutContents();
+        int firstIndex = stdOut.indexOf(EXPECTED_OUTPUT_FROM_HELP);
+        assertFalse(firstIndex == -1);
+        int secondIndex = stdOut.indexOf(EXPECTED_OUTPUT_FROM_HELP, firstIndex + 1);
+        assertFalse(secondIndex == -1);
         assertNoExceptions(shell.getCurrentStandardOutContents(), shell.getCurrentStandardErrContents());
     }
 
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Wed Jun 29 12:23:13 2016 -0400
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/ShellCommand.java	Wed Jun 29 14:22:37 2016 +0200
@@ -37,18 +37,13 @@
 package com.redhat.thermostat.launcher.internal;
 
 import java.io.IOException;
+import java.io.OutputStream;
+import java.io.PrintStream;
 import java.util.Arrays;
 import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import jline.Terminal;
-import jline.TerminalFactory;
-import jline.console.ConsoleReader;
-import jline.console.history.FileHistory;
-import jline.console.history.History;
-import jline.console.history.PersistentHistory;
-
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 
@@ -67,6 +62,13 @@
 import com.redhat.thermostat.shared.locale.Translate;
 import com.redhat.thermostat.storage.core.DbService;
 
+import jline.Terminal;
+import jline.TerminalFactory;
+import jline.console.ConsoleReader;
+import jline.console.history.FileHistory;
+import jline.console.history.History;
+import jline.console.history.PersistentHistory;
+
 public class ShellCommand extends AbstractCommand {
 
     private static final Logger logger = LoggingUtils.getLogger(ShellCommand.class);
@@ -115,6 +117,8 @@
         this.prefs = prefs;
         this.shellPrompt = new ShellPrompt();
 
+        disableJlineLogging();
+
         try {
             Map<String, String> promptConfig = config.getConfiguration("shell-command", "shell-prompt.conf");
             this.shellPrompt.overridePromptConfig(promptConfig);
@@ -122,6 +126,32 @@
             //Do nothing
         }
     }
+
+    private void disableJlineLogging() {
+        /*
+         * newer versions of jline will always log event expansion errors with
+         * stack trace to stderr. Disable that.
+         */
+        OutputStream nullOutputStream = new OutputStream() {
+
+            @Override
+            public void write(int b) throws IOException {
+                // do not write anything
+            }
+
+            @Override
+            public void write(byte[] b) throws IOException {
+                // do not write anything
+            }
+
+            @Override
+            public void write(byte[] b, int off, int len) throws IOException {
+                // do not write anything
+            }
+        };
+
+        jline.internal.Log.setOutput(new PrintStream(nullOutputStream));
+    }
     
     @Override
     public void run(CommandContext ctx) throws CommandException {
@@ -172,15 +202,7 @@
      */
     private boolean handleConsoleInput(ConsoleReader reader, Console console) throws IOException, CommandException {
         String line;
-        try {
-            line = reader.readLine(shellPrompt.getPrompt());
-        } catch (IllegalArgumentException iae) {
-            if (iae.getMessage().endsWith(": event not found")) {
-                console.getError().println(iae.getMessage());
-                return true;
-            }
-            throw iae;
-        }
+        line = reader.readLine(shellPrompt.getPrompt());
         if (line == null) {
             return false;
         }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java	Wed Jun 29 12:23:13 2016 -0400
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/ShellCommandTest.java	Wed Jun 29 14:22:37 2016 +0200
@@ -45,18 +45,12 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.EnumSet;
 
-import com.redhat.thermostat.common.config.ClientPreferences;
-import jline.TerminalFactory;
-import jline.TerminalFactory.Flavor;
-import jline.TerminalFactory.Type;
-import jline.UnixTerminal;
-import jline.console.history.PersistentHistory;
-
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.junit.After;
@@ -65,17 +59,24 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 
-import com.redhat.thermostat.launcher.internal.ShellCommand.HistoryProvider;
 import com.redhat.thermostat.common.Version;
 import com.redhat.thermostat.common.cli.Arguments;
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.common.cli.SimpleArguments;
+import com.redhat.thermostat.common.config.ClientPreferences;
 import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource;
 import com.redhat.thermostat.common.utils.StringUtils;
 import com.redhat.thermostat.launcher.Launcher;
+import com.redhat.thermostat.launcher.internal.ShellCommand.HistoryProvider;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 
+import jline.TerminalFactory;
+import jline.TerminalFactory.Flavor;
+import jline.TerminalFactory.Type;
+import jline.UnixTerminal;
+import jline.console.history.PersistentHistory;
+
 public class ShellCommandTest {
 
     static private final String VERSION = "Thermostat some version";
@@ -157,7 +158,7 @@
         Arguments args = new SimpleArguments();
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
-        assertEquals(INTRO + PROMPT + "quit\n", ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT + "quit\n", makeNewlinesConsistent(ctxFactory.getOutput()));
         assertEquals("", ctxFactory.getError());
     }
 
@@ -168,7 +169,7 @@
         Arguments args = new SimpleArguments();
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
-        assertEquals(INTRO + PROMPT + "q\n", ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT + "q\n", makeNewlinesConsistent(ctxFactory.getOutput()));
         assertEquals("", ctxFactory.getError());
     }
 
@@ -179,7 +180,7 @@
         Arguments args = new SimpleArguments();
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
-        assertEquals(INTRO + PROMPT, ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT, makeNewlinesConsistent(ctxFactory.getOutput()));
         assertEquals("", ctxFactory.getError());
     }
 
@@ -190,7 +191,7 @@
         Arguments args = new SimpleArguments();
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
-        assertEquals(INTRO + PROMPT + "\n" + PROMPT + "exit\n", ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT + "\n" + PROMPT + "exit\n", makeNewlinesConsistent(ctxFactory.getOutput()));
     }
 
     @Test
@@ -214,7 +215,7 @@
         CommandContext ctx = ctxFactory.createContext(args);
         cmd.run(ctx);
 
-        assertEquals(INTRO + PROMPT + "old-history-value\n" + PROMPT + "exit\n", ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT + "old-history-value\n" + PROMPT + "exit\n", makeNewlinesConsistent(ctxFactory.getOutput()));
         assertEquals("", ctxFactory.getError());
 
         verify(launcher).run(new String[] {"old-history-value"}, true);
@@ -240,7 +241,7 @@
         verify(mockHistory).add("add-to-history");
         verify(mockHistory).flush();
 
-        assertEquals(INTRO + PROMPT + "add-to-history\n" + PROMPT + "exit\n", ctxFactory.getOutput());
+        assertEquals(INTRO + PROMPT + "add-to-history\n" + PROMPT + "exit\n", makeNewlinesConsistent(ctxFactory.getOutput()));
         assertEquals("", ctxFactory.getError());
     }
 
@@ -289,23 +290,6 @@
         assertEquals("", ctxFactory.getError());
     }
 
-    private String getOutputWithoutIntro(final TestCommandContextFactory ctxFactory) {
-        String[] allOutput = ctxFactory.getOutput().split("\n");
-        String[] outputWithoutIntro = Arrays.copyOfRange(allOutput, 2, allOutput.length);
-        return StringUtils.join("\n", Arrays.asList(outputWithoutIntro));
-    }
-
-    private String getTabOutput(final String outputToProcess) {
-        String[] allOutput = outputToProcess.split("\n");
-        String tabOutput = "";
-        for (String output : allOutput) {
-            if (!output.startsWith("Thermostat")) {
-                tabOutput += output;
-            }
-        }
-        return tabOutput;
-    }
-
     @Test
     public void testPartiallyFilledInputTabCompletes() throws CommandException {
         ServiceReference ref = mock(ServiceReference.class);
@@ -728,6 +712,40 @@
         assertEquals("", ctxFactory.getError());
     }
 
+    private String getOutputWithoutIntro(final TestCommandContextFactory ctxFactory) {
+        String[] allOutput = makeNewlinesConsistent(ctxFactory.getOutput()).split("\n");
+        String[] outputWithoutIntro = Arrays.copyOfRange(allOutput, 2, allOutput.length);
+        return StringUtils.join("\n", Arrays.asList(outputWithoutIntro));
+    }
+
+    private String getTabOutput(final String outputToProcess) {
+        String[] allOutput = outputToProcess.split("\n");
+        String tabOutput = "";
+        for (String output : allOutput) {
+            if (!output.startsWith("Thermostat")) {
+                tabOutput += output;
+            }
+        }
+        return tabOutput;
+    }
+
+    private String getTabbedInline(TestCommandContextFactory ctxFactory, String input) {
+        String inline = "";
+        String[] lines = makeNewlinesConsistent(ctxFactory.getOutput()).split("\n");
+        for(String line : lines) {
+            if (line.contains(input)) {
+                inline = line.split(PROMPT + input)[1];
+                inline = inline.replaceAll(" ", "");
+                return inline;
+            }
+        }
+        return inline;
+    }
+
+    private String makeNewlinesConsistent(String input) {
+        return input.replace("\r\n", "\n");
+    }
+
     private void createTempFile(String name) throws IOException {
         File file = new File(dir, name);
         file.deleteOnExit();
@@ -740,19 +758,6 @@
         file.createNewFile();
     }
 
-    private String getTabbedInline(TestCommandContextFactory ctxFactory, String input) {
-        String inline = "";
-        String[] lines = ctxFactory.getOutput().split("\n");
-        for(String line : lines) {
-            if (line.contains(input)) {
-                inline = line.split(PROMPT + input)[1];
-                inline = inline.replaceAll(" ", "");
-                return inline;
-            }
-        }
-        return inline;
-    }
-
     private File makeTempDir(String name) {
         File tempDir = new File(System.getProperty("java.io.tmpdir") + File.separator + "sc-" + name);
         tempDir.deleteOnExit();
@@ -852,5 +857,6 @@
         when(infos.getCommandInfos()).thenReturn(infoList);
         cmd.setCommandInfoSource(infos);
     }
+
 }
 
--- a/pom.xml	Wed Jun 29 12:23:13 2016 -0400
+++ b/pom.xml	Wed Jun 29 14:22:37 2016 +0200
@@ -171,7 +171,7 @@
     <commons-codec.osgi-version>1.7.0</commons-codec.osgi-version>
     <commons-fileupload.version>1.2.2</commons-fileupload.version>
 
-    <jline.version>2.9</jline.version>
+    <jline.version>2.13</jline.version>
     <lucene.version>5.1.0_1</lucene.version>
     <lucene.osgi-version>5.1.0.1</lucene.osgi-version>
     <lucene-analysis.bundle.symbolic-name>org.apache.servicemix.bundles.lucene-analyzers-common</lucene-analysis.bundle.symbolic-name>
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/PasswordCredentialsReaderTest.java	Wed Jun 29 12:23:13 2016 -0400
+++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/PasswordCredentialsReaderTest.java	Wed Jun 29 14:22:37 2016 +0200
@@ -79,7 +79,8 @@
         char[] password = credsReader.readPassword();
         assertEquals("second", new String(password));
         assertEquals("Passwords did not match!\n", new String(berr.toByteArray()));
-        assertEquals("tell me the password: \nrepeat the password: \ntell me the password: \nrepeat the password: \n", new String(bout.toByteArray()));
+        assertEquals("tell me the password: \nrepeat the password: \ntell me the password: \nrepeat the password: \n",
+                makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     @Test
@@ -88,8 +89,9 @@
         when(console.getInput()).thenReturn(new ByteArrayInputStream(input.getBytes()));
         char[] password = credsReader.readPassword();
         assertEquals("a", new String(password));
-        assertEquals("Chosen password invalid!\n", new String(berr.toByteArray()));
-        assertEquals("tell me the password: \nrepeat the password: \ntell me the password: \nrepeat the password: \n", new String(bout.toByteArray()));
+        assertEquals("Chosen password invalid!\n", makeNewlinesConsistent(new String(berr.toByteArray())));
+        assertEquals("tell me the password: \nrepeat the password: \ntell me the password: \nrepeat the password: \n",
+                makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     @Test
@@ -99,7 +101,7 @@
         char[] password = credsReader.readPassword();
         assertArrayEquals(new char[] { 'b', 'a', 'r' }, password);
         assertEquals("Expected no errors", "", new String(berr.toByteArray()));
-        assertEquals("tell me the password: \nrepeat the password: \n", new String(bout.toByteArray()));
+        assertEquals("tell me the password: \nrepeat the password: \n", makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     @Test
@@ -109,7 +111,7 @@
         char[] password = credsReader.readPassword();
         assertArrayEquals(new char[] { 'b', 'a', 'r' }, password);
         assertEquals("Expected no errors", "", new String(berr.toByteArray()));
-        assertEquals("tell me the password: \nrepeat the password: \n", new String(bout.toByteArray()));
+        assertEquals("tell me the password: \nrepeat the password: \n", makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     /*
@@ -172,4 +174,9 @@
         }
         return builder.toString();
     }
+
+    private String makeNewlinesConsistent(String input) {
+        return input.replace("\r\n", "\n");
+    }
+
 }
--- a/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/UsernameCredentialsReaderTest.java	Wed Jun 29 12:23:13 2016 -0400
+++ b/setup/command/src/test/java/com/redhat/thermostat/setup/command/internal/cli/UsernameCredentialsReaderTest.java	Wed Jun 29 14:22:37 2016 +0200
@@ -77,7 +77,7 @@
         String username = credsReader.read();
         assertEquals("foo-user", username);
         assertEquals("Expected no errors", "", new String(berr.toByteArray()));
-        assertEquals("tell me the username: foo-user\n", new String(bout.toByteArray()));
+        assertEquals("tell me the username: foo-user\n", makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     @Test
@@ -87,7 +87,7 @@
         String username = credsReader.read();
         assertEquals("try-second-time", username);
         assertEquals("Chosen username '' invalid!\n", new String(berr.toByteArray()));
-        assertEquals("tell me the username: \ntell me the username: try-second-time\n", new String(bout.toByteArray()));
+        assertEquals("tell me the username: \ntell me the username: try-second-time\n", makeNewlinesConsistent(new String(bout.toByteArray())));
     }
     
     /*
@@ -124,4 +124,9 @@
         }
         return builder.toString();
     }
+
+    private String makeNewlinesConsistent(String input) {
+        return input.replace("\r\n", "\n");
+    }
+
 }