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 DecorateTestCase 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 @Test
41 public void testDecorate1() throws Exception {
42 UIViewRoot root = facesContext.getViewRoot();
43 vdl.buildView(facesContext, root, "decorate1.xhtml");
44
45 StringWriter sw = new StringWriter();
46 MockResponseWriter mrw = new MockResponseWriter(sw);
47 facesContext.setResponseWriter(mrw);
48 root.encodeAll(facesContext);
49 sw.flush();
50
51 String response = sw.toString();
52 Assert.assertTrue(response.contains("fragmentNumber1"));
53 Assert.assertTrue(response.contains("decorateContent"));
54 Assert.assertFalse(response.contains("This fragment will not be inserted"));
55 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
56 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
57 }
58
59 @Test
60 public void testDecorate2EmptyDecorate() throws Exception {
61 UIViewRoot root = facesContext.getViewRoot();
62 vdl.buildView(facesContext, root, "decorate2.xhtml");
63
64 StringWriter sw = new StringWriter();
65 MockResponseWriter mrw = new MockResponseWriter(sw);
66 facesContext.setResponseWriter(mrw);
67 root.encodeAll(facesContext);
68 sw.flush();
69
70 String response = sw.toString();
71 Assert.assertFalse(response.contains("This fragment will not be inserted"));
72 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
73 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
74 }
75
76 @Test
77 public void testDecorate3EmptyDecorate() throws Exception {
78 UIViewRoot root = facesContext.getViewRoot();
79 vdl.buildView(facesContext, root, "decorate3.xhtml");
80
81 StringWriter sw = new StringWriter();
82 MockResponseWriter mrw = new MockResponseWriter(sw);
83 facesContext.setResponseWriter(mrw);
84 root.encodeAll(facesContext);
85 sw.flush();
86
87 String response = sw.toString();
88 Assert.assertFalse(response.contains("This fragment will not be inserted"));
89 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
90 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
91 }
92
93 @Test
94 public void testDecorate4DefineOverInsert() throws Exception {
95 UIViewRoot root = facesContext.getViewRoot();
96 vdl.buildView(facesContext, root, "decorate4.xhtml");
97
98 StringWriter sw = new StringWriter();
99 MockResponseWriter mrw = new MockResponseWriter(sw);
100 facesContext.setResponseWriter(mrw);
101 root.encodeAll(facesContext);
102 sw.flush();
103
104 String response = sw.toString();
105 Assert.assertTrue(response.contains("fragmentNumber1"));
106 Assert.assertFalse(response.contains("This fragment will not be inserted"));
107 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
108 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 @Test
125 public void testDecorate5() throws Exception
126 {
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 UIViewRoot root = facesContext.getViewRoot();
166 vdl.buildView(facesContext, root, "decorate5.xhtml");
167
168 StringWriter sw = new StringWriter();
169 MockResponseWriter mrw = new MockResponseWriter(sw);
170 facesContext.setResponseWriter(mrw);
171 root.encodeAll(facesContext);
172 sw.flush();
173
174 String response = sw.toString();
175
176 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
177 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
178
179 response = checkStringInOrder(response, "start first decoration");
180 response = checkStringInOrder(response, "start second decoration");
181 response = checkStringInOrder(response, "start inner text");
182 response = checkStringInOrder(response, "decorate5Content");
183 response = checkStringInOrder(response, "end inner text");
184 response = checkStringInOrder(response, "end second decoration");
185 response = checkStringInOrder(response, "end first decoration");
186 }
187
188 private String checkStringInOrder(String response, String token)
189 {
190 int pos = response.indexOf(token);
191 Assert.assertTrue(pos > -1);
192 return response.substring(pos+token.length());
193 }
194
195
196
197
198
199
200
201 @Test
202 public void testDecorateNested1() throws Exception {
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239 UIViewRoot root = facesContext.getViewRoot();
240 vdl.buildView(facesContext, root, "decorateNested1.xhtml");
241
242 StringWriter sw = new StringWriter();
243 MockResponseWriter mrw = new MockResponseWriter(sw);
244 facesContext.setResponseWriter(mrw);
245 root.encodeAll(facesContext);
246 sw.flush();
247
248 String response = sw.toString();
249 Assert.assertTrue(response.contains("fragmentNumber3"));
250 Assert.assertTrue(response.contains("fragmentNumber2"));
251 Assert.assertFalse(response.contains("fragmentNumber1"));
252 Assert.assertFalse(response.contains("This fragment will not be inserted"));
253 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED"));
254 Assert.assertTrue(response.contains("THIS SHOULD BE RENDERED TOO"));
255 }
256
257
258 @Test
259 public void testDecorateNested2() throws Exception
260 {
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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 UIViewRoot root = facesContext.getViewRoot();
307 vdl.buildView(facesContext, root, "decorateNested2.xhtml");
308
309 StringWriter sw = new StringWriter();
310 MockResponseWriter mrw = new MockResponseWriter(sw);
311 facesContext.setResponseWriter(mrw);
312 root.encodeAll(facesContext);
313 sw.flush();
314
315 String response = sw.toString();
316
317 response = checkStringInOrder(response, "start first decoration");
318 response = checkStringInOrder(response, "start second composition");
319 response = checkStringInOrder(response, "start inner text");
320 response = checkStringInOrder(response, "decorateNested2Content");
321 response = checkStringInOrder(response, "end inner text");
322 response = checkStringInOrder(response, "end second composition");
323 response = checkStringInOrder(response, "end first decoration");
324 }
325 }