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.renderkits;
20  
21  import java.io.StringWriter;
22  
23  import javax.faces.FactoryFinder;
24  import javax.faces.component.html.HtmlInputText;
25  import javax.faces.render.RenderKit;
26  import javax.faces.render.RenderKitFactory;
27  
28  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
29  import org.apache.myfaces.shared.util.ClassUtils;
30  import org.apache.myfaces.test.base.AbstractJsfTestCase;
31  import org.apache.myfaces.test.mock.MockFacesContext12;
32  import org.apache.myfaces.test.mock.MockResponseWriter;
33  
34  /**
35   * @author martin.haimberger
36   */
37  public class OwnRenderkitTest extends AbstractJsfTestCase {
38      private MockResponseWriter writer;
39      private HtmlInputText inputText;
40  
41      private static boolean isOwnRenderKit = false;
42  
43      public static void SetIsOwnRenderKit() {
44          isOwnRenderKit = true;
45      }
46  
47  
48      public OwnRenderkitTest(String name) {
49          super(name);
50      }
51  
52      protected void setUp() throws Exception {
53          super.setUp();
54          addRenderKit();
55          inputText = new HtmlInputText();
56  
57          writer = new MockResponseWriter(new StringWriter(), null, null);
58          facesContext.setResponseWriter(writer);
59          MockFacesContext12.getCurrentInstance();
60  
61          facesContext.getViewRoot().setRenderKitId("OWN_BASIC");
62          facesContext.getRenderKit().addRenderer(
63                  inputText.getFamily(),
64                  inputText.getRendererType(),
65                  new HtmlTextRenderer());
66  
67      }
68  
69      protected void tearDown() throws Exception {
70          super.tearDown();
71          inputText = null;
72          writer = null;
73          isOwnRenderKit = false;
74      }
75  
76      public void testOwnRenderKit() throws Exception {
77  
78          inputText.encodeEnd(facesContext);
79          facesContext.renderResponse();
80  
81          assertTrue(isOwnRenderKit);
82      }
83  
84  
85      private void addRenderKit() {
86          RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
87  
88          String renderKitId = "OWN_BASIC";
89          String renderKitClass = "org.apache.myfaces.renderkits.OwnRenderKitImpl";
90  
91          RenderKit renderKit = (RenderKit) ClassUtils.newInstance(renderKitClass);
92  
93          renderKitFactory.addRenderKit(renderKitId, renderKit);
94  
95      }
96  
97  }