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 com.gargoylesoftware.htmlunit.html.HtmlAnchor;
24  import com.gargoylesoftware.htmlunit.html.HtmlBold;
25  import com.gargoylesoftware.htmlunit.html.HtmlCode;
26  import com.gargoylesoftware.htmlunit.html.HtmlElement;
27  import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
28  import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
29  import com.gargoylesoftware.htmlunit.html.HtmlItalic;
30  import com.gargoylesoftware.htmlunit.html.HtmlMain;
31  import com.gargoylesoftware.htmlunit.html.HtmlPage;
32  import com.gargoylesoftware.htmlunit.html.HtmlParagraph;
33  import com.gargoylesoftware.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          HtmlHeading1 h1 = (HtmlHeading1) elementIterator.next();
63          assertNotNull(h1);
64          assertEquals("Links", h1.asNormalizedText().trim());
65  
66          HtmlParagraph p = (HtmlParagraph) elementIterator.next();
67          assertNotNull(p);
68  
69          // Expected log: [APT Parser] Ambiguous link: 'cdc.html'. If this is a local link, prepend "./"!
70          HtmlAnchor a = (HtmlAnchor) elementIterator.next();
71          assertEquals("Anchor", a.getAttribute("id"));
72          a = (HtmlAnchor) elementIterator.next();
73          assertEquals("cdc.html", a.getAttribute("id"));
74  
75          a = (HtmlAnchor) elementIterator.next();
76          assertEquals("#Anchor", a.getAttribute("href"));
77          a = (HtmlAnchor) elementIterator.next();
78          assertEquals("#Anchor", a.getAttribute("href"));
79  
80          a = (HtmlAnchor) elementIterator.next();
81          assertEquals("Anchor_with_space", a.getAttribute("id"));
82          a = (HtmlAnchor) elementIterator.next();
83          assertEquals("#Anchor_with_space", a.getAttribute("href"));
84  
85          a = (HtmlAnchor) elementIterator.next();
86          assertEquals("http://maven.apache.org/", a.getAttribute("href"));
87          assertEquals("externalLink", a.getAttribute("class"));
88          a = (HtmlAnchor) elementIterator.next();
89          assertEquals("http://maven.apache.org/", a.getAttribute("href"));
90          assertEquals("externalLink", a.getAttribute("class"));
91  
92          // Expected log: [APT Parser] Ambiguous link: 'cdc.html'. If this is a local link, prepend "./"!
93          a = (HtmlAnchor) elementIterator.next();
94          assertEquals("./cdc.html", a.getAttribute("href"));
95          a = (HtmlAnchor) elementIterator.next();
96          assertEquals("#cdc.html", a.getAttribute("href"));
97  
98          a = (HtmlAnchor) elementIterator.next();
99          assertEquals("/index.html", a.getAttribute("href"));
100 
101         section = (HtmlSection) elementIterator.next();
102         assertNotNull(section);
103 
104         h1 = (HtmlHeading1) elementIterator.next();
105         assertNotNull(h1);
106         // Note: htmlunit strips the white space, actual result is ok
107         assertEquals(
108                 "Section formatting: italic bold mono", h1.asNormalizedText().trim());
109 
110         HtmlItalic italic = (HtmlItalic) elementIterator.next();
111         assertEquals("i", italic.getTagName());
112         assertEquals("italic", italic.asNormalizedText().trim());
113 
114         HtmlBold bold = (HtmlBold) elementIterator.next();
115         assertEquals("b", bold.getTagName());
116         assertEquals("bold", bold.asNormalizedText().trim());
117 
118         HtmlCode code = (HtmlCode) elementIterator.next();
119         assertEquals("code", code.getTagName());
120         assertEquals("mono", code.asNormalizedText().trim());
121 
122         section = (HtmlSection) elementIterator.next();
123         assertNotNull(section);
124 
125         HtmlHeading2 h2 = (HtmlHeading2) elementIterator.next();
126         assertNotNull(h2);
127         // Note: htmlunit strips the white space, actual result is ok
128         assertEquals(
129                 "SubSection formatting: italic bold mono", h2.asNormalizedText().trim());
130 
131         italic = (HtmlItalic) elementIterator.next();
132         assertEquals("i", italic.getTagName());
133         assertEquals("italic", italic.asNormalizedText().trim());
134 
135         bold = (HtmlBold) elementIterator.next();
136         assertEquals("b", bold.getTagName());
137         assertEquals("bold", bold.asNormalizedText().trim());
138 
139         code = (HtmlCode) elementIterator.next();
140         assertEquals("code", code.getTagName());
141         assertEquals("mono", code.asNormalizedText().trim());
142 
143         p = (HtmlParagraph) elementIterator.next();
144         assertNotNull(p);
145 
146         italic = (HtmlItalic) elementIterator.next();
147         assertEquals("i", italic.getTagName());
148         assertEquals("italic", italic.asNormalizedText().trim());
149 
150         bold = (HtmlBold) elementIterator.next();
151         assertEquals("b", bold.getTagName());
152         assertEquals("bold", bold.asNormalizedText().trim());
153 
154         code = (HtmlCode) elementIterator.next();
155         assertEquals("code", code.getTagName());
156         assertEquals("mono", code.asNormalizedText().trim());
157 
158         section = (HtmlSection) elementIterator.next();
159         assertNotNull(section);
160 
161         h1 = (HtmlHeading1) elementIterator.next();
162         assertNotNull(h1);
163         assertEquals(
164                 "No Default Anchor in Section Title with Explicit Anchor",
165                 h1.asNormalizedText().trim());
166         a = (HtmlAnchor) elementIterator.next();
167         assertEquals("No_Default_Anchor_in_Section_Title_with_Explicit_Anchor", a.getAttribute("id"));
168 
169         section = (HtmlSection) elementIterator.next();
170         assertNotNull(section);
171     }
172 }