|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ChooseTagHandler.java | 91.7% | 100% | 100% | 98% |
|
1 |
package junit.extensions.xml.elements;
|
|
2 |
|
|
3 |
import junit.extensions.xml.IXMLTestCase;
|
|
4 |
import junit.extensions.xml.XMLException;
|
|
5 |
import junit.extensions.xml.XMLTagResourceBundle;
|
|
6 |
|
|
7 |
import org.w3c.dom.Element;
|
|
8 |
import org.w3c.dom.Node;
|
|
9 |
import org.w3c.dom.NodeList;
|
|
10 |
|
|
11 |
|
|
12 |
/**
|
|
13 |
* <H3>Title:</H3>
|
|
14 |
* ChooseTagHandler
|
|
15 |
* <H3>Description:</H3>
|
|
16 |
* <p>The ChooseTagHandler allows for some
|
|
17 |
* flow control in a test case. The handler is pattered
|
|
18 |
* after the xsl:choose elements.</p>
|
|
19 |
* <p>The choose tag processes its child elements which
|
|
20 |
* should be either when or otherwise elements. Each when
|
|
21 |
* element contains a test attribute which points to a
|
|
22 |
* tag handler. If the tag handler does not assert, then the
|
|
23 |
* test is assumed to process properly. The when element is
|
|
24 |
* passed as the element to process to the test tag handler
|
|
25 |
* defined. If the when does not cause a assertion, then the
|
|
26 |
* test is assumed to be successful, and the child elements
|
|
27 |
* of the when which succeeded will be processed, and the
|
|
28 |
* processing of the Choose element will conclude.<p><p>
|
|
29 |
*
|
|
30 |
* If none of the When elements are tested successful, then
|
|
31 |
* the otherwise elements children will be processed.<p><p>
|
|
32 |
*
|
|
33 |
* <H3>Example:</H3>
|
|
34 |
* <pre>
|
|
35 |
* <choose>
|
|
36 |
* <when test="assertnotnull" refid="ComponentA">
|
|
37 |
* ...do when...
|
|
38 |
* </when>
|
|
39 |
* <when test="assertnotnull" refid="ComponentB">
|
|
40 |
* ...do when...
|
|
41 |
* </when>
|
|
42 |
* <otherwise>
|
|
43 |
* ...do otherwise...
|
|
44 |
* </otherwise>
|
|
45 |
* </choose>
|
|
46 |
* </pre>
|
|
47 |
* "choose" element requires no attributes but should contain
|
|
48 |
* a ordered list of "when" elements and "otherwise".<p><p>
|
|
49 |
*
|
|
50 |
* "when" element requires one attribute "test" which defines a tag handler
|
|
51 |
* to be processed. Other "when" attributes may be required depending
|
|
52 |
* upon the tag handler specified. Children of the when should
|
|
53 |
* be specified which are to be conditionaly processed.
|
|
54 |
*
|
|
55 |
* "otherwise" element requires no attributes. The children elements
|
|
56 |
* will conditionally be processed.
|
|
57 |
*
|
|
58 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
59 |
* <p>Company: JFCUnit Sourceforge project</p>
|
|
60 |
* @author Kevin Wilson
|
|
61 |
* @version 1.0
|
|
62 |
*/
|
|
63 |
public class ChooseTagHandler extends AbstractTagHandler { |
|
64 |
/** choose string. */
|
|
65 |
private String m_choose;
|
|
66 |
|
|
67 |
/** other string. */
|
|
68 |
private String m_other;
|
|
69 |
|
|
70 |
/** when string. */
|
|
71 |
private String m_when;
|
|
72 |
|
|
73 |
/**
|
|
74 |
* Constructor.
|
|
75 |
* @param element Element to be processed.
|
|
76 |
* @param testCase Test case processing tag handler.
|
|
77 |
*/
|
|
78 | 10 |
public ChooseTagHandler(final Element element, final IXMLTestCase testCase) {
|
79 | 10 |
super(element, testCase);
|
80 |
} |
|
81 |
|
|
82 |
/**
|
|
83 |
* process the element.
|
|
84 |
* @throws XMLException is thrown if the element cannot be understood.
|
|
85 |
*/
|
|
86 | 9 |
public void processElement() throws XMLException { |
87 | 9 |
validateElement(); |
88 |
|
|
89 | 9 |
NodeList nodes = getElement().getChildNodes(); |
90 | 9 |
Element otherwise = null;
|
91 |
|
|
92 | 9 |
for (int i = 0; i < nodes.getLength(); i++) { |
93 | 30 |
Node node = nodes.item(i); |
94 |
|
|
95 | 30 |
if (node instanceof Element) { |
96 | 17 |
if (m_when.equals(node.getNodeName())) {
|
97 | 13 |
boolean result = false; |
98 | 13 |
Element e = (Element) node; |
99 |
|
|
100 | 13 |
try {
|
101 | 13 |
XMLTagResourceBundle.getTagHandler( |
102 |
e, |
|
103 |
getXMLTestCase(), |
|
104 |
getString(e, TEST)).processElement(); |
|
105 | 4 |
result = true;
|
106 | 4 |
otherwise = null;
|
107 |
} catch (XMLException xe) {
|
|
108 | 1 |
throw xe;
|
109 |
} catch (Throwable t) {
|
|
110 | 8 |
result = false;
|
111 |
} |
|
112 |
|
|
113 | 12 |
if (result) {
|
114 | 4 |
getXMLTestCase().processChildren(e); |
115 |
|
|
116 | 4 |
return;
|
117 |
} |
|
118 | 4 |
} else if (m_other.equals(node.getNodeName())) { |
119 | 4 |
otherwise = (Element) node; |
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
|
|
124 | 4 |
if (otherwise != null) { |
125 | 3 |
getXMLTestCase().processChildren(otherwise); |
126 |
|
|
127 | 3 |
return;
|
128 |
} |
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Validate that the element is correct.
|
|
133 |
* @throws XMLException may be thrown.
|
|
134 |
*/
|
|
135 | 9 |
public void validateElement() throws XMLException { |
136 | 9 |
setChoose(CHOOSE); |
137 | 9 |
setWhen(WHEN); |
138 | 9 |
setOther(OTHERWISE); |
139 |
|
|
140 | 9 |
super.checkElementTagName(m_choose);
|
141 |
} |
|
142 |
|
|
143 |
/**
|
|
144 |
* Set the choose string.
|
|
145 |
* @param choose Choose string to be used.
|
|
146 |
*/
|
|
147 | 9 |
protected void setChoose(final String choose) { |
148 | 9 |
this.m_choose = choose;
|
149 |
} |
|
150 |
|
|
151 |
/**
|
|
152 |
* Set the other string.
|
|
153 |
* @param other string to be used.
|
|
154 |
*/
|
|
155 | 9 |
protected void setOther(final String other) { |
156 | 9 |
this.m_other = other;
|
157 |
} |
|
158 |
|
|
159 |
/**
|
|
160 |
* Set the when string.
|
|
161 |
* @param when When string to be used.
|
|
162 |
*/
|
|
163 | 9 |
protected void setWhen(final String when) { |
164 | 9 |
this.m_when = when;
|
165 |
} |
|
166 |
} |
|
167 |
|
|