2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30 public class OptimizeTreeTestCase extends AbstractTestCaseConfig {
31
32
33 public OptimizeTreeTestCase(String name) {
34 super(name);
35 }
36
37
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
100
101
102
103
104
105
106
107
108
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
139
140
141
142
143
144
145
146
147
148
149 AttributeBean attr = null;
150 ElementBean child = null;
151 ComponentBean root = createVerbatim(ComponentBean.class, "0");
152
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
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
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
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
210 attr = child.getAttribute("value");
211 assertNotNull(attr);
212 assertEquals("root value", "12", attr.getValue());
213 break;
214 }
215 case 2: {
216
217 attr = child.getAttribute("value");
218 assertNotNull(attr);
219 assertEquals("root value", "3", attr.getValue());
220 break;
221 }
222 case 3: {
223
224 attr = child.getAttribute("value");
225 assertNotNull(attr);
226 assertEquals("root value", "4", attr.getValue());
227 break;
228 }
229 case 4: {
230
231 attr = child.getAttribute("value");
232 assertNotNull(attr);
233 assertEquals("root value", "5", attr.getValue());
234 break;
235 }
236 case 5: {
237
238 attr = child.getAttribute("value");
239 assertNotNull(attr);
240 assertEquals("root value", "6", attr.getValue());
241 break;
242 }
243 case 6: {
244
245 attr = child.getAttribute("value");
246 assertNotNull(attr);
247 assertEquals("root value", "7", attr.getValue());
248 break;
249 }
250 case 7: {
251
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 }