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  package org.apache.shale.clay.config;
18  
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.TreeMap;
22  import java.util.TreeSet;
23  
24  import javax.faces.component.UIComponent;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.commons.chain.Command;
30  import org.apache.shale.clay.component.chain.AbstractCommand;
31  import org.apache.shale.clay.component.chain.ClayContext;
32  import org.apache.shale.clay.component.chain.CreateComponentCommand;
33  import org.apache.shale.clay.component.chain.PropertyValueCommand;
34  import org.apache.shale.clay.config.beans.AttributeBean;
35  import org.apache.shale.clay.config.beans.ComponentBean;
36  import org.apache.shale.clay.config.beans.ElementBean;
37  import org.apache.shale.clay.config.beans.SymbolBean;
38  
39  // tests properties and symbol evaluation
40  public class SymbolsTestCase extends AbstractTestCaseConfig {
41  
42      // Construct a new instance of this test case.
43      public SymbolsTestCase(String name) {
44          super(name);
45      }
46  
47      // Return the tests included in this test case.
48      public static Test suite() {
49  
50          return (new TestSuite(SymbolsTestCase.class));
51  
52      }
53   
54      public void testNestedSymbolReplacement() {
55          
56          
57          Map symbols = new TreeMap();
58          
59          ClayContext clayContext = new ClayContext();
60          clayContext.setFacesContext(facesContext);
61          clayContext.setSymbols(symbols);
62          
63          SymbolBean symbol = this.createSymbol("@a", "@b");
64          symbols.put(symbol.getName(), symbol);
65          
66          symbol = this.createSymbol("@b", "@c");
67          symbols.put(symbol.getName(), symbol);
68          
69          symbol = this.createSymbol("@c", "@d");
70          symbols.put(symbol.getName(), symbol);
71          
72          symbol = this.createSymbol("@d", "test");
73          symbols.put(symbol.getName(), symbol);
74          
75          AbstractCommand.realizeSymbols(clayContext);
76          
77          symbol = (SymbolBean) symbols.get("@a");
78          assertNotNull(symbol);
79          assertEquals("test", symbol.getValue());
80          
81          symbol = (SymbolBean) symbols.get("@b");
82          assertNotNull(symbol);
83          assertEquals("test", symbol.getValue());
84          
85          symbol = (SymbolBean) symbols.get("@c");
86          assertNotNull(symbol);
87          assertEquals("test", symbol.getValue());
88          
89          symbol = (SymbolBean) symbols.get("@d");
90          assertNotNull(symbol);
91          assertEquals("test", symbol.getValue());
92          
93          
94          symbols.clear();
95          
96          symbol = this.createSymbol("@a", "@b");
97          symbols.put(symbol.getName(), symbol);
98          
99          symbol = this.createSymbol("@b", "@a");
100         symbols.put(symbol.getName(), symbol);
101         
102         AbstractCommand.realizeSymbols(clayContext);
103         
104         symbol = (SymbolBean) symbols.get("@a");
105         assertNotNull(symbol);
106         assertEquals("@a", symbol.getValue());
107         
108         symbol = (SymbolBean) symbols.get("@b");
109         assertNotNull(symbol);
110         assertEquals("@a", symbol.getValue());
111         
112         
113         
114         symbols.clear();
115         
116         symbol = this.createSymbol("@foo", "@xbeanx.@xpropertyx");
117         symbols.put(symbol.getName(), symbol);
118         
119         symbol = this.createSymbol("@xbeanx", "@a");
120         symbols.put(symbol.getName(), symbol);
121         
122         symbol = this.createSymbol("@xpropertyx", "@b");
123         symbols.put(symbol.getName(), symbol);
124         
125         symbol = this.createSymbol("@a", "foo");
126         symbols.put(symbol.getName(), symbol);
127         
128         symbol = this.createSymbol("@b", "bar");
129         symbols.put(symbol.getName(), symbol);
130         
131         AbstractCommand.realizeSymbols(clayContext);
132         
133         symbol = (SymbolBean) symbols.get("@foo");
134         assertNotNull(symbol);
135         assertEquals("foo.bar", symbol.getValue());
136         
137     }
138     
139     
140     
141     public void testGenericPropertyCommand () throws Exception {
142         javax.faces.component.html.HtmlOutputText child = (javax.faces.component.html.HtmlOutputText) 
143                                 facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
144        assertNotNull("javax.faces.HtmlOutputText", child);
145        
146        
147        AttributeBean attr = new AttributeBean();
148        attr.setName("value");
149        attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
150        attr.setValue("10");
151        
152        ComponentBean displayElement = new ComponentBean();
153        displayElement.setJsfid("inputText");
154        displayElement.setComponentType("javax.faces.HtmlOutputText");
155        displayElement.setId("testId");
156        displayElement.addAttribute(attr);
157        
158        assertNotNull("attribute case insensitive", displayElement.getAttribute("VaLue"));
159        
160        ClayContext clayContext = new ClayContext();
161        clayContext.setFacesContext(facesContext);
162        clayContext.setChild(child);
163        clayContext.setAttribute(attr);
164        clayContext.setDisplayElement(displayElement);
165               
166        Command command = new PropertyValueCommand();
167        boolean isFinal = command.execute(clayContext);
168        assertEquals("command finished", true, isFinal);       
169        assertEquals("value = 10", child.getValue(), "10");
170 
171        
172        child = (javax.faces.component.html.HtmlOutputText) 
173                                facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
174        assertNotNull("javax.faces.HtmlOutputText", child);
175        clayContext.setChild(child);
176        
177        servletContext.setAttribute("goodYear", "1969");
178        attr.setBindingType(AttributeBean.BINDING_TYPE_VALUE);
179        attr.setValue("#{goodYear}");
180 
181        isFinal = command.execute(clayContext);
182        assertEquals("command finished", true, isFinal);       
183        assertEquals("value = 1969", "1969", child.getValue());
184        
185        child = (javax.faces.component.html.HtmlOutputText) 
186                              facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
187        assertNotNull("javax.faces.HtmlOutputText", child);
188        clayContext.setChild(child);
189        
190        servletContext.setAttribute("ping", "pong");
191        attr.setBindingType(AttributeBean.BINDING_TYPE_EARLY);
192        attr.setValue("#{ping}");
193 
194        isFinal = command.execute(clayContext);
195        assertEquals("command finished", true, isFinal);       
196        assertEquals("value = pong", child.getValue(), "pong");
197 
198 
199        child = (javax.faces.component.html.HtmlOutputText) 
200                                facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
201        assertNotNull("javax.faces.HtmlOutputText", child);
202        clayContext.setChild(child);
203        
204        attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
205        attr.setValue("#{forManfred}");
206 
207        isFinal = command.execute(clayContext);
208        assertEquals("command finished", true, isFinal);       
209        assertEquals("value = #{forManfred}", "#{forManfred}", child.getValue());
210 
211         
212     }
213    
214     //factory method for creating a symbol bean
215     private SymbolBean createSymbol(String name, String value) {
216         SymbolBean symbol = new SymbolBean();
217         symbol.setName(name);
218         symbol.setValue(value);
219         return symbol;
220     }   
221     
222     // test symbolic property replacement
223     public void testSymbolicProperties() throws Exception {
224            javax.faces.component.html.HtmlOutputText child = (javax.faces.component.html.HtmlOutputText) 
225                               facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
226            assertNotNull("javax.faces.HtmlOutputText", child);
227            
228            
229            AttributeBean attr = new AttributeBean();
230            attr.setName("value");
231            attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
232            attr.setValue("@value");  //symbolic attribute 
233                       
234            ComponentBean displayElement = new ComponentBean();
235            displayElement.setJsfid("inputText");
236            displayElement.setComponentType("javax.faces.HtmlOutputText");
237            displayElement.setId("testId");
238            displayElement.addAttribute(attr);
239            displayElement.addSymbol(createSymbol("@value", "10"));
240                       
241            ClayContext clayContext = new ClayContext();
242            clayContext.setFacesContext(facesContext);
243            clayContext.setChild(child);
244            clayContext.setAttribute(attr);
245            clayContext.setDisplayElement(displayElement);
246            clayContext.setSymbols(displayElement.getSymbols());
247            
248            Command command = new PropertyValueCommand();
249            boolean isFinal = command.execute(clayContext);
250            assertEquals("command finished", true, isFinal);       
251            assertEquals("value = 10", "10", child.getValue());
252 
253            
254            // test a symbol value of an el value
255            child = (javax.faces.component.html.HtmlOutputText) 
256                         facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
257            assertNotNull("javax.faces.HtmlOutputText", child);
258 
259            displayElement.addSymbol(createSymbol("@value", "#{value}"));
260            attr.setBindingType(AttributeBean.BINDING_TYPE_EARLY);
261            servletContext.setAttribute("value", "10");
262           
263            clayContext.setFacesContext(facesContext);
264            clayContext.setChild(child);
265            clayContext.setAttribute(attr);
266            clayContext.setDisplayElement(displayElement);
267            clayContext.setSymbols(displayElement.getSymbols());
268            
269            isFinal = command.execute(clayContext);
270            assertEquals("command finished", true, isFinal);       
271            assertEquals("value = 10", "10", child.getValue());
272 
273            
274            // test a symbol value with a null value symbol replacement
275            child = (javax.faces.component.html.HtmlOutputText) 
276                  facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
277            assertNotNull("javax.faces.HtmlOutputText", child);
278 
279            displayElement.addSymbol(createSymbol("@value", null));
280            attr.setBindingType(AttributeBean.BINDING_TYPE_EARLY);
281           
282            clayContext.setFacesContext(facesContext);
283            clayContext.setChild(child);
284            clayContext.setAttribute(attr);
285            clayContext.setDisplayElement(displayElement);
286            clayContext.setSymbols(displayElement.getSymbols());
287            
288            isFinal = command.execute(clayContext);
289            assertEquals("command finished", true, isFinal);       
290            assertEquals("value = null", null, child.getValue());
291 
292 
293            // test a symbol value with an empty String value.  
294            // this will evaluate to null since it is a symbol replacement.
295            child = (javax.faces.component.html.HtmlOutputText) 
296                      facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
297            assertNotNull("javax.faces.HtmlOutputText", child);
298 
299            displayElement.addSymbol(createSymbol("@value", ""));
300            attr.setBindingType(AttributeBean.BINDING_TYPE_EARLY);
301           
302            clayContext.setFacesContext(facesContext);
303            clayContext.setChild(child);
304            clayContext.setAttribute(attr);
305            clayContext.setDisplayElement(displayElement);
306            clayContext.setSymbols(displayElement.getSymbols());
307            
308            isFinal = command.execute(clayContext);
309            assertEquals("command finished", true, isFinal);       
310            assertEquals("value = null", null, child.getValue());
311 
312            //no symbol replacement for a empty string - should return
313            //an empty string.  This allows components like the selectItem
314            //to create an empty select list pick.
315            attr.setValue("");  //empty string           
316            child = (javax.faces.component.html.HtmlOutputText) 
317                         facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
318            assertNotNull("javax.faces.HtmlOutputText", child);
319 
320            clayContext.setFacesContext(facesContext);
321            clayContext.setChild(child);
322            clayContext.setAttribute(attr);
323            clayContext.setDisplayElement(displayElement);
324            
325            isFinal = command.execute(clayContext);
326            assertEquals("command finished", true, isFinal);       
327            assertEquals("value = \"\"", "", child.getValue());
328 
329            //Case insensitive and reoccurring replacement
330            attr.setValue("@TeSt1, @tEst1 never @test2; @test1, @teSt1 till ya @tesT3");  //test multiple symbols           
331            child = (javax.faces.component.html.HtmlOutputText) 
332                      facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
333            assertNotNull("javax.faces.HtmlOutputText", child);
334 
335            displayElement.addSymbol(createSymbol("@test1", "rock"));
336            displayElement.addSymbol(createSymbol("@test2", "stop"));
337            displayElement.addSymbol(createSymbol("@test3", "drop"));
338 
339            clayContext.setFacesContext(facesContext);
340            clayContext.setChild(child);
341            clayContext.setAttribute(attr);
342            clayContext.setDisplayElement(displayElement);
343            // normally done in the AssignChildrenCommand
344            clayContext.setSymbols(displayElement.getSymbols());
345            
346            isFinal = command.execute(clayContext);
347            assertEquals("command finished", true, isFinal);       
348            assertEquals("value = \"rock, rock never stop; rock, rock till ya drop\"", 
349                    "rock, rock never stop; rock, rock till ya drop", child.getValue());
350 
351     }
352 
353     //test component creation using symbols for component id's 
354     public void testCreateComponent()throws Exception {
355         
356         UIComponent parent = (UIComponent) 
357               facesContext.getApplication().createComponent("javax.faces.NamingContainer"); 
358         assertNotNull("javax.faces.NamingContainer", parent);
359         parent.setId("parent");
360         
361         ComponentBean displayElement = new ComponentBean();
362         displayElement.setJsfid("inputText");
363         displayElement.setComponentType("javax.faces.HtmlOutputText");
364         displayElement.setId("@wynn");
365         displayElement.addSymbol(createSymbol("@wynn", "test"));
366         
367         ClayContext clayContext = new ClayContext();
368         clayContext.setFacesContext(facesContext);
369         clayContext.setParent(parent);
370         clayContext.setDisplayElement(displayElement);
371         clayContext.setSymbols(displayElement.getSymbols());
372         clayContext.setJspIds(new TreeSet());
373                 
374         Command command = new CreateComponentCommand();
375         boolean isFinal = command.execute(clayContext);
376         assertEquals("command finished", false, isFinal);
377         
378         UIComponent child = (UIComponent) clayContext.getChild();
379         assertNotNull("child", child);
380         
381         assertEquals("id = test", "test", child.getId());
382         
383         
384         //null component id symbol replacement
385         parent = (UIComponent) 
386              facesContext.getApplication().createComponent("javax.faces.NamingContainer"); 
387         assertNotNull("javax.faces.NamingContainer", parent);
388         parent.setId("parent");
389         
390         displayElement = new ComponentBean();
391         displayElement.setJsfid("inputText");
392         displayElement.setComponentType("javax.faces.HtmlOutputText");
393         displayElement.setId("@wynn");
394         displayElement.addSymbol(createSymbol("@wynn", null));
395         
396         clayContext = new ClayContext();
397         clayContext.setFacesContext(facesContext);
398         clayContext.setParent(parent);
399         clayContext.setDisplayElement(displayElement);
400         clayContext.setSymbols(displayElement.getSymbols());
401         clayContext.setJspIds(new TreeSet());
402                 
403         command = new CreateComponentCommand();
404         try {
405             isFinal = command.execute(clayContext);
406             assertTrue("id replacement failed", false);
407         } catch (RuntimeException e) {
408             assertTrue("null component id", 
409                     e.getMessage().startsWith("The component symbol substitution failed for id \"@wynn\""));    
410         }
411         
412         //missing component id symbol replacement
413         parent = (UIComponent) 
414              facesContext.getApplication().createComponent("javax.faces.NamingContainer"); 
415         assertNotNull("javax.faces.NamingContainer", parent);
416         parent.setId("parent");
417         
418         displayElement = new ComponentBean();
419         displayElement.setJsfid("inputText");
420         displayElement.setComponentType("javax.faces.HtmlOutputText");
421         displayElement.setId("@wynn");
422         
423         clayContext = new ClayContext();
424         clayContext.setFacesContext(facesContext);
425         clayContext.setParent(parent);
426         clayContext.setDisplayElement(displayElement);
427         clayContext.setSymbols(displayElement.getSymbols());
428         clayContext.setJspIds(new TreeSet());
429                 
430         command = new CreateComponentCommand();
431         try {
432             isFinal = command.execute(clayContext);
433             assertTrue("id replacement failed", false);
434         } catch (RuntimeException e) {
435             assertTrue("missing component id", 
436                     e.getMessage().startsWith("The component symbol substitution failed for id \"@wynn\""));    
437         }
438 
439         
440     }
441     
442  
443     public void testSymbolDelimiters() throws Exception {
444 
445         //create a target component
446         javax.faces.component.html.HtmlOutputText child = (javax.faces.component.html.HtmlOutputText) 
447                            facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
448         assertNotNull("javax.faces.HtmlOutputText", child);
449         
450         //setup some metadata
451         AttributeBean attr = new AttributeBean();
452         attr.setName("value");
453         attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
454         attr.setValue("@[a]@[ab]");  //symbolic attribute 
455                    
456         ComponentBean displayElement = new ComponentBean();
457         displayElement.setJsfid("inputText");
458         displayElement.setComponentType("javax.faces.HtmlOutputText");
459         displayElement.setId("testId");
460         displayElement.addAttribute(attr);
461         displayElement.addSymbol(createSymbol("@[ab]", "43"));
462         displayElement.addSymbol(createSymbol("@[a]", "67"));
463              
464         ClayContext clayContext = new ClayContext();
465         clayContext.setFacesContext(facesContext);
466         clayContext.setChild(child);
467         clayContext.setAttribute(attr);
468         clayContext.setDisplayElement(displayElement);
469         // normally done in the AssignChildrenCommand
470         clayContext.setSymbols(displayElement.getSymbols());
471                 
472         Command command = new PropertyValueCommand();
473         boolean isFinal = command.execute(clayContext);
474         assertEquals("command finished", true, isFinal);       
475         assertEquals("value = 6743", "6743", child.getValue());      
476 
477     
478         //create a target component
479         child = (javax.faces.component.html.HtmlOutputText) 
480                            facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
481         assertNotNull("javax.faces.HtmlOutputText", child);
482 
483         attr.setValue("@{a}@{ab}");  //symbolic attribute 
484         displayElement.addSymbol(createSymbol("@{ab}", "43"));
485         displayElement.addSymbol(createSymbol("@{a}", "67"));
486 
487         clayContext.setChild(child);
488 
489         isFinal = command.execute(clayContext);
490         assertEquals("command finished", true, isFinal);       
491         assertEquals("value = 6743", "6743", child.getValue());      
492 
493 
494         
495         //create a target component
496         child = (javax.faces.component.html.HtmlOutputText) 
497                            facesContext.getApplication().createComponent("javax.faces.HtmlOutputText"); 
498         assertNotNull("javax.faces.HtmlOutputText", child);
499 
500         attr.setValue("@(a)@(ab)");  //symbolic attribute 
501         displayElement.addSymbol(createSymbol("@(ab)", "43"));
502         displayElement.addSymbol(createSymbol("@(a)", "67"));
503 
504         clayContext.setChild(child);
505 
506         isFinal = command.execute(clayContext);
507         assertEquals("command finished", true, isFinal);       
508         assertEquals("value = 6743", "6743", child.getValue());      
509         
510         
511     }
512      
513     //test symbol inheritance
514     public void testSymbolInheritance() {
515 
516         //loads the default and the custom address config files
517         loadConfigFile("/org/apache/shale/clay/config/address-config.xml");
518           
519         // test vertical inheritance
520         ComponentBean bean = standardConfigBean.getElement("baseSymbolLabel");
521         assertNotNull(bean);
522         //look for a base symbol definition
523         SymbolBean symbol = (SymbolBean) bean.getSymbols().get("@mystyle");
524         assertNotNull(symbol);
525         assertEquals("@mystyle == color:blue", "color:blue", symbol.getValue());
526 
527         // symbol1Label extends baseSymbolLabel
528         bean = standardConfigBean.getElement("symbol1Label");
529         assertNotNull(bean);
530         //look for inherited symbol
531         symbol = (SymbolBean) bean.getSymbols().get("@mystyle");
532         assertNotNull(symbol);
533         assertEquals("@mystyle == color:blue", "color:blue", symbol.getValue());
534 
535         // symbol2Label extends symbol1Label
536         bean = standardConfigBean.getElement("symbol2Label");
537         assertNotNull(bean);
538         //look for an overridden symbol
539         symbol = (SymbolBean) bean.getSymbols().get("@mystyle");
540         assertNotNull(symbol);
541         assertEquals("@mystyle == color:red", "color:red", symbol.getValue());
542 
543         
544         //test nested/inner element inheritance
545         bean = standardConfigBean.getElement("symbolPanel");
546         assertNotNull(bean);
547         
548         assertEquals(bean.getChildren().size(), 2);
549         Iterator ei = bean.getChildrenIterator();
550         while (ei.hasNext()) {
551             ElementBean ebean = (ElementBean) ei.next();
552             if (ebean.getRenderId() == 1) {
553                 //look for inherited symbol
554                 symbol = (SymbolBean) ebean.getSymbols().get("@mystyle");
555                 assertNotNull(symbol);
556                 assertEquals("@mystyle == color:blue", "color:blue", symbol.getValue());                
557             } else if (ebean.getRenderId() == 2) {
558                 //look for an overridden symbol
559                 symbol = (SymbolBean) ebean.getSymbols().get("@mystyle");
560                 assertNotNull(symbol);
561                 assertEquals("@mystyle == color:red", "color:red", symbol.getValue());
562                 
563             }
564         }
565         
566                
567     }
568 
569     
570 }