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 org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27
28 import java.io.File;
29 import java.util.List;
30
31 import net.grinder.common.GrinderProperties;
32 import net.grinder.engine.common.ScriptLocation;
33 import net.grinder.engine.process.dcr.DCRContextImplementation;
34 import net.grinder.script.NotWrappableTypeException;
35 import net.grinder.scriptengine.DCRContext;
36 import net.grinder.scriptengine.Instrumenter;
37
38 import org.junit.Test;
39 import org.python.core.PyObject;
40 import org.python.core.PySystemState;
41
42
43
44
45
46
47
48 public class TestJythonScriptEngineService
49 extends AbstractJythonScriptEngineServiceTests {
50
51 @Test public void testInitialisationWithCollocatedGrinderAndJythonJars()
52 throws Exception {
53
54 System.clearProperty("python.cachedir");
55 final String originalClasspath = System.getProperty("java.class.path");
56
57 final File grinderJar = new File(getDirectory(), "grinder.jar");
58 grinderJar.createNewFile();
59 final File jythonJar = new File(getDirectory(), "jython.jar");
60 jythonJar.createNewFile();
61
62 System.setProperty("java.class.path",
63 grinderJar.getAbsolutePath() + File.pathSeparator +
64 jythonJar.getAbsolutePath());
65
66 try {
67 m_jythonScriptEngineService.createScriptEngine(m_pyScript);
68
69 assertNotNull(System.getProperty("python.cachedir"));
70 System.clearProperty("python.cachedir");
71 }
72 finally {
73 System.setProperty("java.class.path", originalClasspath);
74 }
75 }
76
77 @Test public void testCreateInstrumentedProxy() throws Exception {
78 final GrinderProperties properties = new GrinderProperties();
79 final DCRContext context = DCRContextImplementation.create(null);
80
81 final List<Instrumenter> instrumenters =
82 new JythonScriptEngineService(properties, context, m_pyScript)
83 .createInstrumenters();
84
85 assertEquals(1, instrumenters.size());
86
87 final Instrumenter instrumenter = instrumenters.get(0);
88
89 assertEquals("byte code transforming instrumenter for Jython 2.5",
90 instrumenter.getDescription());
91
92 final Object foo = new Object();
93
94 PySystemState.initialize();
95
96 instrumenter.createInstrumentedProxy(m_test, m_recorder, foo);
97
98 try {
99 instrumenter.createInstrumentedProxy(m_test,
100 m_recorder,
101 new PyObject());
102 fail("Expected NotWrappableTypeException");
103 }
104 catch (NotWrappableTypeException e) {
105 }
106 }
107
108 @Test public void testInstrument() throws Exception {
109 final GrinderProperties properties = new GrinderProperties();
110 final DCRContextImplementation context = DCRContextImplementation.create(null);
111
112 final List<Instrumenter> instrumenters =
113 new JythonScriptEngineService(properties, context, m_pyScript)
114 .createInstrumenters();
115
116 assertEquals(1, instrumenters.size());
117
118 final Instrumenter instrumenter = instrumenters.get(0);
119
120 final Object foo = new Object();
121
122 instrumenter.instrument(m_test, m_recorder, foo, null);
123 }
124
125 @Test public void testWithNoInstrumenters() throws Exception {
126 final List<Instrumenter> instrumenters =
127 new JythonScriptEngineService(new GrinderProperties(),
128 null,
129 new ScriptLocation(new File("notpy.blah")))
130 .createInstrumenters();
131
132 assertEquals(0, instrumenters.size());
133 }
134 }