1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.shared.renderkit;
20
21 import java.io.IOException;
22 import java.io.StringWriter;
23
24 import javax.el.ELContext;
25 import javax.faces.application.Application;
26 import javax.faces.application.ProjectStage;
27 import javax.faces.application.Resource;
28 import javax.faces.application.ResourceHandler;
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIInput;
31 import javax.faces.component.UIOutput;
32 import javax.faces.component.UIPanel;
33 import javax.faces.component.html.HtmlGraphicImage;
34 import javax.faces.context.FacesContext;
35
36 import junit.framework.Assert;
37
38 import org.apache.myfaces.shared.renderkit.html.HTML;
39 import org.apache.myfaces.test.base.AbstractJsfTestCase;
40 import org.apache.myfaces.test.mock.MockResponseWriter;
41 import org.easymock.classextension.EasyMock;
42 import org.junit.Test;
43
44 public class RendererUtilsTest extends AbstractJsfTestCase {
45
46 private MockResponseWriter writer;
47
48
49
50
51 ResourceHandler resourceHandlerMock;
52
53
54
55
56 Application applicationMock;
57
58
59
60
61 private Resource resourceMock;
62
63 String libraryName = "images";
64
65 String resourceName = "picture.gif";
66
67 String requestPath = "/somePrefix/faces/javax.faces.resource/picture.gif?ln=\"images\"";
68
69
70 HtmlGraphicImage graphicImage = new HtmlGraphicImage();
71
72 private UIPanel parent;
73
74 public RendererUtilsTest(String name) {
75 super(name);
76 }
77
78 protected void setUp() throws Exception {
79 super.setUp();
80
81 writer = new MockResponseWriter(new StringWriter(), null, null);
82 facesContext.setResponseWriter(writer);
83
84 applicationMock = EasyMock.createNiceMock(Application.class);
85 facesContext.setApplication(applicationMock);
86
87 resourceHandlerMock = EasyMock.createNiceMock(ResourceHandler.class);
88 applicationMock.getResourceHandler();
89 EasyMock.expectLastCall().andReturn(resourceHandlerMock);
90
91 applicationMock.getProjectStage();
92 EasyMock.expectLastCall().andReturn(ProjectStage.Development);
93
94 resourceMock = EasyMock.createNiceMock(Resource.class);
95
96 EasyMock.replay(applicationMock);
97
98 graphicImage.getAttributes().put(JSFAttr.LIBRARY_ATTR, libraryName);
99 graphicImage.getAttributes().put(JSFAttr.NAME_ATTR, resourceName);
100 graphicImage.setId("graphicImageId");
101
102 parent = new UIPanel();
103 }
104
105 protected void tearDown() throws Exception {
106 super.tearDown();
107 }
108
109
110
111
112 public void testGetIconSrc() {
113
114
115 resourceHandlerMock.createResource(resourceName, libraryName);
116 EasyMock.expectLastCall().andReturn(resourceMock);
117 resourceMock.getRequestPath();
118 EasyMock.expectLastCall().andReturn(requestPath);
119 EasyMock.replay(resourceHandlerMock);
120 EasyMock.replay(resourceMock);
121
122
123 String iconSrc = RendererUtils.getIconSrc(facesContext, graphicImage,
124 HTML.IMG_ELEM);
125
126 assertEquals(
127 "If name or name/library present, source must be obtained from ResourceHandler",
128 requestPath, iconSrc);
129
130 }
131
132 public void testGetIconSrcResourceNotFound() throws Exception {
133
134 EasyMock.reset(resourceHandlerMock);
135 resourceHandlerMock.createResource(resourceName, libraryName);
136 EasyMock.expectLastCall().andReturn(null);
137 EasyMock.replay(resourceHandlerMock);
138
139
140 String iconSrc = RendererUtils.getIconSrc(facesContext, graphicImage,
141 HTML.IMG_ELEM);
142
143 assertEquals("RES_NOT_FOUND", iconSrc);
144 assertTrue("If resource is not found, a Message must be added",
145 facesContext.getMessages(graphicImage.getClientId(facesContext)).hasNext());
146
147 }
148
149 public void testGetStringValue()
150 {
151
152 UIInput uiInput = new UIInput();
153 Object submittedValue = new Object();
154 uiInput.setSubmittedValue(submittedValue);
155
156 String stringValue = RendererUtils.getStringValue(facesContext, uiInput);
157 assertNotNull(stringValue);
158 assertEquals("If submittedvalue is not String, toString() must be used", submittedValue.toString(), stringValue);
159 }
160
161 public void testGetConvertedUIOutputValue()
162 {
163 UIInput uiInput = new UIInput();
164 StringBuilder submittedValue = new StringBuilder("submittedValue");
165 uiInput.setSubmittedValue(submittedValue);
166
167
168 Object convertedUIOutputValue = RendererUtils.getConvertedUIOutputValue(facesContext, uiInput, submittedValue);
169 assertEquals("If submittedvalue is not String, toString() must be used", submittedValue.toString(), convertedUIOutputValue);
170 }
171
172
173
174
175 @Test
176 public void testRenderChild() throws IOException
177 {
178
179 UIInput uiInput = _setUpComponentStack();
180
181 RendererUtils.renderChild(facesContext, uiInput);
182
183 assertEquals("Invocation must not change the current component", parent, UIComponent.getCurrentComponent(facesContext));
184 }
185
186
187
188
189
190 @Test
191 public void testRenderChild2() throws IOException {
192
193 MockComponent component = new MockComponent();
194
195 RendererUtils.renderChild(facesContext, component);
196 }
197
198 @Test
199 public void testIsRendered()
200 {
201 UIComponent uiComponent = new UIOutput();
202 boolean rendered = RendererUtils.isRendered(facesContext, uiComponent);
203 assertTrue(rendered);
204
205 uiComponent.setRendered(false);
206 rendered = RendererUtils.isRendered(facesContext, uiComponent);
207 assertFalse(rendered);
208
209 uiComponent = _setUpComponentStack();
210 rendered = RendererUtils.isRendered(facesContext, uiComponent);
211 assertFalse(rendered);
212 assertEquals("isRendered must not change current component", parent, UIComponent.getCurrentComponent(facesContext));
213 }
214
215
216
217
218 private class MockRenderedValueExpression extends org.apache.myfaces.test.el.MockValueExpression {
219
220 private final UIComponent toVerify;
221
222 public MockRenderedValueExpression(String expression, Class<?> expectedType, UIComponent toVerify) {
223 super(expression, expectedType);
224 this.toVerify = toVerify;
225 }
226
227 @Override
228 public Object getValue(ELContext elContext) {
229 UIComponent currentComponent = UIComponent.getCurrentComponent(facesContext);
230 Assert.assertEquals("If this VE is called, component on stack must be actual" , currentComponent , toVerify);
231 return false;
232 }
233 }
234
235
236 private class MockComponent extends UIOutput
237 {
238 @Override
239 public boolean isRendered() {
240 return false;
241 }
242 @Override
243 public void encodeBegin(FacesContext context) throws IOException {
244 fail();
245 }
246 @Override
247 public void encodeChildren(FacesContext context) throws IOException {
248 fail();
249 }
250 @Override
251 public void encodeEnd(FacesContext context) throws IOException {
252 fail();
253 }
254 }
255
256 private UIInput _setUpComponentStack() {
257 UIInput uiInput = new UIInput();
258 parent.getChildren().add(uiInput);
259 uiInput.setId("testId");
260
261
262 MockRenderedValueExpression ve = new MockRenderedValueExpression("#{component.id eq 'testId'}", Boolean.class, uiInput);
263 uiInput.setValueExpression("rendered", ve);
264
265
266 parent.pushComponentToEL(facesContext, null);
267 return uiInput;
268 }
269
270 }