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.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 926050 2010-03-22 12:41:01Z 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( "2", th.getAttributeValue( "rowspan" ) );
118         assertEquals( "middle", th.getAttributeValue( "valign" ) );
119 
120         HtmlTableDataCell td = (HtmlTableDataCell) elementIterator.next();
121         td = (HtmlTableDataCell) elementIterator.next();
122         tr = (HtmlTableRow) elementIterator.next();
123         td = (HtmlTableDataCell) elementIterator.next();
124         td = (HtmlTableDataCell) elementIterator.next();
125 
126         p = (HtmlParagraph) elementIterator.next();
127         assertNotNull( p );
128 
129         UnknownHtmlElement unk = (UnknownHtmlElement) elementIterator.next();
130         assertEquals( "u", unk.getTagName() );
131         unk = (UnknownHtmlElement) elementIterator.next();
132         assertEquals( "s", unk.getTagName() );
133         unk = (UnknownHtmlElement) elementIterator.next();
134         assertEquals( "sub", unk.getTagName() );
135         unk = (UnknownHtmlElement) elementIterator.next();
136         assertEquals( "sup", unk.getTagName() );
137 
138         p = (HtmlParagraph) elementIterator.next();
139         assertNotNull( p );
140 
141         unk = (UnknownHtmlElement) elementIterator.next();
142         assertEquals( "b", unk.getTagName() );
143         unk = (UnknownHtmlElement) elementIterator.next();
144         assertEquals( "i", unk.getTagName() );
145         unk = (UnknownHtmlElement) elementIterator.next();
146         assertEquals( "i", unk.getTagName() );
147         unk = (UnknownHtmlElement) elementIterator.next();
148         assertEquals( "b", unk.getTagName() );
149 
150         p = (HtmlParagraph) elementIterator.next();
151         assertNotNull( p );
152         assertEquals( "color: red; margin-left: 20px", p.getAttributeValue( "style" ) );
153 
154         a = (HtmlAnchor) elementIterator.next();
155         assertEquals( "Anchor", a.getAttributeValue( "name" ) );
156 
157         p = (HtmlParagraph) elementIterator.next();
158         assertNotNull( p );
159 
160         a = (HtmlAnchor) elementIterator.next();
161         assertEquals( "#Anchor", a.getAttributeValue( "href" ) );
162         a = (HtmlAnchor) elementIterator.next();
163         assertEquals( "#Anchor", a.getAttributeValue( "href" ) );
164         a = (HtmlAnchor) elementIterator.next();
165         assertEquals( "http://maven.apache.org/", a.getAttributeValue( "href" ) );
166         assertEquals( "externalLink", a.getAttributeValue( "class" ) );
167         a = (HtmlAnchor) elementIterator.next();
168         assertEquals( "./cdc.html", a.getAttributeValue( "href" ) );
169         a = (HtmlAnchor) elementIterator.next();
170         assertEquals( "cdc.html", a.getAttributeValue( "href" ) );
171         a = (HtmlAnchor) elementIterator.next();
172         assertEquals( "cdc.pdf", a.getAttributeValue( "href" ) );
173         a = (HtmlAnchor) elementIterator.next();
174         assertEquals( "./cdc.txt", a.getAttributeValue( "href" ) );
175         a = (HtmlAnchor) elementIterator.next();
176         assertEquals( "/index.html", a.getAttributeValue( "href" ) );
177 
178         div = (HtmlDivision) elementIterator.next();
179         assertEquals( "section", div.getAttributeValue( "class" ) );
180         h2 = (HtmlHeader2) elementIterator.next();
181         assertEquals( "Section without id", h2.asText().trim() );
182         a = (HtmlAnchor) elementIterator.next();
183         assertEquals( "Section_without_id", a.getAttributeValue( "name" ) );
184 
185         div = (HtmlDivision) elementIterator.next();
186         assertEquals( "section", div.getAttributeValue( "class" ) );
187         HtmlHeader3 h3 = (HtmlHeader3) elementIterator.next();
188         assertEquals( "Subsection without id", h3.asText().trim() );
189         a = (HtmlAnchor) elementIterator.next();
190         assertEquals( "Subsection_without_id", a.getAttributeValue( "name" ) );
191 
192         a = (HtmlAnchor) elementIterator.next();
193         assertEquals( "section-id", a.getAttributeValue( "name" ) );
194         div = (HtmlDivision) elementIterator.next();
195         assertEquals( "section", div.getAttributeValue( "class" ) );
196         h2 = (HtmlHeader2) elementIterator.next();
197         assertEquals( "Section with id", h2.asText().trim() );
198 
199         a = (HtmlAnchor) elementIterator.next();
200         assertEquals( "subsection-id", a.getAttributeValue( "name" ) );
201         div = (HtmlDivision) elementIterator.next();
202         assertEquals( "section", div.getAttributeValue( "class" ) );
203         h3 = (HtmlHeader3) elementIterator.next();
204         assertEquals( "Subsection with id", h3.asText().trim() );
205 
206         assertFalse( elementIterator.hasNext() );
207     }
208 }