View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2006 Christian W. Hargraves (engrean@hotmail.com)
4       
5       This library is free software; you can redistribute it and/or
6       modify it under the terms of the GNU Lesser General Public
7       License as published by the Free Software Foundation; either
8       version 2.1 of the License, or (at your option) any later version.
9   
10      This library 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. See the GNU
13      Lesser General Public License for more details.
14  
15      You should have received a copy of the GNU Lesser General Public
16      License along with this library; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19  package net.sf.jameleon;
20  
21  import java.io.File;
22  
23  import org.apache.commons.jelly.JellyTagException;
24  import org.apache.commons.jelly.MissingAttributeException;
25  import org.apache.commons.jelly.XMLOutput;
26  
27  /***
28   * This is currently a tag meant to be nested inside the test-suite tag.
29   * It represents one of the scripts to be included and executed in the test-suite.
30   * If a test-script nested inside a <code>precondition</code> tag fails, then 
31   * none of the following scripts will be executed even if they are nested inside
32   * a postcondition tag.
33   * @jameleon.function name="test-script"
34   */
35  public class TestScriptTag extends JameleonTagSupport {
36  
37  
38      protected TestSuiteTag tst;
39      /***
40       * The script to execute.
41       * @jameleon.attribute required="true"
42       */
43      protected File script;
44      /***
45       * Do not continue executing any following scripts if a script
46       * marked as a precondition fails.
47       * @jameleon.attribute default="false"
48       */
49      protected boolean precondition;
50  
51      public void init() throws MissingAttributeException{
52          testForUnsupportedAttributesCaught();
53          broker.transferAttributes(context);
54          broker.validate(context);
55      }
56  
57      public void setUpEnvironment(){
58          Object obj =  findAncestorWithClass(TestSuiteTag.class);
59          if (obj != null){
60              tst = (TestSuiteTag)obj;
61          }else{
62              throw new ClassCastException("Can only execute a test-script tag under the test-suite tag! "+getClass().getName());
63          }
64          obj =  findAncestorWithClass(PreconditionTag.class);
65          if (obj != null){
66              precondition = true;
67          }
68      }
69  
70      /***
71       * This method executes the script.
72       */
73      public void doTag(XMLOutput out) throws MissingAttributeException, JellyTagException{
74          init();
75          setUpEnvironment();
76          ExecuteTestCase executor = tst.getExecuteTestCase();
77          String errMsg = null;
78          try{
79              errMsg = executor.execute(script);
80          }catch (Exception e){
81              //Do nothing here because we want to continue running the next script
82          }
83          if (precondition && errMsg != null && errMsg.length() > 0) {
84              throw new JellyTagException("PRECONDITION FAILURE: "+errMsg, getFileName(), getElementName(), getColumnNumber(), getLineNumber());
85          }
86          
87      }
88  
89  }