View Javadoc
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.apache.maven.doxia.siterenderer;
20  
21  import java.util.Iterator;
22  
23  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionDescription;
24  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionList;
25  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionTerm;
26  import com.gargoylesoftware.htmlunit.html.HtmlElement;
27  import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
28  import com.gargoylesoftware.htmlunit.html.HtmlHeading3;
29  import com.gargoylesoftware.htmlunit.html.HtmlListItem;
30  import com.gargoylesoftware.htmlunit.html.HtmlMain;
31  import com.gargoylesoftware.htmlunit.html.HtmlOrderedList;
32  import com.gargoylesoftware.htmlunit.html.HtmlPage;
33  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
34  import com.gargoylesoftware.htmlunit.html.HtmlSection;
35  import com.gargoylesoftware.htmlunit.html.HtmlUnorderedList;
36  
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  import static org.junit.jupiter.api.Assertions.assertFalse;
39  import static org.junit.jupiter.api.Assertions.assertNotNull;
40  
41  /**
42   *
43   * @author ltheussl
44   */
45  public class NestedItemsVerifier extends AbstractVerifier {
46      /** {@inheritDoc} */
47      public void verify(String file) throws Exception {
48          HtmlPage page = htmlPage(file);
49          assertNotNull(page);
50  
51          HtmlElement element = page.getHtmlElementById("contentBox");
52          assertNotNull(element);
53          HtmlMain main = (HtmlMain) element;
54          assertNotNull(main);
55  
56          Iterator<HtmlElement> elementIterator = main.getHtmlElementDescendants().iterator();
57  
58          // ----------------------------------------------------------------------
59          // Verify link
60          // ----------------------------------------------------------------------
61  
62          HtmlSection section = (HtmlSection) elementIterator.next();
63          assertNotNull(section);
64  
65          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
66          assertNotNull(h1);
67          assertEquals("List Section", h1.asNormalizedText().trim());
68  
69          // ----------------------------------------------------------------------
70          // Unordered lists
71          // ----------------------------------------------------------------------
72  
73          section = (HtmlSection) elementIterator.next();
74          assertNotNull(section);
75  
76          section = (HtmlSection) elementIterator.next();
77          assertNotNull(section);
78  
79          HtmlHeading3 h3 = (HtmlHeading3) elementIterator.next();
80          assertNotNull(h3);
81          assertEquals("Unordered lists", h3.asNormalizedText().trim());
82  
83          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
84          assertNotNull(p);
85          assertEquals(
86                  "Below is an unordered list, followed by six paragraphs.",
87                  p.asNormalizedText().trim());
88  
89          HtmlUnorderedList ul = (HtmlUnorderedList) elementIterator.next();
90          assertNotNull(ul);
91  
92          HtmlListItem li = (HtmlListItem) elementIterator.next();
93          assertNotNull(li);
94          assertEquals("Item 1.", li.getFirstChild().asNormalizedText().trim());
95  
96          ul = (HtmlUnorderedList) elementIterator.next();
97          assertNotNull(ul);
98  
99          li = (HtmlListItem) elementIterator.next();
100         assertNotNull(li);
101         p = (HtmlParagraph) elementIterator.next();
102         assertNotNull(p);
103         assertEquals("Item 11.", p.getFirstChild().asNormalizedText().trim());
104         li = (HtmlListItem) elementIterator.next();
105         assertNotNull(li);
106         p = (HtmlParagraph) elementIterator.next();
107         assertNotNull(p);
108         assertEquals("Item 12.", p.getFirstChild().asNormalizedText().trim());
109         li = (HtmlListItem) elementIterator.next();
110         assertNotNull(li);
111         assertEquals("Item 13.", li.getFirstChild().asNormalizedText().trim());
112         li = (HtmlListItem) elementIterator.next();
113         assertNotNull(li);
114         assertEquals("Item 14.", li.getFirstChild().asNormalizedText().trim());
115         li = (HtmlListItem) elementIterator.next();
116         assertNotNull(li);
117         assertEquals("Item 2.", li.getFirstChild().asNormalizedText().trim());
118         li = (HtmlListItem) elementIterator.next();
119         assertNotNull(li);
120         assertEquals("Item 3.", li.getFirstChild().asNormalizedText().trim());
121         li = (HtmlListItem) elementIterator.next();
122         assertNotNull(li);
123         assertEquals("Item 4.", li.getFirstChild().asNormalizedText().trim());
124 
125         ul = (HtmlUnorderedList) elementIterator.next();
126         assertNotNull(ul);
127 
128         li = (HtmlListItem) elementIterator.next();
129         assertNotNull(li);
130         assertEquals("Item 41.", li.getFirstChild().asNormalizedText().trim());
131         li = (HtmlListItem) elementIterator.next();
132         assertNotNull(li);
133         assertEquals("Item 42.", li.getFirstChild().asNormalizedText().trim());
134         li = (HtmlListItem) elementIterator.next();
135         assertNotNull(li);
136         assertEquals("Item 43.", li.getFirstChild().asNormalizedText().trim());
137         li = (HtmlListItem) elementIterator.next();
138         assertNotNull(li);
139         assertEquals("Item 44.", li.getFirstChild().asNormalizedText().trim());
140 
141         p = (HtmlParagraph) elementIterator.next();
142         assertNotNull(p);
143         assertEquals("Paragraph 1 below list.", p.asNormalizedText().trim());
144         p = (HtmlParagraph) elementIterator.next();
145         assertNotNull(p);
146         assertEquals("Paragraph 2 below list.", p.asNormalizedText().trim());
147         p = (HtmlParagraph) elementIterator.next();
148         assertNotNull(p);
149         assertEquals("Paragraph 3 below list.", p.asNormalizedText().trim());
150         p = (HtmlParagraph) elementIterator.next();
151         assertNotNull(p);
152         assertEquals("Paragraph 4 below list.", p.asNormalizedText().trim());
153         p = (HtmlParagraph) elementIterator.next();
154         assertNotNull(p);
155         assertEquals("Paragraph 5 below list.", p.asNormalizedText().trim());
156         p = (HtmlParagraph) elementIterator.next();
157         assertNotNull(p);
158         assertEquals("Paragraph 6 below list.", p.asNormalizedText().trim());
159 
160         // ----------------------------------------------------------------------
161         // Ordered lists
162         // ----------------------------------------------------------------------
163 
164         section = (HtmlSection) elementIterator.next();
165         assertNotNull(section);
166 
167         h3 = (HtmlHeading3) elementIterator.next();
168         assertNotNull(h3);
169         assertEquals("Ordered lists", h3.asNormalizedText().trim());
170 
171         p = (HtmlParagraph) elementIterator.next();
172         assertNotNull(p);
173         assertEquals(
174                 "Below is an ordered list, followed by six paragraphs.",
175                 p.asNormalizedText().trim());
176 
177         HtmlOrderedList ol = (HtmlOrderedList) elementIterator.next();
178         assertNotNull(ol);
179 
180         li = (HtmlListItem) elementIterator.next();
181         assertNotNull(li);
182         assertEquals("Item 1.", li.getFirstChild().asNormalizedText().trim());
183 
184         ol = (HtmlOrderedList) elementIterator.next();
185         assertNotNull(ol);
186 
187         li = (HtmlListItem) elementIterator.next();
188         assertNotNull(li);
189         assertEquals("Item 11.", li.getFirstChild().asNormalizedText().trim());
190         li = (HtmlListItem) elementIterator.next();
191         assertNotNull(li);
192         assertEquals("Item 12.", li.getFirstChild().asNormalizedText().trim());
193         li = (HtmlListItem) elementIterator.next();
194         assertNotNull(li);
195         assertEquals("Item 13.", li.getFirstChild().asNormalizedText().trim());
196         li = (HtmlListItem) elementIterator.next();
197         assertNotNull(li);
198         assertEquals("Item 14.", li.getFirstChild().asNormalizedText().trim());
199         li = (HtmlListItem) elementIterator.next();
200         assertNotNull(li);
201         assertEquals("Item 2.", li.getFirstChild().asNormalizedText().trim());
202         li = (HtmlListItem) elementIterator.next();
203         assertNotNull(li);
204         assertEquals("Item 3.", li.getFirstChild().asNormalizedText().trim());
205         li = (HtmlListItem) elementIterator.next();
206         assertNotNull(li);
207         assertEquals("Item 4.", li.getFirstChild().asNormalizedText().trim());
208 
209         ol = (HtmlOrderedList) elementIterator.next();
210         assertNotNull(ol);
211 
212         li = (HtmlListItem) elementIterator.next();
213         assertNotNull(li);
214         assertEquals("Item 41.", li.getFirstChild().asNormalizedText().trim());
215         li = (HtmlListItem) elementIterator.next();
216         assertNotNull(li);
217         assertEquals("Item 42.", li.getFirstChild().asNormalizedText().trim());
218         li = (HtmlListItem) elementIterator.next();
219         assertNotNull(li);
220         assertEquals("Item 43.", li.getFirstChild().asNormalizedText().trim());
221         li = (HtmlListItem) elementIterator.next();
222         assertNotNull(li);
223         assertEquals("Item 44.", li.getFirstChild().asNormalizedText().trim());
224 
225         p = (HtmlParagraph) elementIterator.next();
226         assertNotNull(p);
227         assertEquals("Paragraph 1 below list.", p.asNormalizedText().trim());
228         p = (HtmlParagraph) elementIterator.next();
229         assertNotNull(p);
230         assertEquals("Paragraph 2 below list.", p.asNormalizedText().trim());
231         p = (HtmlParagraph) elementIterator.next();
232         assertNotNull(p);
233         assertEquals("Paragraph 3 below list.", p.asNormalizedText().trim());
234         p = (HtmlParagraph) elementIterator.next();
235         assertNotNull(p);
236         assertEquals("Paragraph 4 below list.", p.asNormalizedText().trim());
237         p = (HtmlParagraph) elementIterator.next();
238         assertNotNull(p);
239         assertEquals("Paragraph 5 below list.", p.asNormalizedText().trim());
240         p = (HtmlParagraph) elementIterator.next();
241         assertNotNull(p);
242         assertEquals("Paragraph 6 below list.", p.asNormalizedText().trim());
243 
244         // ----------------------------------------------------------------------
245         // Definition lists
246         // ----------------------------------------------------------------------
247 
248         section = (HtmlSection) elementIterator.next();
249         assertNotNull(section);
250 
251         h3 = (HtmlHeading3) elementIterator.next();
252         assertNotNull(h3);
253         assertEquals("Definition lists", h3.asNormalizedText().trim());
254 
255         p = (HtmlParagraph) elementIterator.next();
256         assertNotNull(p);
257         assertEquals(
258                 "Below is a definition list, followed by six paragraphs.",
259                 p.asNormalizedText().trim());
260 
261         HtmlDefinitionList dl = (HtmlDefinitionList) elementIterator.next();
262         assertNotNull(dl);
263 
264         HtmlDefinitionTerm dt = (HtmlDefinitionTerm) elementIterator.next();
265         assertNotNull(dt);
266         assertEquals("Term 1.", dt.getFirstChild().asNormalizedText().trim());
267         HtmlDefinitionDescription dd = (HtmlDefinitionDescription) elementIterator.next();
268         assertNotNull(dd);
269         assertEquals("Description 1.", dd.getFirstChild().asNormalizedText().trim());
270 
271         dt = (HtmlDefinitionTerm) elementIterator.next();
272         assertNotNull(dt);
273         assertEquals("Term 2.", dt.getFirstChild().asNormalizedText().trim());
274         dd = (HtmlDefinitionDescription) elementIterator.next();
275         assertNotNull(dd);
276         assertEquals("Description 2.", dd.getFirstChild().asNormalizedText().trim());
277 
278         dl = (HtmlDefinitionList) elementIterator.next();
279         assertNotNull(dl);
280         dt = (HtmlDefinitionTerm) elementIterator.next();
281         assertNotNull(dt);
282         assertEquals("Term 21.", dt.getFirstChild().asNormalizedText().trim());
283         dd = (HtmlDefinitionDescription) elementIterator.next();
284         assertNotNull(dd);
285         assertEquals("Description 21.", dd.getFirstChild().asNormalizedText().trim());
286 
287         p = (HtmlParagraph) elementIterator.next();
288         assertNotNull(p);
289         assertEquals("Paragraph 1 below list.", p.asNormalizedText().trim());
290         p = (HtmlParagraph) elementIterator.next();
291         assertNotNull(p);
292         assertEquals("Paragraph 2 below list.", p.asNormalizedText().trim());
293         p = (HtmlParagraph) elementIterator.next();
294         assertNotNull(p);
295         assertEquals("Paragraph 3 below list.", p.asNormalizedText().trim());
296         p = (HtmlParagraph) elementIterator.next();
297         assertNotNull(p);
298         assertEquals("Paragraph 4 below list.", p.asNormalizedText().trim());
299         p = (HtmlParagraph) elementIterator.next();
300         assertNotNull(p);
301         assertEquals("Paragraph 5 below list.", p.asNormalizedText().trim());
302         p = (HtmlParagraph) elementIterator.next();
303         assertNotNull(p);
304         assertEquals("Paragraph 6 below list.", p.asNormalizedText().trim());
305 
306         assertFalse(elementIterator.hasNext());
307     }
308 }