1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.tiles.definition;
23
24 import java.util.HashMap;
25 import java.util.Locale;
26 import java.util.Map;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import junit.framework.TestSuite;
30 import org.apache.tiles.Attribute;
31 import org.apache.tiles.Definition;
32 import org.apache.tiles.Attribute.AttributeType;
33
34 /***
35 * Tests the DefinitionsImpl class.
36 *
37 * @version $Rev: 616890 $ $Date: 2008-01-30 21:16:51 +0100 (Wed, 30 Jan 2008) $
38 */
39 public class TestDefinitions extends TestCase {
40
41 /***
42 * Creates a new instance of TestDefinitions.
43 *
44 * @param name The name of the test.
45 */
46 public TestDefinitions(String name) {
47 super(name);
48 }
49
50 /***
51 * Start the tests.
52 *
53 * @param theArgs the arguments. Not used
54 */
55 public static void main(String[] theArgs) {
56 junit.textui.TestRunner.main(
57 new String[] { TestDefinitions.class.getName()});
58 }
59
60 /***
61 * @return a test suite (<code>TestSuite</code>) that includes all methods
62 * starting with "test"
63 */
64 public static Test suite() {
65 return new TestSuite(TestDefinitions.class);
66 }
67
68 /***
69 * Tests the inheritance properties of Definition objects.
70 */
71 public void testResolveInheritances() {
72 Map<String, Definition> defs = new HashMap<String, Definition>();
73
74 Definition def = new Definition();
75 def.setName("parent.def1");
76 def.setTemplate("/test1.jsp");
77 Attribute attr = new Attribute();
78 attr.setValue("value1");
79 def.putAttribute("attr1", attr);
80 attr = new Attribute();
81 attr.setValue("tiles.def1");
82
83 def.putAttribute("attr2", attr);
84 defs.put(def.getName(), def);
85 attr = new Attribute();
86 attr.setValue("tiles.def1");
87 attr.setType(AttributeType.STRING);
88 def.putAttribute("attr3", attr);
89 defs.put(def.getName(), def);
90
91 def = new Definition();
92 def.setName("tiles.def1");
93 def.setTemplate("/test2.jsp");
94 defs.put(def.getName(), def);
95
96 def = new Definition();
97 def.setName("child.def1");
98 def.setExtends("parent.def1");
99 attr = new Attribute();
100 attr.setValue("New value");
101 def.putAttribute("attr1", attr);
102 defs.put(def.getName(), def);
103
104 Definitions definitions = new DefinitionsImpl();
105 try {
106 definitions.addDefinitions(defs);
107 } catch (NoSuchDefinitionException e) {
108 fail("Test failure: " + e);
109 }
110
111 def = definitions.getDefinition("parent.def1");
112
113 assertNotNull("Couldn't get parent.", def);
114 assertEquals("Incorrect template value.", "/test1.jsp", def
115 .getTemplate());
116 assertEquals("Incorrect attr1 value", "value1", def
117 .getAttribute("attr1").getValue());
118
119 attr = def.getAttributes().get("attr1");
120 assertNotNull("Dependent attribute not found.", attr);
121 attr = def.getAttributes().get("attr2");
122 assertNotNull("Dependent attribute not found.", attr);
123 attr = def.getAttributes().get("attr3");
124 assertNotNull("Dependent attribute not found.", attr);
125 assertTrue("The attribute 'attr3' should be of type STRING", attr
126 .getType() == AttributeType.STRING);
127
128 def = definitions.getDefinition("child.def1");
129
130 assertNotNull("Couldn't get child.",
131 definitions.getDefinition("child.def1"));
132 assertEquals("Incorrect template value." , "/test1.jsp",
133 definitions.getDefinition("child.def1").getTemplate());
134 assertEquals("Incorrect attr1 value", "New value", definitions
135 .getDefinition("child.def1").getAttribute("attr1").getValue());
136
137 attr = def.getAttributes().get("attr1");
138 assertNotNull("Dependent attribute not found.", attr);
139 attr = def.getAttributes().get("attr2");
140 assertNotNull("Dependent attribute not found.", attr);
141 attr = def.getAttributes().get("attr3");
142 assertNotNull("Dependent attribute not found.", attr);
143 assertTrue("The attribute 'attr3' should be of type STRING", attr
144 .getType() == AttributeType.STRING);
145 }
146
147 /***
148 * Tests the inheritance with localized definitions.
149 */
150 public void testLocalizedResolveInheritances() {
151 Map<String, Definition> defs = new HashMap<String, Definition>();
152 Definition def = new Definition();
153 def.setName("parent.def1");
154 def.setTemplate("/test1.jsp");
155 Attribute attr = new Attribute();
156 attr.setValue("value1");
157 def.putAttribute("attr1", attr);
158 defs.put(def.getName(), def);
159
160 def = new Definition();
161 def.setName("child.def1");
162 def.setExtends("parent.def1");
163 attr = new Attribute();
164 attr.setValue("New value");
165 def.putAttribute("attr1", attr);
166 defs.put(def.getName(), def);
167
168 Map<String, Definition> localDefs = new HashMap<String, Definition>();
169 def = new Definition();
170 def.setName("child.def1");
171 def.setExtends("parent.def1");
172 attr = new Attribute();
173 attr.setValue("US Value");
174 def.putAttribute("attr1", attr);
175 localDefs.put(def.getName(), def);
176
177 Definitions definitions = new DefinitionsImpl();
178 try {
179 definitions.addDefinitions(defs);
180 definitions.addDefinitions(localDefs, Locale.US);
181 } catch (NoSuchDefinitionException e) {
182 fail("Test failure: " + e);
183 }
184
185 assertNotNull("Couldn't get parent.",
186 definitions.getDefinition("parent.def1"));
187 assertEquals("Incorrect template value." , "/test1.jsp",
188 definitions.getDefinition("parent.def1").getTemplate());
189 assertEquals("Incorrect attr1 value", "value1", definitions
190 .getDefinition("parent.def1").getAttribute("attr1").getValue());
191
192 assertNotNull("Couldn't get child.",
193 definitions.getDefinition("child.def1"));
194 assertEquals("Incorrect template value." , "/test1.jsp",
195 definitions.getDefinition("child.def1").getTemplate());
196 assertEquals("Incorrect attr1 value", "New value", definitions
197 .getDefinition("child.def1").getAttribute("attr1").getValue());
198
199 assertNotNull("Couldn't get parent.",
200 definitions.getDefinition("parent.def1", Locale.US));
201 assertEquals("Incorrect template value." , "/test1.jsp",
202 definitions.getDefinition("parent.def1", Locale.US).getTemplate());
203 assertEquals("Incorrect attr1 value", "value1", definitions
204 .getDefinition("parent.def1", Locale.US).getAttribute("attr1")
205 .getValue());
206
207 assertNotNull("Couldn't get child.",
208 definitions.getDefinition("child.def1", Locale.US));
209 assertEquals("Incorrect template value." , "/test1.jsp",
210 definitions.getDefinition("child.def1", Locale.US).getTemplate());
211 assertEquals("Incorrect attr1 value", "US Value", definitions
212 .getDefinition("child.def1", Locale.US).getAttribute("attr1")
213 .getValue());
214 }
215
216 /***
217 * Tests the reset method.
218 */
219 public void testReset() {
220 Map<String, Definition> defs = new HashMap<String, Definition>();
221
222 Definition def = new Definition();
223 def.setName("parent.def1");
224 def.setTemplate("/test1.jsp");
225 Attribute attr = new Attribute();
226 attr.setValue("value1");
227 def.putAttribute("attr1", attr);
228 defs.put(def.getName(), def);
229
230 def = new Definition();
231 def.setName("child.def1");
232 def.setExtends("parent.def1");
233 attr = new Attribute();
234 attr.setValue("New value");
235 def.putAttribute("attr1", attr);
236 defs.put(def.getName(), def);
237
238 Definitions definitions = new DefinitionsImpl();
239 try {
240 definitions.addDefinitions(defs);
241 } catch (NoSuchDefinitionException e) {
242 fail("Test failure: " + e);
243 }
244
245 assertNotNull("Couldn't get parent.",
246 definitions.getDefinition("parent.def1"));
247
248 definitions.reset();
249 assertNull("Definitions should be null.",
250 definitions.getDefinition("parent.def1"));
251 }
252
253 /***
254 * Verifies that attribute dependencies are resolved.
255 *
256 * A definition can have an attribute that points to another definition.
257 * This test verifies that the <code>resolveAttributes</code> method is
258 * executed and attribute dependencies are calculated.
259 */
260 public void testResolveAttributeDependencies() {
261 Map<String, Definition> defs = new HashMap<String, Definition>();
262
263 Definition def = new Definition();
264 def.setName("parent.def1");
265 def.setTemplate("/test1.jsp");
266 Attribute attr = new Attribute();
267 attr.setValue("tiles.def2");
268 attr.setType(AttributeType.DEFINITION);
269 def.putAttribute("attr1", attr);
270 defs.put(def.getName(), def);
271
272 def = new Definition();
273 def.setName("parent.notype.def1");
274 def.setTemplate("/test1.jsp");
275 attr = new Attribute();
276 attr.setValue("tiles.def2");
277
278 def.putAttribute("attr1", attr);
279 defs.put(def.getName(), def);
280
281 def = new Definition();
282 def.setName("tiles.def2");
283 defs.put(def.getName(), def);
284
285 Definitions definitions = new DefinitionsImpl();
286 try {
287 definitions.addDefinitions(defs);
288 } catch (NoSuchDefinitionException e) {
289 fail("Test failure: " + e);
290 }
291
292 defs = new HashMap<String, Definition>(defs);
293 def = new Definition();
294 def.setName("parent.def2");
295 def.setTemplate("/test1.jsp");
296 attr = new Attribute();
297 attr.setValue("tiles.def3");
298 def.putAttribute("attr1", attr);
299 defs.put(def.getName(), def);
300 def = new Definition();
301 def.setName("tiles.def3");
302 defs.put(def.getName(), def);
303
304 try {
305 definitions.addDefinitions(defs, Locale.ITALIAN);
306 } catch (NoSuchDefinitionException e) {
307 fail("Test failure: " + e);
308 }
309
310 Definition newDef = definitions.getDefinition("parent.def1");
311 assertNotNull("Parent definition not found.", newDef);
312
313 Object newAttr = newDef.getAttribute("attr1").getValue();
314 assertNotNull("Dependent attribute not found.", newAttr);
315
316 newDef = definitions.getDefinition("parent.notype.def1");
317 assertNotNull("Parent definition not found.", newDef);
318
319 newAttr = newDef.getAttribute("attr1").getValue();
320 assertNotNull("Dependent attribute not found.", newAttr);
321
322 assertEquals("Incorrect dependent attribute name.", "tiles.def2",
323 newAttr);
324
325
326 newDef = definitions.getDefinition("parent.def1", Locale.ITALIAN);
327 assertNotNull("Parent definition not found.", newDef);
328
329 newAttr = newDef.getAttribute("attr1").getValue();
330 assertNotNull("Dependent attribute not found.", newAttr);
331
332 newDef = definitions.getDefinition("parent.notype.def1",
333 Locale.ITALIAN);
334 assertNotNull("Parent definition not found.", newDef);
335
336 newAttr = newDef.getAttribute("attr1").getValue();
337 assertNotNull("Dependent attribute not found.", newAttr);
338
339 assertEquals("Incorrect dependent attribute name.", "tiles.def2",
340 newAttr);
341
342 newDef = definitions.getDefinition("parent.def2", Locale.ITALIAN);
343 assertNotNull("Parent definition not found.", newDef);
344
345 attr = newDef.getAttributes().get("attr1");
346 assertNotNull("Dependent attribute not found.", attr);
347 }
348 }