1. Project Clover database mar. avr. 16 2024 08:19:06 CEST
  2. Package org.devacfr.maven.skins.reflow

File HtmlToolTest.java

 

Code metrics

0
90
23
1
255
193
23
0,26
3,91
23
1

Classes

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

Contributing tests

This file is covered by 22 tests. .

Source view

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