|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ParentInstanceTagHandler.java | 100% | 100% | 100% | 100% |
|
1 |
/*
|
|
2 |
* Created on Oct 24, 2003
|
|
3 |
*
|
|
4 |
* To change the template for this generated file go to
|
|
5 |
* Window>Preferences>Java>Code Generation>Code and Comments
|
|
6 |
*/
|
|
7 |
package junit.extensions.xml.elements;
|
|
8 |
|
|
9 |
import junit.extensions.xml.IXMLTestCase;
|
|
10 |
import junit.extensions.xml.XMLException;
|
|
11 |
|
|
12 |
import org.w3c.dom.Element;
|
|
13 |
|
|
14 |
import java.awt.Component;
|
|
15 |
|
|
16 |
|
|
17 |
/**
|
|
18 |
* Provide a mechanism for sending debug messages to the
|
|
19 |
* test case developer.
|
|
20 |
*
|
|
21 |
* <H3>Tag Name</H3>
|
|
22 |
* getparent
|
|
23 |
* <H3>Attributes</H3>
|
|
24 |
* <pre>
|
|
25 |
* id - the to assign to parent object [required]
|
|
26 |
* refid - the id of the root object [required]
|
|
27 |
* classname - the classname of the parent [required]
|
|
28 |
* </pre>
|
|
29 |
* <H3>Examples</H3>
|
|
30 |
* <getparent id="parentId" refid="childID" classname="javax.swing.JPanel"/>
|
|
31 |
* @author Kevin Wilson
|
|
32 |
*/
|
|
33 |
public class ParentInstanceTagHandler extends AbstractTagHandler { |
|
34 |
/**
|
|
35 |
* Constructor.
|
|
36 |
* @param element Element to be processed by the tag handler.
|
|
37 |
* @param testcase parent test case.
|
|
38 |
*/
|
|
39 | 6 |
public ParentInstanceTagHandler(final Element element,
|
40 |
final IXMLTestCase testcase) { |
|
41 | 6 |
super(element, testcase);
|
42 |
} |
|
43 |
|
|
44 |
/**
|
|
45 |
* Handle the XML processing of the tag 'getparent'.
|
|
46 |
* @throws XMLException upon failure of processing.
|
|
47 |
*/
|
|
48 | 5 |
public void processElement() throws XMLException { |
49 | 5 |
validateElement(); |
50 |
|
|
51 | 5 |
String refid = getString(REFID); |
52 | 5 |
String id = getString(ID); |
53 | 5 |
String classname = getString(CLASSNAME); |
54 |
|
|
55 | 5 |
Object o = getXMLTestCase().getProperty(refid); |
56 |
|
|
57 | 5 |
if (!(o instanceof Component)) { |
58 | 1 |
throw new XMLException("object not a component:" + o, null, |
59 |
getElement(), |
|
60 |
getXMLTestCase().getPropertyCache()); |
|
61 |
} |
|
62 |
|
|
63 | 4 |
Class cls; |
64 |
|
|
65 | 4 |
try {
|
66 | 4 |
cls = Class.forName(classname); |
67 |
} catch (ClassNotFoundException ex) {
|
|
68 | 1 |
throw new XMLException("Cannot find class:" + classname, ex, |
69 |
getElement(), |
|
70 |
getXMLTestCase().getPropertyCache()); |
|
71 |
} |
|
72 |
|
|
73 | 3 |
Component comp = ((Component) o).getParent(); |
74 |
|
|
75 | 3 |
while (comp != null) { |
76 | 3 |
if (cls.isAssignableFrom(comp.getClass())) {
|
77 | 2 |
getXMLTestCase().addProperty(id, comp); |
78 |
|
|
79 | 2 |
return;
|
80 |
} |
|
81 |
|
|
82 | 1 |
comp = comp.getParent(); |
83 |
} |
|
84 |
|
|
85 | 1 |
getXMLTestCase().addProperty(id, null);
|
86 |
} |
|
87 |
|
|
88 |
/**
|
|
89 |
* Make sure the appropriate tag and attributes are used.
|
|
90 |
* @throws XMLException when validation fails.
|
|
91 |
*/
|
|
92 | 5 |
public void validateElement() throws XMLException { |
93 | 5 |
super.validateElement();
|
94 |
|
|
95 |
// message is a required attribute
|
|
96 | 5 |
checkRequiredAttribute(ID); |
97 | 5 |
checkRequiredAttribute(REFID); |
98 | 5 |
checkRequiredAttribute(CLASSNAME); |
99 |
} |
|
100 |
} |
|
101 |
|
|