2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.tiger.config;
19  
20  import junit.framework.Test;
21  import junit.framework.TestCase;
22  import junit.framework.TestSuite;
23  import org.apache.shale.tiger.managed.config.ListEntriesConfig;
24  import org.apache.shale.tiger.managed.config.ListEntryConfig;
25  import org.apache.shale.tiger.managed.config.ManagedBeanConfig;
26  import org.apache.shale.tiger.managed.config.ManagedPropertyConfig;
27  import org.apache.shale.tiger.managed.config.MapEntriesConfig;
28  import org.apache.shale.tiger.managed.config.MapEntryConfig;
29  
30  /***
31   * <p>Test case for <code>org.apache.shale.tiger.digester.FacesConfigParser</code>.</p>
32   */
33  public class FacesConfigParserTestCase extends TestCase {
34      
35  
36      // ------------------------------------------------------------ Constructors
37  
38  
39      // Construct a new instance of this test case
40      public FacesConfigParserTestCase(String name) {
41          super(name);
42      }
43      
44  
45      // ---------------------------------------------------- Overall Test Methods
46  
47  
48      // Set up instance variables required by this test case.
49      protected void setUp() throws Exception {
50  
51          facesConfig = new FacesConfigConfig();
52          parser = new FacesConfigParser();
53  
54      }
55  
56  
57      // Return the tests included in this test case.
58      public static Test suite() {
59          return new TestSuite(FacesConfigParserTestCase.class);
60      }
61  
62  
63      // Tear down instance variables required by this test case
64      protected void tearDown() throws Exception {
65  
66          facesConfig = null;
67          parser = null;
68  
69      }
70  
71  
72      // ------------------------------------------------------ Instance Variables
73  
74  
75      // FacesConfigConfig instance representing our parsed metadata
76      FacesConfigConfig facesConfig = null;
77  
78  
79      // Parser instance to be tested
80      FacesConfigParser parser = null;
81  
82  
83      // ------------------------------------------------------------ Test Methods
84  
85  
86      // Test parsing configuration resource that has lists in it
87      public void testList() throws Exception {
88  
89          ManagedBeanConfig mb = null;
90          ManagedPropertyConfig mp = null;
91          ListEntriesConfig entries = null;
92          ListEntryConfig entry = null;
93  
94          // Wire up our FacesConfigConfig instance
95          parser.setFacesConfig(facesConfig);
96          assertTrue(facesConfig == parser.getFacesConfig());
97  
98          // Make sure we do validating parses
99          parser.setValidating(true);
100         assertTrue(parser.isValidating());
101 
102         // Parse resource 4
103         parser.setResource(this.getClass().getResource
104                 ("/org/apache/shale/tiger/config/test-config-4.xml"));
105         assertNotNull(parser.getResource());
106         parser.parse();
107         assertEquals(4, facesConfig.getManagedBeans().size());
108 
109         // Validate bean "explicitSqlDateList"
110         mb = facesConfig.getManagedBean("explicitIntegerList");
111         assertNotNull(mb);
112         assertEquals("explicitIntegerList", mb.getName());
113         assertEquals("none", mb.getScope());
114         assertEquals("java.util.Vector", mb.getType());
115         assertNotNull(mb.getListEntries());
116         assertNull(mb.getMapEntries());
117         assertEquals(0, mb.getProperties().size());
118 
119         entries = mb.getListEntries();
120         assertEquals("java.lang.Integer", entries.getValueType());
121         assertEquals(4, entries.getEntries().size());
122         entry = entries.getEntries().get(0);
123         assertNotNull(entry);
124         assertTrue(!entry.isExpression());
125         assertTrue(!entry.isNullValue());
126         assertEquals("123", entry.getValue());
127         entry = entries.getEntries().get(1);
128         assertNotNull(entry);
129         assertTrue(!entry.isExpression());
130         assertTrue(!entry.isNullValue());
131         assertEquals("234", entry.getValue());
132         entry = entries.getEntries().get(2);
133         assertNotNull(entry);
134         assertTrue(!entry.isExpression());
135         assertTrue(entry.isNullValue());
136         assertNull(entry.getValue());
137         entry = entries.getEntries().get(3);
138         assertNotNull(entry);
139         assertTrue(!entry.isExpression());
140         assertTrue(!entry.isNullValue());
141         assertEquals("345", entry.getValue());
142 
143         // Validate bean "explicitStringList"
144         mb = facesConfig.getManagedBean("explicitStringList");
145         assertNotNull(mb);
146         assertEquals("explicitStringList", mb.getName());
147         assertEquals("request", mb.getScope());
148         assertEquals("java.util.LinkedList", mb.getType());
149         assertNotNull(mb.getListEntries());
150         assertNull(mb.getMapEntries());
151         assertEquals(0, mb.getProperties().size());
152 
153         entries = mb.getListEntries();
154         assertEquals("java.lang.String", entries.getValueType());
155         assertEquals(5, entries.getEntries().size());
156         entry = entries.getEntries().get(0);
157         assertNotNull(entry);
158         assertTrue(!entry.isExpression());
159         assertTrue(!entry.isNullValue());
160         assertEquals("foo", entry.getValue());
161         entry = entries.getEntries().get(1);
162         assertNotNull(entry);
163         assertTrue(!entry.isExpression());
164         assertTrue(!entry.isNullValue());
165         assertEquals("bar", entry.getValue());
166         entry = entries.getEntries().get(2);
167         assertNotNull(entry);
168         assertTrue(!entry.isExpression());
169         assertTrue(entry.isNullValue());
170         assertNull(entry.getValue());
171         entry = entries.getEntries().get(3);
172         assertNotNull(entry);
173         assertTrue(!entry.isExpression());
174         assertTrue(!entry.isNullValue());
175         assertEquals("baz", entry.getValue());
176         entry = entries.getEntries().get(4);
177         assertNotNull(entry);
178         assertTrue(!entry.isExpression());
179         assertTrue(!entry.isNullValue());
180         assertEquals("bop", entry.getValue());
181 
182         // Validate bean "implicitStringList"
183         mb = facesConfig.getManagedBean("implicitStringList");
184         assertNotNull(mb);
185         assertEquals("implicitStringList", mb.getName());
186         assertEquals("session", mb.getScope());
187         assertEquals("java.util.ArrayList", mb.getType());
188         assertNotNull(mb.getListEntries());
189         assertNull(mb.getMapEntries());
190         assertEquals(0, mb.getProperties().size());
191 
192         entries = mb.getListEntries();
193         assertEquals(null, entries.getValueType());
194         assertEquals(5, entries.getEntries().size());
195         entry = entries.getEntries().get(0);
196         assertNotNull(entry);
197         assertTrue(!entry.isExpression());
198         assertTrue(!entry.isNullValue());
199         assertEquals("bop", entry.getValue());
200         entry = entries.getEntries().get(1);
201         assertNotNull(entry);
202         assertTrue(!entry.isExpression());
203         assertTrue(entry.isNullValue());
204         assertNull(entry.getValue());
205         entry = entries.getEntries().get(2);
206         assertNotNull(entry);
207         assertTrue(!entry.isExpression());
208         assertTrue(!entry.isNullValue());
209         assertEquals("baz", entry.getValue());
210         entry = entries.getEntries().get(3);
211         assertNotNull(entry);
212         assertTrue(!entry.isExpression());
213         assertTrue(!entry.isNullValue());
214         assertEquals("bar", entry.getValue());
215         entry = entries.getEntries().get(4);
216         assertNotNull(entry);
217         assertTrue(!entry.isExpression());
218         assertTrue(!entry.isNullValue());
219         assertEquals("foo", entry.getValue());
220 
221         // Validate bean "listPropertiesBean"
222         mb = facesConfig.getManagedBean("listPropertiesBean");
223         assertNotNull(mb);
224         assertEquals("listPropertiesBean", mb.getName());
225         assertEquals("application", mb.getScope());
226         assertEquals("org.apache.shale.tiger.config.TestBean4", mb.getType());
227         assertNull(mb.getListEntries());
228         assertNull(mb.getMapEntries());
229         assertEquals(2, mb.getProperties().size());
230 
231         mp = mb.getProperty("emptyList");
232         assertNotNull(mp);
233         assertEquals("emptyList", mp.getName());
234         assertNull(mp.getValue());
235         assertTrue(!mp.isExpression());
236         assertTrue(!mp.isNullValue());
237         assertNotNull(mp.getListEntries());
238         assertEquals(5, mp.getListEntries().getEntries().size());
239 
240         mp = mb.getProperty("fullList");
241         assertNotNull(mp);
242         assertEquals("fullList", mp.getName());
243         assertNull(mp.getValue());
244         assertTrue(!mp.isExpression());
245         assertTrue(!mp.isNullValue());
246         assertNotNull(mp.getListEntries());
247         assertEquals(5, mp.getListEntries().getEntries().size());
248 
249     }
250 
251 
252     // Test parsing configuration resource that has maps in it
253     public void testMap() throws Exception {
254 
255         ManagedBeanConfig mb = null;
256         ManagedPropertyConfig mp = null;
257         MapEntriesConfig entries = null;
258         MapEntryConfig entry = null;
259 
260         // Wire up our FacesConfigConfig instance
261         parser.setFacesConfig(facesConfig);
262         assertTrue(facesConfig == parser.getFacesConfig());
263 
264         // Make sure we do validating parses
265         parser.setValidating(true);
266         assertTrue(parser.isValidating());
267 
268         // Parse resource 5
269         parser.setResource(this.getClass().getResource
270                 ("/org/apache/shale/tiger/config/test-config-5.xml"));
271         assertNotNull(parser.getResource());
272         parser.parse();
273         assertEquals(2, facesConfig.getManagedBeans().size());
274 
275         // Validate bean "stringDateMap"
276         mb = facesConfig.getManagedBean("stringIntegerMap");
277         assertNotNull(mb);
278         assertEquals("stringIntegerMap", mb.getName());
279         assertEquals("none", mb.getScope());
280         assertEquals("java.util.TreeMap", mb.getType());
281         assertNull(mb.getListEntries());
282         assertNotNull(mb.getMapEntries());
283         assertEquals(0, mb.getProperties().size());
284 
285         entries = mb.getMapEntries();
286         assertEquals("java.lang.String", entries.getKeyType());
287         assertEquals("java.lang.Integer", entries.getValueType());
288         assertEquals(4, entries.getEntries().size());
289 
290         entry = entries.getEntries().get(0);
291         assertEquals("First", entry.getKey());
292         assertEquals("123", entry.getValue());
293         assertTrue(!entry.isExpression());
294         assertTrue(!entry.isNullValue());
295 
296         entry = entries.getEntries().get(1);
297         assertEquals("Second", entry.getKey());
298         assertEquals("234", entry.getValue());
299         assertTrue(!entry.isExpression());
300         assertTrue(!entry.isNullValue());
301 
302         entry = entries.getEntries().get(2);
303         assertEquals("Third", entry.getKey());
304         assertNull(entry.getValue());
305         assertTrue(!entry.isExpression());
306         assertTrue(entry.isNullValue());
307 
308         entry = entries.getEntries().get(3);
309         assertEquals("Fourth", entry.getKey());
310         assertEquals("345", entry.getValue());
311         assertTrue(!entry.isExpression());
312         assertTrue(!entry.isNullValue());
313 
314         // Validate bean "mapPropertiesBean"
315         mb = facesConfig.getManagedBean("mapPropertiesBean");
316         assertNotNull(mb);
317         assertEquals("mapPropertiesBean", mb.getName());
318         assertEquals("application", mb.getScope());
319         assertEquals("org.apache.shale.tiger.config.TestBean5", mb.getType());
320         assertNull(mb.getListEntries());
321         assertNull(mb.getMapEntries());
322         assertEquals(2, mb.getProperties().size());
323 
324         mp = mb.getProperty("emptyMap");
325         assertNotNull(mp);
326         assertEquals("emptyMap", mp.getName());
327         assertNull(mp.getValue());
328         assertTrue(!mp.isExpression());
329         assertTrue(!mp.isNullValue());
330         assertNull(mp.getListEntries());
331         assertNotNull(mp.getMapEntries());
332 
333         entries = mp.getMapEntries();
334         assertNull(entries.getKeyType());
335         assertNull(entries.getValueType());
336         assertEquals(3, entries.getEntries().size());
337 
338         entry = entries.getEntries().get(0);
339         assertTrue(!entry.isExpression());
340         assertEquals("First Key", entry.getKey());
341         assertEquals("First Value", entry.getValue());
342 
343         entry = entries.getEntries().get(1);
344         assertTrue(!entry.isExpression());
345         assertEquals("Second Key", entry.getKey());
346         assertEquals("Second Value", entry.getValue());
347 
348         entry = entries.getEntries().get(2);
349         assertTrue(!entry.isExpression());
350         assertEquals("Third Key", entry.getKey());
351         assertEquals("Third Value", entry.getValue());
352 
353         mp = mb.getProperty("fullMap");
354         assertNotNull(mp);
355         assertEquals("fullMap", mp.getName());
356         assertNull(mp.getValue());
357         assertTrue(!mp.isExpression());
358         assertTrue(!mp.isNullValue());
359         assertNull(mp.getListEntries());
360         assertNotNull(mp.getMapEntries());
361 
362         entries = mp.getMapEntries();
363         assertNull(entries.getKeyType());
364         assertNull(entries.getValueType());
365         assertEquals(3, entries.getEntries().size());
366 
367         entry = entries.getEntries().get(0);
368         assertTrue(!entry.isExpression());
369         assertEquals("First Key", entry.getKey());
370         assertEquals("First Value", entry.getValue());
371 
372         entry = entries.getEntries().get(1);
373         assertTrue(!entry.isExpression());
374         assertEquals("Second Key", entry.getKey());
375         assertEquals("Second Value", entry.getValue());
376 
377         entry = entries.getEntries().get(2);
378         assertTrue(!entry.isExpression());
379         assertEquals("Third Key", entry.getKey());
380         assertEquals("Third Value", entry.getValue());
381 
382     }
383 
384 
385         // Test pristine instance of the parser
386     public void testPristine() {
387 
388         assertNull(parser.getFacesConfig());
389         assertNull(parser.getResource());
390         assertTrue(parser.isValidating());
391 
392         assertEquals(0, facesConfig.getManagedBeans().size());
393     }
394 
395 
396     // Test a static parse of our configuration resources that should
397     // reflect appropriate information merging, but should *not* reflect
398     // any annotations on included classes
399     public void testStatic() throws Exception {
400 
401         ManagedBeanConfig mb = null;
402         ManagedPropertyConfig mp = null;
403 
404         // Wire up our FacesConfigConfig instance
405         parser.setFacesConfig(facesConfig);
406         assertTrue(facesConfig == parser.getFacesConfig());
407 
408         // Make sure we do validating parses
409         parser.setValidating(true);
410         assertTrue(parser.isValidating());
411 
412         // Parse resource 0
413         parser.setResource(this.getClass().getResource
414                 ("/org/apache/shale/tiger/config/test-config-0.xml"));
415         assertNotNull(parser.getResource());
416         parser.parse();
417         assertEquals(1, facesConfig.getManagedBeans().size());
418 
419         // Validate bean0 conditions after parsing resource 0
420         mb = facesConfig.getManagedBean("bean0");
421         assertNotNull(mb);
422         assertEquals("bean0", mb.getName());
423         assertEquals("request", mb.getScope());
424         assertNull(mb.getListEntries());
425         assertNull(mb.getMapEntries());
426         assertEquals(0, mb.getProperties().size());
427 
428         // Validate bean1 conditions after parsing resource 0
429         assertNull(mb.getProperty("bean1"));
430 
431         // Validate bean2 conditions after parsing resource 0
432         assertNull(mb.getProperty("bean2"));
433 
434         // Validate bean3 conditions after parsing resource 0
435         assertNull(mb.getProperty("bean3"));
436 
437         // Parse resource 1
438         parser.setResource(this.getClass().getResource
439                 ("/org/apache/shale/tiger/config/test-config-1.xml"));
440         assertNotNull(parser.getResource());
441         parser.parse();
442         assertEquals(2, facesConfig.getManagedBeans().size());
443 
444         // Validate bean0 conditions after parsing resource 1
445         mb = facesConfig.getManagedBean("bean0");
446         assertNotNull(mb);
447         assertEquals("bean0", mb.getName());
448         assertEquals("request", mb.getScope());
449         assertNull(mb.getListEntries());
450         assertNull(mb.getMapEntries());
451         assertEquals(1, mb.getProperties().size());
452         mp = mb.getProperty("stringProperty");
453         assertNotNull(mp);
454         assertNull(mp.getValue());
455         assertTrue(mp.isNullValue());
456 
457         // Validate bean1 conditions after parsing resource 1
458         mb = facesConfig.getManagedBean("bean1");
459         assertNotNull(mb);
460         assertEquals(3, mb.getProperties().size());
461         mp = mb.getProperty("byteProperty");
462         assertNotNull(mp);
463         assertEquals("11", mp.getValue());
464         assertTrue(!mp.isExpression());
465         assertTrue(!mp.isNullValue());
466         mp = mb.getProperty("doubleProperty");
467         assertNotNull(mp);
468         assertEquals("222.0", mp.getValue());
469         assertTrue(!mp.isExpression());
470         assertTrue(!mp.isNullValue());
471         mp = mb.getProperty("intProperty");
472         assertNotNull(mp);
473         assertEquals("44", mp.getValue());
474         assertTrue(!mp.isExpression());
475         assertTrue(!mp.isNullValue());
476 
477         // Validate bean2 conditions after parsing resource 1
478         assertNull(facesConfig.getManagedBean("bean2"));
479 
480         // Validate bean3 conditions after parsing resource 1
481         assertNull(facesConfig.getManagedBean("bean3"));
482 
483         // Parse resource 2
484         parser.setResource(this.getClass().getResource
485                 ("/org/apache/shale/tiger/config/test-config-2.xml"));
486         assertNotNull(parser.getResource());
487         parser.parse();
488         assertEquals(3, facesConfig.getManagedBeans().size());
489 
490         // Validate bean2 conditions after parsing resource 2
491         mb = facesConfig.getManagedBean("bean2");
492         assertNotNull(mb);
493         assertEquals("bean2", mb.getName());
494         assertEquals("request", mb.getScope());
495         assertNull(mb.getListEntries());
496         assertNull(mb.getMapEntries());
497         assertEquals(1, mb.getProperties().size());
498         mp = mb.getProperty("stringProperty");
499         assertNotNull(mp);
500         assertEquals("java.lang.String", mp.getType());
501         assertEquals("Override The Annotation", mp.getValue());
502         assertTrue(!mp.isExpression());
503         assertTrue(!mp.isNullValue());
504 
505         // Parse resource 3
506         parser.setResource(this.getClass().getResource
507                 ("/org/apache/shale/tiger/config/test-config-3.xml"));
508         assertNotNull(parser.getResource());
509         parser.parse();
510         assertEquals(4, facesConfig.getManagedBeans().size());
511 
512         // Validate bean3 conditions after parsing resource 3
513         mb = facesConfig.getManagedBean("bean3");
514         assertNotNull(mb);
515         assertEquals("bean3", mb.getName());
516         assertEquals("none", mb.getScope());
517         assertNull(mb.getListEntries());
518         assertNull(mb.getMapEntries());
519         assertEquals(8, mb.getProperties().size());
520         mp = mb.getProperty("byteProperty");
521         assertNotNull(mp);
522         assertEquals("byte", mp.getType());
523         assertEquals("#{bean1.byteProperty}", mp.getValue());
524         assertTrue(mp.isExpression());
525         assertTrue(!mp.isNullValue());
526         mp = mb.getProperty("charProperty");
527         assertNotNull(mp);
528         assertEquals("char", mp.getType());
529         assertEquals("#{bean1.charProperty}", mp.getValue());
530         assertTrue(mp.isExpression());
531         assertTrue(!mp.isNullValue());
532         mp = mb.getProperty("charProperty");
533         assertNotNull(mp);
534         assertEquals("char", mp.getType());
535         assertEquals("#{bean1.charProperty}", mp.getValue());
536         assertTrue(mp.isExpression());
537         assertTrue(!mp.isNullValue());
538         mp = mb.getProperty("doubleProperty");
539         assertNotNull(mp);
540         assertEquals("double", mp.getType());
541         assertEquals("#{bean1.doubleProperty}", mp.getValue());
542         assertTrue(mp.isExpression());
543         assertTrue(!mp.isNullValue());
544         mp = mb.getProperty("floatProperty");
545         assertNotNull(mp);
546         assertEquals("float", mp.getType());
547         assertEquals("#{bean1.floatProperty}", mp.getValue());
548         assertTrue(mp.isExpression());
549         assertTrue(!mp.isNullValue());
550         mp = mb.getProperty("intProperty");
551         assertNotNull(mp);
552         assertEquals("int", mp.getType());
553         assertEquals("#{bean1.intProperty}", mp.getValue());
554         assertTrue(mp.isExpression());
555         assertTrue(!mp.isNullValue());
556         mp = mb.getProperty("longProperty");
557         assertNotNull(mp);
558         assertEquals("long", mp.getType());
559         assertEquals("#{bean1.longProperty}", mp.getValue());
560         assertTrue(mp.isExpression());
561         assertTrue(!mp.isNullValue());
562         mp = mb.getProperty("shortProperty");
563         assertNotNull(mp);
564         assertEquals("short", mp.getType());
565         assertEquals("#{bean1.shortProperty}", mp.getValue());
566         assertTrue(mp.isExpression());
567         assertTrue(!mp.isNullValue());
568         mp = mb.getProperty("stringProperty");
569         assertNotNull(mp);
570         assertEquals("java.lang.String", mp.getType());
571         assertEquals("#{bean1.stringProperty}", mp.getValue());
572         assertTrue(mp.isExpression());
573         assertTrue(!mp.isNullValue());
574 
575     }
576 
577 
578 }