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.HtmlBold;
25  import org.htmlunit.html.HtmlCode;
26  import org.htmlunit.html.HtmlElement;
27  import org.htmlunit.html.HtmlHeading1;
28  import org.htmlunit.html.HtmlHeading2;
29  import org.htmlunit.html.HtmlImage;
30  import org.htmlunit.html.HtmlItalic;
31  import org.htmlunit.html.HtmlMain;
32  import org.htmlunit.html.HtmlPage;
33  import org.htmlunit.html.HtmlParagraph;
34  import org.htmlunit.html.HtmlPreformattedText;
35  import org.htmlunit.html.HtmlS;
36  import org.htmlunit.html.HtmlSection;
37  import org.htmlunit.html.HtmlSubscript;
38  import org.htmlunit.html.HtmlSuperscript;
39  import org.htmlunit.html.HtmlTable;
40  import org.htmlunit.html.HtmlTableDataCell;
41  import org.htmlunit.html.HtmlTableHeaderCell;
42  import org.htmlunit.html.HtmlTableRow;
43  import org.htmlunit.html.HtmlUnderlined;
44  
45  import static org.junit.jupiter.api.Assertions.assertEquals;
46  import static org.junit.jupiter.api.Assertions.assertFalse;
47  import static org.junit.jupiter.api.Assertions.assertNotNull;
48  import static org.junit.jupiter.api.Assertions.assertSame;
49  
50  /**
51   *
52   *
53   * @author ltheussl
54   */
55  public class AttributesVerifier extends AbstractVerifier {
56      /** {@inheritDoc} */
57      public void verify(String file) throws Exception {
58          HtmlPage page = htmlPage(file);
59          assertNotNull(page);
60  
61          HtmlElement element = page.getHtmlElementById("contentBox");
62          assertNotNull(element);
63          HtmlMain main = (HtmlMain) element;
64          assertNotNull(main);
65  
66          Iterator<HtmlElement> elementIterator = main.getHtmlElementDescendants().iterator();
67  
68          // ----------------------------------------------------------------------
69          //
70          // ----------------------------------------------------------------------
71  
72          HtmlSection section = (HtmlSection) elementIterator.next();
73  
74          HtmlAnchor anchor = (HtmlAnchor) elementIterator.next();
75          assertNotNull(anchor);
76          assertEquals("section", anchor.getAttribute("id"));
77          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
78          assertNotNull(h1);
79          assertEquals("section", h1.asNormalizedText().trim());
80  
81          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
82          assertNotNull(p);
83  
84          assertEquals("ID", p.getAttribute("id"));
85          assertEquals("CLASS", p.getAttribute("class"));
86          assertEquals("TITLE", p.getAttribute("title"));
87          assertEquals("STYLE", p.getAttribute("style"));
88          assertEquals("LANG", p.getAttribute("lang"));
89  
90          HtmlImage img = (HtmlImage) elementIterator.next();
91          assertNotNull(img);
92  
93          assertEquals("project.png", img.getAttribute("src"));
94          assertEquals("150", img.getAttribute("width"));
95          assertEquals("93", img.getAttribute("height"));
96          assertEquals("border: 1px solid silver", img.getAttribute("style"));
97          assertEquals("Project", img.getAttribute("alt"));
98  
99          // test object identity to distinguish the case ATTRIBUTE_VALUE_EMPTY
100         assertSame(img.getAttribute("dummy"), HtmlElement.ATTRIBUTE_NOT_DEFINED);
101 
102         HtmlTable table = (HtmlTable) elementIterator.next();
103         assertEquals("none", table.getAttribute("class"));
104 
105         element = elementIterator.next();
106         // this is a htmlunit bug
107         assertEquals("tbody", element.getTagName());
108 
109         HtmlTableRow tr = (HtmlTableRow) elementIterator.next();
110         HtmlTableHeaderCell th = (HtmlTableHeaderCell) elementIterator.next();
111 
112         th = (HtmlTableHeaderCell) elementIterator.next();
113         assertEquals("text-align: center; width: 50%;", th.getAttribute("style"));
114         assertEquals("2", th.getAttribute("colspan"));
115 
116         tr = (HtmlTableRow) elementIterator.next();
117 
118         th = (HtmlTableHeaderCell) elementIterator.next();
119         assertEquals("2", th.getAttribute("rowspan"));
120         assertEquals("vertical-align: middle;", th.getAttribute("style"));
121 
122         HtmlTableDataCell td = (HtmlTableDataCell) elementIterator.next();
123         td = (HtmlTableDataCell) elementIterator.next();
124         tr = (HtmlTableRow) elementIterator.next();
125         td = (HtmlTableDataCell) elementIterator.next();
126         td = (HtmlTableDataCell) elementIterator.next();
127 
128         p = (HtmlParagraph) elementIterator.next();
129         assertNotNull(p);
130 
131         HtmlUnderlined u = (HtmlUnderlined) elementIterator.next();
132         assertEquals("u", u.getTagName());
133         HtmlS s = (HtmlS) elementIterator.next();
134         assertEquals("s", s.getTagName());
135         HtmlSubscript sub = (HtmlSubscript) elementIterator.next();
136         assertEquals("sub", sub.getTagName());
137         HtmlSuperscript sup = (HtmlSuperscript) elementIterator.next();
138         assertEquals("sup", sup.getTagName());
139 
140         p = (HtmlParagraph) elementIterator.next();
141         assertNotNull(p);
142 
143         HtmlBold b = (HtmlBold) elementIterator.next();
144         assertEquals("b", b.getTagName());
145         HtmlItalic i = (HtmlItalic) elementIterator.next();
146         assertEquals("i", i.getTagName());
147         i = (HtmlItalic) elementIterator.next();
148         assertEquals("i", i.getTagName());
149         b = (HtmlBold) elementIterator.next();
150         assertEquals("b", b.getTagName());
151 
152         p = (HtmlParagraph) elementIterator.next();
153         assertNotNull(p);
154         assertEquals("color: red; margin-left: 20px", p.getAttribute("style"));
155 
156         HtmlAnchor a = (HtmlAnchor) elementIterator.next();
157         assertEquals("Anchor", a.getAttribute("id"));
158 
159         p = (HtmlParagraph) elementIterator.next();
160         assertNotNull(p);
161 
162         a = (HtmlAnchor) elementIterator.next();
163         assertEquals("#Anchor", a.getAttribute("href"));
164         a = (HtmlAnchor) elementIterator.next();
165         assertEquals("#Anchor", a.getAttribute("href"));
166         a = (HtmlAnchor) elementIterator.next();
167         assertEquals("http://maven.apache.org/", a.getAttribute("href"));
168         assertEquals("externalLink", a.getAttribute("class"));
169         a = (HtmlAnchor) elementIterator.next();
170         assertEquals("./cdc.html", a.getAttribute("href"));
171         a = (HtmlAnchor) elementIterator.next();
172         assertEquals("cdc.html", a.getAttribute("href"));
173         a = (HtmlAnchor) elementIterator.next();
174         assertEquals("cdc.pdf", a.getAttribute("href"));
175         a = (HtmlAnchor) elementIterator.next();
176         assertEquals("./cdc.txt", a.getAttribute("href"));
177         a = (HtmlAnchor) elementIterator.next();
178         assertEquals("/index.html", a.getAttribute("href"));
179 
180         HtmlPreformattedText pre = (HtmlPreformattedText) elementIterator.next();
181         assertNotNull(pre);
182         assertEquals("pretty", pre.getAttribute("class"));
183         HtmlCode code = (HtmlCode) elementIterator.next();
184         assertNotNull(code);
185 
186         pre = (HtmlPreformattedText) elementIterator.next();
187         assertNotNull(pre);
188         assertEquals("pretty", pre.getAttribute("id"));
189         code = (HtmlCode) elementIterator.next();
190         assertNotNull(code);
191 
192         section = (HtmlSection) elementIterator.next();
193         anchor = (HtmlAnchor) elementIterator.next();
194         assertNotNull(anchor);
195         assertEquals("Section_without_id", anchor.getAttribute("id"));
196         h1 = (HtmlHeading1) elementIterator.next();
197         assertEquals("Section without id", h1.asNormalizedText().trim());
198 
199         section = (HtmlSection) elementIterator.next();
200         anchor = (HtmlAnchor) elementIterator.next();
201         assertNotNull(anchor);
202         assertEquals("Subsection_without_id", anchor.getAttribute("id"));
203         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
204         assertEquals("Subsection without id", h2.asNormalizedText().trim());
205 
206         a = (HtmlAnchor) elementIterator.next();
207         assertEquals("section-id", a.getAttribute("id"));
208         section = (HtmlSection) elementIterator.next();
209         anchor = (HtmlAnchor) elementIterator.next();
210         assertNotNull(anchor);
211         assertEquals("Section_with_id", anchor.getAttribute("id"));
212         h1 = (HtmlHeading1) elementIterator.next();
213         assertEquals("Section with id", h1.asNormalizedText().trim());
214 
215         a = (HtmlAnchor) elementIterator.next();
216         assertEquals("subsection-id", a.getAttribute("id"));
217         section = (HtmlSection) elementIterator.next();
218         anchor = (HtmlAnchor) elementIterator.next();
219         assertNotNull(anchor);
220         assertEquals("Subsection_with_id", anchor.getAttribute("id"));
221         h2 = (HtmlHeading2) elementIterator.next();
222         assertEquals("Subsection with id", h2.asNormalizedText().trim());
223 
224         a = (HtmlAnchor) elementIterator.next();
225         assertEquals("foo", a.getAttribute("id"));
226         section = (HtmlSection) elementIterator.next();
227         assertEquals("bar", section.getAttribute("class"));
228         assertEquals("foo", section.getAttribute("id"));
229         anchor = (HtmlAnchor) elementIterator.next();
230         assertNotNull(anchor);
231         assertEquals("Section_name", anchor.getAttribute("id"));
232         h1 = (HtmlHeading1) elementIterator.next();
233         assertEquals("Section name", h1.asNormalizedText().trim());
234         assertEquals("", h1.getAttribute("class"));
235 
236         a = (HtmlAnchor) elementIterator.next();
237         assertEquals("subfoo", a.getAttribute("id"));
238         section = (HtmlSection) elementIterator.next();
239         assertEquals("subbar", section.getAttribute("class"));
240         assertEquals("subfoo", section.getAttribute("id"));
241         anchor = (HtmlAnchor) elementIterator.next();
242         assertNotNull(anchor);
243         assertEquals("Subsection_name", anchor.getAttribute("id"));
244         h2 = (HtmlHeading2) elementIterator.next();
245         assertEquals("Subsection name", h2.asNormalizedText().trim());
246         assertEquals("", h2.getAttribute("class"));
247 
248         assertFalse(elementIterator.hasNext());
249     }
250 }