@@ -7,13 +7,16 @@ import org.openstreetmap.josm.plugins.scripting.model.ScriptEngineMetaDataProvid
77import org.openstreetmap.josm.plugins.scripting.ui.ScriptExecutor
88
99import static org.junit.Assert.assertEquals
10+ import static org.junit.Assert.assertTrue
1011
1112class JythonBindingsTest extends JOSMFixtureBasedTest {
1213
1314 public static String __file__holder = null
15+ public static boolean scriptPathInSysPath = false
16+ public static int sum = 0
1417
1518 @Test
16- void ensureDefaultBindingsAreSet () {
19+ void ensure__file__IsSet () {
1720 final jythonDesc = ScriptEngineMetaDataProvider
1821 .getAvailablePluggedScriptEngines()
1922 .filter(desc -> desc. isJython())
@@ -32,4 +35,60 @@ JythonBindingsTest.__file__holder = __file__
3235 assertEquals (scriptFile. getPath(), __file__holder)
3336 scriptFile. delete()
3437 }
38+
39+ @Test
40+ void ensureSysPathIsSet () {
41+ final jythonDesc = ScriptEngineMetaDataProvider
42+ .getAvailablePluggedScriptEngines()
43+ .filter(desc -> desc. isJython())
44+ .findAny()
45+ .orElseThrow(() -> new IllegalStateException (" No Jython scripting engine found" ))
46+
47+ final executor = new ScriptExecutor (MainApplication . getMainFrame())
48+
49+ // generate temporary script file
50+ final scriptFile = File . createTempFile(" test-script" , " " )
51+ scriptFile. write("""
52+ import sys
53+ from org.openstreetmap.josm.plugins.scripting.jsr223 import JythonBindingsTest
54+ for path in sys.path:
55+ if path == '${ scriptFile.parent} ':
56+ JythonBindingsTest.scriptPathInSysPath = True
57+ """ )
58+ executor. runScriptWithPluggedEngine(jythonDesc, scriptFile)
59+ assertTrue (scriptPathInSysPath)
60+ scriptFile. delete()
61+ }
62+
63+ @Test
64+ void ensureImportFromSysPathWorks () {
65+ final jythonDesc = ScriptEngineMetaDataProvider
66+ .getAvailablePluggedScriptEngines()
67+ .filter(desc -> desc. isJython())
68+ .findAny()
69+ .orElseThrow(() -> new IllegalStateException (" No Jython scripting engine found" ))
70+
71+ final executor = new ScriptExecutor (MainApplication . getMainFrame())
72+
73+ // generate temporary script file
74+ final scriptDir = File . createTempDir()
75+ final moduleFile = new File (scriptDir, " my_module.py" )
76+ final scriptFile = new File (scriptDir, " my_script.py" )
77+
78+
79+ moduleFile. write("""
80+ def add(a, b):
81+ return a + b
82+ """ )
83+ scriptFile. write("""
84+ from org.openstreetmap.josm.plugins.scripting.jsr223 import JythonBindingsTest
85+ from my_module import add
86+ JythonBindingsTest.sum = add(5,3)
87+ """ )
88+ executor. runScriptWithPluggedEngine(jythonDesc, scriptFile)
89+ assertEquals (8 , sum)
90+ scriptFile. delete()
91+ moduleFile. delete()
92+
93+ }
3594}
0 commit comments