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