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.groovy;
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 Groovy script launcher implementation.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.2 $
30   */
31  public class GroovyScriptLauncherTest extends ScriptLauncherTest{
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** The Groovy 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 GroovyScriptLauncherTest(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(GroovyScriptLauncherTest.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.groovy");
68        writeToFile(scriptFile, getClass().getResourceAsStream("test.groovy"));
69        // Prepare log file.
70        logFile = new File(workingDir, "groovy.log");
71     }
72  
73     /** Test the execution of script */
74     public void testRunScript(){
75        // Create a groovy launcher.
76        GroovyScriptLauncher launcher = new GroovyScriptLauncher();
77        try{
78           launcher.setScriptPath(scriptFile.getPath());
79           launcher.setLogOutputStream(new FileOutputStream(logFile));
80        }
81        catch (Exception e){
82           fail("Setting the script path should not fail");
83        }
84        // Assert before running script.
85        File groovyDir = new File(workingDir, "groovy");
86        assertFalse("'groovy' directory does not exist", groovyDir.isDirectory());
87        assertFalse("'groovy' directory does not exist", groovyDir.exists());
88        assertTrue("'groovy.log' file does not exist", logFile.exists());
89        // Launch script execution.
90        Properties properties = new Properties();
91        properties.setProperty("working.dir", workingDir.getPath());
92        try {launcher.runScript(properties);}
93        catch (ScriptException se){
94           fail("Running the script should not fail");
95        }
96        // Assert after running script.
97        assertTrue("'groovy' directory now exists", groovyDir.isDirectory());
98        assertTrue("'groovy.log' file now exists", logFile.isFile());
99        assertTrue("'groovy.log' file is not empty", logFile.length() > 0);
100    }
101 }