Jeff Allen avatar Jeff Allen committed 0c48290

Test we get a PlainConsole when we should.

Comments (0)

Files changed (1)

tests/java/org/python/util/jythonTestPlain.java

+package org.python.util;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+import org.python.core.Console;
+import org.python.core.PlainConsole;
+import org.python.core.Py;
+
+/**
+ * Tests of creating and getting the right interactive console.
+ * <p>
+ * System initialisation is a one-time thing normally, and the embedding of a console handler
+ * similarly, so it is difficult to test more than one console choice in a single executable. For
+ * this reason, there are two programs like this one: one that follows the native preference for a
+ * {@link JLineConsole} and this one that induces selection of a {@link PlainConsole}
+ * <p>
+ * <p>
+ * Automated testing of the console seems impossible since, in a scripted context (e.g. as a
+ * subprocess or under Ant) Jython is no longer interactive. To run it at the prompt, suggested
+ * idiom is (all one line):
+ *
+ * <pre>
+ * java -cp build/exposed;build/classes;extlibs/* -Dpython.home=dist
+ *              org.junit.runner.JUnitCore org.python.util.jythonTestPlain
+ * </pre>
+ */
+public class jythonTestPlain {
+
+    private static final String PYTHON_CONSOLE = "python.console";
+    private static String[] commands = {"-c", "import sys; print type(sys._jy_console)"};
+
+    /**
+     * Test that specifying an invalid console class falls back to a plain console. No, really,
+     * "org.python.util.InteractiveConsole" is not a {@link Console}! A warning to that effect will
+     * be issued when the test runs.
+     */
+    @Test
+    public void testFallbackConsole() {
+        System.out.println("testFallbackConsole");
+        System.getProperties().setProperty(PYTHON_CONSOLE, "org.python.util.InteractiveConsole");
+        jython.run(commands);
+        Console console = Py.getConsole();
+        assertEquals(PlainConsole.class, console.getClass());
+    }
+
+    /**
+     * Show that a {@link PlainConsole} may be replaced with a {@link JLineConsole}.
+     */
+    @Test
+    public void testChangeConsole() throws Exception {
+        System.out.println("testChangeConsole");
+        // In the case where this test is run in isolation, cause initialisation with PlainConsole
+        System.getProperties().setProperty(PYTHON_CONSOLE, "org.python.core.PlainConsole");
+        PythonInterpreter interp = new PythonInterpreter();
+        // Now replace it
+        Py.installConsole(new JLineConsole(null));
+        jython.run(commands);
+        Console console = Py.getConsole();
+        assertEquals(JLineConsole.class, console.getClass());
+        interp.cleanup();
+    }
+
+}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.