1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.services.scripting.jsr223;
16  
17  import org.figure8.join.services.scripting.ScriptException;
18  import org.figure8.join.services.scripting.ScriptLauncherTest;
19  
20  import junit.framework.Test;
21  import junit.framework.TestSuite;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.util.Properties;
26  /**
27   * JUnit test case for testing JSR-223 script launcher implementation.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.2 $
30   */
31  public class JSR223ScriptLauncherTest extends ScriptLauncherTest{
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** The JSR-223 script file handler. */
36     private File scriptFile = null;
37     /** The file for storing script logs. */
38     private File logFile = null;
39  
40  
41     // Constructors -------------------------------------------------------------
42  
43     /** Default constructor. Build a test case using a name. */
44     public JSR223ScriptLauncherTest(String testName){
45        super(testName);
46     }
47  
48  
49     // Override of TestCase -----------------------------------------------------
50  
51     /**
52      * Make this class a TestSuite.
53      * @return A TestSuite containing this TestCase.
54      */
55     public static Test suite(){
56        TestSuite suite = new TestSuite(JSR223ScriptLauncherTest.class);
57        return suite;
58     }
59     
60     
61     // Public -------------------------------------------------------------------
62  
63     /** Setup the test fixtures */
64     public void setUp() throws Exception{
65        super.setUp();
66        // Copy script file in this directory.
67        scriptFile = new File(workingDir, "test.js");
68        writeToFile(scriptFile, getClass().getResourceAsStream("test.js"));
69        // Prepare log file.
70        logFile = new File(workingDir, "jsr223.log");
71     }
72  
73     /** Test the execution of script */
74     public void testRunScriptJS(){
75        // Create a jsr launcher.
76        JSR223ScriptLauncher launcher = new JSR223ScriptLauncher();
77        launcher.setEngine("js");
78        try{
79           launcher.setScriptPath(scriptFile.getPath());
80           launcher.setLogOutputStream(new FileOutputStream(logFile));
81        }
82        catch (Exception e){
83           fail("Setting the script path should not fail");
84        }
85        // Assert before running script.
86        File javascriptDir = new File(workingDir, "javascript");
87        assertFalse("'javascript' directory does not exist", javascriptDir.isDirectory());
88        assertFalse("'javascript' directory does not exist", javascriptDir.exists());
89        assertTrue("'jsr223.log' file does not exist", logFile.exists());
90        // Launch script execution.
91        Properties properties = new Properties();
92        properties.setProperty("working.dir", workingDir.getPath());
93        try {launcher.runScript(properties);}
94        catch (ScriptException se){
95           fail("Running the script should not fail");
96        }
97        // Assert after running script.
98        assertTrue("'javascript' directory now exists", javascriptDir.isDirectory());
99        assertTrue("'javascript.log' file now exists", logFile.isFile());
100       assertTrue("'jsr223.log' file is not empty", logFile.length() > 0);
101    }
102 }