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       rootPanel.getChildren().add(button1);
105       rootPanel.getChildren().add(table1);
106       TestUIXPanel tableChild = new TestUIXPanel();
107       tableChild.setId("tableChildId");
108       table1.getChildren().add(tableChild);
109     
110     String relativeId =
111       RenderUtils.getRelativeId(context, button1, "table1");
112     assertEquals("table1", relativeId);
113     
114     relativeId =
115       RenderUtils.getRelativeId(context, button1, ":table1");
116     assertEquals("table1", relativeId);
117     
118     // new way would find nothing, so we'd have to get something logical
119     relativeId =
120       RenderUtils.getRelativeId(context, table1, "someRandomId");
121     assertEquals("table1_Client:someRandomId", relativeId);
122     
123     relativeId =
124       RenderUtils.getRelativeId(context, table1, ":commandButton1");
125     assertEquals("commandButton1", relativeId);
126 
127     // to get to the commandButton from the table, you need to pop out of the
128     // table
129     relativeId =
130       RenderUtils.getRelativeId(context, table1, "::commandButton1");
131     assertEquals("commandButton1", relativeId);
132     
133     // backward compatibility test -- this was the old syntax for siblings to the table.
134     // this should be found by looking at the nc's parent from findRelativeComponent
135     relativeId =
136       RenderUtils.getRelativeId(context, table1, "commandButton1");
137     assertEquals("commandButton1", relativeId);
138        
139     // backward compatibility test -- this was the old syntax for children to the table.
140     relativeId =
141       RenderUtils.getRelativeId(context, table1, "table1:tableChildId");
142     assertEquals("table1:tableChildId", relativeId);
143      // this is the new syntax for children to the table
144     relativeId =
145       RenderUtils.getRelativeId(context, table1, "tableChildId");
146     assertEquals("table1:tableChildId", relativeId);
147 
148   }
149 
150 
151 
152 
153   @SuppressWarnings("unchecked")
154   public void testRelativeSearch()
155   {
156     FacesContext context = FacesContext.getCurrentInstance();
157 
158 
159       // set up component hierarchy
160       UIXForm form = new UIXForm(); form.setId("formId");
161       TestNamingContainer ncRoot = new TestNamingContainer(); ncRoot.setId("ncRoot");
162       TestUIXPanel button1 = new TestUIXPanel();
163       button1.setId("button1");
164       TestUIXPanel button2 = new TestUIXPanel();
165       button2.setId("button2");
166       TestUIXPanel rootButton = new TestUIXPanel();
167       rootButton.setId("rootButton");
168 
169       form.getChildren().add(ncRoot);
170       form.getChildren().add(rootButton);
171       ncRoot.getChildren().add(button1);
172       ncRoot.getChildren().add(button2);
173 
174       TestNamingContainer nc1 = new TestNamingContainer();
175       nc1.setId("nc1");
176 
177           
178       UIXInput inputA = new UIXInput(); inputA.setId("inputA");
179       UIXPanel panel1 = new UIXPanel(); panel1.setId("panel1");
180       UIXInput input1 = new UIXInput(); input1.setId("input1");
181       ncRoot.getChildren().add(nc1);
182       nc1.getChildren().add(inputA);
183       nc1.getChildren().add(panel1);
184       panel1.getChildren().add(input1);
185       
186     /*<f:subview id="ncRoot">
187      *  <commandButton1>
188      *  <commandButton2>
189      *   <f:subview id="nc1">
190      *     <tr:inputText id="inputA" pT="::commandButton1"/>
191            <tr:panelGroupLayout>
192              <tr:inputText
193              id="input1"
194              for="::commandButton1"/>
195            </tr:panelGroupLayout>
196           </f:subview>
197        </f:subview>
198        rootButton
199      */
200       
201     String relativeId =
202       RenderUtils.getRelativeId(context, input1, "::button1");
203     // new way should pop OUT of ONE naming container and will find it
204     assertEquals("ncRoot:button1", relativeId);
205 
206     
207     relativeId =
208       RenderUtils.getRelativeId(context, input1, ":::button1");
209     // new way should pop OUT of TWO naming containers and will find not find it
210     // since it is in ncRoot and the base is now the view root.
211     // so it goes to the old findRelativeComponent, and this will find it.
212     assertEquals("ncRoot:button1", relativeId);
213 
214 
215     relativeId =
216       RenderUtils.getRelativeId(context, input1, "randomPeer");
217     // randomPeer doesn't exist, so new way won't find it.
218     // uses code that doesn't need to find the component to return this:
219     assertEquals("nc1_Client:randomPeer", relativeId);
220     
221     relativeId =
222       RenderUtils.getRelativeId(context, input1, "::randomPeer");
223     // randomPeer doesn't exist, so new way won't find it.
224     // uses code that doesn't need to find the component to return this:
225     assertEquals("ncRoot_Client:randomPeer", relativeId);
226  
227     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
228     relativeId =
229       RenderUtils.getRelativeId(context, input1, ":::rootButton");
230     // new way should pop OUT of both NC with ::: and will find it
231     assertEquals("rootButton", relativeId);
232 
233  
234     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
235     relativeId =
236       RenderUtils.getRelativeId(context, input1, "::rootButton");
237     // new way should pop OUT of one NC with ::, so it can't find it
238     // the 'old' findRelativeComponent can't find it either.
239     // so it returns what the old getRelativeId would have returned
240     // old way goes up to the current naming container nc1
241     // old 'new' way will return nc1_Root:rootButton
242     assertEquals("ncRoot_Client:rootButton", relativeId);
243 
244     
245     // rootButton is child of form and sibling to ncRoot. It's 2 nc up from input1
246     relativeId =
247       RenderUtils.getRelativeId(context, input1, "::::rootButton");
248     // new way should pop OUT of ALL NCs and will find it.
249     assertEquals("rootButton", relativeId);
250     
251 
252     
253     relativeId =
254       RenderUtils.getRelativeId(context, input1, "::::button1");
255     // new way should return this
256     assertEquals("button1", relativeId);
257     
258     relativeId =
259       RenderUtils.getRelativeId(context, input1, ":::::button1");
260     assertEquals("button1", relativeId);
261   }
262 
263 
264 
265 }