1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.store.serialization.xml.internal; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.StringReader; |
25 |
|
|
26 |
|
import org.dom4j.dom.DOMElement; |
27 |
|
import org.dom4j.io.OutputFormat; |
28 |
|
import org.junit.Assert; |
29 |
|
import org.junit.Test; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
@version |
35 |
|
@since |
36 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (34) |
Complexity: 3 |
Complexity Density: 0.1 |
|
37 |
|
public class XMLWriterTest |
38 |
|
{ |
39 |
|
private static final String TEST_CONTENT = |
40 |
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
41 |
|
+ "<root>\n" |
42 |
|
+ " <doc>\n" |
43 |
|
+ " <obj>\n" |
44 |
|
+ " <prop>\n" |
45 |
|
+ " </prop>\n" |
46 |
|
+ " </obj>\n" |
47 |
|
+ " </doc>\n" |
48 |
|
+ "</root>"; |
49 |
|
|
50 |
|
private static final String BASE64_INPUT = |
51 |
|
"This string will be converted to base64, it has to be long enough to see that the " |
52 |
|
+ "lines will be broken at the right length."; |
53 |
|
|
54 |
|
private static final String BASE64_TEST = |
55 |
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
56 |
|
+ "<root>\n" |
57 |
|
+ " <doc>\n" |
58 |
|
+ " <obj>\n" |
59 |
|
+ " <prop>\n" |
60 |
|
+ "VGhpcyBzdHJpbmcgd2lsbCBiZSBjb252ZXJ0ZWQgdG8gYmFzZTY0LCBpdCBoYXMgdG8gYmUgbG9uZyBl\n" |
61 |
|
+ "bm91Z2ggdG8gc2VlIHRoYXQgdGhlIGxpbmVzIHdpbGwgYmUgYnJva2VuIGF0IHRoZSByaWdodCBsZW5n\n" |
62 |
|
+ "dGgu\n" |
63 |
|
+ " </prop>\n" |
64 |
|
+ " </obj>\n" |
65 |
|
+ " </doc>\n" |
66 |
|
+ "</root>"; |
67 |
|
|
68 |
|
private static final String WRITE_STREAM_TEST = |
69 |
|
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
70 |
|
+ "<root>\n" |
71 |
|
+ " <prop>Hello World!</prop>\n" |
72 |
|
+ "</root>"; |
73 |
|
|
74 |
|
private XMLWriter writer; |
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
79 |
1 |
@Test... |
80 |
|
public void testWriteClose() throws Exception |
81 |
|
{ |
82 |
1 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
83 |
1 |
final OutputFormat of = new OutputFormat(" ", true, "UTF-8"); |
84 |
|
|
85 |
1 |
this.writer = new XMLWriter(baos, of); |
86 |
1 |
this.writer.startDocument(); |
87 |
|
|
88 |
1 |
this.writer.writeOpen(new DOMElement("root")); |
89 |
1 |
this.writer.writeOpen(new DOMElement("doc")); |
90 |
1 |
this.writer.writeOpen(new DOMElement("obj")); |
91 |
1 |
this.writer.writeOpen(new DOMElement("prop")); |
92 |
1 |
this.writer.writeClose(new DOMElement("root")); |
93 |
|
|
94 |
1 |
this.writer.endDocument(); |
95 |
|
|
96 |
1 |
Assert.assertEquals("WriteClose didn't write the correct response.", |
97 |
|
TEST_CONTENT, |
98 |
|
new String(baos.toByteArray(), "UTF-8")); |
99 |
|
} |
100 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
101 |
1 |
@Test... |
102 |
|
public void testBase64() throws Exception |
103 |
|
{ |
104 |
1 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
105 |
1 |
final OutputFormat of = new OutputFormat(" ", true, "UTF-8"); |
106 |
|
|
107 |
1 |
this.writer = new XMLWriter(baos, of); |
108 |
1 |
this.writer.startDocument(); |
109 |
|
|
110 |
1 |
this.writer.writeOpen(new DOMElement("root")); |
111 |
1 |
this.writer.writeOpen(new DOMElement("doc")); |
112 |
1 |
this.writer.writeOpen(new DOMElement("obj")); |
113 |
1 |
this.writer.writeBase64(new DOMElement("prop"), |
114 |
|
new ByteArrayInputStream(BASE64_INPUT.getBytes("UTF-8"))); |
115 |
1 |
this.writer.writeClose(new DOMElement("root")); |
116 |
|
|
117 |
1 |
this.writer.endDocument(); |
118 |
|
|
119 |
1 |
Assert.assertEquals("Incorrect response from testBase64.", |
120 |
|
BASE64_TEST, |
121 |
|
new String(baos.toByteArray(), "UTF-8")); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void testWriteStream() throws Exception |
126 |
|
{ |
127 |
1 |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
128 |
1 |
final OutputFormat of = new OutputFormat(" ", true, "UTF-8"); |
129 |
|
|
130 |
1 |
this.writer = new XMLWriter(baos, of); |
131 |
1 |
this.writer.startDocument(); |
132 |
|
|
133 |
1 |
this.writer.writeOpen(new DOMElement("root")); |
134 |
1 |
this.writer.write(new DOMElement("prop"), new StringReader("Hello World!")); |
135 |
1 |
this.writer.writeClose(new DOMElement("root")); |
136 |
|
|
137 |
1 |
this.writer.endDocument(); |
138 |
|
|
139 |
1 |
Assert.assertEquals("Incorrect response from testWriteStream.", |
140 |
|
WRITE_STREAM_TEST, |
141 |
|
new String(baos.toByteArray(), "UTF-8")); |
142 |
|
} |
143 |
|
} |