View Javadoc

1   /*
2   
3       Jameleon - An automation testing tool..
4       Copyright (C) 2003 Christian W. Hargraves (engrean@hotmail.com)
5       
6       This library is free software; you can redistribute it and/or
7       modify it under the terms of the GNU Lesser General Public
8       License as published by the Free Software Foundation; either
9       version 2.1 of the License, or (at your option) any later version.
10  
11      This library is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14      Lesser General Public License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public
17      License along with this library; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20  package net.sf.jameleon;
21  
22  import net.sf.jameleon.function.FunctionTag;
23  
24  import org.apache.commons.jelly.JellyTagException;
25  import org.apache.commons.jelly.MissingAttributeException;
26  import org.apache.commons.jelly.Tag;
27  import org.apache.commons.jelly.XMLOutput;
28  
29  /***
30   * Used to map the value in one variable over to the value of another variable. 
31   * 
32   * sThis is useful when the same functional point is being used more than once or 
33   * when the same variable name is used between different functional points that are 
34   * in the same test-case.
35   * 
36   * The following is an example of creating a list and using it in a tag.
37   * 
38   * <pre><source>
39   * &lt;testcase xmlns="jelly:jameleon"&gt;
40   *       &lt;map-variable toVariable="resultsText" variableType="list"&gt;
41   *           &lt;variable-value&gt;value 1&lt;/variable-value&gt;
42   *           &lt;variable-value&gt;value 2&lt;/variable-value&gt;
43   *           &lt;variable-value&gt;value 3&lt;/variable-value&gt;
44   *           &lt;variable-value&gt;value 4&lt;/variable-value&gt;
45   *       &lt;/map-variable/&gt;
46   *
47   *   &lt;some-session application="someApp" beginSession="true"&gt;
48   *       &lt;some-tag-that-uses-context-variables
49   *           functionId="Verify successful navigation, using a different variable."
50   *           someVariable="${resultsText}/&gt;
51   *   &lt;/some-session&gt;
52   * &lt;/testcase&gt;
53   * </source></pre>
54   * 
55   * The following is an example of mapping one value of a variable to another variable (pretty much useless).
56   * 
57   * <pre><source>
58   * &lt;testcase xmlns="jelly:jameleon"&gt;
59   *
60   *   &lt;some-session application="someApp" beginSession="true"&gt;
61   *       &lt;some-tag-that-uses-context-variables
62   *           functionId="Verify successful navigation, using a different variable."&gt;
63   *           &lt;map-variable
64   *                 fromVariable="resultsText1"
65   *                 toVariable="resultsText"/&gt;
66   *       &lt;/some-tag-that-uses-context-variables&gt;
67   *   &lt;/some-session&gt;
68   * &lt;/testcase&gt;
69   * </source></pre>
70   * 
71   * The following is an example of using the ${varName} notation (recommended)
72   * <pre><source>
73   * &lt;testcase xmlns="jelly:jameleon"&gt;
74   *
75   *   &lt;some-session application="someApp" beginSession="true"&gt;
76   *       &lt;some-tag-that-uses-context-variables
77   *           functionId="Verify successful navigation, using a different variable."
78   *           resultsText="${resultsText1"/&gt;
79   *   &lt;/some-session&gt;
80   * &lt;/testcase&gt;
81   * </source></pre>
82   * 
83   * The following is an example of mapping multiple variable values to a single variable. 
84   * Notice it's variableType is set to "list":
85   * 
86   * <pre><source>
87   * &lt;testcase xmlns="jelly:jameleon"&gt;
88   *
89   *   &lt;some-session application="someApp" beginSession="true"&gt;
90   *       &lt;some-tag-that-uses-context-variables
91   *           functionId="Verify successful navigation, using a different variable."&gt;
92   *           &lt;map-variable
93   *                 fromVariable="resultsText1"
94   *                 toVariable="resultsText"
95   *                 variableType="list"/&gt;
96   *           &lt;map-variable
97   *                 fromVariable="resultsText2"
98   *                 toVariable="resultsText"
99   *                 variableType="list"/&gt;
100  *           &lt;map-variable
101  *                 fromVariable="resultsText3"
102  *                 toVariable="resultsText"
103  *                 variableType="list"/&gt;
104  *       &lt;/some-tag-that-uses-context-variables&gt;
105  *   &lt;/some-session&gt;
106  * &lt;/testcase&gt;
107  * </source></pre>
108  * 
109  * The value(s) can be entered directly like so:
110  * 
111  * <pre><source>
112  * &lt;testcase xmlns="jelly:jameleon"&gt;
113  *
114  *   &lt;some-session application="someApp" beginSession="true"&gt;
115  *       &lt;some-tag-that-uses-context-variables
116  *           functionId="Verify successful navigation, using a different variable."&gt;
117  *           &lt;map-variable toVariable="resultsText" variableType="list"&gt;
118  *               &lt;variable-value&gt;value 1&lt;/variable-value&gt;
119  *               &lt;variable-value&gt;value 2&lt;/variable-value&gt;
120  *               &lt;variable-value&gt;value 3&lt;/variable-value&gt;
121  *               &lt;variable-value&gt;value 4&lt;/variable-value&gt;
122  *           &lt;/map-variable/&gt;
123  *       &lt;/some-tag-that-uses-context-variables&gt;
124  *   &lt;/some-session&gt;
125  * &lt;/testcase&gt;
126  * </source></pre>
127  * 
128  * @jameleon.function name="map-variable"
129  */
130 public class VariableMappingTag extends VariableTag {
131     
132     protected boolean childExecuted = false;
133 
134     /***
135      * An implementation of the <code>doTag</code> method provided by the <code>TagSupport</code> class.
136      * Maps the value in the <code>fromVariable</code> over to the original variable name, <code>toVariable</code>.
137      */
138     public void doTag(XMLOutput out) throws MissingAttributeException, JellyTagException{
139         init();
140         invokeBody(out);
141         if (fromVariable != null && 
142             fromVariable.length() > 0 &&
143             toVariable != null &&
144             toVariable.length() > 0 &&
145             variableType != null &&
146             variableType.length() >0) {
147             setVariableValue(toVariable, context.getVariable(fromVariable));
148         }else if (!childExecuted) {
149             super.doTag(out);
150         }
151     }
152 
153     /***
154      * Used to validate everything is set up correctly.
155      * @throws MissingAttributeException - When a required attribute isn't set.
156      * @throws JellyTagException - When this tag is used out of context.
157      */
158     protected void validate()throws MissingAttributeException, JellyTagException{
159         Tag tag = getParent();
160         if ( ! (tag instanceof FunctionTag) ) {
161             throw new JellyTagException("This tag can only be imbedded in a tag represented by a class that extends FunctionTag.");
162         }
163         if ( fromVariable == null || fromVariable.trim().equals("") )  {
164             throw new MissingAttributeException("fromVariable is a required attribute!");
165         }else{
166             value = (String)context.getVariable(fromVariable);
167         }
168         super.validate();
169     }
170 
171     /***
172      *  Gets the variable name in the CSV file being mapped to the officially supported function tag variable name..
173      *  @return the variable name to map from or the variable that does not a <code>set</code> method for in the function point.
174      */
175     public String getFromVariable(){
176         return this.fromVariable;
177     }
178 
179     /***
180      * Sets the variable name in the CSV file being mapped to the officially supported function tag variable name
181      * method. This variable does not a <code>set</code> method for in the function point.
182      * @param fromVariable - The variable name to map from.
183      */
184     public void setFromVariable(String fromVariable){
185         this.fromVariable = fromVariable;
186     }
187 
188     /***
189      * @return The variable name to be mapped to the <code>fromVariable</code> or the originally supported variable name that has a set method for it
190      * in the function point.
191      */
192     public String getToVariable(){
193         return this.toVariable;
194     }
195 
196     /***
197      * Sets the variable name to map the <code>fromVariable</code> to. This is the originally supported variable name that has a set method for it
198      * in the function point.
199      * @param toVariable - the variable name to be mapped to the <code>fromVariable</code>.
200      */
201     public void setToVariable(String toVariable){
202         this.toVariable = toVariable;
203     }
204 
205     /***
206      * Gets the type of the variable being stored. Currently, this only supports List.
207      * @return The type of the variable being stored. Currently, this only supports List.
208      */
209     public String getVariableType(){
210         return this.variableType;
211     }
212 
213     /***
214      * Sets the type of the variable being stored. Currently, this only supports List.
215      * @param variableType - The type of the variable being stored. Currently, this only supports List.
216      */
217     public void setVariableType(String variableType){
218         this.variableType = variableType;
219     }
220 
221     protected void setChildExecuted(boolean childExecuted){
222         this.childExecuted = childExecuted;
223     }
224 
225 }