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.HtmlAnchor;
24  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionDescription;
25  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionList;
26  import com.gargoylesoftware.htmlunit.html.HtmlDefinitionTerm;
27  import com.gargoylesoftware.htmlunit.html.HtmlDivision;
28  import com.gargoylesoftware.htmlunit.html.HtmlElement;
29  import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
30  import com.gargoylesoftware.htmlunit.html.HtmlListItem;
31  import com.gargoylesoftware.htmlunit.html.HtmlMain;
32  import com.gargoylesoftware.htmlunit.html.HtmlOrderedList;
33  import com.gargoylesoftware.htmlunit.html.HtmlPage;
34  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
35  import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
36  import com.gargoylesoftware.htmlunit.html.HtmlSection;
37  
38  import static org.junit.jupiter.api.Assertions.assertEquals;
39  import static org.junit.jupiter.api.Assertions.assertFalse;
40  import static org.junit.jupiter.api.Assertions.assertNotNull;
41  
42  /**
43   *
44   * @author ltheussl
45   */
46  public class FaqVerifier extends AbstractVerifier {
47      /** {@inheritDoc} */
48      public void verify(String file) throws Exception {
49          HtmlPage page = htmlPage(file);
50          assertNotNull(page);
51  
52          HtmlElement element = page.getHtmlElementById("contentBox");
53          assertNotNull(element);
54          HtmlMain main = (HtmlMain) element;
55          assertNotNull(main);
56  
57          Iterator<HtmlElement> elementIterator = main.getHtmlElementDescendants().iterator();
58  
59          // ----------------------------------------------------------------------
60          //
61          // ----------------------------------------------------------------------
62  
63          HtmlSection section = (HtmlSection) elementIterator.next();
64  
65          HtmlAnchor a = (HtmlAnchor) elementIterator.next();
66          assertEquals("top", a.getAttribute("id"));
67  
68          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
69          assertEquals("Often Asked Questions", h1.asNormalizedText().trim());
70  
71          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
72          element = elementIterator.next();
73          assertEquals("b", element.getTagName());
74          assertEquals("Contributing", element.asNormalizedText().trim());
75  
76          HtmlOrderedList ol = (HtmlOrderedList) elementIterator.next();
77          assertEquals(
78                  "One stupid question & a silly answer?",
79                  ol.getFirstElementChild().asNormalizedText().trim());
80  
81          HtmlListItem li = (HtmlListItem) elementIterator.next();
82          assertEquals(
83                  "One stupid question & a silly answer?",
84                  li.getFirstElementChild().asNormalizedText().trim());
85  
86          a = (HtmlAnchor) elementIterator.next();
87          assertEquals("#stupid-question", a.getAttribute("href"));
88  
89          element = elementIterator.next();
90          assertEquals("b", element.getTagName());
91          assertEquals("stupid", element.asNormalizedText().trim());
92  
93          p = (HtmlParagraph) elementIterator.next();
94          element = elementIterator.next();
95          assertEquals("b", element.getTagName());
96          assertEquals("Using Maven", element.asNormalizedText().trim());
97  
98          ol = (HtmlOrderedList) elementIterator.next();
99          assertEquals(
100                 "How do I disable a report on my site?",
101                 ol.getFirstElementChild().asNormalizedText().trim());
102 
103         li = (HtmlListItem) elementIterator.next();
104         assertNotNull(li);
105         assertEquals(
106                 "How do I disable a report on my site?",
107                 li.getFirstElementChild().asNormalizedText().trim());
108 
109         a = (HtmlAnchor) elementIterator.next();
110         assertEquals("#disable-reports", a.getAttribute("href"));
111 
112         section = (HtmlSection) elementIterator.next();
113 
114         a = (HtmlAnchor) elementIterator.next();
115         assertEquals("contributing", a.getAttribute("id"));
116 
117         h1 = (HtmlHeading1) elementIterator.next();
118         assertEquals("Contributing", h1.asNormalizedText().trim());
119 
120         HtmlDefinitionList dl = (HtmlDefinitionList) elementIterator.next();
121 
122         a = (HtmlAnchor) elementIterator.next();
123         assertEquals("stupid-question", a.getAttribute("id"));
124 
125         HtmlDefinitionTerm dt = (HtmlDefinitionTerm) elementIterator.next();
126         assertEquals(
127                 "One stupid question & a silly answer?", dt.asNormalizedText().trim());
128 
129         element = elementIterator.next();
130         assertEquals("b", element.getTagName());
131         assertEquals("stupid", element.asNormalizedText().trim());
132 
133         HtmlDefinitionDescription dd = (HtmlDefinitionDescription) elementIterator.next();
134 
135         p = (HtmlParagraph) elementIterator.next();
136 
137         a = (HtmlAnchor) elementIterator.next();
138         assertEquals("#using", a.getAttribute("href"));
139         assertEquals("local link", a.asNormalizedText().trim());
140 
141         a = (HtmlAnchor) elementIterator.next();
142         assertEquals("./cdc.html", a.getAttribute("href"));
143         assertEquals("source document", a.asNormalizedText().trim());
144 
145         a = (HtmlAnchor) elementIterator.next();
146         assertEquals("http://maven.apache.org/?l=a&m=b", a.getAttribute("href"));
147         assertEquals("external link", a.asNormalizedText().trim());
148 
149         element = elementIterator.next();
150         assertEquals("i", element.getTagName());
151         assertEquals("italic", element.asNormalizedText().trim());
152 
153         element = elementIterator.next();
154         assertEquals("b", element.getTagName());
155         assertEquals("non-US-ASCII characters: àéèç", element.asNormalizedText().trim());
156 
157         p = (HtmlParagraph) elementIterator.next();
158 
159         a = (HtmlAnchor) elementIterator.next();
160         assertEquals("#top", a.getAttribute("href"));
161         assertEquals("[top]", a.asNormalizedText().trim());
162 
163         section = (HtmlSection) elementIterator.next();
164 
165         a = (HtmlAnchor) elementIterator.next();
166         assertEquals("using", a.getAttribute("id"));
167 
168         h1 = (HtmlHeading1) elementIterator.next();
169         assertEquals("Using Maven", h1.asNormalizedText().trim());
170 
171         dl = (HtmlDefinitionList) elementIterator.next();
172 
173         a = (HtmlAnchor) elementIterator.next();
174         assertEquals("disable-reports", a.getAttribute("id"));
175 
176         dt = (HtmlDefinitionTerm) elementIterator.next();
177         assertEquals(
178                 "How do I disable a report on my site?",
179                 dt.getFirstChild().asNormalizedText().trim());
180 
181         dd = (HtmlDefinitionDescription) elementIterator.next();
182 
183         p = (HtmlParagraph) elementIterator.next();
184 
185         element = elementIterator.next();
186         assertEquals("code", element.getTagName());
187         assertEquals("<source></source>", element.asNormalizedText().trim());
188 
189         HtmlDivision div = (HtmlDivision) elementIterator.next();
190         assertEquals("verbatim source", div.getAttribute("class"));
191 
192         HtmlPreformattedText pre = (HtmlPreformattedText) elementIterator.next();
193         assertEquals("<source>1.5</source>", pre.asNormalizedText().trim());
194 
195         p = (HtmlParagraph) elementIterator.next();
196 
197         a = (HtmlAnchor) elementIterator.next();
198         assertEquals("#top", a.getAttribute("href"));
199         assertEquals("[top]", a.asNormalizedText().trim());
200 
201         assertFalse(elementIterator.hasNext());
202     }
203 }