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.html;
16  
17  import java.util.ArrayList;
18  import java.util.Collections;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.htmlunit.SimpleWebTestCase;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * Tests for {@link DomAttr}.
27   *
28   * @author Denis N. Antonioli
29   * @author Ahmed Ashour
30   * @author David K. Taylor
31   * @author Frank Danek
32   * @author Ronald Brill
33   */
34  public class HtmlAttrTest extends SimpleWebTestCase {
35  
36      /** Test object. */
37      private final DomAttr htmlAttr_ = new DomAttr(null, null, ENTRY_KEY, ENTRY_VALUE, false);
38  
39      /** Single test key value. */
40      private static final String ENTRY_KEY = "key";
41  
42      /** Single test attribute value. */
43      private static final String ENTRY_VALUE = "value";
44  
45      /** A single dummy HtmlElement. Necessary, because HtmlAttr's constructor calls the method getPage(). */
46      static final HtmlElement HTML_ELEMENT;
47  
48      static {
49          final Map<String, DomAttr> emptyMap = Collections.emptyMap();
50          HTML_ELEMENT = new HtmlElement("dummy", null, emptyMap) {
51              @Override
52              public HtmlPage getPage() {
53                  return null;
54              }
55          };
56      }
57  
58      /**
59       * Constructor.
60       */
61      public HtmlAttrTest() {
62          htmlAttr_.setParentNode(HTML_ELEMENT);
63      }
64  
65      /**
66       * Tests {@link DomAttr#getName()}.
67       */
68      @Test
69      public void getName() {
70          assertEquals(ENTRY_KEY, htmlAttr_.getName());
71      }
72  
73      /**
74       * Tests {@link DomAttr#getNodeName()}.
75       */
76      @Test
77      public void getNodeName() {
78          assertEquals(ENTRY_KEY, htmlAttr_.getNodeName());
79      }
80  
81      /**
82       * Tests {@link DomAttr#getNodeType()}.
83       */
84      @Test
85      public void getNodeType() {
86          assertEquals(org.w3c.dom.Node.ATTRIBUTE_NODE, htmlAttr_.getNodeType());
87      }
88  
89      /**
90       * Tests {@link DomAttr#getNodeValue()}.
91       */
92      @Test
93      public void getNodeValue() {
94          assertEquals(ENTRY_VALUE, htmlAttr_.getNodeValue());
95      }
96  
97      /**
98       * Tests {@link DomAttr#getValue()}.
99       */
100     @Test
101     public void getValue() {
102         assertEquals(ENTRY_VALUE, htmlAttr_.getValue());
103     }
104 
105     /**
106      * Tests {@link DomAttr#setValue(String)}.
107      */
108     @Test
109     public void setValue() {
110         htmlAttr_.setValue("foo");
111         assertEquals("foo", htmlAttr_.getValue());
112     }
113 
114     /**
115      * Tests {@link DomAttr#getParentNode()}.
116      */
117     @Test
118     public void getParent() {
119         assertSame(HTML_ELEMENT, htmlAttr_.getParentNode());
120     }
121 
122     /**
123      * Test nodeType of {@link DomAttr}.
124      *
125      * @throws Exception if the test fails
126      */
127     @Test
128     public void nodeType() throws Exception {
129         final String content = DOCTYPE_HTML
130             + "<html><head><title>foo</title><script>\n"
131             + "  function test() {\n"
132             + "    var attr = document.createAttribute('myAttrib');\n"
133             + "    alert(attr.nodeType);\n"
134             + "  }\n"
135             + "</script></head><body onload='test()'>\n"
136             + "</body></html>";
137         final String[] expectedAlerts = {"2"};
138         final List<String> collectedAlerts = new ArrayList<>();
139         loadPage(content, collectedAlerts);
140         assertEquals(expectedAlerts, collectedAlerts);
141     }
142 }