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.component;
20  
21  import javax.faces.component.NamingContainer;
22  import javax.faces.context.FacesContext;
23  import javax.faces.render.Renderer;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
29  
30  import org.apache.myfaces.trinidad.component.UIXPanel;
31  
32  public class ClientIdCachingTest extends FacesTestCase
33  {
34    public static final Test suite()
35    {
36      return new TestSuite(ClientIdCachingTest.class);
37    }
38    
39    public static void main(String[] args) throws Throwable
40    {
41      junit.textui.TestRunner.run(suite());
42    }
43  
44    public ClientIdCachingTest(
45      String testName)
46    {
47      super(testName);
48    }
49  
50    static private class TestPanel extends UIXPanel
51    {
52      protected Renderer getRenderer(FacesContext context)
53      {
54        return null;
55      }
56    }
57  
58    static private class TestNamingContainer extends TestPanel
59                                             implements NamingContainer
60    {
61    }
62  
63    // Test nested NamingContainer callbacks
64    @SuppressWarnings("unchecked")
65    public void testBasic()
66    {
67      TestNamingContainer a = new TestNamingContainer(); a.setId("a");
68      TestNamingContainer b = new TestNamingContainer(); b.setId("b");
69      TestNamingContainer d = new TestNamingContainer(); d.setId("d");
70      TestPanel e = new TestPanel(); e.setId("e");
71      TestPanel g = new TestPanel(); g.setId("g");
72      a.getChildren().add(b);
73      b.getChildren().add(d);
74      b.getChildren().add(g);
75      d.getChildren().add(e);
76      
77      FacesContext context = FacesContext.getCurrentInstance();
78      assertEquals("a:b:d:e", e.getClientId(context));
79    }
80    
81    public void testSetId()
82    {
83      TestNamingContainer a = new TestNamingContainer(); a.setId("a");
84      TestNamingContainer b = new TestNamingContainer(); b.setId("b");
85      TestNamingContainer d = new TestNamingContainer(); d.setId("d");
86      TestPanel e = new TestPanel(); e.setId("e");
87      TestPanel g = new TestPanel(); g.setId("g");
88      a.getChildren().add(b);
89      b.getChildren().add(d);
90      b.getChildren().add(g);
91      d.getChildren().add(e);
92  
93      // prime    
94      FacesContext context = FacesContext.getCurrentInstance();
95      assertEquals("a:b:d:e", e.getClientId(context));
96  
97      // set the component's id using accessor
98      e.setId("ePrime");
99      assertEquals("a:b:d:ePrime", e.getClientId(context));
100     
101     // set the component's id using attribute map
102     e.getAttributes().put("id", "eDoublePrime");
103     assertEquals("a:b:d:eDoublePrime", e.getClientId(context));
104     
105 
106     // set an ancsestor's id using accessor
107     b.setId("bPrime");
108     assertEquals("a:bPrime:d:eDoublePrime", e.getClientId(context));
109     
110     // set the component's id using attribute map
111     b.getAttributes().put("id", "bDoublePrime");
112     assertEquals("a:bDoublePrime:d:eDoublePrime", e.getClientId(context));
113   }
114 
115   public void testMoving()
116   {
117     TestNamingContainer a = new TestNamingContainer(); a.setId("a");
118     TestNamingContainer b = new TestNamingContainer(); b.setId("b");
119     TestNamingContainer c = new TestNamingContainer(); c.setId("c");
120     TestNamingContainer d = new TestNamingContainer(); d.setId("d");
121     TestPanel e = new TestPanel(); e.setId("e");
122     TestPanel f = new TestPanel(); f.setId("f");
123     TestPanel g = new TestPanel(); g.setId("g");
124     a.getChildren().add(b);
125     a.getChildren().add(c);
126     b.getChildren().add(d);
127     b.getChildren().add(g);
128     d.getChildren().add(e);
129     d.getChildren().add(f);
130 
131     // prime    
132     FacesContext context = FacesContext.getCurrentInstance();
133     assertEquals("a:b:d:e", e.getClientId(context));
134 
135     // move within same NamingContainer--no clientId change
136     f.getChildren().add(e);
137     assertEquals("a:b:d:e", e.getClientId(context));
138     
139     // move between NamingContainers
140     g.getChildren().add(e);
141     assertEquals("a:b:e", e.getClientId(context)); 
142   }
143 }