1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.view.facelets.tag.jstl.core;
21
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.el.ExpressionFactory;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIForm;
31 import javax.faces.component.UIViewRoot;
32 import javax.faces.component.html.HtmlCommandButton;
33 import javax.faces.component.html.HtmlForm;
34 import javax.faces.component.html.HtmlInputText;
35 import javax.faces.component.html.HtmlOutputText;
36 import javax.faces.context.FacesContext;
37
38 import org.apache.myfaces.renderkit.html.HtmlButtonRenderer;
39 import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
40 import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
41 import org.apache.myfaces.test.mock.MockResponseWriter;
42 import org.apache.myfaces.view.facelets.ELExpressionCacheMode;
43 import org.apache.myfaces.view.facelets.FaceletTestCase;
44 import org.apache.myfaces.view.facelets.bean.Employee;
45 import org.apache.myfaces.view.facelets.impl.FaceletCompositionContextImpl;
46 import org.apache.myfaces.view.facelets.util.FastWriter;
47 import org.junit.Assert;
48 import org.junit.Test;
49
50 public final class JstlCoreTestCase extends FaceletTestCase {
51
52 @Override
53 protected ExpressionFactory createExpressionFactory()
54 {
55 return new org.apache.el.ExpressionFactoryImpl();
56 }
57
58 @Override
59 protected void setupComponents() throws Exception
60 {
61 application.addComponent(UIViewRoot.COMPONENT_TYPE,
62 UIViewRoot.class.getName());
63 application.addComponent(HtmlForm.COMPONENT_TYPE,
64 HtmlForm.class.getName());
65 application.addComponent(HtmlCommandButton.COMPONENT_TYPE,
66 HtmlCommandButton.class.getName());
67 application.addComponent(HtmlInputText.COMPONENT_TYPE,
68 HtmlInputText.class.getName());
69 application.addComponent(HtmlOutputText.COMPONENT_TYPE,
70 HtmlOutputText.class.getName());
71
72 }
73
74 @Override
75 protected void setupConvertersAndValidators() throws Exception
76 {
77 }
78
79 @Override
80 protected void setupRenderers() throws Exception
81 {
82 renderKit.addRenderer(UIForm.COMPONENT_FAMILY,
83 "javax.faces.Form", new HtmlFormRenderer());
84 renderKit.addRenderer(HtmlCommandButton.COMPONENT_FAMILY,
85 "javax.faces.Button", new HtmlButtonRenderer());
86 renderKit.addRenderer(HtmlInputText.COMPONENT_FAMILY,
87 "javax.faces.Text", new HtmlTextRenderer());
88 renderKit.addRenderer(HtmlOutputText.COMPONENT_FAMILY,
89 "javax.faces.Text", new HtmlTextRenderer());
90 }
91
92 @Test
93 public void testIf() throws Exception {
94 Map session = facesContext.getExternalContext().getSessionMap();
95 Employee e = new Employee();
96 session.put("employee", e);
97
98 UIViewRoot root = facesContext.getViewRoot();
99
100
101 e.setManagement(true);
102 vdl.buildView(facesContext, root,"if.xml");
103 UIComponent c = root.findComponent("form");
104 Assert.assertNotNull("form is null", c);
105
106
107 e.setManagement(false);
108
109 facesContext.setViewRoot(facesContext.getApplication().getViewHandler()
110 .createView(facesContext, "/test"));
111 root = facesContext.getViewRoot();
112 vdl.buildView(facesContext, root,"if.xml");
113 c = root.findComponent("form");
114 Assert.assertNull("form is not null", c);
115 }
116
117 @Test
118 public void testForEach() throws Exception {
119 FacesContext facesContext = FacesContext.getCurrentInstance();
120 Map session = facesContext.getExternalContext().getSessionMap();
121 Collection c = new ArrayList();
122 for (int i = 0; i < 10; i++) {
123 c.add(new Character((char)('A' + i)));
124 }
125 session.put("list", c);
126 Map m = new HashMap();
127 for (int i = 0; i < 10; i++) {
128 m.put("" + i, "" + i);
129 }
130
131 UIViewRoot root = facesContext.getViewRoot();
132 vdl.buildView(facesContext, root,"forEach.xml");
133
134 StringWriter sw = new StringWriter();
135 MockResponseWriter mrw = new MockResponseWriter(sw);
136 facesContext.setResponseWriter(mrw);
137
138 root.encodeAll(facesContext);
139 sw.flush();
140 }
141
142 @Test
143 public void testForEach1() throws Exception {
144 FacesContext facesContext = FacesContext.getCurrentInstance();
145 Map session = facesContext.getExternalContext().getSessionMap();
146 Collection c = new ArrayList();
147 for (int i = 0; i < 10; i++) {
148 c.add(new Character((char)('A' + i)));
149 }
150 session.put("list", c);
151
152 UIViewRoot root = facesContext.getViewRoot();
153 vdl.buildView(facesContext, root,"forEach1.xhtml");
154
155 StringWriter sw = new StringWriter();
156 MockResponseWriter mrw = new MockResponseWriter(sw);
157 facesContext.setResponseWriter(mrw);
158
159 root.encodeAll(facesContext);
160 sw.flush();
161
162 Assert.assertTrue(sw.toString().contains("A = true/false"));
163 Assert.assertTrue(sw.toString().contains("B = false/false"));
164 Assert.assertTrue(sw.toString().contains("C = false/false"));
165 Assert.assertTrue(sw.toString().contains("D = false/false"));
166 Assert.assertTrue(sw.toString().contains("E = false/false"));
167 Assert.assertTrue(sw.toString().contains("F = false/false"));
168 Assert.assertTrue(sw.toString().contains("G = false/false"));
169 Assert.assertTrue(sw.toString().contains("H = false/false"));
170 Assert.assertTrue(sw.toString().contains("I = false/false"));
171 Assert.assertTrue(sw.toString().contains("J = false/true"));
172 }
173
174
175
176
177
178
179
180 @Test
181 public void testForEach2() throws Exception {
182 FacesContext facesContext = FacesContext.getCurrentInstance();
183 Map session = facesContext.getExternalContext().getSessionMap();
184 Collection c = new ArrayList();
185 for (int i = 0; i < 10; i++) {
186 c.add(new Character((char)('A' + i)));
187 }
188 session.put("list", c);
189
190 UIViewRoot root = facesContext.getViewRoot();
191 vdl.buildView(facesContext, root,"forEach2.xhtml");
192
193 StringWriter sw = new StringWriter();
194 MockResponseWriter mrw = new MockResponseWriter(sw);
195 facesContext.setResponseWriter(mrw);
196
197 root.encodeAll(facesContext);
198 sw.flush();
199
200 Assert.assertTrue(sw.toString().contains("value1 = /"));
201 }
202
203 @Test
204 public void testForEach2CacheAlways() throws Exception
205 {
206 servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS,
207 ELExpressionCacheMode.always.toString());
208 testForEach2();
209 }
210
211
212
213
214
215
216
217 @Test
218 public void testForEach3() throws Exception {
219 FacesContext facesContext = FacesContext.getCurrentInstance();
220 Map session = facesContext.getExternalContext().getSessionMap();
221 Collection c = new ArrayList();
222 for (int i = 0; i < 10; i++) {
223 c.add(new Character((char)('A' + i)));
224 }
225 session.put("list", c);
226
227 UIViewRoot root = facesContext.getViewRoot();
228 vdl.buildView(facesContext, root,"forEach3.xhtml");
229
230 StringWriter sw = new StringWriter();
231 MockResponseWriter mrw = new MockResponseWriter(sw);
232 facesContext.setResponseWriter(mrw);
233
234 root.encodeAll(facesContext);
235 sw.flush();
236
237 Assert.assertTrue(sw.toString().contains("A = true/false"));
238 Assert.assertTrue(sw.toString().contains("B = false/false"));
239 Assert.assertTrue(sw.toString().contains("C = false/false"));
240 Assert.assertTrue(sw.toString().contains("D = false/false"));
241 Assert.assertTrue(sw.toString().contains("E = false/false"));
242 Assert.assertTrue(sw.toString().contains("F = false/false"));
243 Assert.assertTrue(sw.toString().contains("G = false/false"));
244 Assert.assertTrue(sw.toString().contains("H = false/false"));
245 Assert.assertTrue(sw.toString().contains("I = false/false"));
246 Assert.assertTrue(sw.toString().contains("J = false/true"));
247 Assert.assertFalse(sw.toString().contains("value1 ="));
248 }
249
250 @Test
251 public void testForEach3CacheAlways() throws Exception
252 {
253 servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS,
254 ELExpressionCacheMode.always.toString());
255 testForEach3();
256 }
257
258
259
260
261
262
263
264 @Test
265 public void testForEach4() throws Exception {
266 FacesContext facesContext = FacesContext.getCurrentInstance();
267 Map session = facesContext.getExternalContext().getSessionMap();
268 Collection c = new ArrayList();
269 for (int i = 0; i < 10; i++) {
270 c.add(new Character((char)('A' + i)));
271 }
272 session.put("list", c);
273
274 UIViewRoot root = facesContext.getViewRoot();
275 vdl.buildView(facesContext, root,"forEach4.xhtml");
276
277 StringWriter sw = new StringWriter();
278 MockResponseWriter mrw = new MockResponseWriter(sw);
279 facesContext.setResponseWriter(mrw);
280
281 root.encodeAll(facesContext);
282 sw.flush();
283
284 Assert.assertTrue(sw.toString().contains("value = A"));
285 Assert.assertTrue(sw.toString().contains("value = B"));
286 Assert.assertTrue(sw.toString().contains("value = C"));
287 Assert.assertTrue(sw.toString().contains("value = D"));
288 Assert.assertTrue(sw.toString().contains("value = E"));
289 Assert.assertTrue(sw.toString().contains("value = F"));
290 Assert.assertTrue(sw.toString().contains("value = G"));
291 Assert.assertTrue(sw.toString().contains("value = H"));
292 Assert.assertTrue(sw.toString().contains("value = I"));
293 Assert.assertTrue(sw.toString().contains("value = J"));
294 Assert.assertTrue(!sw.toString().contains("value = value"));
295 Assert.assertTrue(!sw.toString().contains("A = A"));
296 }
297
298 @Test
299 public void testForEach4CacheAlways() throws Exception
300 {
301 servletContext.addInitParameter(FaceletCompositionContextImpl.INIT_PARAM_CACHE_EL_EXPRESSIONS,
302 ELExpressionCacheMode.always.toString());
303 testForEach4();
304 }
305
306 }