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 junit.framework.Test;
22  import junit.framework.TestCase;
23  import junit.framework.TestSuite;
24  
25  import org.apache.myfaces.trinidad.component.UIXPanel;
26  
27  public class AddRemoveTest extends TestCase
28  {
29    public static final Test suite()
30    {
31      return new TestSuite(AddRemoveTest.class);
32    }
33    
34    public static void main(String[] args) throws Throwable
35    {
36      junit.textui.TestRunner.run(suite());
37    }
38  
39    public AddRemoveTest(
40      String testName)
41    {
42      super(testName);
43    }
44  
45    @SuppressWarnings("unchecked")
46    public void testAddChild()
47    {
48      UIXPanel a = new UIXPanel();
49      UIXPanel b = new UIXPanel();
50      UIXPanel c = new UIXPanel();
51      UIXPanel d = new UIXPanel();
52      UIXPanel e = new UIXPanel();
53  
54      a.getChildren().add(b);
55      a.getChildren().add(c);
56      a.getFacets().put("dChild", d);
57  
58      assertEquals(b.getParent(), a);
59      assertEquals(c.getParent(), a);
60      assertEquals(d.getParent(), a);
61      assertEquals(a.getChildren().get(0), b);
62      assertEquals(a.getChildren().get(1), c);
63  
64      // Remove item 0, which had better be "b"
65      a.getChildren().remove(0);
66      assertEquals(b.getParent(), null);
67      assertEquals(c.getParent(), a);
68      assertEquals(a.getChildren().get(0), c);
69  
70      // Now add "d" to "e", and verify it's in "e", but not in "d"
71      e.getChildren().add(d);
72      assertEquals(null, a.getFacets().get("dChild"));
73      assertEquals(e, d.getParent());
74      
75      // Now move "c" over to "e" too
76      e.getChildren().add(c);
77      assertEquals(0, a.getChildren().size());
78      assertEquals(2, e.getChildren().size());
79      assertEquals(e, c.getParent());
80  
81      //Make sure the children are in the right order under "e" 
82      assertEquals(e.getChildren().get(0), d);
83      assertEquals(e.getChildren().get(1), c);
84    }
85  
86    @SuppressWarnings("unchecked")
87    public void testAddFacet()
88    {
89      UIXPanel a = new UIXPanel();
90      UIXPanel b = new UIXPanel();
91      UIXPanel c = new UIXPanel();
92      UIXPanel d = new UIXPanel();
93      UIXPanel e = new UIXPanel();
94  
95      a.getChildren().add(b);
96      a.getChildren().add(c);
97      a.getFacets().put("dChild", d);
98  
99      // Now add "d" to "e", and verify it's in "e", but not in "d"
100     e.getFacets().put("otherChild", d);
101     assertEquals(null, a.getFacets().get("dChild"));
102     assertEquals(e, d.getParent());
103     
104     // Now move "c" over to "e" too, and make sure it's gone from a.
105     e.getFacets().put("otherOtherChild", c);
106     assertEquals(e, c.getParent());
107     assertEquals(1, a.getChildren().size());
108     assertEquals(b, a.getChildren().get(0));
109   }
110 
111   @SuppressWarnings("unchecked")
112   public void testMoveFromFacetToChild()
113   {
114     UIXPanel a = new UIXPanel();
115     UIXPanel b = new UIXPanel();
116     a.getChildren().add(b);
117 
118     a.getFacets().put("child", b);
119     assertEquals(a, b.getParent());
120     assertEquals(0, a.getChildren().size());
121     assertEquals(b, a.getFacets().get("child"));
122   }
123 
124   @SuppressWarnings("unchecked")
125   public void testMoveFromChildToFacet()
126   {
127     UIXPanel a = new UIXPanel();
128     UIXPanel b = new UIXPanel();
129     a.getFacets().put("child", b);
130 
131 
132     a.getChildren().add(b);
133     assertEquals(a, b.getParent());
134     assertEquals(1, a.getChildren().size());
135     assertEquals(b, a.getChildren().get(0));
136     assertEquals(null, a.getFacets().get("child"));
137   }
138 }