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 static org.junit.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertNotNull;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import org.junit.Test;
29  
30  /**
31   * Tests for {@link NamedAttrNodeMapImpl}.
32   *
33   * @author Ronald Brill
34   * @author Frank Danek
35   */
36  public class NamedAttrNodeMapImplTest {
37  
38      /**
39       * Test constructors.
40       * @throws Exception if an error occurs
41       */
42      @Test
43      public void construction() throws Exception {
44          try {
45              new NamedAttrNodeMapImpl(null, true);
46              fail("IllegalArgumentException expected.");
47          }
48          catch (final IllegalArgumentException e) {
49              // expected
50          }
51  
52          try {
53              new NamedAttrNodeMapImpl(null, false);
54              fail("IllegalArgumentException expected.");
55          }
56          catch (final IllegalArgumentException e) {
57              // expected
58          }
59  
60          final DomElement dom = new HtmlBreak("", null, null);
61  
62          NamedAttrNodeMapImpl map = new NamedAttrNodeMapImpl(dom, true);
63          assertTrue(map.isEmpty());
64  
65          final Map<String, DomAttr> attribs = new HashMap<>();
66          map = new NamedAttrNodeMapImpl(dom, true, attribs);
67          assertTrue(map.isEmpty());
68      }
69  
70      /**
71       * Test the case sensitive version.
72       * @throws Exception if an error occurs
73       */
74      @Test
75      public void caseSensitive() throws Exception {
76          final DomElement dom = new HtmlBreak("", null, null);
77  
78          NamedAttrNodeMapImpl map = new NamedAttrNodeMapImpl(dom, true);
79          assertTrue(map.isEmpty());
80          map.put("Key", new DomAttr(null, "", "", null, false));
81          assertTrue(map.containsKey("Key"));
82          assertFalse(map.containsKey("key"));
83          assertNotNull(map.get("Key"));
84          assertNull(map.get("key"));
85  
86          final Map<String, DomAttr> attribs = new HashMap<>();
87          attribs.put("Key", new DomAttr(null, "", "", null, false));
88  
89          map = new NamedAttrNodeMapImpl(dom, true, attribs);
90          assertTrue(map.containsKey("Key"));
91          assertFalse(map.containsKey("key"));
92          assertNotNull(map.get("Key"));
93          assertNull(map.get("key"));
94      }
95  
96      /**
97       * Test the case insensitive version.
98       * @throws Exception if an error occurs
99       */
100     @Test
101     public void caseInSensitive() throws Exception {
102         final DomElement dom = new HtmlBreak("", null, null);
103 
104         NamedAttrNodeMapImpl map = new NamedAttrNodeMapImpl(dom, false);
105         assertTrue(map.isEmpty());
106         map.put("Key", new DomAttr(null, "", "", null, false));
107         assertTrue(map.containsKey("Key"));
108         assertTrue(map.containsKey("key"));
109         assertNotNull(map.get("Key"));
110         assertNotNull(map.get("key"));
111 
112         final Map<String, DomAttr> attribs = new HashMap<>();
113         attribs.put("Key", new DomAttr(null, "", "", null, false));
114 
115         map = new NamedAttrNodeMapImpl(dom, false, attribs);
116         assertTrue(map.containsKey("Key"));
117         assertTrue(map.containsKey("key"));
118         assertNotNull(map.get("Key"));
119         assertNotNull(map.get("key"));
120     }
121 
122     /**
123      * Test the case insensitive version.
124      * @throws Exception if an error occurs
125      */
126     @Test
127     public void preseveOrderCaseInsensitive() throws Exception {
128         final DomElement dom = new HtmlBreak("", null, null);
129 
130         final NamedAttrNodeMapImpl map = new NamedAttrNodeMapImpl(dom, false);
131         assertTrue(map.isEmpty());
132         map.put("Key1", new DomAttr(null, "", "attr1", null, false));
133         map.put("Key2", new DomAttr(null, "", "attr2", null, false));
134 
135         assertTrue(map.containsKey("Key1"));
136         assertTrue(map.containsKey("Key2"));
137 
138         assertEquals("attr1", map.item(0).getNodeName());
139         assertEquals("attr2", map.item(1).getNodeName());
140 
141         final Iterator<String> keys = map.keySet().iterator();
142         assertEquals("key1", keys.next());
143         assertEquals("key2", keys.next());
144 
145         final Iterator<Map.Entry<String, DomAttr>> attrs = map.entrySet().iterator();
146 
147         Map.Entry<String, DomAttr> entry = attrs.next();
148         assertEquals("key1", entry.getKey());
149         assertEquals("attr1", entry.getValue().getNodeName());
150 
151         entry = attrs.next();
152         assertEquals("key2", entry.getKey());
153         assertEquals("attr2", entry.getValue().getNodeName());
154     }
155 
156     /**
157      * Test the case insensitive version.
158      * @throws Exception if an error occurs
159      */
160     @Test
161     public void preseveOrderCaseSensitive() throws Exception {
162         final DomElement dom = new HtmlBreak("", null, null);
163 
164         final NamedAttrNodeMapImpl map = new NamedAttrNodeMapImpl(dom, true);
165         assertTrue(map.isEmpty());
166         map.put("Key1", new DomAttr(null, "", "attr1", null, false));
167         map.put("Key2", new DomAttr(null, "", "attr2", null, false));
168 
169         assertTrue(map.containsKey("Key1"));
170         assertTrue(map.containsKey("Key2"));
171 
172         assertEquals("attr1", map.item(0).getNodeName());
173         assertEquals("attr2", map.item(1).getNodeName());
174 
175         final Iterator<String> keys = map.keySet().iterator();
176         assertEquals("Key1", keys.next());
177         assertEquals("Key2", keys.next());
178 
179         final Iterator<Map.Entry<String, DomAttr>> attrs = map.entrySet().iterator();
180 
181         Map.Entry<String, DomAttr> entry = attrs.next();
182         assertEquals("Key1", entry.getKey());
183         assertEquals("attr1", entry.getValue().getNodeName());
184 
185         entry = attrs.next();
186         assertEquals("Key2", entry.getKey());
187         assertEquals("attr2", entry.getValue().getNodeName());
188     }
189 }