1. Project Clover database mer. févr. 4 2026 12:48:28 CET
  2. Package org.devacfr.maven.skins.reflow

File HtmlToolTest.java

 

Code metrics

0
90
23
1
248
190
23
0,26
3,91
23
1

Classes

Class Line # Actions
HtmlToolTest 35 90 0% 23 0
1.0100%
 

Contributing tests

This file is covered by 44 tests. .

Source view

1    /*
2    * Copyright 2012-2025 Christophe Friederich
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10    * Unless required by applicable law or agreed to in writing, software
11    * distributed under the License is distributed on an "AS IS" BASIS,
12    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    * See the License for the specific language governing permissions and
14    * limitations under the License.
15    */
16    package org.devacfr.maven.skins.reflow;
17   
18    import static org.hamcrest.MatcherAssert.assertThat;
19    import static org.hamcrest.Matchers.contains;
20   
21    import com.google.common.collect.ImmutableMap;
22    import java.io.IOException;
23    import java.util.Collections;
24    import java.util.List;
25    import java.util.stream.Collectors;
26    import org.apache.velocity.tools.ToolContext;
27    import org.apache.velocity.tools.generic.ValueParser;
28    import org.devacfr.maven.skins.reflow.HtmlTool.ExtractResult;
29    import org.devacfr.maven.skins.reflow.HtmlTool.IdElement;
30    import org.devacfr.maven.skins.reflow.HtmlTool.JoinSeparator;
31    import org.devacfr.testing.jupiter.TestCase;
32    import org.junit.jupiter.api.BeforeEach;
33    import org.junit.jupiter.api.Test;
34   
 
35    public class HtmlToolTest extends TestCase {
36   
37    private HtmlTool htmlTool;
38   
 
39  44 toggle @BeforeEach
40    public void setup() {
41  44 htmlTool = new HtmlTool();
42  44 htmlTool.configure(new ValueParser(ImmutableMap.<String, Object> builder()
43    .put("velocityContext",
44    new ToolContext(ImmutableMap.<String, Object> builder().put("outputEncoding", "utf-8").build()))
45    .build()));
46    }
47   
48    /**
49    * Simple split Test
50    */
 
51  2 toggle @Test
52    public void shouldSplitBodyFragment() {
53  2 final String body = getActualResource();
54  2 final List<String> fragments = htmlTool.split(body, "hr");
55  2 assertEquals(2, fragments.size());
56  2 assertEquals("<h2>section1</h2>", fragments.get(0));
57  2 assertEquals("<h2>section2</h2>", fragments.get(1));
58    }
59   
60    /**
61    * Test split html with unexiting element.
62    */
 
63  2 toggle @Test
64    public void shouldNotSplit() {
65  2 final String body = getActualResource();
66  2 final List<String> fragments = htmlTool.split(body, "p");
67  2 assertEquals(1, fragments.size());
68    }
69   
70    /**
71    * Test split not include nested separator.
72    */
 
73  2 toggle @Test
74    public void shouldSplitRecursively() {
75  2 final String body = getActualResource();
76  2 final List<String> fragments = htmlTool.split(body, ".section");
77  2 assertEquals(2, fragments.size());
78  2 assertEquals("<h2>section1</h2>\n<div></div>", fragments.get(0));
79  2 assertEquals("<div></div>", fragments.get(1));
80    }
81   
 
82  2 toggle @Test
83    public void shouldSplitJoinSeparatorBefore() {
84  2 final String body = getActualResource();
85  2 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.BEFORE);
86  2 assertEquals(2, fragments.size());
87  2 assertEquals("<h2>section1</h2>\n<hr>", fragments.get(0));
88  2 assertEquals("<h2>section2</h2>", fragments.get(1));
89   
90  2 fragments = htmlTool.split(body, "hr", "before");
91  2 assertEquals(2, fragments.size());
92  2 assertEquals("<h2>section1</h2>\n<hr>", fragments.get(0));
93  2 assertEquals("<h2>section2</h2>", fragments.get(1));
94    }
95   
 
96  2 toggle @Test
97    public void shouldSplitJoinSeparatorAfter() {
98  2 final String body = getActualResource();
99  2 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.AFTER);
100  2 assertEquals(2, fragments.size());
101  2 assertEquals("<h2>section1</h2>", fragments.get(0));
102  2 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(1));
103   
104  2 fragments = htmlTool.split(body, "hr", "after");
105  2 assertEquals(2, fragments.size());
106  2 assertEquals("<h2>section1</h2>", fragments.get(0));
107  2 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(1));
108    }
109   
 
110  2 toggle @Test
111    public void shouldSplitJoinSeparatorNo() {
112  2 final String body = getActualResource();
113  2 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.NO);
114  2 assertEquals(2, fragments.size());
115  2 assertEquals("<h2>section1</h2>", fragments.get(0));
116  2 assertEquals("<h2>section2</h2>", fragments.get(1));
117   
118  2 fragments = htmlTool.split(body, "hr", "");
119  2 assertEquals(2, fragments.size());
120  2 assertEquals("<h2>section1</h2>", fragments.get(0));
121  2 assertEquals("<h2>section2</h2>", fragments.get(1));
122    }
123   
 
124  2 toggle @Test
125    public void shouldSplitOnStarts() {
126  2 final String body = getActualResource();
127  2 final List<String> fragments = htmlTool.splitOnStarts(body, "hr");
128  2 assertEquals(1, fragments.size());
129  2 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(0));
130    }
131   
 
132  2 toggle @Test
133    public void fixTableHeadsWithTagListReportOuput() throws IOException {
134  2 verify(htmlTool::fixTableHeads);
135    }
136   
 
