View Javadoc

1   // Copyright (C) 2005 - 2012 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit tests for {@link JythonScriptEngine}.
45   *
46   * @author Philip Aston
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 }