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