View Javadoc
1   package org.apache.maven.doxia.siterenderer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
23  import com.gargoylesoftware.htmlunit.html.HtmlBold;
24  import com.gargoylesoftware.htmlunit.html.HtmlDivision;
25  import com.gargoylesoftware.htmlunit.html.HtmlElement;
26  import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
27  import com.gargoylesoftware.htmlunit.html.HtmlHeading3;
28  import com.gargoylesoftware.htmlunit.html.HtmlImage;
29  import com.gargoylesoftware.htmlunit.html.HtmlItalic;
30  import com.gargoylesoftware.htmlunit.html.HtmlPage;
31  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
32  import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
33  import com.gargoylesoftware.htmlunit.html.HtmlS;
34  import com.gargoylesoftware.htmlunit.html.HtmlSubscript;
35  import com.gargoylesoftware.htmlunit.html.HtmlSuperscript;
36  import com.gargoylesoftware.htmlunit.html.HtmlTable;
37  import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
38  import com.gargoylesoftware.htmlunit.html.HtmlTableHeaderCell;
39  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
40  import com.gargoylesoftware.htmlunit.html.HtmlUnderlined;
41  
42  import java.util.Iterator;
43  
44  
45  /**
46   *
47   *
48   * @author ltheussl
49   */
50  public class AttributesVerifier
51      extends AbstractVerifier
52  {
53      /** {@inheritDoc} */
54      public void verify( String file )
55              throws Exception
56      {
57          HtmlPage page = htmlPage( file );
58          assertNotNull( page );
59  
60          HtmlElement element = page.getHtmlElementById( "contentBox" );
61          assertNotNull( element );
62          HtmlDivision division = (HtmlDivision) element;
63          assertNotNull( division );
64  
65          Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
66  
67          // ----------------------------------------------------------------------
68          //
69          // ----------------------------------------------------------------------
70  
71          HtmlDivision div = (HtmlDivision) elementIterator.next();
72          assertEquals( "section", div.getAttribute( "class" ) );
73  
74          HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
75          assertNotNull( h2 );
76          assertEquals( "section", h2.asText().trim() );
77  
78          HtmlAnchor a = (HtmlAnchor) elementIterator.next();
79          assertNotNull( a );
80          assertEquals( "section", a.getAttribute( "name" ) );
81  
82          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
83          assertNotNull( p );
84  
85          assertEquals( "ID",  p.getAttribute( "id" ) );
86          assertEquals( "CLASS", p.getAttribute( "class" ) );
87          assertEquals( "TITLE", p.getAttribute( "title" ) );
88          assertEquals( "STYLE", p.getAttribute( "style" ) );
89          assertEquals( "LANG", p.getAttribute( "lang" ) );
90  
91          HtmlImage img = (HtmlImage) elementIterator.next();
92          assertNotNull( img );
93  
94          assertEquals( "project.png", img.getAttribute( "src" ) );
95          assertEquals( "150", img.getAttribute( "width" ) );
96          assertEquals( "93", img.getAttribute( "height" ) );
97          assertEquals( "border: 1px solid silver", img.getAttribute( "style" ) );
98          assertEquals( "Project", img.getAttribute( "alt" ) );
99  
100         // test object identity to distinguish the case ATTRIBUTE_VALUE_EMPTY
101         assertTrue( img.getAttribute( "dummy" ) == HtmlElement.ATTRIBUTE_NOT_DEFINED );
102 
103         HtmlTable table = (HtmlTable) elementIterator.next();
104         assertEquals( "1", table.getAttribute( "border" ) );
105         assertEquals( "none", table.getAttribute( "class" ) );
106 
107         element = elementIterator.next();
108         // this is a htmlunit bug
109         assertEquals( "tbody", element.getTagName() );
110 
111         HtmlTableRow tr = (HtmlTableRow) elementIterator.next();
112         HtmlTableHeaderCell th = (HtmlTableHeaderCell) elementIterator.next();
113 
114         th = (HtmlTableHeaderCell) elementIterator.next();
115         assertEquals( "center", th.getAttribute( "align" ) );
116         assertEquals( "2", th.getAttribute( "colspan" ) );
117         assertEquals( "50%", th.getAttribute( "width" ) );
118 
119         tr = (HtmlTableRow) elementIterator.next();
120 
121         th = (HtmlTableHeaderCell) elementIterator.next();
122         assertEquals( "2", th.getAttribute( "rowspan" ) );
123         assertEquals( "middle", th.getAttribute( "valign" ) );
124 
125         HtmlTableDataCell td = (HtmlTableDataCell) elementIterator.next();
126         td = (HtmlTableDataCell) elementIterator.next();
127         tr = (HtmlTableRow) elementIterator.next();
128         td = (HtmlTableDataCell) elementIterator.next();
129         td = (HtmlTableDataCell) elementIterator.next();
130 
131         p = (HtmlParagraph) elementIterator.next();
132         assertNotNull( p );
133 
134         HtmlUnderlined u = (HtmlUnderlined) elementIterator.next();
135         assertEquals( "u", u.getTagName() );
136         HtmlS s = (HtmlS) elementIterator.next();
137         assertEquals( "s", s.getTagName() );
138         HtmlSubscript sub = (HtmlSubscript) elementIterator.next();
139         assertEquals( "sub", sub.getTagName() );
140         HtmlSuperscript sup = (HtmlSuperscript) elementIterator.next();
141         assertEquals( "sup", sup.getTagName() );
142 
143         p = (HtmlParagraph) elementIterator.next();
144         assertNotNull( p );
145 
146         HtmlBold b = (HtmlBold) elementIterator.next();
147         assertEquals( "b", b.getTagName() );
148         HtmlItalic i = (HtmlItalic) elementIterator.next();
149         assertEquals( "i", i.getTagName() );
150         i = (HtmlItalic) elementIterator.next();
151         assertEquals( "i", i.getTagName() );
152         b = (HtmlBold) elementIterator.next();
153         assertEquals( "b", b.getTagName() );
154 
155         p = (HtmlParagraph) elementIterator.next();
156         assertNotNull( p );
157         assertEquals( "color: red; margin-left: 20px", p.getAttribute( "style" ) );
158 
159         a = (HtmlAnchor) elementIterator.next();
160         assertEquals( "Anchor", a.getAttribute( "name" ) );
161 
162         p = (HtmlParagraph) elementIterator.next();
163         assertNotNull( p );
164 
165         a = (HtmlAnchor) elementIterator.next();
166         assertEquals( "#Anchor", a.getAttribute( "href" ) );
167         a = (HtmlAnchor) elementIterator.next();
168         assertEquals( "#Anchor", a.getAttribute( "href" ) );
169         a = (HtmlAnchor) elementIterator.next();
170         assertEquals( "http://maven.apache.org/", a.getAttribute( "href" ) );
171         assertEquals( "externalLink", a.getAttribute( "class" ) );
172         a = (HtmlAnchor) elementIterator.next();
173         assertEquals( "./cdc.html", a.getAttribute( "href" ) );
174         a = (HtmlAnchor) elementIterator.next();
175         assertEquals( "cdc.html", a.getAttribute( "href" ) );
176         a = (HtmlAnchor) elementIterator.next();
177         assertEquals( "cdc.pdf", a.getAttribute( "href" ) );
178         a = (HtmlAnchor) elementIterator.next();
179         assertEquals( "./cdc.txt", a.getAttribute( "href" ) );
180         a = (HtmlAnchor) elementIterator.next();
181         assertEquals( "/index.html", a.getAttribute( "href" ) );
182 
183         div = (HtmlDivision) elementIterator.next();
184         assertEquals( "source", div.getAttribute( "class" ) );
185         HtmlPreformattedText pre = (HtmlPreformattedText) elementIterator.next();
186         assertEquals( "pretty", pre.getAttribute( "class" ) );
187 
188         div = (HtmlDivision) elementIterator.next();
189         assertEquals( "source", div.getAttribute( "class" ) );
190         assertEquals( "", div.getAttribute( "id" ) );
191         pre = (HtmlPreformattedText) elementIterator.next();
192         assertEquals( "pretty", pre.getAttribute( "id" ) );
193 
194         div = (HtmlDivision) elementIterator.next();
195         assertEquals( "section", div.getAttribute( "class" ) );
196         h2 = (HtmlHeading2) elementIterator.next();
197         assertEquals( "Section without id", h2.asText().trim() );
198         a = (HtmlAnchor) elementIterator.next();
199         assertEquals( "Section_without_id", a.getAttribute( "name" ) );
200 
201         div = (HtmlDivision) elementIterator.next();
202         assertEquals( "section", div.getAttribute( "class" ) );
203         HtmlHeading3 h3 = (HtmlHeading3) elementIterator.next();
204         assertEquals( "Subsection without id", h3.asText().trim() );
205         a = (HtmlAnchor) elementIterator.next();
206         assertEquals( "Subsection_without_id", a.getAttribute( "name" ) );
207 
208         a = (HtmlAnchor) elementIterator.next();
209         assertEquals( "section-id", a.getAttribute( "name" ) );
210         div = (HtmlDivision) elementIterator.next();
211         assertEquals( "section", div.getAttribute( "class" ) );
212         h2 = (HtmlHeading2) elementIterator.next();
213         assertEquals( "Section with id", h2.asText().trim() );
214         a = (HtmlAnchor) elementIterator.next();
215         assertEquals( "Section_with_id", a.getAttribute( "name" ) );
216 
217         a = (HtmlAnchor) elementIterator.next();
218         assertEquals( "subsection-id", a.getAttribute( "name" ) );
219         div = (HtmlDivision) elementIterator.next();
220         assertEquals( "section", div.getAttribute( "class" ) );
221         h3 = (HtmlHeading3) elementIterator.next();
222         assertEquals( "Subsection with id", h3.asText().trim() );
223         a = (HtmlAnchor) elementIterator.next();
224         assertEquals( "Subsection_with_id", a.getAttribute( "name" ) );
225 
226         a = (HtmlAnchor) elementIterator.next();
227         assertEquals( "foo", a.getAttribute( "name" ) );
228         div = (HtmlDivision) elementIterator.next();
229         assertEquals( "bar", div.getAttribute( "class" ) );
230         assertEquals( "foo", div.getAttribute( "id" ) );
231         h2 = (HtmlHeading2) elementIterator.next();
232         assertEquals( "Section name", h2.asText().trim() );
233         assertEquals( "", h2.getAttribute( "class" ) );
234         a = (HtmlAnchor) elementIterator.next();
235         assertEquals( "Section_name", a.getAttribute( "name" ) );
236 
237         a = (HtmlAnchor) elementIterator.next();
238         assertEquals( "subfoo", a.getAttribute( "name" ) );
239         div = (HtmlDivision) elementIterator.next();
240         assertEquals( "subbar", div.getAttribute( "class" ) );
241         assertEquals( "subfoo", div.getAttribute( "id" ) );
242         h3 = (HtmlHeading3) elementIterator.next();
243         assertEquals( "Subsection name", h3.asText().trim() );
244         assertEquals( "", h3.getAttribute( "class" ) );
245         a = (HtmlAnchor) elementIterator.next();
246         assertEquals( "Subsection_name", a.getAttribute( "name" ) );
247 
248         assertFalse( elementIterator.hasNext() );
249     }
250 }