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