1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.xml

File WemToSaxTest.java

 

Code metrics

0
25
6
1
135
97
6
0.24
4.17
6
1

Classes

Class Line # Actions
WemToSaxTest 43 25 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.rendering.wikimodel.xml;
21   
22    import java.io.Reader;
23    import java.io.StringReader;
24    import java.io.StringWriter;
25   
26    import javax.xml.transform.Result;
27    import javax.xml.transform.Source;
28    import javax.xml.transform.dom.DOMResult;
29    import javax.xml.transform.dom.DOMSource;
30    import javax.xml.transform.stream.StreamResult;
31   
32    import org.w3c.dom.Document;
33    import org.xwiki.rendering.wikimodel.IWikiParser;
34    import org.xwiki.rendering.wikimodel.confluence.ConfluenceExtendedWikiParser;
35    import org.xwiki.rendering.wikimodel.xml.sax.WemReader;
36   
37    import junit.framework.TestCase;
38   
39    /**
40    * @version $Id: b1b474a13916d0cf74fdd18b7b7d6183acd49aaa $
41    * @since 4.0M1
42    */
 
43    public class WemToSaxTest extends TestCase
44    {
45    /**
46    * @param name
47    */
 
48  1 toggle public WemToSaxTest(String name)
49    {
50  1 super(name);
51    }
52   
 
53  5 toggle protected IWikiParser newWikiParser()
54    {
55  5 return new ConfluenceExtendedWikiParser();
56    // return new ConfluenceWikiParser();
57    // return new CommonWikiParser();
58    }
59   
 
60  5 toggle public Document parseDocument(Reader reader, IWikiParser parser)
61    throws Exception
62    {
63  5 try {
64  5 Document document = XmlUtil.newDocument();
65  5 DOMResult result = new DOMResult(document);
66  5 WemReader xmlReader = new WemReader(parser);
67  5 XmlUtil.write(reader, xmlReader, result);
68  5 return document;
69    } finally {
70  5 reader.close();
71    }
72    }
73   
 
74  5 toggle public Document parseDocument(String str, IWikiParser parser)
75    throws Exception
76    {
77  5 StringReader reader = new StringReader(str);
78  5 return parseDocument(reader, parser);
79    }
80   
 
81  1 toggle public void test() throws Exception
82    {
83  1 test("abc", "<w:p><w:format>abc</w:format></w:p>");
84  1 test(
85    "h1.abc",
86    ""
87    + "<w:section w:absLevel=\"2\" w:docLevel=\"1\" w:headerLevel=\"1\">"
88    + "<w:header w:level=\"1\">"
89    + "<w:format>abc</w:format>"
90    + "</w:header>"
91    + "<w:content w:absLevel=\"2\" w:docLevel=\"1\" w:headerLevel=\"1\"/>"
92    + "</w:section>");
93  1 test(
94    "h1.abc\npara",
95    ""
96    + "<w:section w:absLevel=\"2\" w:docLevel=\"1\" w:headerLevel=\"1\">"
97    + "<w:header w:level=\"1\">"
98    + "<w:format>abc</w:format>"
99    + "</w:header>"
100    + "<w:content w:absLevel=\"2\" w:docLevel=\"1\" w:headerLevel=\"1\">"
101    + "<w:p><w:format>para</w:format></w:p>"
102    + "</w:content>"
103    + "</w:section>");
104  1 test("* item one", ""
105    + "<w:ul>"
106    + "<w:li><w:format>item one</w:format></w:li>"
107    + "</w:ul>");
108  1 test("* item one\n* item two", ""
109    + "<w:ul>"
110    + "<w:li><w:format>item one</w:format></w:li>"
111    + "<w:li><w:format>item two</w:format></w:li>"
112    + "</w:ul>");
113    }
114   
 
115  5 toggle private void test(String wiki, String xml) throws Exception
116    {
117  5 IWikiParser parser = newWikiParser();
118  5 Document doc = parseDocument(wiki, parser);
119  5 StringWriter writer = new StringWriter();
120  5 Source input = new DOMSource(doc.getDocumentElement());
121  5 Result output = new StreamResult(writer);
122  5 XmlUtil.write(input, output, false);
123  5 String result = writer.toString();
124  5 String fullXml = ""
125    +
126    "<w:document xmlns:w=\"http://www.wikimodel.org/ns/wem#\" xmlns:u=\"http://www.wikimodel.org/ns/user-defined-params#\">"
127    + "<w:section w:absLevel=\"1\" w:docLevel=\"1\" w:headerLevel=\"0\">"
128    + "<w:content w:absLevel=\"1\" w:docLevel=\"1\" w:headerLevel=\"0\">"
129    + xml
130    + "</w:content>"
131    + "</w:section>"
132    + "</w:document>";
133  5 assertEquals(fullXml, result);
134    }
135    }