1
|
|
package junit.extensions.xml;
|
2
|
|
|
3
|
|
import java.io.IOException;
|
4
|
|
import java.io.InputStream;
|
5
|
|
import java.io.PipedInputStream;
|
6
|
|
import java.io.PipedOutputStream;
|
7
|
|
|
8
|
|
import org.w3c.dom.Document;
|
9
|
|
import org.w3c.dom.Element;
|
10
|
|
import junit.extensions.xml.elements.ElementFixture;
|
11
|
|
import junit.framework.TestCase;
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
public class XMLUtilT
|
22
|
|
extends TestCase {
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
|
private XMLUtil m_xmlUtil = null;
|
27
|
|
|
28
|
|
|
29
|
|
|
30
|
|
private ElementFixture m_elementFixture;
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
6
|
public XMLUtilT(final String name) {
|
37
|
6
|
super(name);
|
38
|
|
}
|
39
|
|
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
6
|
protected void setUp() throws Exception {
|
45
|
6
|
super.setUp();
|
46
|
6
|
m_xmlUtil = null;
|
47
|
6
|
m_elementFixture = new ElementFixture(this);
|
48
|
|
|
49
|
6
|
m_elementFixture.setUp();
|
50
|
|
}
|
51
|
|
|
52
|
|
|
53
|
|
|
54
|
|
|
55
|
|
|
56
|
6
|
protected void tearDown() throws Exception {
|
57
|
6
|
m_xmlUtil = null;
|
58
|
6
|
m_elementFixture.tearDown();
|
59
|
|
|
60
|
6
|
m_elementFixture = null;
|
61
|
6
|
super.tearDown();
|
62
|
|
}
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
1
|
public void testGetAttribute() {
|
68
|
1
|
Element element = m_elementFixture.newElement("TEST",
|
69
|
|
new String[] {
|
70
|
|
"string", "value",
|
71
|
|
"false", "false",
|
72
|
|
"true", "true"
|
73
|
|
});
|
74
|
|
|
75
|
1
|
String name = "string";
|
76
|
1
|
String expectedReturn = "value";
|
77
|
1
|
String actualReturn = m_xmlUtil.getAttribute(element, name);
|
78
|
1
|
assertEquals("string", expectedReturn, actualReturn);
|
79
|
|
|
80
|
1
|
expectedReturn = "TEST";
|
81
|
1
|
actualReturn = m_xmlUtil.getName(element);
|
82
|
1
|
assertEquals("name", expectedReturn, actualReturn);
|
83
|
|
|
84
|
1
|
actualReturn = m_xmlUtil.getPath(element);
|
85
|
1
|
expectedReturn = "Automated Suite.Automated TestCase.TEST";
|
86
|
1
|
assertEquals("path", expectedReturn, actualReturn);
|
87
|
|
|
88
|
1
|
boolean bReturn = m_xmlUtil.getBooleanAttributeValue(element, "true");
|
89
|
1
|
assertTrue("boolean true", bReturn);
|
90
|
|
|
91
|
1
|
bReturn = m_xmlUtil.getBooleanAttributeValue(element, "false");
|
92
|
1
|
assertTrue("boolean false", !bReturn);
|
93
|
|
|
94
|
1
|
assertTrue("hasAttribute true", m_xmlUtil.hasAttribute(element, "true"));
|
95
|
1
|
assertTrue("hasAttribute false", !m_xmlUtil.hasAttribute(element, "xtrue"));
|
96
|
|
}
|
97
|
|
|
98
|
|
|
99
|
|
|
100
|
|
|
101
|
1
|
public void testParse() {
|
102
|
|
|
103
|
1
|
try {
|
104
|
1
|
PipedOutputStream pos = new PipedOutputStream();
|
105
|
1
|
PipedInputStream pis = new PipedInputStream(pos);
|
106
|
1
|
XMLUtil.writeFile(null, pos, m_elementFixture.getDocument());
|
107
|
1
|
pos.close();
|
108
|
1
|
Document d = XMLUtil.parse(pis);
|
109
|
|
} catch (IOException ex) {
|
110
|
0
|
fail("Exception thrown");
|
111
|
|
}
|
112
|
|
}
|
113
|
|
|
114
|
|
|
115
|
|
|
116
|
|
|
117
|
1
|
public void testReadFileFromClassContext() {
|
118
|
1
|
Class clz = this.getClass();
|
119
|
1
|
String fileName = "XMLUtilT.class";
|
120
|
1
|
InputStream actualReturn = m_xmlUtil.readFileFromClassContext(clz, fileName);
|
121
|
1
|
assertNotNull("InputStream is null", actualReturn);
|
122
|
|
}
|
123
|
|
|
124
|
|
|
125
|
|
|
126
|
|
|
127
|
1
|
public void testReadFileFromClassContextNotFound() {
|
128
|
1
|
Class clz = this.getClass();
|
129
|
1
|
String fileName = "XMLUtilT.classx";
|
130
|
1
|
InputStream actualReturn = m_xmlUtil.readFileFromClassContext(clz, fileName);
|
131
|
1
|
assertNull("InputStream is should be null", actualReturn);
|
132
|
|
}
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
1
|
public void testReadFileFromClasspath() {
|
138
|
1
|
String fileName = "junit/extensions/xml/XMLUtilT.class";
|
139
|
1
|
InputStream actualReturn = m_xmlUtil.readFileFromClasspath(fileName);
|
140
|
1
|
assertNotNull("Input stream is null", actualReturn);
|
141
|
|
}
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
1
|
public void testReadFileFromClasspathNotFound() {
|
147
|
1
|
String fileName = "junit/extensions/xml/XMLUtilT.classx";
|
148
|
1
|
Exception ex = null;
|
149
|
1
|
try {
|
150
|
1
|
InputStream actualReturn = m_xmlUtil.readFileFromClasspath(fileName);
|
151
|
|
} catch (Exception e) {
|
152
|
1
|
ex = e;
|
153
|
|
}
|
154
|
1
|
assertNotNull("Exception not thrown", ex);
|
155
|
|
}
|
156
|
|
|
157
|
|
}
|
158
|
|
|