1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.ui.template;
20
21 import java.io.StringWriter;
22
23 import javax.faces.component.UIViewRoot;
24
25 import org.apache.myfaces.test.mock.MockResponseWriter;
26 import org.apache.myfaces.view.facelets.FaceletTestCase;
27 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31 public class CompositionTestCase extends FaceletTestCase {
32
33 @Override
34 protected void setUpServletObjects() throws Exception
35 {
36 super.setUpServletObjects();
37 servletContext.addInitParameter(FaceletViewDeclarationLanguage.PARAM_SKIP_COMMENTS, "true");
38 }
39
40
41
42
43
44
45 @Test
46 public void testCompositionTrimEffect() throws Exception {
47 UIViewRoot root = facesContext.getViewRoot();
48 vdl.buildView(facesContext, root, "compositionTrim.xhtml");
49
50 StringWriter sw = new StringWriter();
51 MockResponseWriter mrw = new MockResponseWriter(sw);
52 facesContext.setResponseWriter(mrw);
53 root.encodeAll(facesContext);
54 sw.flush();
55
56 String response = sw.toString();
57 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
58 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
59 }
60
61
62
63
64
65
66
67
68
69
70 @Test
71 public void testComposition1() throws Exception {
72 UIViewRoot root = facesContext.getViewRoot();
73 vdl.buildView(facesContext, root, "composition1.xhtml");
74
75 StringWriter sw = new StringWriter();
76 MockResponseWriter mrw = new MockResponseWriter(sw);
77 facesContext.setResponseWriter(mrw);
78 root.encodeAll(facesContext);
79 sw.flush();
80
81 String response = sw.toString();
82 Assert.assertTrue(response.contains("fragmentNumber1"));
83 Assert.assertTrue(response.contains("compositionContent"));
84 Assert.assertFalse(response.contains("This fragment will not be inserted"));
85 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
86 }
87
88
89
90
91
92
93
94 @Test
95 public void testComposition2EmptyComposition() throws Exception {
96 UIViewRoot root = facesContext.getViewRoot();
97 vdl.buildView(facesContext, root, "composition2.xhtml");
98
99 StringWriter sw = new StringWriter();
100 MockResponseWriter mrw = new MockResponseWriter(sw);
101 facesContext.setResponseWriter(mrw);
102 root.encodeAll(facesContext);
103 sw.flush();
104
105 String response = sw.toString();
106 Assert.assertFalse(response.contains("This fragment will not be inserted"));
107 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
108 }
109
110
111
112
113
114
115
116 @Test
117 public void testComposition3EmptyComposition() throws Exception {
118 UIViewRoot root = facesContext.getViewRoot();
119 vdl.buildView(facesContext, root, "composition3.xhtml");
120
121 StringWriter sw = new StringWriter();
122 MockResponseWriter mrw = new MockResponseWriter(sw);
123 facesContext.setResponseWriter(mrw);
124 root.encodeAll(facesContext);
125 sw.flush();
126
127 String response = sw.toString();
128 Assert.assertFalse(response.contains("This fragment will not be inserted"));
129 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
130 }
131
132
133
134
135
136
137 @Test
138 public void testComposition4DefineOverInsert() throws Exception {
139 UIViewRoot root = facesContext.getViewRoot();
140 vdl.buildView(facesContext, root, "composition4.xhtml");
141
142 StringWriter sw = new StringWriter();
143 MockResponseWriter mrw = new MockResponseWriter(sw);
144 facesContext.setResponseWriter(mrw);
145 root.encodeAll(facesContext);
146 sw.flush();
147
148 String response = sw.toString();
149 Assert.assertTrue(response.contains("fragmentNumber1"));
150 Assert.assertFalse(response.contains("This fragment will not be inserted"));
151 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
152 }
153
154
155
156
157
158
159
160
161
162 @Test
163 public void testComposition5() throws Exception {
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 UIViewRoot root = facesContext.getViewRoot();
195 vdl.buildView(facesContext, root, "composition5.xhtml");
196
197 StringWriter sw = new StringWriter();
198 MockResponseWriter mrw = new MockResponseWriter(sw);
199 facesContext.setResponseWriter(mrw);
200 root.encodeAll(facesContext);
201 sw.flush();
202
203 String response = sw.toString();
204 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
205
206 response = checkStringInOrder(response, "start second composition");
207 response = checkStringInOrder(response, "composition5Content");
208 response = checkStringInOrder(response, "end second composition");
209 }
210
211 private String checkStringInOrder(String response, String token)
212 {
213 int pos = response.indexOf(token);
214 Assert.assertTrue(pos > -1);
215 return response.substring(pos+token.length());
216 }
217
218
219
220
221
222
223
224
225 @Test
226 public void testCompositionNested1() throws Exception {
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262 UIViewRoot root = facesContext.getViewRoot();
263 vdl.buildView(facesContext, root, "compositionNested1.xhtml");
264
265 StringWriter sw = new StringWriter();
266 MockResponseWriter mrw = new MockResponseWriter(sw);
267 facesContext.setResponseWriter(mrw);
268 root.encodeAll(facesContext);
269 sw.flush();
270
271 String response = sw.toString();
272 Assert.assertTrue(response.contains("fragmentNumber2"));
273 Assert.assertFalse(response.contains("fragmentNumber1"));
274 Assert.assertFalse(response.contains("This fragment will not be inserted"));
275 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
276 }
277
278 @Test
279 public void testCompositionNested2() throws Exception
280 {
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 UIViewRoot root = facesContext.getViewRoot();
325 vdl.buildView(facesContext, root, "compositionNested2.xhtml");
326
327 StringWriter sw = new StringWriter();
328 MockResponseWriter mrw = new MockResponseWriter(sw);
329 facesContext.setResponseWriter(mrw);
330 root.encodeAll(facesContext);
331 sw.flush();
332
333 String response = sw.toString();
334 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
335
336 response = checkStringInOrder(response, "start first decoration");
337 response = checkStringInOrder(response, "start second composition");
338 response = checkStringInOrder(response, "start inner text");
339 response = checkStringInOrder(response, "compositionNested2Content");
340 response = checkStringInOrder(response, "end inner text");
341 response = checkStringInOrder(response, "end second composition");
342 response = checkStringInOrder(response, "end first decoration");
343 }
344 }