1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.tiles;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.LinkedHashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.easymock.EasyMock;
32
33 import junit.framework.TestCase;
34
35 /***
36 * Tests <code>BasicAttributeContext</code>.
37 *
38 * @version $Rev: 788344 $ $Date: 2009-06-25 14:47:40 +0200 (gio, 25 giu 2009) $
39 */
40 public class BasicAttributeContextTest extends TestCase {
41
42 /***
43 * Tests {@link BasicAttributeContext#BasicAttributeContext()}.
44 */
45 public void testBasicAttributeContext() {
46 AttributeContext context = new BasicAttributeContext();
47 assertNull("There are some spurious attributes", context
48 .getLocalAttributeNames());
49 assertNull("There are some spurious attributes", context
50 .getCascadedAttributeNames());
51 }
52
53 /***
54 * Tests {@link BasicAttributeContext#BasicAttributeContext(Map)}.
55 */
56 public void testBasicAttributeContextMapOfStringAttribute() {
57 Map<String, Attribute> name2attrib = new HashMap<String, Attribute>();
58 Attribute attribute = new Attribute("Value 1");
59 name2attrib.put("name1", attribute);
60 attribute = new Attribute("Value 2");
61 name2attrib.put("name2", attribute);
62 AttributeContext context = new BasicAttributeContext(name2attrib);
63 attribute = context.getAttribute("name1");
64 assertNotNull("Attribute name1 not found", attribute);
65 assertEquals("Attribute name1 has not been set correctly", "Value 1",
66 attribute.getValue());
67 attribute = context.getAttribute("name2");
68 assertNotNull("Attribute name2 not found", attribute);
69 assertEquals("Attribute name2 has not been set correctly", "Value 2",
70 attribute.getValue());
71 }
72
73 /***
74 * Tests
75 * {@link BasicAttributeContext#BasicAttributeContext(AttributeContext)}.
76 */
77 public void testBasicAttributeContextAttributeContext() {
78 Set<String> localAttributes = new LinkedHashSet<String>();
79 Set<String> cascadedAttributes = new LinkedHashSet<String>();
80 localAttributes.add("local1");
81 localAttributes.add("local2");
82 cascadedAttributes.add("cascaded1");
83 cascadedAttributes.add("cascaded2");
84 AttributeContext toCopy = EasyMock.createMock(AttributeContext.class);
85 EasyMock.expect(toCopy.getLocalAttributeNames()).andReturn(
86 localAttributes);
87 EasyMock.expect(toCopy.getLocalAttribute("local1")).andReturn(
88 new Attribute("value1")).anyTimes();
89 EasyMock.expect(toCopy.getLocalAttribute("local2")).andReturn(
90 new Attribute("value2")).anyTimes();
91 EasyMock.expect(toCopy.getCascadedAttributeNames()).andReturn(
92 cascadedAttributes);
93 EasyMock.expect(toCopy.getCascadedAttribute("cascaded1")).andReturn(
94 new Attribute("value3")).anyTimes();
95 EasyMock.expect(toCopy.getCascadedAttribute("cascaded2")).andReturn(
96 new Attribute("value4")).anyTimes();
97 Attribute templateAttribute = new Attribute("/template.jsp",
98 "expression", "role1,role2", "template");
99 EasyMock.expect(toCopy.getTemplateAttribute()).andReturn(templateAttribute);
100 Set<String> roles = new HashSet<String>();
101 roles.add("role1");
102 roles.add("role2");
103 EasyMock.expect(toCopy.getPreparer()).andReturn("my.preparer.Preparer");
104 EasyMock.replay(toCopy);
105 BasicAttributeContext context = new BasicAttributeContext(toCopy);
106 assertEquals("The template has not been set correctly",
107 "/template.jsp", context.getTemplateAttribute().getValue());
108 assertEquals("The template expression has not been set correctly",
109 "expression", context.getTemplateAttribute().getExpression());
110 assertEquals("The roles are not the same", roles, context
111 .getTemplateAttribute().getRoles());
112 assertEquals("The preparer has not been set correctly",
113 "my.preparer.Preparer", context.getPreparer());
114 Attribute attribute = context.getLocalAttribute("local1");
115 assertNotNull("Attribute local1 not found", attribute);
116 assertEquals("Attribute local1 has not been set correctly", "value1",
117 attribute.getValue());
118 attribute = context.getLocalAttribute("local2");
119 assertNotNull("Attribute local2 not found", attribute);
120 assertEquals("Attribute local2 has not been set correctly", "value2",
121 attribute.getValue());
122 attribute = context.getCascadedAttribute("cascaded1");
123 assertNotNull("Attribute cascaded1 not found", attribute);
124 assertEquals("Attribute cascaded1 has not been set correctly",
125 "value3", attribute.getValue());
126 attribute = context.getCascadedAttribute("cascaded2");
127 assertNotNull("Attribute cascaded2 not found", attribute);
128 assertEquals("Attribute cascaded2 has not been set correctly",
129 "value4", attribute.getValue());
130 }
131
132 /***
133 * Tests
134 * {@link BasicAttributeContext#BasicAttributeContext(BasicAttributeContext)}.
135 */
136 public void testBasicAttributeContextBasicAttributeContext() {
137 AttributeContext toCopy = new BasicAttributeContext();
138 toCopy.putAttribute("name1", new Attribute("value1"), false);
139 toCopy.putAttribute("name2", new Attribute("value2"), true);
140 Attribute templateAttribute = Attribute
141 .createTemplateAttribute("/template.jsp");
142 Set<String> roles = new HashSet<String>();
143 roles.add("role1");
144 roles.add("role2");
145 templateAttribute.setRoles(roles);
146 toCopy.setTemplateAttribute(templateAttribute);
147 toCopy.setPreparer("my.preparer.Preparer");
148 AttributeContext context = new BasicAttributeContext(toCopy);
149 assertEquals("The template has not been set correctly",
150 "/template.jsp", context.getTemplateAttribute().getValue());
151 assertEquals("The roles are not the same", roles, context
152 .getTemplateAttribute().getRoles());
153 assertEquals("The preparer has not been set correctly",
154 "my.preparer.Preparer", context.getPreparer());
155 Attribute attribute = context.getLocalAttribute("name1");
156 assertNotNull("Attribute name1 not found", attribute);
157 assertEquals("Attribute name1 has not been set correctly", "value1",
158 attribute.getValue());
159 attribute = context.getCascadedAttribute("name2");
160 assertNotNull("Attribute name2 not found", attribute);
161 assertEquals("Attribute name2 has not been set correctly", "value2",
162 attribute.getValue());
163 }
164
165 /***
166 * Tests {@link BasicAttributeContext#inheritCascadedAttributes(AttributeContext)}.
167 */
168 public void testInheritCascadedAttributes() {
169 AttributeContext toCopy = new BasicAttributeContext();
170 toCopy.putAttribute("name1", new Attribute("value1"), false);
171 toCopy.putAttribute("name2", new Attribute("value2"), true);
172 AttributeContext context = new BasicAttributeContext();
173 context.inheritCascadedAttributes(toCopy);
174 Attribute attribute = context.getLocalAttribute("name1");
175 assertNull("Attribute name1 found", attribute);
176 attribute = context.getCascadedAttribute("name2");
177 assertNotNull("Attribute name2 not found", attribute);
178 assertEquals("Attribute name2 has not been set correctly", "value2",
179 attribute.getValue());
180 }
181
182 /***
183 * Tests {@link BasicAttributeContext#inherit(BasicAttributeContext)}
184 * testing inheritance between {@link ListAttribute} instances.
185 */
186 @SuppressWarnings("unchecked")
187 public void testInheritListAttribute() {
188 AttributeContext toCopy = new BasicAttributeContext();
189 ListAttribute parentListAttribute = new ListAttribute();
190 parentListAttribute.add("first");
191 toCopy.putAttribute("list", parentListAttribute);
192 AttributeContext context = new BasicAttributeContext();
193 ListAttribute listAttribute = new ListAttribute();
194 listAttribute.setInherit(true);
195 listAttribute.add("second");
196 context.putAttribute("list", listAttribute);
197 context.inherit(toCopy);
198 ListAttribute result = (ListAttribute) context.getAttribute("list");
199 assertNotNull("The attribute must exist", result);
200 List<Object> value = (List<Object>) result.getValue();
201 assertNotNull("The list must exist", value);
202 assertEquals("The size is not correct", 2, value.size());
203 assertEquals("The first element is not correct", "first", value.get(0));
204 assertEquals("The second element is not correct", "second", value.get(1));
205
206 context = new BasicAttributeContext();
207 listAttribute = new ListAttribute();
208 listAttribute.add("second");
209 context.putAttribute("list", listAttribute);
210 context.inherit(toCopy);
211 result = (ListAttribute) context.getAttribute("list");
212 assertNotNull("The attribute must exist", result);
213 value = (List<Object>) result.getValue();
214 assertNotNull("The list must exist", value);
215 assertEquals("The size is not correct", 1, value.size());
216 assertEquals("The second element is not correct", "second", value.get(0));
217 }
218
219 /***
220 * Tests {@link BasicAttributeContext#inheritCascadedAttributes(AttributeContext)}.
221 */
222 public void testInherit() {
223 AttributeContext toCopy = new BasicAttributeContext();
224 toCopy.putAttribute("name1", new Attribute("value1"), true);
225 toCopy.putAttribute("name2", new Attribute("value2"), true);
226 toCopy.putAttribute("name3", new Attribute("value3"), false);
227 toCopy.putAttribute("name4", new Attribute("value4"), false);
228 AttributeContext context = new BasicAttributeContext();
229 toCopy.putAttribute("name1", new Attribute("newValue1"), true);
230 toCopy.putAttribute("name3", new Attribute("newValue3"), false);
231 context.inherit(toCopy);
232 Attribute attribute = context.getCascadedAttribute("name1");
233 assertNotNull("Attribute name1 not found", attribute);
234 assertEquals("Attribute name1 has not been set correctly", "newValue1",
235 attribute.getValue());
236 attribute = context.getCascadedAttribute("name2");
237 assertNotNull("Attribute name2 not found", attribute);
238 assertEquals("Attribute name2 has not been set correctly", "value2",
239 attribute.getValue());
240 attribute = context.getLocalAttribute("name3");
241 assertNotNull("Attribute name3 not found", attribute);
242 assertEquals("Attribute name3 has not been set correctly", "newValue3",
243 attribute.getValue());
244 attribute = context.getLocalAttribute("name4");
245 assertNotNull("Attribute name4 not found", attribute);
246 assertEquals("Attribute name4 has not been set correctly", "value4",
247 attribute.getValue());
248 }
249
250 /***
251 * Tests {@link BasicAttributeContext#addAll(Map)}.
252 */
253 public void testAddAll() {
254 AttributeContext context = new BasicAttributeContext();
255 Map<String, Attribute> name2attrib = new HashMap<String, Attribute>();
256 Attribute attribute = new Attribute("Value 1");
257 name2attrib.put("name1", attribute);
258 attribute = new Attribute("Value 2");
259 name2attrib.put("name2", attribute);
260 context.addAll(name2attrib);
261 attribute = context.getAttribute("name1");
262 assertNotNull("Attribute name1 not found", attribute);
263 assertEquals("Attribute name1 has not been set correctly", "Value 1",
264 attribute.getValue());
265 attribute = context.getAttribute("name2");
266 assertNotNull("Attribute name2 not found", attribute);
267 assertEquals("Attribute name2 has not been set correctly", "Value 2",
268 attribute.getValue());
269 }
270
271 /***
272 * Tests {@link BasicAttributeContext#addMissing(Map)}.
273 */
274 public void testAddMissing() {
275 Map<String, Attribute> name2attrib = new HashMap<String, Attribute>();
276 Attribute attribute = new Attribute("Value 1");
277 name2attrib.put("name1", attribute);
278 attribute = new Attribute("Value 2");
279 name2attrib.put("name2", attribute);
280 AttributeContext context = new BasicAttributeContext(name2attrib);
281 name2attrib.remove("name2");
282 name2attrib.put("name1", new Attribute("Value 1a"));
283 name2attrib.put("name3", new Attribute("Value 3"));
284 context.addMissing(name2attrib);
285 attribute = context.getAttribute("name1");
286 assertNotNull("Attribute name1 not found", attribute);
287 assertEquals("Attribute name1 has not been set correctly", "Value 1",
288 attribute.getValue());
289 attribute = context.getAttribute("name2");
290 assertNotNull("Attribute name2 not found", attribute);
291 assertEquals("Attribute name2 has not been set correctly", "Value 2",
292 attribute.getValue());
293 attribute = context.getAttribute("name3");
294 assertNotNull("Attribute name3 not found", attribute);
295 assertEquals("Attribute name3 has not been set correctly", "Value 3",
296 attribute.getValue());
297 }
298
299 /***
300 * Tests {@link BasicAttributeContext#getAttribute(String)}.
301 */
302 public void testGetAttribute() {
303 AttributeContext context = new BasicAttributeContext();
304 context.putAttribute("name1", new Attribute("value1"), false);
305 context.putAttribute("name2", new Attribute("value2"), true);
306 context.putAttribute("name3", new Attribute("value3a"), true);
307 context.putAttribute("name3", new Attribute("value3"), false);
308 Attribute attribute = context.getAttribute("name1");
309 assertNotNull("Attribute name1 not found", attribute);
310 assertEquals("Attribute name1 has not been set correctly", "value1",
311 attribute.getValue());
312 attribute = context.getAttribute("name2");
313 assertNotNull("Attribute name2 not found", attribute);
314 assertEquals("Attribute name2 has not been set correctly", "value2",
315 attribute.getValue());
316 attribute = context.getAttribute("name3");
317 assertNotNull("Attribute name3 not found", attribute);
318 assertEquals("Attribute name3 has not been set correctly", "value3",
319 attribute.getValue());
320 }
321
322 /***
323 * Tests {@link BasicAttributeContext#getLocalAttribute(String)}.
324 */
325 public void testGetLocalAttribute() {
326 AttributeContext context = new BasicAttributeContext();
327 context.putAttribute("name1", new Attribute("value1"), false);
328 context.putAttribute("name2", new Attribute("value2"), true);
329 context.putAttribute("name3", new Attribute("value3a"), true);
330 context.putAttribute("name3", new Attribute("value3"), false);
331 Attribute attribute = context.getLocalAttribute("name1");
332 assertNotNull("Attribute name1 not found", attribute);
333 assertEquals("Attribute name1 has not been set correctly", "value1",
334 attribute.getValue());
335 attribute = context.getLocalAttribute("name2");
336 assertNull("Attribute name2 found", attribute);
337 attribute = context.getLocalAttribute("name3");
338 assertNotNull("Attribute name3 not found", attribute);
339 assertEquals("Attribute name3 has not been set correctly", "value3",
340 attribute.getValue());
341 }
342
343 /***
344 * Tests {@link BasicAttributeContext#getCascadedAttribute(String)}.
345 */
346 public void testGetCascadedAttribute() {
347 AttributeContext context = new BasicAttributeContext();
348 context.putAttribute("name1", new Attribute("value1"), false);
349 context.putAttribute("name2", new Attribute("value2"), true);
350 context.putAttribute("name3", new Attribute("value3a"), true);
351 context.putAttribute("name3", new Attribute("value3"), false);
352 Attribute attribute = context.getCascadedAttribute("name1");
353 assertNull("Attribute name1 found", attribute);
354 attribute = context.getCascadedAttribute("name2");
355 assertNotNull("Attribute name2 not found", attribute);
356 assertEquals("Attribute name2 has not been set correctly", "value2",
357 attribute.getValue());
358 attribute = context.getCascadedAttribute("name3");
359 assertNotNull("Attribute name3 not found", attribute);
360 assertEquals("Attribute name3 has not been set correctly", "value3a",
361 attribute.getValue());
362 }
363
364 /***
365 * Tests {@link BasicAttributeContext#getLocalAttributeNames()}.
366 */
367 public void testGetLocalAttributeNames() {
368 AttributeContext context = new BasicAttributeContext();
369 context.putAttribute("name1", new Attribute("value1"), false);
370 context.putAttribute("name2", new Attribute("value2"), true);
371 context.putAttribute("name3", new Attribute("value3a"), true);
372 context.putAttribute("name3", new Attribute("value3"), false);
373 Set<String> names = context.getLocalAttributeNames();
374 assertTrue("Attribute name1 is not present", names.contains("name1"));
375 assertFalse("Attribute name2 is present", names.contains("name2"));
376 assertTrue("Attribute name3 is not present", names.contains("name3"));
377 }
378
379 /***
380 * Tests {@link BasicAttributeContext#getCascadedAttributeNames()}.
381 */
382 public void testGetCascadedAttributeNames() {
383 AttributeContext context = new BasicAttributeContext();
384 context.putAttribute("name1", new Attribute("value1"), false);
385 context.putAttribute("name2", new Attribute("value2"), true);
386 context.putAttribute("name3", new Attribute("value3a"), true);
387 context.putAttribute("name3", new Attribute("value3"), false);
388 Set<String> names = context.getCascadedAttributeNames();
389 assertFalse("Attribute name1 is present", names.contains("name1"));
390 assertTrue("Attribute name2 is not present", names.contains("name2"));
391 assertTrue("Attribute name3 is not present", names.contains("name3"));
392 }
393
394 /***
395 * Tests {@link BasicAttributeContext#putAttribute(String, Attribute)}.
396 */
397 public void testPutAttributeStringAttribute() {
398 AttributeContext context = new BasicAttributeContext();
399 context.putAttribute("name1", new Attribute("value1"));
400 Attribute attribute = context.getLocalAttribute("name1");
401 assertNotNull("Attribute name1 not found", attribute);
402 assertEquals("Attribute name1 has not been set correctly", "value1",
403 attribute.getValue());
404 attribute = context.getCascadedAttribute("name1");
405 assertNull("Attribute name1 found", attribute);
406 }
407
408 /***
409 * Tests
410 * {@link BasicAttributeContext#putAttribute(String, Attribute, boolean)}.
411 */
412 public void testPutAttributeStringAttributeBoolean() {
413 AttributeContext context = new BasicAttributeContext();
414 context.putAttribute("name1", new Attribute("value1"), false);
415 context.putAttribute("name2", new Attribute("value2"), true);
416 Attribute attribute = context.getLocalAttribute("name1");
417 assertNotNull("Attribute name1 not found", attribute);
418 assertEquals("Attribute name1 has not been set correctly", "value1",
419 attribute.getValue());
420 attribute = context.getCascadedAttribute("name1");
421 assertNull("Attribute name1 found", attribute);
422 attribute = context.getCascadedAttribute("name2");
423 assertNotNull("Attribute name2 not found", attribute);
424 assertEquals("Attribute name2 has not been set correctly", "value2",
425 attribute.getValue());
426 attribute = context.getLocalAttribute("name2");
427 assertNull("Attribute name2 found", attribute);
428 }
429
430 /***
431 * Tests {@link BasicAttributeContext#clear()}.
432 */
433 public void testClear() {
434 AttributeContext context = new BasicAttributeContext();
435 context.putAttribute("name1", new Attribute("value1"), false);
436 context.putAttribute("name2", new Attribute("value2"), true);
437 context.clear();
438 Set<String> names = context.getLocalAttributeNames();
439 assertTrue("There are local attributes", names == null
440 || names.isEmpty());
441 names = context.getCascadedAttributeNames();
442 assertTrue("There are cascaded attributes", names == null
443 || names.isEmpty());
444 }
445
446 /***
447 * Tests {@link BasicAttributeContext} for the TILES-429 bug.
448 */
449 public void testTiles429() {
450 AttributeContext toCopy = new BasicAttributeContext();
451 toCopy.putAttribute("name1", new Attribute("value1"), false);
452 toCopy.putAttribute("name2", new Attribute("value2"), true);
453 List<Object> listOfObjects = new ArrayList<Object>();
454 listOfObjects.add(1);
455 ListAttribute listAttribute = new ListAttribute(listOfObjects);
456 listAttribute.setInherit(true);
457 toCopy.putAttribute("name3", listAttribute);
458 Attribute templateAttribute = Attribute
459 .createTemplateAttribute("/template.jsp");
460 Set<String> roles = new HashSet<String>();
461 roles.add("role1");
462 roles.add("role2");
463 templateAttribute.setRoles(roles);
464 toCopy.setTemplateAttribute(templateAttribute);
465 toCopy.setPreparer("my.preparer.Preparer");
466 AttributeContext context = new BasicAttributeContext(toCopy);
467 Attribute attribute = context.getAttribute("name1");
468 attribute.setValue("newValue1");
469 attribute = context.getAttribute("name1");
470 assertEquals("newValue1", attribute.getValue());
471 attribute = toCopy.getAttribute("name1");
472 assertEquals("value1", attribute.getValue());
473 attribute = context.getAttribute("name3");
474 assertTrue(attribute instanceof ListAttribute);
475 assertTrue(((ListAttribute) attribute).isInherit());
476 }
477 }