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.clojure;
23  
24  import static java.util.Collections.singleton;
25  import static net.grinder.testutility.AssertUtilities.assertContains;
26  import static net.grinder.testutility.FileUtilities.createFile;
27  import static org.junit.Assert.assertEquals;
28  import static org.junit.Assert.assertNull;
29  import static org.junit.Assert.fail;
30  
31  import java.io.File;
32  import java.util.Collections;
33  import java.util.List;
34  
35  import net.grinder.engine.common.EngineException;
36  import net.grinder.engine.common.ScriptLocation;
37  import net.grinder.scriptengine.Instrumenter;
38  import net.grinder.scriptengine.ScriptEngineService;
39  import net.grinder.scriptengine.ScriptEngineService.ScriptEngine;
40  import net.grinder.testutility.AbstractJUnit4FileTestCase;
41  import net.grinder.util.BlockingClassLoader;
42  import net.grinder.util.Directory;
43  
44  import org.junit.Test;
45  
46  
47  /**
48   * Unit tests for {@link ClojureScriptEngineService}.
49   *
50   * @author Philip Aston
51   */
52  public class TestClojureScriptEngineService extends AbstractJUnit4FileTestCase {
53  
54    @Test public void testCreateInstrumenters() throws Exception {
55      final List<? extends Instrumenter> instrumenters =
56        new ClojureScriptEngineService().createInstrumenters();
57  
58      assertEquals(0, instrumenters.size());
59    }
60  
61    @Test public void testCreateScriptEngineWrongType() throws Exception {
62  
63      final ScriptLocation someScript =
64        new ScriptLocation(new File("some.thing"));
65  
66      final ScriptEngine result =
67        new ClojureScriptEngineService().createScriptEngine(someScript);
68  
69      assertNull(result);
70    }
71  
72    @Test public void testCreateScriptEngine() throws Exception {
73  
74      final ScriptLocation script =
75        new ScriptLocation(new Directory(getDirectory()), new File("my.clj"));
76  
77      createFile(script.getFile(),
78                 "(fn [] (fn [] ()))");
79  
80      final ScriptEngine result =
81        new ClojureScriptEngineService().createScriptEngine(script);
82  
83      assertContains(result.getDescription(), "Clojure");
84    }
85  
86    @Test public void testCreateScriptEngineNoClojure() throws Exception {
87  
88      final ScriptLocation script =
89        new ScriptLocation(new Directory(getDirectory()), new File("my.clj"));
90  
91      final ClassLoader blockingLoader =
92        new BlockingClassLoader(singleton("clojure.*"),
93                                singleton("net.grinder.scriptengine.clojure.*"),
94                                Collections.<String>emptySet(),
95                                false);
96  
97      final ScriptEngineService service =
98        (ScriptEngineService) blockingLoader.loadClass(
99           ClojureScriptEngineService.class.getName()).newInstance();
100 
101     try {
102       service.createScriptEngine(script);
103       fail("Expected EngineException");
104     }
105     catch (EngineException e) {
106       assertContains(e.getMessage(), "classpath");
107     }
108   }
109 }