View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.doxia.siterenderer;
20  
21  import java.util.Iterator;
22  
23  import org.htmlunit.html.HtmlAnchor;
24  import org.htmlunit.html.HtmlBold;
25  import org.htmlunit.html.HtmlCode;
26  import org.htmlunit.html.HtmlElement;
27  import org.htmlunit.html.HtmlHeading1;
28  import org.htmlunit.html.HtmlHeading2;
29  import org.htmlunit.html.HtmlItalic;
30  import org.htmlunit.html.HtmlMain;
31  import org.htmlunit.html.HtmlPage;
32  import org.htmlunit.html.HtmlParagraph;
33  import org.htmlunit.html.HtmlSection;
34  
35  import static org.junit.jupiter.api.Assertions.assertEquals;
36  import static org.junit.jupiter.api.Assertions.assertNotNull;
37  
38  /**
39   * Verifies apt transformations.
40   *
41   * @author ltheussl
42   */
43  public class AptVerifier extends AbstractVerifier {
44      /** {@inheritDoc} */
45      public void verify(String file) throws Exception {
46          HtmlPage page = htmlPage(file);
47          assertNotNull(page);
48  
49          HtmlElement element = page.getHtmlElementById("contentBox");
50          assertNotNull(element);
51          HtmlMain main = (HtmlMain) element;
52          assertNotNull(main);
53  
54          Iterator<HtmlElement> elementIterator = main.getHtmlElementDescendants().iterator();
55  
56          // ----------------------------------------------------------------------
57          //
58          // ----------------------------------------------------------------------
59  
60          HtmlSection section = (HtmlSection) elementIterator.next();
61  
62          HtmlAnchor anchor = (HtmlAnchor) elementIterator.next();
63          assertNotNull(anchor);
64          assertEquals("Links", anchor.getAttribute("id"));
65          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
66          assertNotNull(h1);
67          assertEquals("Links", h1.asNormalizedText().trim());
68  
69          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
70          assertNotNull(p);
71  
72          // Expected log: [APT Parser] Ambiguous link: 'cdc.html'. If this is a local link, prepend "./"!
73          HtmlAnchor a = (HtmlAnchor) elementIterator.next();
74          assertEquals("Anchor", a.getAttribute("id"));
75          a = (HtmlAnchor) elementIterator.next();
76          assertEquals("cdc.html", a.getAttribute("id"));
77  
78          a = (HtmlAnchor) elementIterator.next();
79          assertEquals("#Anchor", a.getAttribute("href"));
80          a = (HtmlAnchor) elementIterator.next();
81          assertEquals("#Anchor", a.getAttribute("href"));
82  
83          a = (HtmlAnchor) elementIterator.next();
84          assertEquals("Anchor_with_space", a.getAttribute("id"));
85          a = (HtmlAnchor) elementIterator.next();
86          assertEquals("#Anchor_with_space", a.getAttribute("href"));
87  
88          a = (HtmlAnchor) elementIterator.next();
89          assertEquals("http://maven.apache.org/", a.getAttribute("href"));
90          assertEquals("externalLink", a.getAttribute("class"));
91          a = (HtmlAnchor) elementIterator.next();
92          assertEquals("http://maven.apache.org/", a.getAttribute("href"));
93          assertEquals("externalLink", a.getAttribute("class"));
94  
95          // Expected log: [APT Parser] Ambiguous link: 'cdc.html'. If this is a local link, prepend "./"!
96          a = (HtmlAnchor) elementIterator.next();
97          assertEquals("./cdc.html", a.getAttribute("href"));
98          a = (HtmlAnchor) elementIterator.next();
99          assertEquals("#cdc.html", a.getAttribute("href"));
100 
101         a = (HtmlAnchor) elementIterator.next();
102         assertEquals("/index.html", a.getAttribute("href"));
103 
104         section = (HtmlSection) elementIterator.next();
105         assertNotNull(section);
106 
107         anchor = (HtmlAnchor) elementIterator.next();
108         assertNotNull(anchor);
109         assertEquals("Section_formatting.3A_italic_bold_mono", anchor.getAttribute("id"));
110         h1 = (HtmlHeading1) elementIterator.next();
111         assertNotNull(h1);
112         // Note: htmlunit strips the white space, actual result is ok
113         assertEquals(
114                 "Section formatting: italic bold mono", h1.asNormalizedText().trim());
115 
116         HtmlItalic italic = (HtmlItalic) elementIterator.next();
117         assertEquals("i", italic.getTagName());
118         assertEquals("italic", italic.asNormalizedText().trim());
119 
120         HtmlBold bold = (HtmlBold) elementIterator.next();
121         assertEquals("b", bold.getTagName());
122         assertEquals("bold", bold.asNormalizedText().trim());
123 
124         HtmlCode code = (HtmlCode) elementIterator.next();
125         assertEquals("code", code.getTagName());
126         assertEquals("mono", code.asNormalizedText().trim());
127 
128         section = (HtmlSection) elementIterator.next();
129         assertNotNull(section);
130 
131         anchor = (HtmlAnchor) elementIterator.next();
132         assertNotNull(anchor);
133         assertEquals("SubSection_formatting.3A_italic_bold_mono", anchor.getAttribute("id"));
134         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
135         assertNotNull(h2);
136         // Note: htmlunit strips the white space, actual result is ok
137         assertEquals(
138                 "SubSection formatting: italic bold mono", h2.asNormalizedText().trim());
139 
140         italic = (HtmlItalic) elementIterator.next();
141         assertEquals("i", italic.getTagName());
142         assertEquals("italic", italic.asNormalizedText().trim());
143 
144         bold = (HtmlBold) elementIterator.next();
145         assertEquals("b", bold.getTagName());
146         assertEquals("bold", bold.asNormalizedText().trim());
147 
148         code = (HtmlCode) elementIterator.next();
149         assertEquals("code", code.getTagName());
150         assertEquals("mono", code.asNormalizedText().trim());
151 
152         p = (HtmlParagraph) elementIterator.next();
153         assertNotNull(p);
154 
155         italic = (HtmlItalic) elementIterator.next();
156         assertEquals("i", italic.getTagName());
157         assertEquals("italic", italic.asNormalizedText().trim());
158 
159         bold = (HtmlBold) elementIterator.next();
160         assertEquals("b", bold.getTagName());
161         assertEquals("bold", bold.asNormalizedText().trim());
162 
163         code = (HtmlCode) elementIterator.next();
164         assertEquals("code", code.getTagName());
165         assertEquals("mono", code.asNormalizedText().trim());
166 
167         section = (HtmlSection) elementIterator.next();
168         assertNotNull(section);
169 
170         h1 = (HtmlHeading1) elementIterator.next();
171         assertNotNull(h1);
172         assertEquals(
173                 "No Default Anchor in Section Title with Explicit Anchor",
174                 h1.asNormalizedText().trim());
175         a = (HtmlAnchor) elementIterator.next();
176         assertEquals("No_Default_Anchor_in_Section_Title_with_Explicit_Anchor", a.getAttribute("id"));
177 
178         section = (HtmlSection) elementIterator.next();
179         assertNotNull(section);
180     }
181 }