View Javadoc

1   /*
2       Jameleon - An automation testing tool..
3       Copyright (C) 2003 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.util.ArrayList;
22  import java.util.List;
23  import java.util.Iterator;
24  
25  import org.apache.commons.jelly.MissingAttributeException;
26  import org.apache.commons.jelly.JellyTagException;
27  import org.apache.commons.jelly.TagSupport;
28  import org.apache.commons.jelly.XMLOutput;
29  
30  /***
31   * Used for mapping a List to another variable. 
32   * The implementation of this class will be to decide if the List is a list of values or a List of variabl names.
33   * NOTE:<br>
34   * No setters or getters defined for the instances variables defined below. If these variables are to be defined 
35   * in the tag as attributes, then in the subclass of this class, the setters and getters must be defined for the 
36   * appropriate attributes.
37   */
38  public abstract class VariableTag extends TagSupport {
39      
40      protected static final String LIST = "list";
41      protected static final String STRING = "string";
42  
43      protected TestCaseTag tct;
44      /***
45       * The variable name to map from. This is the variable name is normally <b>not</b> supported by the function tag.
46       */
47      protected String fromVariable;
48      /***
49       * The value of the fromVariable.
50       */
51      protected String value;
52      /***
53       * The variable name to map the <code>fromVariable</code> to. This is the variable name <b>is</b> supported by the function tag.
54       */
55      protected String toVariable;
56      /***
57       * The type of the variable being stored. Currently, this only supports List and String (the default).
58       */
59      protected String variableType = STRING;
60  
61      /***
62       * An implementation of the <code>doTag</code> method provided by the <code>TagSupport</code> class.
63       * Maps the value in the <code>fromVariable</code> over to the original variable name, <code>toVariable</code>.
64       */
65      public void doTag(XMLOutput out) throws MissingAttributeException, JellyTagException{
66          //To support children elements
67          init();
68          validate();
69          setVariableValue(toVariable, value);
70      }
71  
72      public void setVariableValue(String toVariable, Object value){
73          if ( variableType.equalsIgnoreCase(STRING) ) {
74              context.setVariable(toVariable, value);
75          }else if ( variableType.equalsIgnoreCase(LIST) ) {
76              List l = null;
77              Object to = context.getVariable(toVariable);
78              if ( to != null && to instanceof List ) {
79                  l = (List)to;
80              }else if ( to == null || to instanceof String ) {
81                  l = new ArrayList();
82                  if (to instanceof String ) {
83                      //Add the original String type to the beginning of the list.
84                      l.add(to);
85                  }
86              }
87              if (value instanceof List) {
88                  Iterator it = ((List)value).iterator();
89                  while (it.hasNext()) {
90                      l.add(it.next());
91                  }
92              }else{
93                  l.add(value);
94              }
95              setVariable(toVariable,l);
96          }
97      }
98  
99      /***
100      * Used to validate everything is set up correctly.
101      * @throws MissingAttributeException - When a required attribute isn't set.
102      * @throws JellyTagException - When this tag is used out of context.
103      */
104     protected void validate()throws MissingAttributeException, JellyTagException{
105         if (toVariable == null){
106             throw new MissingAttributeException("toVariable is a required attribute!");
107         }else if ( !(STRING.equalsIgnoreCase(variableType)) && !(LIST.equalsIgnoreCase(variableType)) ) {
108             throw new MissingAttributeException("variableType must be set to either string or list!");
109         }
110     }
111 
112     protected void setVariable(String key, Object value){
113         init();
114         context.setVariable(key, value);
115         if (tct != null){
116             tct.getKeySet().add(key);
117         }
118     }
119 
120     protected void init(){
121         if (tct == null) {
122             Object obj =  findAncestorWithClass(TestCaseTag.class);
123             if (obj != null){
124                 tct = (TestCaseTag)obj;
125             }
126         }
127     }
128 }