2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.clay.config;
18  
19  import java.util.Iterator;
20  
21  import junit.framework.Test;
22  import junit.framework.TestSuite;
23  
24  import org.apache.shale.clay.config.beans.AttributeBean;
25  import org.apache.shale.clay.config.beans.ComponentBean;
26  import org.apache.shale.clay.config.beans.ElementBean;
27  import org.apache.shale.clay.config.beans.TemplateConfigBean;
28  
29  // tests squeezing the component tree
30  public class OptimizeTreeTestCase extends AbstractTestCaseConfig {
31  
32      // Construct a new instance of this test case.
33      public OptimizeTreeTestCase(String name) {
34          super(name);
35      }
36  
37      // Return the tests included in this test case.
38      public static Test suite() {
39  
40          return (new TestSuite(OptimizeTreeTestCase.class));
41  
42      }
43  
44      protected void setUp() throws Exception {
45          super.setUp();
46      }
47  
48      private ComponentBean createVerbatim(Class classz, String value)
49              throws InstantiationException, IllegalAccessException {
50  
51          ComponentBean target = (ComponentBean) classz.newInstance();
52          target.setJsfid("verbatim");
53          target.setComponentType("javax.faces.HtmlOutputText");
54  
55          AttributeBean attr = new AttributeBean();
56          attr.setBindingType(AttributeBean.BINDING_TYPE_VALUE);
57          attr.setName("value");
58          attr.setValue(value);
59          target.addAttribute(attr);
60  
61          attr = new AttributeBean();
62          attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
63          attr.setName("escape");
64          attr.setValue(Boolean.FALSE.toString());
65          target.addAttribute(attr);
66  
67          attr = new AttributeBean();
68          attr.setBindingType(AttributeBean.BINDING_TYPE_NONE);
69          attr.setName("isTransient");
70          attr.setValue(Boolean.TRUE.toString());
71          target.addAttribute(attr);
72  
73          return target;
74      }
75  
76      public void testRollup() throws Exception {
77  
78          ComponentBean root = createVerbatim(ComponentBean.class, "0");
79          for (int i = 1; i < 10; i++) {
80              ElementBean child = (ElementBean) createVerbatim(ElementBean.class,
81                      String.valueOf(i));
82              child.setRenderId(i);
83              root.addChild(child);
84          }
85  
86          ((TemplateConfigBean) htmlTemplateConfigBean).optimizeTree(root);
87  
88          assertEquals("#Children", 0, root.getChildren().size());
89  
90          AttributeBean attr = root.getAttribute("value");
91          assertNotNull(attr);
92  
93          assertEquals("root value", "0123456789", attr.getValue());
94  
95      }
96  
97      public void testRollupNested() throws Exception {
98  
99          // root 0
100         // + 1
101         // + 2
102         // + 3
103         // + 4
104         //   + 5
105         //   + 6
106         //   + 7
107         //   + 8
108         //   + 9
109         ComponentBean root = createVerbatim(ComponentBean.class, "0");
110         ElementBean lastChild = null;
111         for (int i = 1; i < 5; i++) {
112             lastChild = (ElementBean) createVerbatim(ElementBean.class, String
113                     .valueOf(i));
114             lastChild.setRenderId(i);
115             root.addChild(lastChild);
116         }
117 
118         for (int i = 5; i < 10; i++) {
119             ElementBean child = (ElementBean) createVerbatim(ElementBean.class,
120                     String.valueOf(i));
121             child.setRenderId(i);
122             lastChild.addChild(child);
123         }
124 
125         ((TemplateConfigBean) htmlTemplateConfigBean).optimizeTree(root);
126 
127         assertEquals("#Children", 0, root.getChildren().size());
128 
129         AttributeBean attr = root.getAttribute("value");
130         assertNotNull(attr);
131 
132         assertEquals("root value", "0123456789", attr.getValue());
133 
134     }
135 
136     public void testInterwoven() throws Exception {
137 
138         // 0
139         // +1
140         // +2
141         // +3 not verbatim
142         // +4
143         // +5 not verbatim
144         // +6
145         // +7 not verbatim
146         // +8
147         // +9
148 
149         AttributeBean attr = null;
150         ElementBean child = null;
151         ComponentBean root = createVerbatim(ComponentBean.class, "0");
152         // root is not a verbatim
153         root.setJsfid("outputText");
154 
155         child = (ElementBean) createVerbatim(ElementBean.class, "1");
156         child.setRenderId(1);
157         root.addChild(child);
158         
159 
160         child = (ElementBean) createVerbatim(ElementBean.class, "2");
161         child.setRenderId(2);
162         root.addChild(child);
163 
164         child = (ElementBean) createVerbatim(ElementBean.class, "3");
165         child.setRenderId(3);
166         // root is not a verbatim
167         child.setJsfid("outputText");
168         root.addChild(child);
169 
170         child = (ElementBean) createVerbatim(ElementBean.class, "4");
171         child.setRenderId(4);
172         root.addChild(child);
173 
174         child = (ElementBean) createVerbatim(ElementBean.class, "5");
175         child.setRenderId(5);
176         // root is not a verbatim
177         child.setJsfid("outputText");
178         root.addChild(child);
179 
180         child = (ElementBean) createVerbatim(ElementBean.class, "6");
181         child.setRenderId(6);
182         root.addChild(child);
183 
184         child = (ElementBean) createVerbatim(ElementBean.class, "7");
185         child.setRenderId(7);
186         // root is not a verbatim
187         child.setJsfid("outputText");
188         root.addChild(child);
189 
190         child = (ElementBean) createVerbatim(ElementBean.class, "8");
191         child.setRenderId(8);
192         root.addChild(child);
193 
194         child = (ElementBean) createVerbatim(ElementBean.class, "9");
195         child.setRenderId(9);
196         root.addChild(child);
197 
198         ((TemplateConfigBean) htmlTemplateConfigBean).optimizeTree(root);
199 
200         assertEquals("#Children", 7, root.getChildren().size());
201 
202         Iterator ci = root.getChildren().iterator();
203         int i = 0;
204         while (ci.hasNext()) {
205             child = (ElementBean) ci.next();
206 
207             switch (++i) {
208             case 1: {
209                 // first two nodes merged
210                 attr = child.getAttribute("value");
211                 assertNotNull(attr);
212                 assertEquals("root value", "12", attr.getValue());
213                 break;
214             }
215             case 2: {
216                 // non-verbatim
217                 attr = child.getAttribute("value");
218                 assertNotNull(attr);
219                 assertEquals("root value", "3", attr.getValue());
220                 break;
221             }
222             case 3: {
223                 // verbatim non-adjacent
224                 attr = child.getAttribute("value");
225                 assertNotNull(attr);
226                 assertEquals("root value", "4", attr.getValue());
227                 break;
228             }
229             case 4: {
230                 // non-verbatim
231                 attr = child.getAttribute("value");
232                 assertNotNull(attr);
233                 assertEquals("root value", "5", attr.getValue());
234                 break;
235             }
236             case 5: {
237                 // verbatim non-adjacent
238                 attr = child.getAttribute("value");
239                 assertNotNull(attr);
240                 assertEquals("root value", "6", attr.getValue());
241                 break;
242             }
243             case 6: {
244                 // non-verbatim
245                 attr = child.getAttribute("value");
246                 assertNotNull(attr);
247                 assertEquals("root value", "7", attr.getValue());
248                 break;
249             }
250             case 7: {
251                 // first two nodes merged
252                 attr = child.getAttribute("value");
253                 assertNotNull(attr);
254                 assertEquals("root value", "89", attr.getValue());
255                 break;
256             }
257             
258             };
259         }
260 
261     }
262 
263 }