1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
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   * <testcase xmlns="jelly:jameleon">
40   *       <map-variable toVariable="resultsText" variableType="list">
41   *           <variable-value>value 1</variable-value>
42   *           <variable-value>value 2</variable-value>
43   *           <variable-value>value 3</variable-value>
44   *           <variable-value>value 4</variable-value>
45   *       </map-variable/>
46   *
47   *   <some-session application="someApp" beginSession="true">
48   *       <some-tag-that-uses-context-variables
49   *           functionId="Verify successful navigation, using a different variable."
50   *           someVariable="${resultsText}/>
51   *   </some-session>
52   * </testcase>
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   * <testcase xmlns="jelly:jameleon">
59   *
60   *   <some-session application="someApp" beginSession="true">
61   *       <some-tag-that-uses-context-variables
62   *           functionId="Verify successful navigation, using a different variable.">
63   *           <map-variable
64   *                 fromVariable="resultsText1"
65   *                 toVariable="resultsText"/>
66   *       </some-tag-that-uses-context-variables>
67   *   </some-session>
68   * </testcase>
69   * </source></pre>
70   * 
71   * The following is an example of using the ${varName} notation (recommended)
72   * <pre><source>
73   * <testcase xmlns="jelly:jameleon">
74   *
75   *   <some-session application="someApp" beginSession="true">
76   *       <some-tag-that-uses-context-variables
77   *           functionId="Verify successful navigation, using a different variable."
78   *           resultsText="${resultsText1"/>
79   *   </some-session>
80   * </testcase>
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   * <testcase xmlns="jelly:jameleon">
88   *
89   *   <some-session application="someApp" beginSession="true">
90   *       <some-tag-that-uses-context-variables
91   *           functionId="Verify successful navigation, using a different variable.">
92   *           <map-variable
93   *                 fromVariable="resultsText1"
94   *                 toVariable="resultsText"
95   *                 variableType="list"/>
96   *           <map-variable
97   *                 fromVariable="resultsText2"
98   *                 toVariable="resultsText"
99   *                 variableType="list"/>
100  *           <map-variable
101  *                 fromVariable="resultsText3"
102  *                 toVariable="resultsText"
103  *                 variableType="list"/>
104  *       </some-tag-that-uses-context-variables>
105  *   </some-session>
106  * </testcase>
107  * </source></pre>
108  * 
109  * The value(s) can be entered directly like so:
110  * 
111  * <pre><source>
112  * <testcase xmlns="jelly:jameleon">
113  *
114  *   <some-session application="someApp" beginSession="true">
115  *       <some-tag-that-uses-context-variables
116  *           functionId="Verify successful navigation, using a different variable.">
117  *           <map-variable toVariable="resultsText" variableType="list">
118  *               <variable-value>value 1</variable-value>
119  *               <variable-value>value 2</variable-value>
120  *               <variable-value>value 3</variable-value>
121  *               <variable-value>value 4</variable-value>
122  *           </map-variable/>
123  *       </some-tag-that-uses-context-variables>
124  *   </some-session>
125  * </testcase>
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 }