|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XMLTestSuite.java | 91.7% | 100% | 100% | 98.7% |
|
1 |
package junit.extensions.xml;
|
|
2 |
|
|
3 |
import junit.framework.Test;
|
|
4 |
import junit.framework.TestSuite;
|
|
5 |
|
|
6 |
import org.w3c.dom.Element;
|
|
7 |
import org.w3c.dom.NodeList;
|
|
8 |
|
|
9 |
import java.io.InputStream;
|
|
10 |
|
|
11 |
|
|
12 |
/**
|
|
13 |
* Test Case for running XML Script based testing.
|
|
14 |
*
|
|
15 |
* @author Kevin Wilson
|
|
16 |
*/
|
|
17 |
public class XMLTestSuite extends TestSuite implements IXMLTestSuite, |
|
18 |
XMLConstants { |
|
19 |
/**
|
|
20 |
* Element for this test suite.
|
|
21 |
*/
|
|
22 |
private Element m_element;
|
|
23 |
|
|
24 |
/**
|
|
25 |
* Parent Test Suite.
|
|
26 |
*/
|
|
27 |
private IXMLTest m_parent = null; |
|
28 |
|
|
29 |
/**
|
|
30 |
* The name of the test xml script file - found from the classpath.
|
|
31 |
*/
|
|
32 |
private String m_fileName;
|
|
33 |
|
|
34 |
/**
|
|
35 |
* Collection of valid procedures for the test case.
|
|
36 |
*/
|
|
37 |
private final XMLObjectCache m_procedures = new XMLObjectCache(); |
|
38 |
|
|
39 |
/**
|
|
40 |
* Object cahce used to hold globals set by the test case.
|
|
41 |
*/
|
|
42 |
private XMLPropertyCache m_propertyCache = new XMLPropertyCache(); |
|
43 |
|
|
44 |
/**
|
|
45 |
* A no-argument constructor to support junit 3.8.
|
|
46 |
*/
|
|
47 | 24 |
public XMLTestSuite() {
|
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* A constructor accepting the name of the test script file.
|
|
52 |
*
|
|
53 |
* @param fileName The name of the test script file (found from the classpath)
|
|
54 |
*/
|
|
55 | 3 |
public XMLTestSuite(final String fileName) {
|
56 | 3 |
this(fileName,
|
57 |
XMLUtil.parse(XMLUtil.readFileFromClasspath(fileName)) |
|
58 |
.getDocumentElement()); |
|
59 |
} |
|
60 |
|
|
61 |
/**
|
|
62 |
* A constructor accepting the name of the test script file.
|
|
63 |
*
|
|
64 |
* @param name A dummy name for the file
|
|
65 |
* @param stream The contents of the test script file
|
|
66 |
*/
|
|
67 | 2 |
public XMLTestSuite(final String name, final InputStream stream) {
|
68 | 2 |
this(name,
|
69 |
XMLUtil.parse(stream).getDocumentElement()); |
|
70 |
} |
|
71 |
|
|
72 |
/**
|
|
73 |
* A constructor accepting the name of the test script file and the element to be processed.
|
|
74 |
*
|
|
75 |
* @param fileName The name of the test script file (found from the classpath)
|
|
76 |
* @param element The element to be processed.
|
|
77 |
*/
|
|
78 | 581 |
public XMLTestSuite(final String fileName, final Element element) {
|
79 | 581 |
super(fileName + ":" + XMLUtil.getPath(element)); |
80 | 581 |
this.m_fileName = fileName;
|
81 | 581 |
this.m_element = element;
|
82 | 581 |
processChildren(m_element); |
83 |
} |
|
84 |
|
|
85 |
/**
|
|
86 |
* Get the debug state.
|
|
87 |
* @return true if debug is enabled.
|
|
88 |
*/
|
|
89 | 1244 |
public boolean getDebug() { |
90 | 1244 |
Object str = getProperty(DEBUG); |
91 |
|
|
92 | 1244 |
if (str instanceof String) { |
93 | 2 |
return Boolean.valueOf((String) str).booleanValue();
|
94 | 1242 |
} else if (str instanceof Boolean) { |
95 | 2 |
return ((Boolean) str).booleanValue();
|
96 |
} |
|
97 |
|
|
98 | 1240 |
return (m_propertyCache.get(DEBUG) != null); |
99 |
} |
|
100 |
|
|
101 |
/**
|
|
102 |
* Set the parent of this test case.
|
|
103 |
* @param parent Parent test.
|
|
104 |
*/
|
|
105 | 24 |
public final void setParent(final IXMLTest parent) { |
106 | 24 |
this.m_parent = parent;
|
107 | 24 |
m_propertyCache.setParent(parent.getPropertyCache()); |
108 | 24 |
m_procedures.setParent(parent.getProcedureCache()); |
109 |
} |
|
110 |
|
|
111 |
/**
|
|
112 |
* Returns the filename.
|
|
113 |
*
|
|
114 |
* @return String The name of the test xml script file
|
|
115 |
*/
|
|
116 | 1 |
public String getFileName() {
|
117 | 1 |
return m_fileName;
|
118 |
} |
|
119 |
|
|
120 |
/**
|
|
121 |
* Get the procedure by the given name.
|
|
122 |
* @param name The name of the procedure to
|
|
123 |
* be returned.
|
|
124 |
* @return XMLProcedure matching the name
|
|
125 |
* or null if it does not exist.
|
|
126 |
*/
|
|
127 | 3 |
public IXMLProcedure getProcedure(final String name) {
|
128 | 3 |
return (IXMLProcedure) m_procedures.get(name);
|
129 |
} |
|
130 |
|
|
131 |
/**
|
|
132 |
* Get the procedure cache.
|
|
133 |
* @return procedure cache for this test case.
|
|
134 |
*/
|
|
135 | 797 |
public XMLObjectCache getProcedureCache() {
|
136 | 797 |
return m_procedures;
|
137 |
} |
|
138 |
|
|
139 |
/**
|
|
140 |
* Get the value of a property.
|
|
141 |
* @param name Name of the property to be returned.
|
|
142 |
* @return Value of the property.
|
|
143 |
*/
|
|
144 | 1249 |
public Object getProperty(final String name) {
|
145 | 1249 |
return getPropertyCache().get(name);
|
146 |
} |
|
147 |
|
|
148 |
/**
|
|
149 |
* Get the found object cache.
|
|
150 |
* @return Object cache for this test case.
|
|
151 |
*/
|
|
152 | 2067 |
public XMLObjectCache getPropertyCache() {
|
153 | 2067 |
return m_propertyCache;
|
154 |
} |
|
155 |
|
|
156 |
/**
|
|
157 |
* Reverse lookup the property name from the value of the property.
|
|
158 |
* @param value Value to be looked up.
|
|
159 |
* @return First property with the same value.
|
|
160 |
*/
|
|
161 | 1 |
public String getPropertyName(final Object value) {
|
162 | 1 |
return getPropertyCache().getName(value);
|
163 |
} |
|
164 |
|
|
165 |
/**
|
|
166 |
* Get the property names.
|
|
167 |
* @return Get a array of property names
|
|
168 |
*/
|
|
169 | 1 |
public String[] getPropertyNames() {
|
170 | 1 |
return getPropertyCache().getNames();
|
171 |
} |
|
172 |
|
|
173 |
/**
|
|
174 |
* Add the given XML file to the TestSuite.
|
|
175 |
*
|
|
176 |
* @param xmlFileName The name of the test script file to be added.
|
|
177 |
*/
|
|
178 | 1 |
public void addFile(final String xmlFileName) { |
179 | 1 |
XMLTestSuite suite = new XMLTestSuite(xmlFileName);
|
180 | 1 |
suite.setParent(this);
|
181 | 1 |
addTest(suite); |
182 |
} |
|
183 |
|
|
184 |
/**
|
|
185 |
* Add a procedure to the test suite.
|
|
186 |
* @param proc Procedure to be added.
|
|
187 |
*/
|
|
188 | 5 |
public void addProcedure(final IXMLProcedure proc) { |
189 | 5 |
if (proc == null) { |
190 | 1 |
throw new IllegalArgumentException("procedure must be specified"); |
191 |
} |
|
192 |
|
|
193 | 4 |
m_procedures.put( |
194 |
proc.getName(), |
|
195 |
proc); |
|
196 |
} |
|
197 |
|
|
198 |
/**
|
|
199 |
* Adds a found object to the cache.
|
|
200 |
* @param name Name of the object.
|
|
201 |
* @param value Value of the object.
|
|
202 |
*/
|
|
203 | 17 |
public void addProperty(final String name, final Object value) { |
204 | 17 |
getPropertyCache().put(name, value); |
205 |
} |
|
206 |
|
|
207 |
/**
|
|
208 |
* Add a test to the test suite.
|
|
209 |
* @param test Test to be added.
|
|
210 |
*/
|
|
211 | 621 |
public void addTest(final Test test) { |
212 | 621 |
if (test instanceof IXMLTest) { |
213 | 621 |
((IXMLTest) test).setParent(this);
|
214 |
} |
|
215 |
|
|
216 | 621 |
super.addTest(test);
|
217 |
} |
|
218 |
|
|
219 |
/**
|
|
220 |
* Clear the properties.
|
|
221 |
*/
|
|
222 | 1 |
public void clearProperties() { |
223 | 1 |
getPropertyCache().clear(); |
224 |
} |
|
225 |
|
|
226 |
/**
|
|
227 |
* Remove a property.
|
|
228 |
* @param name Name of the property to be removed.
|
|
229 |
*/
|
|
230 | 1 |
public void removeProperty(final String name) { |
231 | 1 |
getPropertyCache().remove(name); |
232 |
} |
|
233 |
|
|
234 |
/**
|
|
235 |
* Resolve embeded property names withing a string.
|
|
236 |
* @param s String to be resolved.
|
|
237 |
* @return String without property names.
|
|
238 |
*/
|
|
239 | 1254 |
public String resolveProperties(final String s) {
|
240 | 1254 |
return m_propertyCache.resolve(s);
|
241 |
} |
|
242 |
|
|
243 |
/**
|
|
244 |
* Process the children of the test suite.
|
|
245 |
* @param e Element which contains the children to be processed.
|
|
246 |
*/
|
|
247 | 581 |
private void processChildren(final Element e) { |
248 |
// Get the children and add to the suite.
|
|
249 | 581 |
NodeList children = e.getChildNodes(); |
250 | 581 |
Element child; |
251 |
|
|
252 | 581 |
for (int i = 0; i < children.getLength(); i++) { |
253 | 1009 |
if (children.item(i) instanceof Element) { |
254 | 926 |
child = (Element) children.item(i); |
255 |
|
|
256 | 926 |
String name = child.getTagName(); |
257 |
|
|
258 | 926 |
try {
|
259 | 926 |
XMLTagResourceBundle.getTagHandler(child, this, name)
|
260 |
.processElement(); |
|
261 |
} catch (XMLException xe) {
|
|
262 | 306 |
throw xe;
|
263 |
} catch (Throwable t) {
|
|
264 | 3 |
throw new XMLException( |
265 |
t.getMessage(), |
|
266 |
t, |
|
267 |
child, |
|
268 |
getPropertyCache()); |
|
269 |
} |
|
270 |
} |
|
271 |
} |
|
272 |
} |
|
273 |
} |
|
274 |
|
|