1. Project Clover database mar. janv. 20 2026 12:32:22 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 22 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  22 toggle @BeforeEach
40    public void setup() {
41  22 htmlTool = new HtmlTool();
42  22 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  1 toggle @Test
52    public void shouldSplitBodyFragment() {
53  1 final String body = getActualResource();
54  1 final List<String> fragments = htmlTool.split(body, "hr");
55  1 assertEquals(2, fragments.size());
56  1 assertEquals("<h2>section1</h2>", fragments.get(0));
57  1 assertEquals("<h2>section2</h2>", fragments.get(1));
58    }
59   
60    /**
61    * Test split html with unexiting element.
62    */
 
63  1 toggle @Test
64    public void shouldNotSplit() {
65  1 final String body = getActualResource();
66  1 final List<String> fragments = htmlTool.split(body, "p");
67  1 assertEquals(1, fragments.size());
68    }
69   
70    /**
71    * Test split not include nested separator.
72    */
 
73  1 toggle @Test
74    public void shouldSplitRecursively() {
75  1 final String body = getActualResource();
76  1 final List<String> fragments = htmlTool.split(body, ".section");
77  1 assertEquals(2, fragments.size());
78  1 assertEquals("<h2>section1</h2>\n<div></div>", fragments.get(0));
79  1 assertEquals("<div></div>", fragments.get(1));
80    }
81   
 
82  1 toggle @Test
83    public void shouldSplitJoinSeparatorBefore() {
84  1 final String body = getActualResource();
85  1 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.BEFORE);
86  1 assertEquals(2, fragments.size());
87  1 assertEquals("<h2>section1</h2>\n<hr>", fragments.get(0));
88  1 assertEquals("<h2>section2</h2>", fragments.get(1));
89   
90  1 fragments = htmlTool.split(body, "hr", "before");
91  1 assertEquals(2, fragments.size());
92  1 assertEquals("<h2>section1</h2>\n<hr>", fragments.get(0));
93  1 assertEquals("<h2>section2</h2>", fragments.get(1));
94    }
95   
 
96  1 toggle @Test
97    public void shouldSplitJoinSeparatorAfter() {
98  1 final String body = getActualResource();
99  1 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.AFTER);
100  1 assertEquals(2, fragments.size());
101  1 assertEquals("<h2>section1</h2>", fragments.get(0));
102  1 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(1));
103   
104  1 fragments = htmlTool.split(body, "hr", "after");
105  1 assertEquals(2, fragments.size());
106  1 assertEquals("<h2>section1</h2>", fragments.get(0));
107  1 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(1));
108    }
109   
 
110  1 toggle @Test
111    public void shouldSplitJoinSeparatorNo() {
112  1 final String body = getActualResource();
113  1 List<String> fragments = htmlTool.split(body, "hr", JoinSeparator.NO);
114  1 assertEquals(2, fragments.size());
115  1 assertEquals("<h2>section1</h2>", fragments.get(0));
116  1 assertEquals("<h2>section2</h2>", fragments.get(1));
117   
118  1 fragments = htmlTool.split(body, "hr", "");
119  1 assertEquals(2, fragments.size());
120  1 assertEquals("<h2>section1</h2>", fragments.get(0));
121  1 assertEquals("<h2>section2</h2>", fragments.get(1));
122    }
123   
 
124  1 toggle @Test
125    public void shouldSplitOnStarts() {
126  1 final String body = getActualResource();
127  1 final List<String> fragments = htmlTool.splitOnStarts(body, "hr");
128  1 assertEquals(1, fragments.size());
129  1 assertEquals("<hr>\n<h2>section2</h2>", fragments.get(0));
130    }
131   
 
132  1 toggle @Test
133    public void fixTableHeadsWithTagListReportOuput() throws IOException {
134  1 verify(htmlTool::fixTableHeads);
135    }
136   
 
137  1 toggle @Test
138    public void shouldReorderToTopOneSection() {
139  1 verify(content -> htmlTool.reorderToTop(content, "a:has(img), img", 1, "<div class=\"caption\"></div>"), "html");
140    }
141   
 
142  1 toggle @Test
143    public void shouldExtract() {
144  1 final String section = getActualResource("html");
145   
146  1 final ExtractResult results = htmlTool.extract(section, "a:has(img), img", 3);
147  1 assertNotNull(results);
148  1 final List<String> actuals = results.getExtracted();
149  1 assertNotNull(actuals);
150  1 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  1 final String remainder = results.getRemainder();
155  1 assertEquals(getExpectedResource("html"), remainder);
156    }
157   
 
