View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import static org.apache.http.client.utils.DateUtils.formatDate;
18  
19  import java.util.ArrayList;
20  import java.util.Date;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.ListIterator;
24  
25  import org.apache.commons.lang3.time.DateUtils;
26  import org.htmlunit.html.DomElement;
27  import org.htmlunit.html.DomNode;
28  import org.htmlunit.html.DomNodeList;
29  import org.htmlunit.html.Html;
30  import org.htmlunit.html.HtmlDivision;
31  import org.htmlunit.html.HtmlPage;
32  import org.htmlunit.util.MimeType;
33  import org.htmlunit.util.NameValuePair;
34  import org.junit.jupiter.api.Test;
35  
36  /**
37   * Tests for {@link SgmlPage}.
38   *
39   * @author Ahmed Ashour
40   */
41  public final class SgmlPageTest extends WebServerTestCase {
42  
43      /**
44       * Do not cleanup WebResponse between pages if it is cached.
45       * @throws Exception if the test fails
46       */
47      @Test
48      public void onlyCacheToCleanUpWebResponse() throws Exception {
49          try (WebClient webClient = getWebClient()) {
50              webClient.getOptions().setMaxInMemory(3);
51  
52              final List<NameValuePair> headers = new ArrayList<>();
53              headers.add(new NameValuePair("Expires", formatDate(DateUtils.addHours(new Date(), 1))));
54              getMockWebConnection().setDefaultResponse("something", 200, "Ok", MimeType.TEXT_HTML, headers);
55              startWebServer(getMockWebConnection());
56  
57              webClient.getPage(URL_FIRST);
58              webClient.getPage(URL_FIRST);
59          }
60      }
61  
62      /**
63       * @throws Exception if the test fails
64       */
65      @Test
66      public void getElementsByTagNameAsterisk() throws Exception {
67          final String html = DOCTYPE_HTML
68              + "<html><head><title>First</title></head>\n"
69              + "<body>\n"
70              + "<form><input type='button' name='button1' value='pushme'></form>\n"
71              + "<div>a</div> <div>b</div> <div>c</div>\n"
72              + "</body></html>";
73  
74          final HtmlPage page = loadPage(html);
75  
76          final DomNodeList<DomElement> elements = page.getElementsByTagName("*");
77  
78          assertEquals(9, elements.getLength());
79          validateDomNodeList(elements);
80  
81          final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
82          page.getBody().appendChild(newDiv);
83          assertEquals(10, elements.getLength());
84          validateDomNodeList(elements);
85      }
86  
87      private <E extends DomNode> void validateDomNodeList(final DomNodeList<E> nodes) {
88          assertEquals(nodes.getLength(), nodes.size());
89          final Iterator<E> nodesIterator = nodes.iterator();
90          for (int i = 0; i < nodes.getLength(); i++) {
91              assertEquals(nodes.item(i), nodes.get(i));
92              assertEquals(nodes.item(i), nodesIterator.next());
93              assertEquals(i, nodes.indexOf(nodes.item(i)));
94          }
95          assertEquals(false, nodesIterator.hasNext());
96          final ListIterator<E> nodesListIterator = nodes.listIterator();
97          assertEquals(nodes.item(0), nodesListIterator.next());
98          assertEquals(nodes.item(1), nodesListIterator.next());
99          assertEquals(nodes.item(1), nodesListIterator.previous());
100     }
101 
102     /**
103      * @throws Exception if the test fails
104      */
105     @Test
106     public void getElementsByTagNameNSAsterisk() throws Exception {
107         final String html = DOCTYPE_HTML
108             + "<html><head><title>First</title></head>\n"
109             + "<body>\n"
110             + "<form><input type='button' name='button1' value='pushme'></form>\n"
111             + "<div>a</div> <div>b</div> <div>c</div>\n"
112             + "</body></html>";
113 
114         final HtmlPage page = loadPage(html);
115 
116         final DomNodeList<DomElement> elements = page.getElementsByTagNameNS(Html.XHTML_NAMESPACE, "*");
117 
118         assertEquals(9, elements.getLength());
119         validateDomNodeList(elements);
120 
121         final HtmlDivision newDiv = new HtmlDivision(HtmlDivision.TAG_NAME, page, null);
122         page.getBody().appendChild(newDiv);
123         assertEquals(10, elements.getLength());
124         validateDomNodeList(elements);
125     }
126 
127 }