1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.officeimporter.internal.cleaner

File LineBreakOfficeCleaningTest.java

 

Code metrics

0
40
6
1
140
77
6
0.15
6.67
6
1

Classes

Class Line # Actions
LineBreakOfficeCleaningTest 39 40 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 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.officeimporter.internal.cleaner;
21   
22    import java.io.StringReader;
23    import java.util.Collections;
24   
25    import org.jmock.Expectations;
26    import org.junit.Assert;
27    import org.junit.Test;
28    import org.w3c.dom.Document;
29    import org.w3c.dom.NodeList;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.xml.html.HTMLCleanerConfiguration;
32   
33    /**
34    * Test case for cleaning html {@code<br/>} elements in {@link OfficeHTMLCleaner}.
35    *
36    * @version $Id: ca1e8da725ba94ce5e5c68aa7ec83bbde9c006cd $
37    * @since 1.8
38    */
 
39    public class LineBreakOfficeCleaningTest extends AbstractHTMLCleaningTest
40    {
41    /**
42    * {@code <br/>} elements placed next to paragraph elements should be converted to {@code<div
43    * class="wikikmodel-emptyline"/>} elements.
44    */
 
45  1 toggle @Test
46    public void testLineBreaksNextToParagraphElements()
47    {
48  1 checkLineBreakReplacements("<div><br/><br/><p>para</p></div>", 0, 2);
49  1 checkLineBreakReplacements("<div><p>para</p><br/><br/></div>", 0, 2);
50  1 checkLineBreakReplacements("<div><p>para</p><br/><br/><p>para</p></div>", 0, 2);
51    }
52   
53    /**
54    * {@code <br/>} elements placed next to list elements should be converted to {@code<div
55    * class="wikikmodel-emptyline"/>} elements.
56    */
 
57  1 toggle @Test
58    public void testLineBreaksNextToListElements()
59    {
60  1 checkLineBreakReplacements("<div><br/><br/><ol><li>para</li></ol></div>", 0, 2);
61  1 checkLineBreakReplacements("<div><ol><li>para</li></ol><br/><br/></div>", 0, 2);
62  1 checkLineBreakReplacements("<div><ol><li>para</li></ol><br/><br/><ol><li>para</li></ol></div>", 0, 2);
63   
64  1 checkLineBreakReplacements("<div><br/><br/><ul><li>para</li></ul></div>", 0, 2);
65  1 checkLineBreakReplacements("<div><ul><li>para</li></ul><br/><br/></div>", 0, 2);
66  1 checkLineBreakReplacements("<div><ul><li>para</li></ul><br/><br/><ul><li>para</li></ul></div>", 0, 2);
67    }
68   
69    /**
70    * {@code <br/>} elements placed next to html heading elements should be converted to {@code<div
71    * class="wikikmodel-emptyline"/>} elements.
72    */
 
73  1 toggle @Test
74    public void testLineBreaksNextToHeadingElements()
75    {
76  1 checkLineBreakReplacements("<div><br/><br/><h1>test</h1></div>", 0, 2);
77  1 checkLineBreakReplacements("<div><h1>test</h1><br/><br/></div>", 0, 2);
78  1 checkLineBreakReplacements("<div><h1>test</h1><br/><br/><h1>test</h1></div>", 0, 2);
79   
80  1 checkLineBreakReplacements("<div><br/><br/><h2>test</h2></div>", 0, 2);
81  1 checkLineBreakReplacements("<div><h2>test</h2><br/><br/></div>", 0, 2);
82  1 checkLineBreakReplacements("<div><h2>test</h2><br/><br/><h2>test</h2></div>", 0, 2);
83   
84  1 checkLineBreakReplacements("<div><br/><br/><h3>test</h3></div>", 0, 2);
85  1 checkLineBreakReplacements("<div><h3>test</h3><br/><br/></div>", 0, 2);
86  1 checkLineBreakReplacements("<div><h3>test</h3><br/><br/><h3>test</h3></div>", 0, 2);
87   
88  1 checkLineBreakReplacements("<div><br/><br/><h4>test</h4></div>", 0, 2);
89  1 checkLineBreakReplacements("<div><h4>test</h4><br/><br/></div>", 0, 2);
90  1 checkLineBreakReplacements("<div><h4>test</h4><br/><br/><h4>test</h4></div>", 0, 2);
91   
92  1 checkLineBreakReplacements("<div><br/><br/><h5>test</h5></div>", 0, 2);
93  1 checkLineBreakReplacements("<div><h5>test</h5><br/><br/></div>", 0, 2);
94  1 checkLineBreakReplacements("<div><h5>test</h5><br/><br/><h5>test</h5></div>", 0, 2);
95   
96  1 checkLineBreakReplacements("<div><br/><br/><h6>test</h6></div>", 0, 2);
97  1 checkLineBreakReplacements("<div><h6>test</h6><br/><br/></div>", 0, 2);
98  1 checkLineBreakReplacements("<div><h6>test</h6><br/><br/><h6>test</h6></div>", 0, 2);
99    }
100   
101    /**
102    * {@code <br/>} elements placed next to html table elements should be converted to {@code<div
103    * class="wikikmodel-emptyline"/>} elements.
104    */
 
105  1 toggle @Test
106    public void testLineBreaksNextToTableElements()
107    {
108  1 checkLineBreakReplacements("<div><br/><br/><table><tr><td>test</td></tr></table></div>", 0, 2);
109  1 checkLineBreakReplacements("<div><table><tr><td>test</td></tr></table><br/><br/></div>", 0, 2);
110  1 checkLineBreakReplacements(
111    "<div><table><tr><td>test</td></tr></table><br/><br/><table><tr><td>test</td></tr></table></div>", 0, 2);
112    }
113   
114    /**
115    * Utility methods for checking if {@code <br/>} elements are properly converted to {@code<div
116    * class="wikikmodel-emptyline"/>} elements.
117    *
118    * @param html the html content.
119    * @param expectedBrCount expected count of {@code <br/>} elements after cleaning.
120    * @param expectedDivCount expected count of {@code<div class="wikikmodel-emptyline"/>} elements after cleaning.
121    */
 
122  30 toggle private void checkLineBreakReplacements(String html, int expectedBrCount, int expectedDivCount)
123    {
124  30 getMockery().checking(new Expectations()
125    {
 
126  30 toggle {
127  30 allowing(mockDocumentReferenceResolver).resolve("Import.Test");
128  30 will(returnValue(new DocumentReference("wiki", "Import", "Test")));
129    }
130    });
131   
132  30 HTMLCleanerConfiguration configuration = this.officeHTMLCleaner.getDefaultConfiguration();
133  30 configuration.setParameters(Collections.singletonMap("targetDocument", "Import.Test"));
134  30 Document doc = officeHTMLCleaner.clean(new StringReader(header + html + footer), configuration);
135  30 NodeList lineBreaks = doc.getElementsByTagName("br");
136  30 Assert.assertEquals(expectedBrCount, lineBreaks.getLength());
137  30 NodeList divs = doc.getElementsByTagName("div");
138  30 Assert.assertEquals(expectedDivCount, divs.getLength());
139    }
140    }