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   * @version $Id: EntitiesVerifier.java 1737482 2016-04-02 09:56:25Z hboutemy $
40   */
41  public class EntitiesVerifier
42      extends AbstractVerifier
43  {
44      /** {@inheritDoc} */
45      public void verify( String file )
46              throws Exception
47      {
48          HtmlPage page = htmlPage( file );
49          assertNotNull( page );
50  
51          HtmlMeta author = (HtmlMeta) page.getElementsByName( "author" ).get( 0 );
52          assertNotNull( author );
53          assertTrue( author.toString().indexOf( "Ligature &#198;" ) > 0 );
54          assertEquals( "Ligature \u00C6", author.getContentAttribute() );
55  
56          author = (HtmlMeta) page.getElementsByName( "author" ).get( 1 );
57          assertNotNull( author );
58          assertTrue( author.toString().indexOf( "Ampersand &amp;" ) > 0 );
59          assertEquals( "Ampersand &", author.getContentAttribute() );
60  
61          author = (HtmlMeta) page.getElementsByName( "author" ).get( 2 );
62          assertNotNull( author );
63          assertTrue( author.toString().indexOf( "Less than &lt;" ) > 0 );
64          assertEquals( "Less than <", author.getContentAttribute() );
65  
66          author = (HtmlMeta) page.getElementsByName( "author" ).get( 3 );
67          assertNotNull( author );
68          assertTrue( author.toString().indexOf( "Greater than &gt;" ) > 0 );
69          assertEquals( "Greater than >", author.getContentAttribute() );
70  
71          author = (HtmlMeta) page.getElementsByName( "author" ).get( 4 );
72          assertNotNull( author );
73          assertTrue( author.getContentAttribute().equals( "Apostrophe '" ) );
74          assertEquals( "Apostrophe '", author.getContentAttribute() );
75  
76          author = (HtmlMeta) page.getElementsByName( "author" ).get( 5 );
77          assertNotNull( author );
78          assertTrue( author.toString().indexOf( "Quote &quot;" ) > 0 );
79          assertEquals( "Quote \"", author.getContentAttribute() );
80  
81          author = (HtmlMeta) page.getElementsByName( "author" ).get( 6 );
82          assertNotNull( author );
83          assertTrue( author.toString().indexOf( "test@email.com" ) > 0 );
84          assertEquals( "test@email.com", author.getContentAttribute() );
85  
86          author = (HtmlMeta) page.getElementsByName( "author" ).get( 7 );
87          assertNotNull( author );
88          assertTrue( author.toString().indexOf( "test&#169;email.com" ) > 0 );
89          assertEquals( "test\u00A9email.com", author.getContentAttribute() );
90  
91          HtmlElement element = page.getHtmlElementById( "contentBox" );
92          assertNotNull( element );
93          HtmlDivision division = (HtmlDivision) element;
94          assertNotNull( division );
95  
96          Iterator<HtmlElement> elementIterator = division.getHtmlElementDescendants().iterator();
97  
98          // ----------------------------------------------------------------------
99          //
100         // ----------------------------------------------------------------------
101 
102         HtmlDivision div = (HtmlDivision) elementIterator.next();
103         assertNotNull( div );
104         assertEquals( "section", div.getAttribute( "class" ) );
105 
106         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
107         assertNotNull( h2 );
108         assertEquals( h2.asText().trim(), "section name with entities: '&' '\u0391' ' ' '\uD7ED'" );
109 
110         HtmlAnchor a = (HtmlAnchor) elementIterator.next();
111         assertNotNull( a );
112         assertEquals( "section_name_with_entities:____", 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 }