|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
XMLWriterT.java | - | 100% | 100% | 100% |
|
1 |
package junit.extensions.xml;
|
|
2 |
|
|
3 |
import java.io.ByteArrayOutputStream;
|
|
4 |
import java.io.UnsupportedEncodingException;
|
|
5 |
import java.util.Arrays;
|
|
6 |
|
|
7 |
import org.w3c.dom.Attr;
|
|
8 |
import org.w3c.dom.Element;
|
|
9 |
import org.w3c.dom.NamedNodeMap;
|
|
10 |
import junit.extensions.xml.elements.ElementFixture;
|
|
11 |
import junit.framework.TestCase;
|
|
12 |
|
|
13 |
/**
|
|
14 |
* <p>Title: Test the XMLWriter.</p>
|
|
15 |
* <p>Description: </p>
|
|
16 |
* <p>Copyright: Copyright (c) 2003</p>
|
|
17 |
* <p>Company: </p>
|
|
18 |
* @author Kevin Wilson
|
|
19 |
* @version 1.0
|
|
20 |
*/
|
|
21 |
public class XMLWriterT |
|
22 |
extends TestCase {
|
|
23 |
/**
|
|
24 |
* XML Writer to be tested.
|
|
25 |
*/
|
|
26 |
private XMLWriter m_xmlWriter = null; |
|
27 |
/**
|
|
28 |
* Element fixutre.
|
|
29 |
*/
|
|
30 |
private ElementFixture m_elementFixture;
|
|
31 |
|
|
32 |
/**
|
|
33 |
* Constructor.
|
|
34 |
* @param name Name of the test.
|
|
35 |
*/
|
|
36 | 2 |
public XMLWriterT(final String name) {
|
37 | 2 |
super(name);
|
38 |
} |
|
39 |
|
|
40 |
/**
|
|
41 |
* Set up the test.
|
|
42 |
* @throws Exception may be thrown.
|
|
43 |
*/
|
|
44 | 2 |
protected void setUp() throws Exception { |
45 | 2 |
super.setUp();
|
46 | 2 |
m_elementFixture = new ElementFixture(this); |
47 | 2 |
m_elementFixture.setUp(); |
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Tear down the test.
|
|
52 |
* @throws Exception may be thrown.
|
|
53 |
*/
|
|
54 | 2 |
protected void tearDown() throws Exception { |
55 | 2 |
m_xmlWriter = null;
|
56 | 2 |
m_elementFixture.tearDown(); |
57 | 2 |
m_elementFixture = null;
|
58 | 2 |
super.tearDown();
|
59 |
} |
|
60 |
|
|
61 |
/**
|
|
62 |
* Test writing a XML.
|
|
63 |
*/
|
|
64 | 1 |
public void testWrite() { |
65 |
|
|
66 | 1 |
m_xmlWriter = new XMLWriter();
|
67 | 1 |
ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
|
68 | 1 |
try {
|
69 | 1 |
m_xmlWriter.setOutput(bos, null);
|
70 |
} catch (UnsupportedEncodingException ex) {
|
|
71 |
} |
|
72 |
|
|
73 | 1 |
m_xmlWriter.write(m_elementFixture.getDocument()); |
74 |
|
|
75 | 1 |
String result = bos.toString(); |
76 | 1 |
String expResult = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
|
77 |
+ "<suite name=\"Automated Suite\">"
|
|
78 |
+ "<test name=\"Automated TestCase\"/>"
|
|
79 |
+ "</suite>";
|
|
80 |
|
|
81 | 1 |
assertEquals(expResult, result); |
82 |
|
|
83 | 1 |
m_xmlWriter.setCanonical(true);
|
84 | 1 |
bos = new ByteArrayOutputStream();
|
85 | 1 |
try {
|
86 | 1 |
m_xmlWriter.setOutput(bos, null);
|
87 |
} catch (UnsupportedEncodingException ex) {
|
|
88 |
} |
|
89 | 1 |
m_xmlWriter.write(m_elementFixture.getDocument()); |
90 | 1 |
result = bos.toString(); |
91 |
|
|
92 | 1 |
expResult = "<suite name=\"Automated Suite\">"
|
93 |
+ "<test name=\"Automated TestCase\"/>"
|
|
94 |
+ "</suite>";
|
|
95 | 1 |
assertEquals(expResult, result); |
96 |
|
|
97 |
} |
|
98 |
|
|
99 |
/**
|
|
100 |
* Test the attribute sorting.
|
|
101 |
*/
|
|
102 | 1 |
public void testSortAttributes() { |
103 | 1 |
m_xmlWriter = new XMLWriter();
|
104 | 1 |
Element e = m_elementFixture.newElement("TEST", new String[] { |
105 |
"bAttr", "TEST", |
|
106 |
"aAttr", "TEST", |
|
107 |
"cAttr", "TEST" |
|
108 |
}); |
|
109 |
|
|
110 |
|
|
111 | 1 |
NamedNodeMap attrs = e.getAttributes(); |
112 | 1 |
Attr[] expectedReturn = new Attr[] {
|
113 |
e.getAttributeNode("aAttr"),
|
|
114 |
e.getAttributeNode("bAttr"),
|
|
115 |
e.getAttributeNode("cAttr")
|
|
116 |
}; |
|
117 | 1 |
Attr[] actualReturn = m_xmlWriter.sortAttributes(attrs); |
118 | 1 |
boolean result = Arrays.equals(actualReturn, expectedReturn);
|
119 | 1 |
assertTrue("return value", result);
|
120 |
} |
|
121 |
|
|
122 |
|
|
123 |
} |
|
124 |
|
|