|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FileTagHandler.java | 100% | 88.9% | 100% | 91.3% |
|
1 |
package junit.extensions.xml.elements;
|
|
2 |
|
|
3 |
import junit.extensions.xml.IXMLTestSuite;
|
|
4 |
import junit.extensions.xml.XMLException;
|
|
5 |
import junit.extensions.xml.XMLTagResourceBundle;
|
|
6 |
import junit.extensions.xml.XMLUtil;
|
|
7 |
|
|
8 |
import org.w3c.dom.Element;
|
|
9 |
|
|
10 |
import java.io.File;
|
|
11 |
|
|
12 |
|
|
13 |
/**
|
|
14 |
* This class includes the given file into a test suite.
|
|
15 |
* <H3>Tag Name</H3>
|
|
16 |
* file
|
|
17 |
*
|
|
18 |
* <H3>Required Attributes</H3>
|
|
19 |
* name - file name of the given file.
|
|
20 |
* relative - value true if the file should be located relative to
|
|
21 |
* the current file. Default value is false for backward compatibility.
|
|
22 |
* <H3>Example</H3>
|
|
23 |
* <pre>
|
|
24 |
* <suite name="MyTest">
|
|
25 |
* <file name="moresuites.xml"/>
|
|
26 |
* <file name="extras/moresuites.xml" relative="true"/>
|
|
27 |
* </suite>
|
|
28 |
* </pre>
|
|
29 |
*
|
|
30 |
* @author <a href="mailto:vraravam@thoughtworks.com">Vijay Aravamudhan : ThoughtWorks Inc.</a>
|
|
31 |
*/
|
|
32 |
public class FileTagHandler extends AbstractTagHandler { |
|
33 |
/**
|
|
34 |
* Default constructor.
|
|
35 |
*
|
|
36 |
* @param element The element to be processed.
|
|
37 |
* @param testSuite The XMLTestSuite that uses this element.
|
|
38 |
*/
|
|
39 | 312 |
public FileTagHandler(final Element element, final IXMLTestSuite testSuite) {
|
40 | 312 |
super(element, testSuite);
|
41 |
} |
|
42 |
|
|
43 |
/**
|
|
44 |
* Process the file element.
|
|
45 |
* @throws XMLException is thrown if the element cannot be understood.
|
|
46 |
*/
|
|
47 | 311 |
public void processElement() throws XMLException { |
48 | 311 |
validateElement(); |
49 |
|
|
50 | 311 |
String name = getString(NAME); |
51 |
|
|
52 | 311 |
if (getBoolean(RELATIVE)) {
|
53 | 1 |
Element curdoc = getElement().getOwnerDocument() |
54 |
.getDocumentElement(); |
|
55 | 1 |
String location = curdoc.getAttribute(JFCFILELOC); |
56 | 1 |
int index = location.lastIndexOf(File.separator);
|
57 | 1 |
name = location.substring(0, index + 1) + name; |
58 |
} |
|
59 |
|
|
60 | 311 |
try {
|
61 | 311 |
Element doc = XMLUtil.parse(XMLUtil.readFileFromClasspath(name)) |
62 |
.getDocumentElement(); |
|
63 | 310 |
doc.setAttribute(JFCFILELOC, name); |
64 |
|
|
65 | 310 |
String tag = doc.getTagName(); |
66 | 310 |
XMLTagResourceBundle.getTagHandler( |
67 |
doc, |
|
68 |
getTest(), |
|
69 |
tag).processElement(); |
|
70 |
} catch (OutOfMemoryError err) {
|
|
71 | 0 |
throw new XMLException("Out of memory check for recursive file includes.", |
72 |
err, |
|
73 |
getElement(), |
|
74 |
getXMLTestSuite().getPropertyCache()); |
|
75 |
} catch (XMLException xe) {
|
|
76 | 307 |
throw xe;
|
77 |
} catch (Exception e) {
|
|
78 | 0 |
throw new XMLException( |
79 |
e.getMessage(), |
|
80 |
e, |
|
81 |
getElement(), |
|
82 |
getTest().getPropertyCache()); |
|
83 |
|
|
84 |
// if (e.getCause() instanceof StackOverflowError) {
|
|
85 |
// throw new XMLException(
|
|
86 |
// "StackOverflowError check for recursive file includes.", e,
|
|
87 |
// getElement(), getXMLTestSuite().getPropertyCache());
|
|
88 |
// }
|
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
/**
|
|
93 |
* Validate that the name element has been added.
|
|
94 |
* @throws XMLException if the name attribute is not specified.
|
|
95 |
*/
|
|
96 | 311 |
public void validateElement() throws XMLException { |
97 |
// do the default validations from the super class
|
|
98 | 311 |
super.validateElement();
|
99 |
|
|
100 | 311 |
checkRequiredAttribute(NAME); |
101 |
} |
|
102 |
} |
|
103 |
|
|