View Javadoc

1   // Copyright (C) 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;
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   * Common tests for Jython script engine services.
45   *
46   * @author Philip Aston
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 }