View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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  	 * ResourceHandler nice easy mock
50  	 */
51  	ResourceHandler resourceHandlerMock;
52  
53  	/**
54  	 * {@link Application} nice easy mock
55  	 */
56  	Application applicationMock;
57  
58  	/**
59  	 * A {@link Resource} nice easy mock
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  	// a Component instance:
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 		// Training a mock:
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 		// Tested method :
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 		// Training a mock:
134 		EasyMock.reset(resourceHandlerMock);
135 		resourceHandlerMock.createResource(resourceName, libraryName);
136 		EasyMock.expectLastCall().andReturn(null);
137 		EasyMock.replay(resourceHandlerMock);
138 
139 		// Tested method :
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         // Test for situation where submittedValue IS NOT String: 
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      * test for MYFACES-3126
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      * Test that no method encode* are called if component is not rendered 
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      * Verifies the current component on stack
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     /** Verifies no call to encode* methods */
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        // simlulate that parent panel encodes children and is on the stack:
266        parent.pushComponentToEL(facesContext, null);
267         return uiInput;
268     }
269 
270 }