View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   */
19  package org.apache.myfaces.trinidad.util;
20  
21  import javax.faces.component.NamingContainer;
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.UIForm;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.trinidad.component.UIXCommand;
29  import org.apache.myfaces.trinidad.component.UIXInput;
30  import org.apache.myfaces.trinidad.component.UIXPanel;
31  import org.apache.myfaces.trinidad.component.UIXTable;
32  
33  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
34  
35  public class FindRelativeComponentTest extends FacesTestCase
36  {
37    public static final Test suite()
38    {
39      return new TestSuite(FindRelativeComponentTest.class);
40    }
41    
42    public static void main(String[] args) throws Throwable
43    {
44      junit.textui.TestRunner.run(suite());
45    }
46  
47    public FindRelativeComponentTest(
48      String testName)
49    {
50      super(testName);
51    }
52  
53    static private class TestNamingContainer extends UIXPanel
54                                             implements NamingContainer
55    {
56  
57    }
58  
59    
60    // Test sibling components
61    @SuppressWarnings("unchecked")
62    public void testSiblingButtons()
63    {
64  
65        UIXCommand button1 = new UIXCommand();
66        button1.setId("commandButton1");
67        UIXCommand button2 = new UIXCommand();
68        button2.setId("commandButton2");
69        UIXPanel rootPanel = new UIXPanel();
70        rootPanel.getChildren().add(button1);
71        rootPanel.getChildren().add(button2);
72        
73        UIComponent cmp = 
74            ComponentUtils.findRelativeComponent(button2, "commandButton1");
75        // old and new are the same
76        assertEquals(cmp, button1);
77        
78        cmp = ComponentUtils.findRelativeComponent(button2, "::::::commandButton1");
79        // old and new are the same
80        assertEquals(cmp, button1);
81  
82      
83    }
84      
85    // Test sibling components where one is a table and one is a button
86    @SuppressWarnings("unchecked")
87    public void testSiblingWithTable()
88    {
89        // panel
90        //    table1
91        //       tableChild
92        //    button1 (table1 & button1 peers)
93        UIXCommand button1 = new UIXCommand();
94        button1.setId("commandButton1");
95    
96        UIXTable table1 = new UIXTable();
97        table1.setId("table1");
98        UIXPanel rootPanel = new UIXPanel();
99        rootPanel.getChildren().add(button1);
100       rootPanel.getChildren().add(table1);
101       UIXPanel tableChild = new UIXPanel();
102       tableChild.setId("tableChildId");
103       table1.getChildren().add(tableChild);
104       
105       
106       UIComponent cmp =
107         ComponentUtils.findRelativeComponent(table1,"::commandButton1");
108        // new & old (because  in old it starts from the viewRoot)
109       assertEquals(button1, cmp);
110       
111       cmp = ComponentUtils.findRelativeComponent(table1, "commandButton1");
112       // old & new are the same
113       // uses findComponent rules if it doesn't start with multiple colons
114       // - if this UIComponent is a NamingContainer it will serve as the basis.
115       assertEquals(null, cmp);
116       
117     cmp = ComponentUtils.findRelativeComponent(button1, "table1");
118     assertEquals(table1, cmp);
119     
120     cmp = ComponentUtils.findRelativeComponent(button1, "tableChildId");
121     assertEquals(null, cmp);
122     
123     cmp = ComponentUtils.findRelativeComponent(button1, "table1:tableChildId");
124     assertEquals(tableChild, cmp);
125     
126     cmp = ComponentUtils.findRelativeComponent(table1, "tableChildId");
127     assertEquals(tableChild, cmp);
128     
129     cmp = ComponentUtils.findRelativeComponent(tableChild, "table1");
130     assertEquals(table1, cmp);
131     
132     cmp = ComponentUtils.findRelativeComponent(tableChild, ":commandButton1");
133     assertEquals(button1, cmp);
134     
135     cmp = ComponentUtils.findRelativeComponent(tableChild, ":::commandButton1");
136     assertEquals(button1, cmp);
137     
138     cmp = ComponentUtils.findRelativeComponent(tableChild, "::commandButton1");   
139     assertEquals(button1, cmp); // new way
140     //assertEquals(null, cmp); // old way
141     
142 
143   }
144     
145 
146 
147   @SuppressWarnings("unchecked")
148   public void testRelativeSearch()
149   {
150     /*<f:subview id="ncRoot">
151      *  <commandButton1>
152      *  <commandButton2>
153      *     <f:subview id="nc1">
154      *     <tr:inputText id="inputA" pT="::commandButton1"/>
155            <tr:panelGroupLayout>
156              <tr:inputText 
157              id="input1"
158              partialTriggers="::commandButton1"/>
159            </tr:panelGroupLayout>
160           </f:subview>
161        </f:subview>
162      */
163 
164     // set up component hierarchy
165     UIForm form = new UIForm();
166     TestNamingContainer ncRoot = new TestNamingContainer(); ncRoot.setId("ncRoot");
167     UIXCommand button1 = new UIXCommand();
168     button1.setId("commandButton1");
169     UIXCommand button2 = new UIXCommand();
170     button2.setId("commandButton2");
171     
172     form.getChildren().add(ncRoot);
173     ncRoot.getChildren().add(button1);
174     ncRoot.getChildren().add(button2);
175       
176     TestNamingContainer nc = new TestNamingContainer(); nc.setId("nc1");
177     UIXInput inputA = new UIXInput(); inputA.setId("inputA");
178     UIXPanel panel = new UIXPanel(); panel.setId("panel1");
179     UIXInput input1 = new UIXInput(); input1.setId("input1");  
180     ncRoot.getChildren().add(nc);
181     nc.getChildren().add(inputA);
182     nc.getChildren().add(panel);
183     panel.getChildren().add(input1);
184       
185     // input1's parent is panel. panel's parent is nc1 (::) goes there. ::: goes to ncRoot
186     // in old way. New way pops OUT of nc1 with '::'.
187     UIComponent cmp =
188       ComponentUtils.findRelativeComponent(input1,":::commandButton1");
189     assertEquals(button1, cmp); // old way
190     // assertEquals(null, cmp); // new way (popped too far), so the code looks the old way
191     
192     cmp = ComponentUtils.findRelativeComponent(input1, "::::ncRoot:commandButton1");
193     assertEquals(button1, cmp); // old way & new way
194     
195     cmp = ComponentUtils.findRelativeComponent(input1, ":::ncRoot:commandButton1");
196     assertEquals(button1, cmp); // old way
197     
198     
199     // inputA's parent is nc1. ::  will get you there. : will pop you out. /old way
200     // :: will pop you out of nc1
201     cmp = ComponentUtils.findRelativeComponent(inputA, ":::commandButton1");
202     assertEquals(button1, cmp); // old way
203     //assertEquals(null, cmp);   // new way
204     
205     cmp = ComponentUtils.findRelativeComponent(inputA, "::ncRoot:commandButton1");
206     //assertEquals(null, cmp); // old way
207     assertEquals(button1, cmp);   // new way    
208  
209   }
210 
211 
212 }
213 
214