158  1 toggle @Test
159    public void shouldSetAttribute() {
160  1 final String content = "<div><span></span></div>";
161  1 final String actual = htmlTool.setAttr(content, "span", "class", "section");
162  1 assertEquals("<div>\n <span class=\"section\"></span>\n</div>", actual);
163    }
164   
 
165  1 toggle @Test
166    public void shouldgetAttribute() {
167  1 final String content = "<div class=\"section\"><span class=\"title\"></span></div>";
168  1 final List<String> actuals = htmlTool.getAttr(content, "span", "class");
169  1 assertThat(actuals, contains("title"));
170    }
171   
 
172  1 toggle @Test
173    public void shouldAddClass() {
174  1 final String content = "<div><span></span></div>";
175  1 final String actual = htmlTool.addClass(content, "span", "section");
176  1 assertEquals("<div>\n" + " <span class=\"section\"></span>\n" + "</div>", actual);
177    }
178   
 
179  1 toggle @Test
180    public void shouldWrapElement() {
181  1 final String content = "<div><span></span></div>";
182  1 final String actual = htmlTool.wrap(content, "span", "<a href=\"http://reflow.com\"></a>", 1);
183  1 assertEquals("<div>\n <a href=\"http://reflow.com\"><span></span></a>\n</div>", actual);
184    }
185   
 
186  1 toggle @Test
187    public void shouldRemoveElement() {
188  1 final String content = "<div><span></span></div>";
189  1 final String actual = htmlTool.remove(content, "span");
190  1 assertEquals("<div></div>", actual);
191    }
192   
 
193  1 toggle @Test
194    public void shouldReplace() {
195  1 final String content = "<div><span></span></div>";
196  1 final String actual = htmlTool.replace(content, "span", "<a href=\"http://reflow.com\"></a>");
197  1 assertEquals("<div>\n <a href=\"http://reflow.com\"></a>\n</div>", actual);
198    }
199   
 
200  1 toggle @Test
201    public void shouldReplaceWith() {
202  1 final String actual = htmlTool
203    .replaceWith("<p>text <tt>foo value</tt> end text.</p>", "tt", "<code class=\"literal\">");
204  1 assertEquals("<p>text <code class=\"literal\">foo value</code> end text.</p>", actual);
205    }
206   
 
207  1 toggle @Test
208    public void shouldExtractText() {
209  1 final String content = "<div><span>paragraph</span></div>";
210  1 final List<String> actuals = htmlTool.text(content, "span");
211  1 assertThat(actuals, contains("paragraph"));
212    }
213   
 
214  1 toggle @Test
215    public void shouldHeadingAnchorToId() {
216  1 verify(content -> htmlTool.headingAnchorToId(content), "html");
217    }
218   
 
219  1 toggle @Test
220    public void shouldEnsureHeadingIds() {
221  1 verify(content -> htmlTool.ensureHeadingIds("page", "overview", content, HtmlTool.DEFAULT_SLUG_SEPARATOR), "html");
222    }
223   
 
224  1 toggle @Test
225    public void shouldEnsureHeadingIdsForFrame() {
226  1 verify(content -> htmlTool.ensureHeadingIds("frame", "overview", content, HtmlTool.DEFAULT_SLUG_SEPARATOR), "html");
227    }
228   
 
229  1 toggle @Test
230    public void shouldHeadingTree() {
231  1 final String content = htmlTool
232    .ensureHeadingIds("page", "overview", getActualResource("html"), HtmlTool.DEFAULT_SLUG_SEPARATOR);
233  1 final List<? extends IdElement> idElements = htmlTool.headingTree(content, Collections.emptyList());
234    // check root element
235  1 assertThat(idElements.stream().map(IdElement::getId).collect(Collectors.toList()),
236    contains("apache-maven-site-plugin"));
237  1 assertThat(idElements.stream().map(IdElement::getTagName).collect(Collectors.toList()), contains("h2"));
238  1 assertThat(idElements.stream().map(IdElement::getText).collect(Collectors.toList()),
239    contains("Apache Maven Site Plugin"));
240   
241    // check children
242  1 assertThat(idElements.stream().map(IdElement::getHeadingLevel).collect(Collectors.toList()), contains(2));
243  1 assertThat(idElements.get(0).getItems().stream().map(IdElement::getId).collect(Collectors.toList()),
244    contains("goals-overview", "usage"));
245  1 assertThat(idElements.get(0).getItems().stream().map(IdElement::getHeadingLevel).collect(Collectors.toList()),
246    contains(3, 3));
247    }
248    }