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.ant;
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 Ant script launcher implementation.
28   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
29   * @version $Revision: 1.2 $
30   */
31  public class AntScriptLauncherTest extends ScriptLauncherTest{
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** The Ant build file handler. */
36     private File buildFile = null;
37     /** The file for storing build logs. */
38     private File logFile = null;
39  
40  
41     // Constructors -------------------------------------------------------------
42  
43     /** Default constructor. Build a test case using a name. */
44     public AntScriptLauncherTest(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(AntScriptLauncherTest.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 buid file in this directory.
67        buildFile = new File(workingDir, "build.xml");
68        writeToFile(buildFile, getClass().getResourceAsStream("build.xml"));
69        // Prepare log file.
70        logFile = new File(workingDir, "ant.log");
71     }
72  
73     /** Test the execution of script with default target */
74     public void testRunScript(){
75        // Create a ant launcher with default target.
76        AntScriptLauncher launcher = new AntScriptLauncher();
77        try{
78           launcher.setScriptPath(buildFile.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 fullDir = new File(workingDir, "full");
86        File mytargetDir = new File(workingDir, "mytarget");
87        assertFalse("'full' directory does not exist", fullDir.isDirectory());
88        assertFalse("'full' directory does not exist", fullDir.exists());
89        assertFalse("'mytarget' directory does not exist", mytargetDir.isDirectory());
90        assertFalse("'mytarget' directory does not exist", mytargetDir.exists());
91        assertTrue("'ant.log' file does not exist", logFile.exists());
92        // Launch script execution.
93        Properties properties = new Properties();
94        properties.setProperty("working.dir", workingDir.getPath());
95        try {launcher.runScript(properties);}
96        catch (ScriptException se){
97           fail("Running the script should not fail");
98        }
99        // Assert after running script.
100       assertTrue("'full' directory now exists", fullDir.isDirectory());
101       assertTrue("'mytarget' directory now exists", mytargetDir.isDirectory());
102       assertTrue("'ant.log' file now exists", logFile.isFile());
103       assertTrue("'ant.log' file is not empty", logFile.length() > 0);
104    }
105 
106    /** Test the execution of script with specified target */
107    public void testRunScriptWithTarget(){
108       // Create a ant launcher with specified target.
109       AntScriptLauncher launcher = new AntScriptLauncher();
110       launcher.setTarget("mytarget");
111       try {launcher.setScriptPath(buildFile.getPath());}
112       catch (Exception e){
113          fail("Setting the script path should not fail");
114       }
115       // Assert before running script.
116       File fullDir = new File(workingDir, "full");
117       File mytargetDir = new File(workingDir, "mytarget");
118       assertFalse("'full' directory does not exist", fullDir.isDirectory());
119       assertFalse("'full' directory does not exist", fullDir.exists());
120       assertFalse("'mytarget' directory does not exist", mytargetDir.isDirectory());
121       assertFalse("'mytarget' directory does not exist", mytargetDir.exists());
122       // Launch script execution.
123       Properties properties = new Properties();
124       properties.setProperty("working.dir", workingDir.getPath());
125       try {launcher.runScript(properties);}
126       catch (ScriptException se){
127          fail("Running the script should not fail");
128       }
129       // Assert after running script.
130       assertFalse("'full' directory does not exist", fullDir.isDirectory());
131       assertTrue("'mytarget' directory now exist", mytargetDir.isDirectory());
132    }
133 }