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.HtmlSection;
35  import com.gargoylesoftware.htmlunit.html.HtmlSubscript;
36  import com.gargoylesoftware.htmlunit.html.HtmlSuperscript;
37  import com.gargoylesoftware.htmlunit.html.HtmlTable;
38  import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
39  import com.gargoylesoftware.htmlunit.html.HtmlTableHeaderCell;
40  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
41  import com.gargoylesoftware.htmlunit.html.HtmlUnderlined;
42  
43  import java.util.Iterator;
44  
45  
46  /**
47   *
48   *
49   * @author ltheussl
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          HtmlSection section = (HtmlSection) elementIterator.next();
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         assertSame( 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         HtmlDivision 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         section = (HtmlSection) elementIterator.next();
195         h2 = (HtmlHeading2) elementIterator.next();
196         assertEquals( "Section without id", h2.asText().trim() );
197         a = (HtmlAnchor) elementIterator.next();
198         assertEquals( "Section_without_id", a.getAttribute( "name" ) );
199 
200         section = (HtmlSection) elementIterator.next();
201         HtmlHeading3 h3 = (HtmlHeading3) elementIterator.next();
202         assertEquals( "Subsection without id", h3.asText().trim() );
203         a = (HtmlAnchor) elementIterator.next();
204         assertEquals( "Subsection_without_id", a.getAttribute( "name" ) );
205 
206         a = (HtmlAnchor) elementIterator.next();
207         assertEquals( "section-id", a.getAttribute( "name" ) );
208         section = (HtmlSection) elementIterator.next();
209         h2 = (HtmlHeading2) elementIterator.next();
210         assertEquals( "Section with id", h2.asText().trim() );
211         a = (HtmlAnchor) elementIterator.next();
212         assertEquals( "Section_with_id", a.getAttribute( "name" ) );
213 
214         a = (HtmlAnchor) elementIterator.next();
215         assertEquals( "subsection-id", a.getAttribute( "name" ) );
216         section = (HtmlSection) elementIterator.next();
217         h3 = (HtmlHeading3) elementIterator.next();
218         assertEquals( "Subsection with id", h3.asText().trim() );
219         a = (HtmlAnchor) elementIterator.next();
220         assertEquals( "Subsection_with_id", a.getAttribute( "name" ) );
221 
222         a = (HtmlAnchor) elementIterator.next();
223         assertEquals( "foo", a.getAttribute( "name" ) );
224         section = (HtmlSection) elementIterator.next();
225         assertEquals( "bar", section.getAttribute( "class" ) );
226         assertEquals( "foo", section.getAttribute( "id" ) );
227         h2 = (HtmlHeading2) elementIterator.next();
228         assertEquals( "Section name", h2.asText().trim() );
229         assertEquals( "", h2.getAttribute( "class" ) );
230         a = (HtmlAnchor) elementIterator.next();
231         assertEquals( "Section_name", a.getAttribute( "name" ) );
232 
233         a = (HtmlAnchor) elementIterator.next();
234         assertEquals( "subfoo", a.getAttribute( "name" ) );
235         section = (HtmlSection) elementIterator.next();
236         assertEquals( "subbar", section.getAttribute( "class" ) );
237         assertEquals( "subfoo", section.getAttribute( "id" ) );
238         h3 = (HtmlHeading3) elementIterator.next();
239         assertEquals( "Subsection name", h3.asText().trim() );
240         assertEquals( "", h3.getAttribute( "class" ) );
241         a = (HtmlAnchor) elementIterator.next();
242         assertEquals( "Subsection_name", a.getAttribute( "name" ) );
243 
244         assertFalse( elementIterator.hasNext() );
245     }
246 }