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.render;
20  
21  
22  import javax.faces.component.NamingContainer;
23  import javax.faces.context.FacesContext;
24  import javax.faces.render.Renderer;
25  
26  import junit.framework.Test;
27  import junit.framework.TestCase;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.trinidad.component.UIXForm;
31  import org.apache.myfaces.trinidad.component.UIXInput;
32  import org.apache.myfaces.trinidad.component.UIXPanel;
33  
34  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
35  
36  public class RenderUtilsTest extends FacesTestCase
37  {
38    public static final Test suite()
39    {
40      return new TestSuite(RenderUtilsTest.class);
41    }
42  
43    public static void main(String[] args) throws Throwable
44    {
45      junit.textui.TestRunner.run(suite());
46    }
47  
48    public RenderUtilsTest(
49      String testName)
50    {
51      super(testName);
52    }
53  
54    static private class TestNamingContainer extends UIXPanel
55                                             implements NamingContainer
56    {
57      @Override
58      public String getClientId(FacesContext context)
59      {
60        // NOTE - client ids cannot be cached because the generated
61        // value has to be dynamically calculated in some cases (UIData)
62  
63        String clientId = getId() + "_Client";
64        return clientId;
65      }
66      
67      public String getContainerClientId(FacesContext p1)
68      {
69        return getId();
70      }
71      
72      protected Renderer getRenderer(FacesContext context)
73      {
74        return null;
75      }
76    }
77    
78    static private class TestUIXPanel extends UIXPanel
79    {
80  
81      
82      protected Renderer getRenderer(FacesContext context)
83      {
84        return null;
85      }
86    }
87  
88  
89    // Test sibling components where one is a table and one is a button
90    @SuppressWarnings("unchecked")
91    public void testButtonAndNamingContainerSiblings()
92    {
93      FacesContext context = FacesContext.getCurrentInstance();
94  
95        // rootPanel
96        //     button1
97        //     table1
98        TestUIXPanel button1 = new TestUIXPanel();
99        button1.setId("commandButton1");
100       TestNamingContainer table1 = new TestNamingContainer();
101       table1.setId("table1");
102       TestUIXPanel rootPanel = new TestUIXPanel();
103       rootPanel.setId("rootPanel");
104       
105       context.getViewRoot().getChildren().add(rootPanel);
106       
107       rootPanel.getChildren().add(button1);
108       rootPanel.getChildren().add(table1);
109       TestUIXPanel tableChild = new TestUIXPanel();
110       tableChild.setId("tableChildId");
111       table1.getChildren().add(tableChild);
112     
113     String relativeId =
114       RenderUtils.getRelativeId(context, button1, "table1");
115     assertEquals("table1", relativeId);
116     
117     relativeId =
118       RenderUtils.getRelativeId(context, button1, ":table1");
119     assertEquals("table1", relativeId);
120     
121     // new way would find nothing, so we'd have to get something logical
122     relativeId =
123       RenderUtils.getRelativeId(context, table1, "someRandomId");
124     assertEquals("table1_Client:someRandomId", relativeId);
125     
126     relativeId =
127       RenderUtils.getRelativeId(context, table1, ":commandButton1");
128     assertEquals("commandButton1", relativeId);
129 
130     // to get to the commandButton from the table, you need to pop out of the
131     // table
132     relativeId =
133       RenderUtils.getRelativeId(context, table1, "::commandButton1");
134     assertEquals("commandButton1", relativeId);
135     
136     // backward compatibility test -- this was the old syntax for siblings to the table.
137     // this should be found by looking at the nc's parent from findRelativeComponent
138     relativeId =
139       RenderUtils.getRelativeId(context, table1, "commandButton1");
140     assertEquals("commandButton1", relativeId);
141        
142     // backward compatibility test -- this was the old syntax for children to the table.
143     relativeId =
144       RenderUtils.getRelativeId(context, table1, "table1:tableChildId");
145     assertEquals("table1:tableChildId", relativeId);
146      // this is the new syntax for children to the table
147     relativeId =
148       RenderUtils.getRelativeId(context, table1, "tableChildId");
149     assertEquals("table1:tableChildId", relativeId);
150 
151   }
152 
153 
154 
155 
156   @SuppressWarnings("unchecked")
157   public void testRelativeSearch()
158   {
159     FacesContext context = FacesContext.getCurrentInstance();
160 
161 
162       // set up component hierarchy
163       UIXForm form = new UIXForm(); form.setId("formId");
164       TestNamingContainer ncRoot = new TestNamingContainer(); ncRoot.setId("ncRoot");
165       TestUIXPanel button1 = new TestUIXPanel();
166       button1.setId("button1");
167       TestUIXPanel button2 = new TestUIXPanel();
168       button2.setId("button2");
169       TestUIXPanel rootButton = new TestUIXPanel();
170       rootButton.setId("rootButton");
171 
172       context.getViewRoot().getChildren().add(form);
173 
174       form.getChildren().add(ncRoot);
175       form.getChildren().add(rootButton);
176       ncRoot.getChildren().add(button1);
177       ncRoot.getChildren().add(button2);
178 
179       TestNamingContainer nc1 = new TestNamingContainer();
180       nc1.setId("nc1");
181 
182           
183       UIXInput inputA = new UIXInput(); inputA.setId("inputA");
184       UIXPanel panel1 = new UIXPanel(); panel1.setId("panel1");
185       UIXInput input1 = new UIXInput(); input1.setId("input1");
186       ncRoot.getChildren().add(nc1);
187       nc1.getChildren().add(inputA);
188       nc1.getChildren().add(panel1);
189       panel1.getChildren().add(input1);
190       
191     /*<f:subview id="ncRoot">
192      *  <commandButton1>
193      *  <commandButton2>
194      *   <f:subview id="nc1">
195      *     <tr:inputText id="inputA" pT="::commandButton1"/>
196            <tr:panelGroupLayout>
197              <tr:inputText
198              id="input1"
199              for="::commandButton1"/>
200            </tr:panelGroupLayout>
201           </f:subview>
202        </f:subview>
203        rootButton
204      */
205       
206     String relativeId =
207       RenderUtils.getRelativeId(context, input1, "::button1");
208     // new way should pop OUT of ONE naming container and will find it
209     assertEquals("ncRoot:button1", relativeId);
210 
211     
212     relativeId =
213       RenderUtils.getRelativeId(context, input1, ":::button1");
214     // new way should pop OUT of TWO naming containers and will find not find it
215     // since it is in ncRoot and the base is now the view root.
216     // so it goes to the old findRelativeComponent, and this will find it.
217     assertEquals("ncRoot:button1", relativeId);
218 
219 
220     relativeId =
221       RenderUtils.getRelativeId(context, input1, "randomPeer");
222     // randomPeer doesn't exist, so new way won't find it.
223     // uses code that doesn't need to find the component to return this:
224     assertEquals("nc1_Client:randomPeer", relativeId);
225     
226     relativeId =
227       RenderUtils.getRelativeId(context, input1, "::randomPeer");
228     // randomPeer doesn't exist, so new way won't find it.
229     // uses code that doesn't need to find the component to return this:
230     assertEquals("ncRoot_Client:randomPeer", relativeId);
231  
232     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
233     relativeId =
234       RenderUtils.getRelativeId(context, input1, ":::rootButton");
235     // new way should pop OUT of both NC with ::: and will find it
236     assertEquals("rootButton", relativeId);
237 
238  
239     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
240     relativeId =
241       RenderUtils.getRelativeId(context, input1, "::rootButton");
242     // new way should pop OUT of one NC with ::, so it can't find it
243     // the 'old' findRelativeComponent can't find it either.
244     // so it returns what the old getRelativeId would have returned
245     // old way goes up to the current naming container nc1
246     // old 'new' way will return nc1_Root:rootButton
247     assertEquals("ncRoot_Client:rootButton", relativeId);
248 
249     
250     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
251     relativeId =
252       RenderUtils.getRelativeId(context, input1, "::::rootButton");
253     // new way should pop OUT of ALL NCs and will find it.
254     assertEquals("rootButton", relativeId);
255     
256 
257     
258     relativeId =
259       RenderUtils.getRelativeId(context, input1, "::::button1");
260     // new way should return this
261     assertEquals("button1", relativeId);
262     
263     relativeId =
264       RenderUtils.getRelativeId(context, input1, ":::::button1");
265     assertEquals("button1", relativeId);
266   }
267 
268 
269 
270 }