|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ElementFixture.java | 83.3% | 95.2% | 100% | 95.1% |
|
1 |
package junit.extensions.xml.elements;
|
|
2 |
|
|
3 |
import java.io.File;
|
|
4 |
import java.io.FileOutputStream;
|
|
5 |
import java.io.IOException;
|
|
6 |
import javax.xml.parsers.DocumentBuilder;
|
|
7 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
8 |
import javax.xml.parsers.ParserConfigurationException;
|
|
9 |
|
|
10 |
import org.w3c.dom.Document;
|
|
11 |
import org.w3c.dom.Element;
|
|
12 |
import junit.extensions.xml.IXMLTestCase;
|
|
13 |
import junit.extensions.xml.IXMLTestSuite;
|
|
14 |
import junit.extensions.xml.XMLTestCase;
|
|
15 |
import junit.extensions.xml.XMLTestSuite;
|
|
16 |
import junit.extensions.xml.XMLUtil;
|
|
17 |
import junit.extensions.jfcunit.xml.JFCXMLTestCase;
|
|
18 |
|
|
19 |
/**
|
|
20 |
* <p>Title: ElementFixture is used to generate DOM Elements</p>
|
|
21 |
* <p>Description: This class is used to generate DOM elements
|
|
22 |
* given a name and array of attribute and values.</p>
|
|
23 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
24 |
* <p>Company: </p>
|
|
25 |
* @author Kevin L. Wilson
|
|
26 |
* @version 1.0
|
|
27 |
*/
|
|
28 |
public class ElementFixture { |
|
29 |
|
|
30 |
/**
|
|
31 |
* DOM Docuement.
|
|
32 |
*/
|
|
33 |
private Document m_doc;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* DOM Element for the test suite.
|
|
37 |
*/
|
|
38 |
private Element m_suiteElement;
|
|
39 |
|
|
40 |
/**
|
|
41 |
* DOM Element for the test case.
|
|
42 |
*/
|
|
43 |
private Element m_testElement;
|
|
44 |
|
|
45 |
/**
|
|
46 |
* Test case using the m_testElement.
|
|
47 |
*/
|
|
48 |
private IXMLTestCase m_tc;
|
|
49 |
|
|
50 |
/**
|
|
51 |
* Test suite using the m_suiteElement.
|
|
52 |
*/
|
|
53 |
private IXMLTestSuite m_suite;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Constructor.
|
|
57 |
* @param obj Not used.
|
|
58 |
*/
|
|
59 | 240 |
public ElementFixture(final Object obj) {
|
60 |
|
|
61 |
} |
|
62 |
|
|
63 |
/**
|
|
64 |
* Setup the fixture.
|
|
65 |
* A new document will be created with a default test suite
|
|
66 |
* element which contains a default test case element.
|
|
67 |
*/
|
|
68 | 240 |
public void setUp() { |
69 | 240 |
DocumentBuilderFactory df = javax.xml.parsers.DocumentBuilderFactory.newInstance(); |
70 | 240 |
try {
|
71 | 240 |
DocumentBuilder db = df.newDocumentBuilder(); |
72 | 240 |
m_doc = db.newDocument(); |
73 | 240 |
m_suiteElement = m_doc.createElement("suite");
|
74 | 240 |
m_suiteElement.setAttribute("name", "Automated Suite"); |
75 | 240 |
m_doc.appendChild(m_suiteElement); |
76 |
|
|
77 | 240 |
m_testElement = m_doc.createElement("test");
|
78 | 240 |
m_testElement.setAttribute("name", "Automated TestCase"); |
79 | 240 |
m_suiteElement.appendChild(m_testElement); |
80 |
|
|
81 | 240 |
m_suite = new XMLTestSuite("TEST", m_suiteElement); |
82 |
// m_tc = new XMLTestCase("TESTCASE", m_testElement);
|
|
83 |
// m_tc.setParent(m_suite);
|
|
84 |
|
|
85 |
} catch (ParserConfigurationException ex) {
|
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
/**
|
|
90 |
* Get the test case which uses the test case element.
|
|
91 |
* @return the test case.
|
|
92 |
*/
|
|
93 | 162 |
public XMLTestCase getTestCase() {
|
94 | 162 |
if (m_tc == null) { |
95 | 157 |
m_tc = new XMLTestCase("ABC", m_testElement); |
96 | 157 |
m_tc.setParent(m_suite); |
97 |
} |
|
98 | 162 |
return (XMLTestCase) m_tc;
|
99 |
} |
|
100 |
|
|
101 |
/**
|
|
102 |
* Get the test case as a JFCXMLTestCase implementation.
|
|
103 |
* @return JFCXMLTestCase test case.
|
|
104 |
*/
|
|
105 | 16 |
public JFCXMLTestCase getJFCTestCase() {
|
106 | 16 |
if (m_tc == null) { |
107 | 16 |
m_tc = new JFCXMLTestCase("ABC", m_testElement); |
108 | 16 |
m_tc.setParent(m_suite); |
109 |
} |
|
110 | 16 |
return (JFCXMLTestCase) m_tc;
|
111 |
} |
|
112 |
|
|
113 |
/**
|
|
114 |
* Get a test suite which uses the test suite element.
|
|
115 |
* @return the test suite.
|
|
116 |
*/
|
|
117 | 17 |
public IXMLTestSuite getTestSuite() {
|
118 | 17 |
return m_suite;
|
119 |
} |
|
120 |
|
|
121 |
/**
|
|
122 |
* Get the test suite element.
|
|
123 |
* @return test suite element.
|
|
124 |
*/
|
|
125 | 14 |
public Element getSuiteElement() {
|
126 | 14 |
return m_suiteElement;
|
127 |
} |
|
128 |
|
|
129 |
/**
|
|
130 |
* Get the test case element.
|
|
131 |
* @return the test case element.
|
|
132 |
*/
|
|
133 | 25 |
public Element getTestElement() {
|
134 | 25 |
return m_testElement;
|
135 |
} |
|
136 |
|
|
137 |
/**
|
|
138 |
* Get the Document element.
|
|
139 |
* @return the document maintained by this element fixture.
|
|
140 |
*/
|
|
141 | 11 |
public Document getDocument() {
|
142 | 11 |
return m_doc;
|
143 |
} |
|
144 |
|
|
145 |
/**
|
|
146 |
* Tear down the test fixture.
|
|
147 |
*/
|
|
148 | 239 |
public void tearDown() { |
149 | 239 |
m_doc = null;
|
150 | 239 |
m_tc = null;
|
151 | 239 |
m_suite = null;
|
152 |
} |
|
153 |
|
|
154 |
/**
|
|
155 |
* Create a new element with the testCase as the parent.
|
|
156 |
* @param name Name of the new child.
|
|
157 |
* @param attrs Attribute value pairs.
|
|
158 |
* @return new child element.
|
|
159 |
*/
|
|
160 | 209 |
public Element newElement(final String name, final String[] attrs) {
|
161 | 209 |
return newChild(m_testElement, name, attrs);
|
162 |
} |
|
163 |
|
|
164 |
/**
|
|
165 |
* Create a new child element.
|
|
166 |
* @param parent Parent element of the new child.
|
|
167 |
* @param name Name of the new child.
|
|
168 |
* @param attrs Attribute value pairs.
|
|
169 |
* @return new child element.
|
|
170 |
*/
|
|
171 | 238 |
public Element newChild(final Element parent, final String name, final String[] attrs) {
|
172 | 238 |
Element e = m_doc.createElement(name); |
173 | 238 |
for (int i = 0; i < attrs.length; i += 2) { |
174 | 544 |
e.setAttribute(attrs[i], attrs[i + 1]); |
175 |
} |
|
176 | 238 |
parent.appendChild(e); |
177 | 238 |
return e;
|
178 |
} |
|
179 |
|
|
180 |
/**
|
|
181 |
* Create a test file with the current document.
|
|
182 |
* @return File file created.
|
|
183 |
*/
|
|
184 | 4 |
public File getSuiteFile() {
|
185 | 4 |
String name = null;
|
186 | 4 |
try {
|
187 | 4 |
File file = File.createTempFile("test", ".xml"); |
188 | 4 |
name = file.getAbsolutePath(); |
189 | 4 |
saveFile(file); |
190 | 4 |
return file;
|
191 |
} catch (IOException ioe) {
|
|
192 | 0 |
RuntimeException re = new RuntimeException("Could not create file:" |
193 |
+ name); |
|
194 | 0 |
throw re;
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
/**
|
|
199 |
* Save the current document to a file.
|
|
200 |
* @param file to be saved.
|
|
201 |
* @throws IOException may be thrown.
|
|
202 |
*/
|
|
203 | 8 |
public void saveFile(final File file) throws IOException { |
204 | 8 |
FileOutputStream fos = new FileOutputStream(file);
|
205 | 8 |
XMLUtil.writeFile("UTF-8", fos, getDocument());
|
206 |
} |
|
207 |
} |
|
208 |
|
|