|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Expression.java | 50% | 86.4% | 88.9% | 81.1% |
|
1 |
package abbot.script;
|
|
2 |
|
|
3 |
import java.util.*;
|
|
4 |
|
|
5 |
import org.jdom.*;
|
|
6 |
import org.jdom.Element;
|
|
7 |
import bsh.*;
|
|
8 |
import abbot.AssertionFailedError;
|
|
9 |
|
|
10 |
/** Provides evaluation of arbitrary Java expressions. Any Java expression is
|
|
11 |
supported, with a more loose syntax if desired. See the
|
|
12 |
<a href=http://www.beanshell.org/docs.html>beanshell documentation</a> for
|
|
13 |
complete details of the extended features available in this evaluator.
|
|
14 |
<p>
|
|
15 |
Note that any variables declared or assigned will be available to any
|
|
16 |
subsequent steps in the same Script.
|
|
17 |
*/
|
|
18 |
|
|
19 |
public class Expression extends Step { |
|
20 |
|
|
21 |
public static final String TAG_EXPRESSION = "expression"; |
|
22 |
|
|
23 |
private static final String USAGE = "<expression>{java/beanshell expression}</expression>"; |
|
24 |
private String expression = ""; |
|
25 |
|
|
26 | 1 |
public Expression(Resolver resolver, Element el, Map attributes) {
|
27 | 1 |
super(resolver, attributes);
|
28 |
|
|
29 | 1 |
String expr = null;
|
30 | 1 |
Iterator iter = el.getContent().iterator(); |
31 | 1 |
while (iter.hasNext()) {
|
32 | 1 |
Object o = iter.next(); |
33 | 1 |
if (o instanceof CDATA) { |
34 | 1 |
expr = ((CDATA)o).getText(); |
35 | 1 |
break;
|
36 |
} |
|
37 |
} |
|
38 | 1 |
if (expr == null) |
39 | 0 |
expr = el.getText(); |
40 | 1 |
setExpression(expr); |
41 |
} |
|
42 |
|
|
43 | 2 |
public Expression(Resolver resolver, String description) {
|
44 | 2 |
super(resolver, description);
|
45 |
} |
|
46 |
|
|
47 | 0 |
public String getDefaultDescription() {
|
48 | 0 |
return getExpression();
|
49 |
} |
|
50 | 1 |
public String getUsage() { return USAGE; } |
51 | 2 |
public String getXMLTag() { return TAG_EXPRESSION; } |
52 |
|
|
53 | 1 |
protected Element addContent(Element el) {
|
54 | 1 |
return el.addContent(new CDATA(getExpression())); |
55 |
} |
|
56 |
|
|
57 | 9 |
public void setExpression(String text) { |
58 | 9 |
expression = text; |
59 |
} |
|
60 |
|
|
61 | 6 |
public String getExpression() {
|
62 | 6 |
return expression;
|
63 |
} |
|
64 |
|
|
65 |
/** Evaluates the expression. */
|
|
66 | 1 |
protected void runStep() throws Throwable { |
67 | 1 |
Interpreter sh = (Interpreter) |
68 |
getResolver().getProperty(Script.INTERPRETER); |
|
69 | 1 |
try {
|
70 | 1 |
sh.eval(getExpression()); |
71 |
} |
|
72 |
catch(TargetError e) {
|
|
73 | 0 |
throw e.getTarget();
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
|
|