1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.scriptengine.jython;
23
24 import static net.grinder.testutility.FileUtilities.createFile;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28
29 import java.io.File;
30
31 import net.grinder.common.StubTest;
32 import net.grinder.engine.common.ScriptLocation;
33 import net.grinder.scriptengine.Recorder;
34 import net.grinder.testutility.AbstractJUnit4FileTestCase;
35 import net.grinder.util.Directory;
36
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41
42
43
44
45
46
47
48 public abstract class AbstractJythonScriptEngineServiceTests
49 extends AbstractJUnit4FileTestCase {
50
51 protected final net.grinder.common.Test m_test =
52 new StubTest(1, "foo");
53
54 protected ScriptLocation m_pyScript;
55
56 protected JythonScriptEngineService m_jythonScriptEngineService =
57 new JythonScriptEngineService();
58
59 @Mock protected Recorder m_recorder;
60
61 public AbstractJythonScriptEngineServiceTests() {
62 super();
63 }
64
65 @Before
66 public void setUp() throws Exception {
67 MockitoAnnotations.initMocks(this);
68
69 m_pyScript =
70 new ScriptLocation(new Directory(getDirectory()), new File("some.py"));
71
72 createFile(m_pyScript.getFile(),
73 "class TestRunner: pass");
74 }
75
76 @Test
77 public void testInitialisationWithEmptyClasspath() throws Exception {
78 System.clearProperty("python.cachedir");
79 final String originalClasspath = System.getProperty("java.class.path");
80
81 System.setProperty("java.class.path", "");
82
83 try {
84 m_jythonScriptEngineService.createScriptEngine(m_pyScript);
85
86 assertNotNull(System.getProperty("python.cachedir"));
87 System.clearProperty("python.cachedir");
88 }
89 finally {
90 System.setProperty("java.class.path", originalClasspath);
91 System.clearProperty("python.cachedir");
92 }
93 }
94
95 @Test public void testInitialisationWithNonCollocatedGrinderAndJythonJars()
96 throws Exception {
97
98 final String temporaryCacheDir = getDirectory().getPath();
99
100 System.setProperty("python.cachedir", temporaryCacheDir);
101
102 m_jythonScriptEngineService.createScriptEngine(m_pyScript);
103
104 assertEquals(temporaryCacheDir, System.getProperty("python.cachedir"));
105 }
106
107 @Test public void testInitialisationWithCacheDir() throws Exception {
108
109 System.clearProperty("python.cachedir");
110 final String originalClasspath = System.getProperty("java.class.path");
111
112 final File anotherDirectory = new File(getDirectory(), "foo");
113 anotherDirectory.mkdirs();
114 final File grinderJar = new File(anotherDirectory, "grinder.jar");
115 grinderJar.createNewFile();
116 final File jythonJar = new File(getDirectory(), "jython.jar");
117 jythonJar.createNewFile();
118
119 System.setProperty("java.class.path",
120 grinderJar.getAbsolutePath() + File.pathSeparator +
121 jythonJar.getAbsolutePath());
122
123 try {
124 m_jythonScriptEngineService.createScriptEngine(m_pyScript);
125 assertNull(System.getProperty("python.cachedir"));
126 }
127 finally {
128 System.setProperty("java.class.path", originalClasspath);
129 }
130 }
131
132 @Test public void testNoMatch() throws Exception {
133 final ScriptLocation notPyScript = new ScriptLocation(new File("blah.clj"));
134
135 assertNull(m_jythonScriptEngineService.createScriptEngine(notPyScript));
136 }
137
138 }