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 import javax.faces.application.ViewHandler;
23
24 import javax.faces.component.UIViewRoot;
25
26 import org.apache.myfaces.test.mock.MockResponseWriter;
27 import org.apache.myfaces.view.facelets.FaceletTestCase;
28 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
29 import org.junit.Assert;
30 import org.junit.Test;
31
32 public class CompositionTestCase extends FaceletTestCase {
33
34 @Override
35 protected void setUpServletObjects() throws Exception
36 {
37 super.setUpServletObjects();
38 servletContext.addInitParameter(ViewHandler.FACELETS_SKIP_COMMENTS_PARAM_NAME, "true");
39 }
40
41
42
43
44
45
46 @Test
47 public void testCompositionTrimEffect() throws Exception {
48 UIViewRoot root = facesContext.getViewRoot();
49 vdl.buildView(facesContext, root, "compositionTrim.xhtml");
50
51 StringWriter sw = new StringWriter();
52 MockResponseWriter mrw = new MockResponseWriter(sw);
53 facesContext.setResponseWriter(mrw);
54 root.encodeAll(facesContext);
55 sw.flush();
56
57 String response = sw.toString();
58 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
59 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
60 }
61
62
63
64
65
66
67
68
69
70
71 @Test
72 public void testComposition1() throws Exception {
73 UIViewRoot root = facesContext.getViewRoot();
74 vdl.buildView(facesContext, root, "composition1.xhtml");
75
76 StringWriter sw = new StringWriter();
77 MockResponseWriter mrw = new MockResponseWriter(sw);
78 facesContext.setResponseWriter(mrw);
79 root.encodeAll(facesContext);
80 sw.flush();
81
82 String response = sw.toString();
83 Assert.assertTrue(response.contains("fragmentNumber1"));
84 Assert.assertTrue(response.contains("compositionContent"));
85 Assert.assertFalse(response.contains("This fragment will not be inserted"));
86 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
87 }
88
89
90
91
92
93
94
95 @Test
96 public void testComposition2EmptyComposition() throws Exception {
97 UIViewRoot root = facesContext.getViewRoot();
98 vdl.buildView(facesContext, root, "composition2.xhtml");
99
100 StringWriter sw = new StringWriter();
101 MockResponseWriter mrw = new MockResponseWriter(sw);
102 facesContext.setResponseWriter(mrw);
103 root.encodeAll(facesContext);
104 sw.flush();
105
106 String response = sw.toString();
107 Assert.assertFalse(response.contains("This fragment will not be inserted"));
108 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
109 }
110
111
112
113
114
115
116
117 @Test
118 public void testComposition3EmptyComposition() throws Exception {
119 UIViewRoot root = facesContext.getViewRoot();
120 vdl.buildView(facesContext, root, "composition3.xhtml");
121
122 StringWriter sw = new StringWriter();
123 MockResponseWriter mrw = new MockResponseWriter(sw);
124 facesContext.setResponseWriter(mrw);
125 root.encodeAll(facesContext);
126 sw.flush();
127
128 String response = sw.toString();
129 Assert.assertFalse(response.contains("This fragment will not be inserted"));
130 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
131 }
132
133
134
135
136
137
138 @Test
139 public void testComposition4DefineOverInsert() throws Exception {
140 UIViewRoot root = facesContext.getViewRoot();
141 vdl.buildView(facesContext, root, "composition4.xhtml");
142
143 StringWriter sw = new StringWriter();
144 MockResponseWriter mrw = new MockResponseWriter(sw);
145 facesContext.setResponseWriter(mrw);
146 root.encodeAll(facesContext);
147 sw.flush();
148
149 String response = sw.toString();
150 Assert.assertTrue(response.contains("fragmentNumber1"));
151 Assert.assertFalse(response.contains("This fragment will not be inserted"));
152 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
153 }
154
155
156
157
158
159
160
161
162
163 @Test
164 public void testComposition5() throws Exception {
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
195 UIViewRoot root = facesContext.getViewRoot();
196 vdl.buildView(facesContext, root, "composition5.xhtml");
197
198 StringWriter sw = new StringWriter();
199 MockResponseWriter mrw = new MockResponseWriter(sw);
200 facesContext.setResponseWriter(mrw);
201 root.encodeAll(facesContext);
202 sw.flush();
203
204 String response = sw.toString();
205 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
206
207 response = checkStringInOrder(response, "start second composition");
208 response = checkStringInOrder(response, "composition5Content");
209 response = checkStringInOrder(response, "end second composition");
210 }
211
212 private String checkStringInOrder(String response, String token)
213 {
214 int pos = response.indexOf(token);
215 Assert.assertTrue(pos > -1);
216 return response.substring(pos+token.length());
217 }
218
219
220
221
222
223
224
225
226 @Test
227 public void testCompositionNested1() throws Exception {
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
263 UIViewRoot root = facesContext.getViewRoot();
264 vdl.buildView(facesContext, root, "compositionNested1.xhtml");
265
266 StringWriter sw = new StringWriter();
267 MockResponseWriter mrw = new MockResponseWriter(sw);
268 facesContext.setResponseWriter(mrw);
269 root.encodeAll(facesContext);
270 sw.flush();
271
272 String response = sw.toString();
273 Assert.assertTrue(response.contains("fragmentNumber2"));
274 Assert.assertFalse(response.contains("fragmentNumber1"));
275 Assert.assertFalse(response.contains("This fragment will not be inserted"));
276 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
277 }
278
279 @Test
280 public void testCompositionNested2() throws Exception
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
325 UIViewRoot root = facesContext.getViewRoot();
326 vdl.buildView(facesContext, root, "compositionNested2.xhtml");
327
328 StringWriter sw = new StringWriter();
329 MockResponseWriter mrw = new MockResponseWriter(sw);
330 facesContext.setResponseWriter(mrw);
331 root.encodeAll(facesContext);
332 sw.flush();
333
334 String response = sw.toString();
335 Assert.assertFalse(response.contains("THIS SHOULD NOT BE RENDERED"));
336
337 response = checkStringInOrder(response, "start first decoration");
338 response = checkStringInOrder(response, "start second composition");
339 response = checkStringInOrder(response, "start inner text");
340 response = checkStringInOrder(response, "compositionNested2Content");
341 response = checkStringInOrder(response, "end inner text");
342 response = checkStringInOrder(response, "end second composition");
343 response = checkStringInOrder(response, "end first decoration");
344 }
345 }