1
2
3
4
5
6
7
8
9
10
11
12
13
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
32
33
34
35
36 public class NamedAttrNodeMapImplTest {
37
38
39
40
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
50 }
51
52 try {
53 new NamedAttrNodeMapImpl(null, false);
54 fail("IllegalArgumentException expected.");
55 }
56 catch (final IllegalArgumentException e) {
57
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
72
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
98
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
124
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
158
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 }