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.net.URL;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Map;
29 import java.util.Collections;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.tiles.context.TilesRequestContext;
38
39 /***
40 * Tests the UrlDefinitionsFactory.
41 *
42 * @version $Rev: 571250 $ $Date: 2007-08-30 19:32:28 +0200 (Thu, 30 Aug 2007) $
43 */
44 public class TestUrlDefinitionsFactory extends TestCase {
45
46 /***
47 * The logging object.
48 */
49 private static final Log LOG =
50 LogFactory.getLog(TestUrlDefinitionsFactory.class);
51
52 /***
53 * The number of foreseen URLs with postfixes.
54 */
55 private static final int POSTFIX_COUNT = 3;
56
57 /***
58 * Creates a new instance of TestUrlDefinitionsFactory.
59 *
60 * @param name The name of the test.
61 */
62 public TestUrlDefinitionsFactory(String name) {
63 super(name);
64 }
65
66 /***
67 * Start the tests.
68 *
69 * @param theArgs the arguments. Not used
70 */
71 public static void main(String[] theArgs) {
72 junit.textui.TestRunner.main(
73 new String[]{TestUrlDefinitionsFactory.class.getName()});
74 }
75
76 /***
77 * @return a test suite (<code>TestSuite</code>) that includes all methods
78 * starting with "test"
79 */
80 public static Test suite() {
81 return new TestSuite(TestUrlDefinitionsFactory.class);
82 }
83
84 /***
85 * Tests the readDefinitions method under normal conditions.
86 *
87 * @throws Exception If something goes wrong.
88 */
89 @SuppressWarnings("unchecked")
90 public void testReadDefinitions() throws Exception {
91 DefinitionsFactory factory = new UrlDefinitionsFactory();
92
93
94 URL url1 = this.getClass().getClassLoader().getResource(
95 "org/apache/tiles/config/defs1.xml");
96 assertNotNull("Could not load defs1 file.", url1);
97 URL url2 = this.getClass().getClassLoader().getResource(
98 "org/apache/tiles/config/defs2.xml");
99 assertNotNull("Could not load defs2 file.", url2);
100 URL url3 = this.getClass().getClassLoader().getResource(
101 "org/apache/tiles/config/defs3.xml");
102 assertNotNull("Could not load defs3 file.", url3);
103
104 factory.init(Collections.EMPTY_MAP);
105 factory.addSource(url1);
106 factory.addSource(url2);
107 factory.addSource(url3);
108
109
110 Definitions definitions = factory.readDefinitions();
111
112 assertNotNull("test.def1 definition not found.", definitions.getDefinition("test.def1"));
113 assertNotNull("test.def2 definition not found.", definitions.getDefinition("test.def2"));
114 assertNotNull("test.def3 definition not found.", definitions.getDefinition("test.def3"));
115 }
116
117 /***
118 * Tests the getDefinition method.
119 *
120 * @throws Exception If something goes wrong.
121 */
122 @SuppressWarnings("unchecked")
123 public void testGetDefinition() throws Exception {
124 DefinitionsFactory factory = new UrlDefinitionsFactory();
125
126
127 URL url1 = this.getClass().getClassLoader().getResource(
128 "org/apache/tiles/config/defs1.xml");
129 assertNotNull("Could not load defs1 file.", url1);
130 URL url2 = this.getClass().getClassLoader().getResource(
131 "org/apache/tiles/config/defs2.xml");
132 assertNotNull("Could not load defs2 file.", url2);
133 URL url3 = this.getClass().getClassLoader().getResource(
134 "org/apache/tiles/config/defs3.xml");
135 assertNotNull("Could not load defs3 file.", url3);
136
137 factory.addSource(url1);
138 factory.addSource(url2);
139 factory.addSource(url3);
140 factory.init(Collections.EMPTY_MAP);
141
142 TilesRequestContext emptyContext = new MockOnlyLocaleTilesContext(null);
143 TilesRequestContext usContext = new MockOnlyLocaleTilesContext(Locale.US);
144 TilesRequestContext frenchContext = new MockOnlyLocaleTilesContext(Locale.FRENCH);
145 TilesRequestContext chinaContext = new MockOnlyLocaleTilesContext(Locale.CHINA);
146 TilesRequestContext canadaFrenchContext = new MockOnlyLocaleTilesContext(Locale.CANADA_FRENCH);
147
148 assertNotNull("test.def1 definition not found.", factory.getDefinition("test.def1", emptyContext));
149 assertNotNull("test.def2 definition not found.", factory.getDefinition("test.def2", emptyContext));
150 assertNotNull("test.def3 definition not found.", factory.getDefinition("test.def3", emptyContext));
151 assertNotNull("test.common definition not found.", factory.getDefinition("test.common", emptyContext));
152 assertNotNull("test.common definition in US locale not found.", factory
153 .getDefinition("test.common", usContext));
154 assertNotNull("test.common definition in FRENCH locale not found.",
155 factory.getDefinition("test.common", frenchContext));
156 assertNotNull("test.common definition in CHINA locale not found.",
157 factory.getDefinition("test.common", chinaContext));
158 assertNotNull(
159 "test.common.french definition in FRENCH locale not found.",
160 factory.getDefinition("test.common.french", frenchContext));
161 assertNotNull(
162 "test.common.french definition in CANADA_FRENCH locale not found.",
163 factory
164 .getDefinition("test.common.french",
165 canadaFrenchContext));
166 assertNotNull("test.def.toextend definition not found.", factory
167 .getDefinition("test.def.toextend", emptyContext));
168 assertNotNull("test.def.overridden definition not found.", factory
169 .getDefinition("test.def.overridden", emptyContext));
170 assertNotNull(
171 "test.def.overridden definition in FRENCH locale not found.",
172 factory.getDefinition("test.def.overridden", frenchContext));
173
174 assertEquals("Incorrect default country value", "default", factory
175 .getDefinition("test.def1", emptyContext).getAttribute(
176 "country").getValue());
177 assertEquals("Incorrect US country value", "US", factory.getDefinition(
178 "test.def1", usContext).getAttribute("country").getValue());
179 assertEquals("Incorrect France country value", "France", factory
180 .getDefinition("test.def1", frenchContext).getAttribute(
181 "country").getValue());
182 assertEquals("Incorrect Chinese country value (should be default)",
183 "default", factory.getDefinition("test.def1", chinaContext)
184 .getAttribute("country").getValue());
185 assertEquals("Incorrect default country value", "default", factory
186 .getDefinition("test.def.overridden", emptyContext)
187 .getAttribute("country").getValue());
188 assertEquals("Incorrect default title value",
189 "Definition to be overridden", factory.getDefinition(
190 "test.def.overridden", emptyContext).getAttribute(
191 "title").getValue());
192 assertEquals("Incorrect France country value", "France", factory
193 .getDefinition("test.def.overridden", frenchContext)
194 .getAttribute("country").getValue());
195 assertEquals("Incorrect France title value",
196 "Definition to be extended", factory.getDefinition(
197 "test.def.overridden", frenchContext).getAttribute(
198 "title").getValue());
199 }
200
201 /***
202 * Tests addSource with a bad source object type.
203 *
204 * @throws Exception If something goes wrong.
205 */
206 @SuppressWarnings("unchecked")
207 public void testBadSourceType() throws Exception {
208 try {
209 DefinitionsFactory factory = new UrlDefinitionsFactory();
210
211 factory.init(Collections.EMPTY_MAP);
212 factory.addSource("Bad object.");
213
214 fail("Should've thrown exception.");
215 } catch (DefinitionsFactoryException e) {
216 if (LOG.isDebugEnabled()) {
217 LOG.debug("Bad class name intercepted, it is ok", e);
218 }
219
220 }
221 }
222
223 /***
224 * Tests the addDefinitions method under normal
225 * circumstances.
226 *
227 * @throws Exception If something goes wrong.
228 */
229 @SuppressWarnings("unchecked")
230 public void testReadByLocale() throws Exception {
231 MockPublicUrlDefinitionsFactory factory = new MockPublicUrlDefinitionsFactory();
232
233
234 URL url1 = this.getClass().getClassLoader().getResource(
235 "org/apache/tiles/config/defs1.xml");
236 assertNotNull("Could not load defs1 file.", url1);
237 URL url2 = this.getClass().getClassLoader().getResource(
238 "org/apache/tiles/config/defs2.xml");
239 assertNotNull("Could not load defs2 file.", url2);
240 URL url3 = this.getClass().getClassLoader().getResource(
241 "org/apache/tiles/config/defs3.xml");
242 assertNotNull("Could not load defs3 file.", url3);
243
244 factory.init(Collections.EMPTY_MAP);
245 factory.addSource(url1);
246 factory.addSource(url2);
247 factory.addSource(url3);
248
249
250 Definitions definitions = factory.readDefinitions();
251 factory.addDefinitions(definitions,
252 new MockOnlyLocaleTilesContext(Locale.US));
253 factory.addDefinitions(definitions,
254 new MockOnlyLocaleTilesContext(Locale.FRENCH));
255
256 assertNotNull("test.def1 definition not found.", definitions.getDefinition("test.def1"));
257 assertNotNull("test.def1 US definition not found.", definitions.getDefinition("test.def1", Locale.US));
258 assertNotNull("test.def1 France definition not found.", definitions.getDefinition("test.def1", Locale.FRENCH));
259 assertNotNull("test.def1 China should return default.", definitions.getDefinition("test.def1", Locale.CHINA));
260
261 assertEquals("Incorrect default country value", "default", definitions
262 .getDefinition("test.def1").getAttribute("country").getValue());
263 assertEquals("Incorrect US country value", "US", definitions
264 .getDefinition("test.def1", Locale.US).getAttribute("country")
265 .getValue());
266 assertEquals("Incorrect France country value", "France", definitions
267 .getDefinition("test.def1", Locale.FRENCH).getAttribute(
268 "country").getValue());
269 assertEquals("Incorrect Chinese country value (should default)",
270 "default", definitions.getDefinition("test.def1", Locale.CHINA)
271 .getAttribute("country").getValue());
272 }
273
274 /***
275 * Tests the isContextProcessed method.
276 *
277 * @throws Exception If something goes wrong.
278 */
279 @SuppressWarnings("unchecked")
280 public void testIsContextProcessed() throws Exception {
281 MockPublicUrlDefinitionsFactory factory = new MockPublicUrlDefinitionsFactory();
282
283
284 URL url1 = this.getClass().getClassLoader().getResource(
285 "org/apache/tiles/config/defs1.xml");
286 assertNotNull("Could not load defs1 file.", url1);
287
288 factory.init(Collections.EMPTY_MAP);
289 factory.addSource(url1);
290
291
292 Definitions definitions = factory.readDefinitions();
293 TilesRequestContext tilesContext =
294 new MockOnlyLocaleTilesContext(Locale.US);
295 assertFalse("Locale should not be processed.",
296 factory.isContextProcessed(tilesContext));
297
298 factory.addDefinitions(definitions, tilesContext);
299 assertTrue("Locale should be processed.",
300 factory.isContextProcessed(tilesContext));
301 }
302
303 /***
304 * Tests the reader init param.
305 *
306 * @throws Exception If something goes wrong.
307 */
308 public void testReaderParam() throws Exception {
309 Map<String, String> params = new HashMap<String, String>();
310 params.put(DefinitionsFactory.READER_IMPL_PROPERTY,
311 "org.apache.tiles.definition.MockDefinitionsReader");
312
313 int instanceCount = MockDefinitionsReader.getInstanceCount();
314
315 DefinitionsFactory factory = new UrlDefinitionsFactory();
316
317
318 URL url1 = this.getClass().getClassLoader().getResource(
319 "org/apache/tiles/config/defs1.xml");
320 assertNotNull("Could not load defs1 file.", url1);
321
322 factory.init(params);
323 factory.addSource(url1);
324
325 assertEquals("MockDefinitionsReader not used.",
326 instanceCount + 1,
327 MockDefinitionsReader.getInstanceCount());
328 }
329
330 /***
331 * Tests the calculatePostfixes method.
332 */
333 public void testCalculatePostfixes() {
334 Locale locale = Locale.US;
335
336 List<String> posts = UrlDefinitionsFactory.calculatePostfixes(locale);
337 assertEquals(POSTFIX_COUNT, posts.size());
338 assertTrue(posts.contains("_en_US"));
339 assertTrue(posts.contains("_en"));
340
341 locale = Locale.ENGLISH;
342 posts = UrlDefinitionsFactory.calculatePostfixes(locale);
343 assertEquals(2, posts.size());
344 assertTrue(posts.contains("_en"));
345 }
346
347 /***
348 * Tests the concatPostfix method.
349 */
350 public void testConcatPostfix() {
351 UrlDefinitionsFactory factory = new UrlDefinitionsFactory();
352 String postfix = "_en_US";
353 assertEquals("a_en_US", factory.concatPostfix("a", postfix));
354 assertEquals("a_en_US.jsp", factory.concatPostfix("a.jsp", postfix));
355 assertEquals("file_en_US.jsp", factory.concatPostfix("file.jsp", postfix));
356 assertEquals("./path/file_en_US.jsp", factory.concatPostfix("./path/file.jsp", postfix));
357 }
358 }