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 org.htmlunit.html.HtmlAnchor;
24  import org.htmlunit.html.HtmlElement;
25  import org.htmlunit.html.HtmlHeading1;
26  import org.htmlunit.html.HtmlListItem;
27  import org.htmlunit.html.HtmlMain;
28  import org.htmlunit.html.HtmlPage;
29  import org.htmlunit.html.HtmlParagraph;
30  import org.htmlunit.html.HtmlSection;
31  import org.htmlunit.html.HtmlUnorderedList;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertFalse;
35  import static org.junit.jupiter.api.Assertions.assertNotNull;
36  
37  /**
38   *
39   * @author ltheussl
40   */
41  public class MultipleBlockVerifier extends AbstractVerifier {
42      /** {@inheritDoc} */
43      public void verify(String file) throws Exception {
44          HtmlPage page = htmlPage(file);
45          assertNotNull(page);
46  
47          HtmlElement element = page.getHtmlElementById("contentBox");
48          assertNotNull(element);
49          HtmlMain main = (HtmlMain) element;
50          assertNotNull(main);
51  
52          Iterator<HtmlElement> elementIterator = main.getHtmlElementDescendants().iterator();
53  
54          // ----------------------------------------------------------------------
55          // Verify link
56          // ----------------------------------------------------------------------
57  
58          HtmlSection section = (HtmlSection) elementIterator.next();
59          assertNotNull(section);
60  
61          HtmlAnchor anchor = (HtmlAnchor) elementIterator.next();
62          assertNotNull(anchor);
63          assertEquals("section_name", anchor.getAttribute("id"));
64          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
65          assertNotNull(h1);
66          assertEquals("section name", h1.asNormalizedText().trim());
67  
68          // ----------------------------------------------------------------------
69          // Paragraph
70          // ----------------------------------------------------------------------
71  
72          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
73          assertNotNull(p);
74          assertEquals("text", p.asNormalizedText().trim());
75  
76          // ----------------------------------------------------------------------
77          // Unordered list
78          // ----------------------------------------------------------------------
79  
80          HtmlUnorderedList ul = (HtmlUnorderedList) elementIterator.next();
81          assertNotNull(ul);
82  
83          HtmlListItem li = (HtmlListItem) elementIterator.next();
84          assertNotNull(li);
85          assertEquals("list1", li.getFirstChild().asNormalizedText().trim());
86  
87          // ----------------------------------------------------------------------
88          // Paragraph
89          // ----------------------------------------------------------------------
90  
91          p = (HtmlParagraph) elementIterator.next();
92          assertNotNull(p);
93          assertEquals("text2", p.asNormalizedText().trim());
94  
95          // ----------------------------------------------------------------------
96          // Unordered list
97          // ----------------------------------------------------------------------
98  
99          ul = (HtmlUnorderedList) elementIterator.next();
100         assertNotNull(ul);
101 
102         li = (HtmlListItem) elementIterator.next();
103         assertNotNull(li);
104         assertEquals("list1", li.getFirstChild().asNormalizedText().trim());
105 
106         // ----------------------------------------------------------------------
107         // Paragraph
108         // ----------------------------------------------------------------------
109 
110         p = (HtmlParagraph) elementIterator.next();
111         assertNotNull(p);
112         assertEquals("text3", p.asNormalizedText().trim());
113 
114         // ----------------------------------------------------------------------
115         // Unordered list
116         // ----------------------------------------------------------------------
117 
118         ul = (HtmlUnorderedList) elementIterator.next();
119         assertNotNull(ul);
120 
121         li = (HtmlListItem) elementIterator.next();
122         assertNotNull(li);
123 
124         p = (HtmlParagraph) elementIterator.next();
125         assertNotNull(p);
126         assertEquals("list1", p.getFirstChild().asNormalizedText().trim());
127 
128         assertFalse(elementIterator.hasNext());
129     }
130 }