1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.store.serialization.xml.internal

File XMLWriterTest.java

 

Code metrics

0
31
3
1
143
93
3
0.1
10.33
3
1

Classes

Class Line # Actions
XMLWriterTest 37 31 0% 3 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Tests XMLWriter
33    *
34    * @version $Id: e113fe7ac6881effbcdbbe60aa4784dcebc690bd $
35    * @since 3.0M2
36    */
 
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    * Make sure writeClose closes internediet nodes.
78    */
 
79  1 toggle @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   
 
101  1 toggle @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   
 
124  1 toggle @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    }