1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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              
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  }