137  2 toggle @Test
138    public void shouldReorderToTopOneSection() {
139  2 verify(content -> htmlTool.reorderToTop(content, "a:has(img), img", 1, "<div class=\"caption\"></div>"), "html");
140    }
141   
 
142  2 toggle @Test
143    public void shouldExtract() {
144  2 final String section = getActualResource("html");
145   
146  2 final ExtractResult results = htmlTool.extract(section, "a:has(img), img", 3);
147  2 assertNotNull(results);
148  2 final List<String> actuals = results.getExtracted();
149  2 assertNotNull(actuals);
150  2 assertThat(actuals,
151    contains("<a href=\"themes/bootswatch-cerulean.html\"><img src=\"images/1.png\"></a>",
152    "<img src=\"images/2.png\">",
153    "<img src=\"images/3.png\">"));
154  2 final String remainder = results.getRemainder();
155  2 assertEquals(getExpectedResource("html"), remainder);
156    }
157   
 
158  2 toggle @Test
159    public void shouldSetAttribute() {
160  2 final String content = "<div><span></span></div>";
161  2 final String actual = htmlTool.setAttr(content, "span", "class", "section");
162  2 assertEquals("<div>\n <span class=\"section\"></span>\n</div>", actual);
163    }
164   
 
165  2 toggle @Test
166    public void shouldgetAttribute() {
167  2 final String content = "<div class=\"section\"><span class=\"title\"></span></div>";
168  2 final List<String> actuals = htmlTool.getAttr(content, "span", "class");
169  2 assertThat(actuals, contains("title"));
170    }
171   
 
172  2 toggle @Test
173    public void shouldAddClass() {
174  2 final String content = "<div><span></span></div>";
175  2 final String actual = htmlTool.addClass(content, "span", "section");
176  2 assertEquals("<div>\n" + " <span class=\"section\"></span>\n" + "</div>", actual);
177    }
178   
 
179  2 toggle @Test
180    public void shouldWrapElement() {
181  2 final String content = "<div><span></span></div>";
182  2 final String actual = htmlTool.wrap(content, "span", "<a href=\"http://reflow.com\"></a>", 1);
183  2 assertEquals("<div>\n <a href=\"http://reflow.com\"><span></span></a>\n</div>", actual);
184    }
185   
 
186  2 toggle @Test
187    public void shouldRemoveElement() {
188  2 final String content = "<div><span></span></div>";
189  2 final String actual = htmlTool.remove(content, "span");
190  2 assertEquals("<div></div>", actual);
191    }
192   
 
193  2 toggle @Test
194    public void shouldReplace() {
195  2 final String content = "<div><span></span></div>";
196  2 final String actual = htmlTool.replace(content, "span", "<a href=\"http://reflow.com\"></a>");
197  2 assertEquals("<div>\n <a href=\"http://reflow.com\"></a>\n</div>", actual);
198    }
199   
 
200  2 toggle @Test
201    public void shouldReplaceWith() {
202  2 final String actual = htmlTool
203    .replaceWith("<p>text <tt>foo value</tt> end text.</p>", "tt", "<code class=\"literal\">");
204  2 assertEquals("<p>text <code class=\"literal\">foo value</code> end text.</p>", actual);
205    }
206   
 
207  2 toggle @Test
208    public void shouldExtractText() {
209  2 final String content = "<div><span>paragraph</span></div>";
210  2 final List<String> actuals = htmlTool.text(content, "span");
211  2 assertThat(actuals, contains("paragraph"));
212    }
213   
 
214  2 toggle @Test
215    public void shouldHeadingAnchorToId() {
216  2 verify(content -> htmlTool.headingAnchorToId(content), "html");
217    }
218   
 
219  2 toggle @Test
220    public void shouldEnsureHeadingIds() {
221  2 verify(content -> htmlTool.ensureHeadingIds("page", "overview", content, HtmlTool.DEFAULT_SLUG_SEPARATOR), "html");
222    }
223   
 
224  2 toggle @Test
225    public void shouldEnsureHeadingIdsForFrame() {
226  2 verify(content -> htmlTool.ensureHeadingIds("frame", "overview", content, HtmlTool.DEFAULT_SLUG_SEPARATOR), "html");
227    }
228   
 
229  2 toggle @Test
230    public void shouldHeadingTree() {
231  2 final String content = htmlTool
232    .ensureHeadingIds("page", "overview", getActualResource("html"), HtmlTool.DEFAULT_SLUG_SEPARATOR);
233  2 final List<? extends IdElement> idElements = htmlTool.headingTree(content, Collections.emptyList());
234    // check root element
235  2 assertThat(idElements.stream().map(IdElement::getId).collect(Collectors.toList()),
236    contains("apache-maven-site-plugin"));
237  2 assertThat(idElements.stream().map(IdElement::getTagName).collect(Collectors.toList()), contains("h2"));
238  2 assertThat(idElements.stream().map(IdElement::getText).collect(Collectors.toList()),
239    contains("Apache Maven Site Plugin"));
240   
241    // check children
242  2 assertThat(idElements.stream().map(IdElement::getHeadingLevel).collect(Collectors.toList()), contains(2));
243  2 assertThat(idElements.get(0).getItems().stream().map(IdElement::getId).collect(Collectors.toList()),
244    contains("goals-overview", "usage"));
245  2 assertThat(idElements.get(0).getItems().stream().map(IdElement::getHeadingLevel).collect(Collectors.toList()),
246    contains(3, 3));
247    }
248    }