File: jythonTest.java

package info (click to toggle)
jython 2.5.3-16%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,772 kB
  • ctags: 106,434
  • sloc: python: 351,322; java: 216,349; xml: 1,584; sh: 330; perl: 114; ansic: 102; makefile: 45
file content (88 lines) | stat: -rw-r--r-- 2,616 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package org.python.util;

import java.lang.reflect.Method;
import java.util.Properties;

import org.python.core.PySystemState;

import junit.framework.TestCase;

/**
 * Tests for creating the right interactive console.
 */
public class jythonTest extends TestCase {

    private static final String PYTHON_CONSOLE = "python.console";

    private Properties _originalRegistry;

    @Override
    protected void setUp() throws Exception {
        _originalRegistry = PySystemState.registry;
        Properties registry;
        if (_originalRegistry != null) {
            registry = new Properties(_originalRegistry);
        } else {
            registry = new Properties();
        }
        PySystemState.registry = registry;
    }

    @Override
    protected void tearDown() throws Exception {
        PySystemState.registry = _originalRegistry;
    }

    /**
     * test the default behavior
     * 
     * @throws Exception
     */
    public void testNewInterpreter() throws Exception {
        assertEquals(JLineConsole.class, invokeNewInterpreter(true).getClass());
    }

    /**
     * test registry override
     * 
     * @throws Exception
     */
    public void testNewInterpreter_registry() throws Exception {
        PySystemState.registry.setProperty(PYTHON_CONSOLE, "org.python.util.InteractiveConsole");
        assertEquals(InteractiveConsole.class, invokeNewInterpreter(true).getClass());
    }

    /**
     * test fallback in case of an invalid registry value
     * 
     * @throws Exception
     */
    public void testNewInterpreter_unknown() throws Exception {
        PySystemState.registry.setProperty(PYTHON_CONSOLE, "foo.bar.NoConsole");
        assertEquals(JLineConsole.class, invokeNewInterpreter(true).getClass());
    }

    /**
     * test non-interactive fallback to legacy console
     * 
     * @throws Exception
     */
    public void testNewInterpreter_NonInteractive() throws Exception {
        assertEquals(InteractiveConsole.class, invokeNewInterpreter(false).getClass());
    }

    /**
     * Invoke the private static method 'newInterpreter(boolean)' on jython.class
     * 
     * @throws Exception
     */
    private InteractiveConsole invokeNewInterpreter(boolean interactiveStdin) throws Exception {
        Method method = jython.class.getDeclaredMethod("newInterpreter", Boolean.TYPE);
        assertNotNull(method);
        method.setAccessible(true);
        Object result = method.invoke(null, interactiveStdin);
        assertNotNull(result);
        assertTrue(result instanceof InteractiveConsole);
        return (InteractiveConsole)result;
    }
}