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.HtmlBold;
25  import com.gargoylesoftware.htmlunit.html.HtmlDivision;
26  import com.gargoylesoftware.htmlunit.html.HtmlElement;
27  import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
28  import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
29  import com.gargoylesoftware.htmlunit.html.HtmlImage;
30  import com.gargoylesoftware.htmlunit.html.HtmlItalic;
31  import com.gargoylesoftware.htmlunit.html.HtmlMain;
32  import com.gargoylesoftware.htmlunit.html.HtmlPage;
33  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
34  import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
35  import com.gargoylesoftware.htmlunit.html.HtmlS;
36  import com.gargoylesoftware.htmlunit.html.HtmlSection;
37  import com.gargoylesoftware.htmlunit.html.HtmlSubscript;
38  import com.gargoylesoftware.htmlunit.html.HtmlSuperscript;
39  import com.gargoylesoftware.htmlunit.html.HtmlTable;
40  import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
41  import com.gargoylesoftware.htmlunit.html.HtmlTableHeaderCell;
42  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
43  import com.gargoylesoftware.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          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
75          assertNotNull(h1);
76          assertEquals("section", h1.asNormalizedText().trim());
77  
78          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
79          assertNotNull(p);
80  
81          assertEquals("ID", p.getAttribute("id"));
82          assertEquals("CLASS", p.getAttribute("class"));
83          assertEquals("TITLE", p.getAttribute("title"));
84          assertEquals("STYLE", p.getAttribute("style"));
85          assertEquals("LANG", p.getAttribute("lang"));
86  
87          HtmlImage img = (HtmlImage) elementIterator.next();
88          assertNotNull(img);
89  
90          assertEquals("project.png", img.getAttribute("src"));
91          assertEquals("150", img.getAttribute("width"));
92          assertEquals("93", img.getAttribute("height"));
93          assertEquals("border: 1px solid silver", img.getAttribute("style"));
94          assertEquals("Project", img.getAttribute("alt"));
95  
96          // test object identity to distinguish the case ATTRIBUTE_VALUE_EMPTY
97          assertSame(img.getAttribute("dummy"), HtmlElement.ATTRIBUTE_NOT_DEFINED);
98  
99          HtmlTable table = (HtmlTable) elementIterator.next();
100         assertEquals("none", table.getAttribute("class"));
101 
102         element = elementIterator.next();
103         // this is a htmlunit bug
104         assertEquals("tbody", element.getTagName());
105 
106         HtmlTableRow tr = (HtmlTableRow) elementIterator.next();
107         HtmlTableHeaderCell th = (HtmlTableHeaderCell) elementIterator.next();
108 
109         th = (HtmlTableHeaderCell) elementIterator.next();
110         assertEquals("text-align: center; width: 50%;", th.getAttribute("style"));
111         assertEquals("2", th.getAttribute("colspan"));
112 
113         tr = (HtmlTableRow) elementIterator.next();
114 
115         th = (HtmlTableHeaderCell) elementIterator.next();
116         assertEquals("2", th.getAttribute("rowspan"));
117         assertEquals("vertical-align: middle;", th.getAttribute("style"));
118 
119         HtmlTableDataCell td = (HtmlTableDataCell) elementIterator.next();
120         td = (HtmlTableDataCell) elementIterator.next();
121         tr = (HtmlTableRow) elementIterator.next();
122         td = (HtmlTableDataCell) elementIterator.next();
123         td = (HtmlTableDataCell) elementIterator.next();
124 
125         p = (HtmlParagraph) elementIterator.next();
126         assertNotNull(p);
127 
128         HtmlUnderlined u = (HtmlUnderlined) elementIterator.next();
129         assertEquals("u", u.getTagName());
130         HtmlS s = (HtmlS) elementIterator.next();
131         assertEquals("s", s.getTagName());
132         HtmlSubscript sub = (HtmlSubscript) elementIterator.next();
133         assertEquals("sub", sub.getTagName());
134         HtmlSuperscript sup = (HtmlSuperscript) elementIterator.next();
135         assertEquals("sup", sup.getTagName());
136 
137         p = (HtmlParagraph) elementIterator.next();
138         assertNotNull(p);
139 
140         HtmlBold b = (HtmlBold) elementIterator.next();
141         assertEquals("b", b.getTagName());
142         HtmlItalic i = (HtmlItalic) elementIterator.next();
143         assertEquals("i", i.getTagName());
144         i = (HtmlItalic) elementIterator.next();
145         assertEquals("i", i.getTagName());
146         b = (HtmlBold) elementIterator.next();
147         assertEquals("b", b.getTagName());
148 
149         p = (HtmlParagraph) elementIterator.next();
150         assertNotNull(p);
151         assertEquals("color: red; margin-left: 20px", p.getAttribute("style"));
152 
153         HtmlAnchor a = (HtmlAnchor) elementIterator.next();
154         assertEquals("Anchor", a.getAttribute("id"));
155 
156         p = (HtmlParagraph) elementIterator.next();
157         assertNotNull(p);
158 
159         a = (HtmlAnchor) elementIterator.next();
160         assertEquals("#Anchor", a.getAttribute("href"));
161         a = (HtmlAnchor) elementIterator.next();
162         assertEquals("#Anchor", a.getAttribute("href"));
163         a = (HtmlAnchor) elementIterator.next();
164         assertEquals("http://maven.apache.org/", a.getAttribute("href"));
165         assertEquals("externalLink", a.getAttribute("class"));
166         a = (HtmlAnchor) elementIterator.next();
167         assertEquals("./cdc.html", a.getAttribute("href"));
168         a = (HtmlAnchor) elementIterator.next();
169         assertEquals("cdc.html", a.getAttribute("href"));
170         a = (HtmlAnchor) elementIterator.next();
171         assertEquals("cdc.pdf", a.getAttribute("href"));
172         a = (HtmlAnchor) elementIterator.next();
173         assertEquals("./cdc.txt", a.getAttribute("href"));
174         a = (HtmlAnchor) elementIterator.next();
175         assertEquals("/index.html", a.getAttribute("href"));
176 
177         HtmlDivision div = (HtmlDivision) elementIterator.next();
178         assertEquals("verbatim source", div.getAttribute("class"));
179         HtmlPreformattedText pre = (HtmlPreformattedText) elementIterator.next();
180         assertEquals("pretty", pre.getAttribute("class"));
181 
182         div = (HtmlDivision) elementIterator.next();
183         assertEquals("verbatim source", div.getAttribute("class"));
184         assertEquals("", div.getAttribute("id"));
185         pre = (HtmlPreformattedText) elementIterator.next();
186         assertEquals("pretty", pre.getAttribute("id"));
187 
188         section = (HtmlSection) elementIterator.next();
189         h1 = (HtmlHeading1) elementIterator.next();
190         assertEquals("Section without id", h1.asNormalizedText().trim());
191 
192         section = (HtmlSection) elementIterator.next();
193         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
194         assertEquals("Subsection without id", h2.asNormalizedText().trim());
195 
196         a = (HtmlAnchor) elementIterator.next();
197         assertEquals("section-id", a.getAttribute("id"));
198         section = (HtmlSection) elementIterator.next();
199         h1 = (HtmlHeading1) elementIterator.next();
200         assertEquals("Section with id", h1.asNormalizedText().trim());
201 
202         a = (HtmlAnchor) elementIterator.next();
203         assertEquals("subsection-id", a.getAttribute("id"));
204         section = (HtmlSection) elementIterator.next();
205         h2 = (HtmlHeading2) elementIterator.next();
206         assertEquals("Subsection with id", h2.asNormalizedText().trim());
207 
208         a = (HtmlAnchor) elementIterator.next();
209         assertEquals("foo", a.getAttribute("id"));
210         section = (HtmlSection) elementIterator.next();
211         assertEquals("bar", section.getAttribute("class"));
212         assertEquals("foo", section.getAttribute("id"));
213         h1 = (HtmlHeading1) elementIterator.next();
214         assertEquals("Section name", h1.asNormalizedText().trim());
215         assertEquals("", h1.getAttribute("class"));
216 
217         a = (HtmlAnchor) elementIterator.next();
218         assertEquals("subfoo", a.getAttribute("id"));
219         section = (HtmlSection) elementIterator.next();
220         assertEquals("subbar", section.getAttribute("class"));
221         assertEquals("subfoo", section.getAttribute("id"));
222         h2 = (HtmlHeading2) elementIterator.next();
223         assertEquals("Subsection name", h2.asNormalizedText().trim());
224         assertEquals("", h2.getAttribute("class"));
225 
226         assertFalse(elementIterator.hasNext());
227     }
228 }