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.HtmlHeading2;
26  import com.gargoylesoftware.htmlunit.html.HtmlHeading3;
27  import com.gargoylesoftware.htmlunit.html.HtmlHeading4;
28  import com.gargoylesoftware.htmlunit.html.HtmlMeta;
29  import com.gargoylesoftware.htmlunit.html.HtmlPage;
30  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
31  import com.gargoylesoftware.htmlunit.html.HtmlPreformattedText;
32  
33  import java.util.Iterator;
34  
35  /**
36   * Verify the <code>site/xdoc/entityTest.xml</code>
37   *
38   * @author ltheussl
39   */
40  public class EntitiesVerifier
41      extends AbstractVerifier
42  {
43      /** {@inheritDoc} */
44      public void verify( String file )
45              throws Exception
46      {
47          HtmlPage page = htmlPage( file );
48          assertNotNull( page );
49  
50          HtmlMeta author = (HtmlMeta) page.getElementsByName( "author" ).get( 0 );
51          assertNotNull( author );
52          assertTrue( author.toString().indexOf( "Ligature &#198;" ) > 0 );
53          assertEquals( "Ligature \u00C6", author.getContentAttribute() );
54  
55          author = (HtmlMeta) page.getElementsByName( "author" ).get( 1 );
56          assertNotNull( author );
57          assertTrue( author.toString().indexOf( "Ampersand &amp;" ) > 0 );
58          assertEquals( "Ampersand &", author.getContentAttribute() );
59  
60          author = (HtmlMeta) page.getElementsByName( "author" ).get( 2 );
61          assertNotNull( author );
62          assertTrue( author.toString().indexOf( "Less than &lt;" ) > 0 );
63          assertEquals( "Less than <", author.getContentAttribute() );
64  
65          author = (HtmlMeta) page.getElementsByName( "author" ).get( 3 );
66          assertNotNull( author );
67          assertTrue( author.toString().indexOf( "Greater than &gt;" ) > 0 );
68          assertEquals( "Greater than >", author.getContentAttribute() );
69  
70          author = (HtmlMeta) page.getElementsByName( "author" ).get( 4 );
71          assertNotNull( author );
72          assertTrue( author.getContentAttribute().equals( "Apostrophe '" ) );
73          assertEquals( "Apostrophe '", author.getContentAttribute() );
74  
75          author = (HtmlMeta) page.getElementsByName( "author" ).get( 5 );
76          assertNotNull( author );
77          assertTrue( author.toString().indexOf( "Quote &quot;" ) > 0 );
78          assertEquals( "Quote \"", author.getContentAttribute() );
79  
80          author = (HtmlMeta) page.getElementsByName( "author" ).get( 6 );
81          assertNotNull( author );
82          assertTrue( author.toString().indexOf( "test@email.com" ) > 0 );
83          assertEquals( "test@email.com", author.getContentAttribute() );
84  
85          author = (HtmlMeta) page.getElementsByName( "author" ).get( 7 );
86          assertNotNull( author );
87          assertTrue( author.toString().indexOf( "test&#169;email.com" ) > 0 );
88          assertEquals( "test\u00A9email.com", author.getContentAttribute() );
89  
90          HtmlElement element = page.getHtmlElementById( "contentBox" );
91          assertNotNull( element );
92          HtmlDivision division = (HtmlDivision) element;
93          assertNotNull( division );
94  
95          Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
96  
97          // ----------------------------------------------------------------------
98          //
99          // ----------------------------------------------------------------------
100 
101         HtmlDivision div = (HtmlDivision) elementIterator.next();
102         assertNotNull( div );
103         assertEquals( "section", div.getAttribute( "class" ) );
104 
105         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
106         assertNotNull( h2 );
107         assertEquals( h2.asText().trim(), "section name with entities: '&' '\u0391' ' ' '\uD7ED'" );
108 
109         HtmlAnchor a = (HtmlAnchor) elementIterator.next();
110         assertNotNull( a );
111         assertEquals( "section_name_with_entities:_.27.26.27_.27.CE.91.27_.27.C2.A0.27_.27.3F.3F.27",
112                       a.getAttribute( "name" ) );
113 
114         div = (HtmlDivision) elementIterator.next();
115         assertNotNull( div );
116         assertEquals( "section", div.getAttribute( "class" ) );
117 
118         div = (HtmlDivision) elementIterator.next();
119         assertNotNull( div );
120         assertEquals( "section", div.getAttribute( "class" ) );
121 
122         HtmlHeading4 h4 = (HtmlHeading4) elementIterator.next();
123         assertNotNull( h4 );
124         assertEquals( "Entities", h4.asText().trim() );
125 
126         a = (HtmlAnchor) elementIterator.next();
127         assertNotNull( a );
128         assertEquals( "Entities", a.getAttribute( "name" ) );
129 
130         div = (HtmlDivision) elementIterator.next();
131 
132         HtmlHeading3 h3 = (HtmlHeading3) elementIterator.next();
133         assertNotNull( h3 );
134         assertEquals( "Generic Entities: '&' '<' '>' '\"' '''", h3.asText().trim() );
135 
136         a = (HtmlAnchor) elementIterator.next();
137 
138         HtmlParagraph p = (HtmlParagraph) elementIterator.next();
139         assertNotNull( p );
140         assertEquals( "'&' '<' '>' '\"' '''", p.asText().trim() );
141 
142         div = (HtmlDivision) elementIterator.next();
143 
144         h3 = (HtmlHeading3) elementIterator.next();
145         assertNotNull( h3 );
146         assertEquals( "Local Entities: '\u0391' '\u0392' '\u0393' '\uD7ED'", h3.asText().trim() );
147 
148         a = (HtmlAnchor) elementIterator.next();
149 
150         p = (HtmlParagraph) elementIterator.next();
151         assertNotNull( p );
152         assertEquals( "'\u0391' '\u0392' '\u0393' '\uD7ED\uD7ED' '\u0159\u0159' '\u0159'", p.asText().trim() );
153 
154         div = (HtmlDivision) elementIterator.next();
155 
156         h3 = (HtmlHeading3) elementIterator.next();
157         assertNotNull( h3 );
158         assertEquals( "DTD Entities: ' ' '\u00A1' '\u00A2'", h3.asText().trim() );
159 
160         a = (HtmlAnchor) elementIterator.next();
161 
162         p = (HtmlParagraph) elementIterator.next();
163         assertNotNull( p );
164         assertEquals( "' ' '\u00A1' '\u00A2'", p.asText().trim() );
165 
166         div = (HtmlDivision) elementIterator.next();
167         assertNotNull( div );
168         assertEquals( "section", div.getAttribute( "class" ) );
169 
170         h4 = (HtmlHeading4) elementIterator.next();
171         assertNotNull( h4 );
172         assertEquals( "CDATA", h4.asText().trim() );
173 
174         a = (HtmlAnchor) elementIterator.next();
175         assertNotNull( a );
176         assertEquals( "CDATA", a.getAttribute( "name" ) );
177 
178         div = (HtmlDivision) elementIterator.next();
179         assertNotNull( div );
180         assertEquals( "source", div.getAttribute( "class" ) );
181 
182         HtmlPreformattedText pre = (HtmlPreformattedText) elementIterator.next();
183         assertNotNull( pre );
184         assertEquals( "<project xmlns:ant=\"jelly:ant\">", pre.asText().trim() );
185 
186         p = (HtmlParagraph) elementIterator.next();
187         assertNotNull( p );
188         assertEquals( "'&nbsp;' '&iexcl;'", p.asText().trim() );
189 
190         assertFalse( elementIterator.hasNext() );
191     }
192 }