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.HtmlDivision;
24  import com.gargoylesoftware.htmlunit.html.HtmlElement;
25  import com.gargoylesoftware.htmlunit.html.HtmlHeader2;
26  import com.gargoylesoftware.htmlunit.html.HtmlHeader3;
27  import com.gargoylesoftware.htmlunit.html.HtmlImage;
28  import com.gargoylesoftware.htmlunit.html.HtmlPage;
29  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
30  import com.gargoylesoftware.htmlunit.html.HtmlTable;
31  import com.gargoylesoftware.htmlunit.html.HtmlTableDataCell;
32  import com.gargoylesoftware.htmlunit.html.HtmlTableHeaderCell;
33  import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
34  import com.gargoylesoftware.htmlunit.html.UnknownHtmlElement;
35  
36  import java.util.Iterator;
37  
38  
39  /**
40   * 
41   *
42   * @author ltheussl
43   * @version $Id: AttributesVerifier.java 732140 2009-01-06 22:01:29Z ltheussl $
44   */
45  public class AttributesVerifier
46      extends AbstractVerifier
47  {
48      /** {@inheritDoc} */
49      public void verify( String file )
50              throws Exception
51      {
52          HtmlPage page = htmlPage( file );
53          assertNotNull( page );
54  
55          HtmlElement element = page.getHtmlElementById( "contentBox" );
56          assertNotNull( element );
57          HtmlDivision division = (HtmlDivision) element;
58          assertNotNull( division );
59  
60          Iterator elementIterator = division.getAllHtmlChildElements();
61  
62          // ----------------------------------------------------------------------
63          //
64          // ----------------------------------------------------------------------
65  
66          HtmlDivision div = (HtmlDivision) elementIterator.next();
67          assertEquals( "section", div.getAttributeValue( "class" ) );
68  
69          HtmlHeader2 h2 = (HtmlHeader2) elementIterator.next();
70          assertNotNull( h2 );
71          assertEquals( "section", h2.asText().trim() );
72  
73          HtmlAnchor a = (HtmlAnchor) elementIterator.next();
74          assertNotNull( a );
75          assertEquals( "section", a.getAttributeValue( "name" ) );
76  
77          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
78          assertNotNull( p );
79  
80          assertEquals( "ID",  p.getAttributeValue( "id" ) );
81          assertEquals( "CLASS", p.getAttributeValue( "class" ) );
82          assertEquals( "TITLE", p.getAttributeValue( "title" ) );
83          assertEquals( "STYLE", p.getAttributeValue( "style" ) );
84          assertEquals( "LANG", p.getAttributeValue( "lang" ) );
85  
86          HtmlImage img = (HtmlImage) elementIterator.next();
87          assertNotNull( img );
88  
89          assertEquals( "project.png", img.getAttributeValue( "src" ) );
90          assertEquals( "150", img.getAttributeValue( "width" ) );
91          assertEquals( "93", img.getAttributeValue( "height" ) );
92          assertEquals( "border: 1px solid silver", img.getAttributeValue( "style" ) );
93          assertEquals( "Project", img.getAttributeValue( "alt" ) );
94  
95          // test object identity to distinguish the case ATTRIBUTE_VALUE_EMPTY
96          assertTrue( img.getAttributeValue( "dummy" ) == HtmlElement.ATTRIBUTE_NOT_DEFINED );
97  
98          HtmlTable table = (HtmlTable) elementIterator.next();
99          assertEquals( "1", table.getAttributeValue( "border" ) );
100         assertEquals( "none", table.getAttributeValue( "class" ) );
101 
102         element = (HtmlElement) 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( "center", th.getAttributeValue( "align" ) );
111         assertEquals( "2", th.getAttributeValue( "colspan" ) );
112         assertEquals( "50%", th.getAttributeValue( "width" ) );
113 
114         tr = (HtmlTableRow) elementIterator.next();
115 
116         th = (HtmlTableHeaderCell) elementIterator.next();
117         assertEquals( "left", th.getAttributeValue( "align" ) );
118         assertEquals( "2", th.getAttributeValue( "rowspan" ) );
119         assertEquals( "middle", th.getAttributeValue( "valign" ) );
120 
121         HtmlTableDataCell td = (HtmlTableDataCell) elementIterator.next();
122         td = (HtmlTableDataCell) elementIterator.next();
123         tr = (HtmlTableRow) elementIterator.next();
124         td = (HtmlTableDataCell) elementIterator.next();
125         td = (HtmlTableDataCell) elementIterator.next();
126 
127         p = (HtmlParagraph) elementIterator.next();
128         assertNotNull( p );
129 
130         UnknownHtmlElement unk = (UnknownHtmlElement) elementIterator.next();
131         assertEquals( "u", unk.getTagName() );
132         unk = (UnknownHtmlElement) elementIterator.next();
133         assertEquals( "s", unk.getTagName() );
134         unk = (UnknownHtmlElement) elementIterator.next();
135         assertEquals( "sub", unk.getTagName() );
136         unk = (UnknownHtmlElement) elementIterator.next();
137         assertEquals( "sup", unk.getTagName() );
138 
139         p = (HtmlParagraph) elementIterator.next();
140         assertNotNull( p );
141 
142         unk = (UnknownHtmlElement) elementIterator.next();
143         assertEquals( "b", unk.getTagName() );
144         unk = (UnknownHtmlElement) elementIterator.next();
145         assertEquals( "i", unk.getTagName() );
146         unk = (UnknownHtmlElement) elementIterator.next();
147         assertEquals( "i", unk.getTagName() );
148         unk = (UnknownHtmlElement) elementIterator.next();
149         assertEquals( "b", unk.getTagName() );
150 
151         p = (HtmlParagraph) elementIterator.next();
152         assertNotNull( p );
153         assertEquals( "color: red; margin-left: 20px", p.getAttributeValue( "style" ) );
154 
155         a = (HtmlAnchor) elementIterator.next();
156         assertEquals( "Anchor", a.getAttributeValue( "name" ) );
157 
158         p = (HtmlParagraph) elementIterator.next();
159         assertNotNull( p );
160 
161         a = (HtmlAnchor) elementIterator.next();
162         assertEquals( "#Anchor", a.getAttributeValue( "href" ) );
163         a = (HtmlAnchor) elementIterator.next();
164         assertEquals( "#Anchor", a.getAttributeValue( "href" ) );
165         a = (HtmlAnchor) elementIterator.next();
166         assertEquals( "http://maven.apache.org/", a.getAttributeValue( "href" ) );
167         assertEquals( "externalLink", a.getAttributeValue( "class" ) );
168         a = (HtmlAnchor) elementIterator.next();
169         assertEquals( "./cdc.html", a.getAttributeValue( "href" ) );
170         a = (HtmlAnchor) elementIterator.next();
171         assertEquals( "cdc.html", a.getAttributeValue( "href" ) );
172         a = (HtmlAnchor) elementIterator.next();
173         assertEquals( "cdc.pdf", a.getAttributeValue( "href" ) );
174         a = (HtmlAnchor) elementIterator.next();
175         assertEquals( "./cdc.txt", a.getAttributeValue( "href" ) );
176         a = (HtmlAnchor) elementIterator.next();
177         assertEquals( "/index.html", a.getAttributeValue( "href" ) );
178 
179         div = (HtmlDivision) elementIterator.next();
180         assertEquals( "section", div.getAttributeValue( "class" ) );
181         h2 = (HtmlHeader2) elementIterator.next();
182         assertEquals( "Section without id", h2.asText().trim() );
183         a = (HtmlAnchor) elementIterator.next();
184         assertEquals( "Section_without_id", a.getAttributeValue( "name" ) );
185 
186         div = (HtmlDivision) elementIterator.next();
187         assertEquals( "section", div.getAttributeValue( "class" ) );
188         HtmlHeader3 h3 = (HtmlHeader3) elementIterator.next();
189         assertEquals( "Subsection without id", h3.asText().trim() );
190         a = (HtmlAnchor) elementIterator.next();
191         assertEquals( "Subsection_without_id", a.getAttributeValue( "name" ) );
192 
193         a = (HtmlAnchor) elementIterator.next();
194         assertEquals( "section-id", a.getAttributeValue( "name" ) );
195         div = (HtmlDivision) elementIterator.next();
196         assertEquals( "section", div.getAttributeValue( "class" ) );
197         h2 = (HtmlHeader2) elementIterator.next();
198         assertEquals( "Section with id", h2.asText().trim() );
199 
200         a = (HtmlAnchor) elementIterator.next();
201         assertEquals( "subsection-id", a.getAttributeValue( "name" ) );
202         div = (HtmlDivision) elementIterator.next();
203         assertEquals( "section", div.getAttributeValue( "class" ) );
204         h3 = (HtmlHeader3) elementIterator.next();
205         assertEquals( "Subsection with id", h3.asText().trim() );
206 
207         assertFalse( elementIterator.hasNext() );
208     }
209 }