View Javadoc

1   // Copyright (C) 2005 - 2011 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.instrumentation.dcr;
23  
24  import static org.junit.Assert.assertEquals;
25  
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  import net.grinder.engine.process.dcr.DCRContextImplementation;
30  import net.grinder.scriptengine.CompositeInstrumenter;
31  import net.grinder.scriptengine.Instrumenter;
32  import net.grinder.scriptengine.java.JavaScriptEngineService;
33  import net.grinder.testutility.Jython25Runner;
34  import net.grinder.testutility.RandomStubFactory;
35  
36  import org.junit.Test;
37  import org.junit.runner.RunWith;
38  import org.python.core.PyObject;
39  import org.python.core.PyProxy;
40  
41  
42  /**
43   * Unit tests for {@link JythonInstrumenter}.
44   *
45   * @author Philip Aston
46   */
47  @RunWith(Jython25Runner.class)
48  public class TestJython25Instrumenter
49    extends AbstractJythonDCRInstrumenterTestCase {
50  
51    private static Instrumenter createInstrumenter() throws Exception {
52      final List<Instrumenter> instrumenters = new ArrayList<Instrumenter>();
53  
54      instrumenters.add(
55        new Jython25Instrumenter(DCRContextImplementation.create(null)));
56  
57      instrumenters.addAll(
58        new JavaScriptEngineService(DCRContextImplementation.create(null))
59        .createInstrumenters());
60  
61      return new CompositeInstrumenter(instrumenters);
62    }
63  
64    public TestJython25Instrumenter() throws Exception {
65      super(createInstrumenter());
66    }
67  
68    @Test public void testVersion() throws Exception {
69      assertVersion("2.5");
70    }
71  
72    @Test public void testBrokenPyProxy() throws Exception {
73      final RandomStubFactory<PyProxy> pyProxyStubFactory =
74        RandomStubFactory.create(PyProxy.class);
75      final PyProxy pyProxy = pyProxyStubFactory.getStub();
76  
77      assertNotWrappable(pyProxy);
78    }
79  
80    @Test public void testCreateProxyWithJavaClassAnd__call__() throws Exception {
81      m_interpreter.exec("from grinder.test import MyClass");
82      final PyObject pyJavaType = m_interpreter.get("MyClass");
83      createInstrumentedProxy(m_test, m_recorder, pyJavaType);
84  
85      m_interpreter.exec("result2 = MyClass.__call__(1, 2, 3)");
86      final PyObject result2 = m_interpreter.get("result2");
87      assertEquals(m_one, result2.invoke("getA"));
88      m_recorderStubFactory.assertSuccess("start");
89      m_recorderStubFactory.assertSuccess("end", true);
90      m_recorderStubFactory.assertNoMoreCalls();
91    }
92  }