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  
20  package org.apache.myfaces.tobago.internal.config;
21  
22  import org.apache.myfaces.test.base.junit4.AbstractJsfTestCase;
23  import org.apache.myfaces.test.config.ResourceBundleVarNames;
24  import org.apache.myfaces.test.mock.MockFacesContext;
25  import org.apache.myfaces.test.mock.MockHttpServletRequest;
26  import org.apache.myfaces.tobago.component.RendererTypes;
27  import org.apache.myfaces.tobago.component.Tags;
28  import org.apache.myfaces.tobago.component.UIBadge;
29  import org.apache.myfaces.tobago.component.UIBox;
30  import org.apache.myfaces.tobago.component.UIButton;
31  import org.apache.myfaces.tobago.component.UIButtons;
32  import org.apache.myfaces.tobago.component.UIGridLayout;
33  import org.apache.myfaces.tobago.component.UIIn;
34  import org.apache.myfaces.tobago.component.UILink;
35  import org.apache.myfaces.tobago.component.UILinks;
36  import org.apache.myfaces.tobago.component.UIOut;
37  import org.apache.myfaces.tobago.component.UIPanel;
38  import org.apache.myfaces.tobago.component.UIPopup;
39  import org.apache.myfaces.tobago.component.UISection;
40  import org.apache.myfaces.tobago.component.UISegmentLayout;
41  import org.apache.myfaces.tobago.component.UISelectBooleanCheckbox;
42  import org.apache.myfaces.tobago.component.UISelectItem;
43  import org.apache.myfaces.tobago.component.UISelectManyCheckbox;
44  import org.apache.myfaces.tobago.component.UISelectManyListbox;
45  import org.apache.myfaces.tobago.component.UISelectOneChoice;
46  import org.apache.myfaces.tobago.component.UISelectOneRadio;
47  import org.apache.myfaces.tobago.component.UISeparator;
48  import org.apache.myfaces.tobago.component.UIStyle;
49  import org.apache.myfaces.tobago.context.TobagoContext;
50  import org.apache.myfaces.tobago.internal.renderkit.renderer.BadgeRenderer;
51  import org.apache.myfaces.tobago.internal.renderkit.renderer.BoxRenderer;
52  import org.apache.myfaces.tobago.internal.renderkit.renderer.ButtonRenderer;
53  import org.apache.myfaces.tobago.internal.renderkit.renderer.ButtonsRenderer;
54  import org.apache.myfaces.tobago.internal.renderkit.renderer.GridLayoutRenderer;
55  import org.apache.myfaces.tobago.internal.renderkit.renderer.InRenderer;
56  import org.apache.myfaces.tobago.internal.renderkit.renderer.LinkRenderer;
57  import org.apache.myfaces.tobago.internal.renderkit.renderer.LinksRenderer;
58  import org.apache.myfaces.tobago.internal.renderkit.renderer.OutRenderer;
59  import org.apache.myfaces.tobago.internal.renderkit.renderer.PanelRenderer;
60  import org.apache.myfaces.tobago.internal.renderkit.renderer.PopupRenderer;
61  import org.apache.myfaces.tobago.internal.renderkit.renderer.SectionRenderer;
62  import org.apache.myfaces.tobago.internal.renderkit.renderer.SegmentLayoutRenderer;
63  import org.apache.myfaces.tobago.internal.renderkit.renderer.SelectBooleanCheckboxRenderer;
64  import org.apache.myfaces.tobago.internal.renderkit.renderer.SelectManyCheckboxRenderer;
65  import org.apache.myfaces.tobago.internal.renderkit.renderer.SelectManyListboxRenderer;
66  import org.apache.myfaces.tobago.internal.renderkit.renderer.SelectOneChoiceRenderer;
67  import org.apache.myfaces.tobago.internal.renderkit.renderer.SelectOneRadioRenderer;
68  import org.apache.myfaces.tobago.internal.renderkit.renderer.SeparatorRenderer;
69  import org.apache.myfaces.tobago.internal.renderkit.renderer.StyleRenderer;
70  import org.apache.myfaces.tobago.internal.webapp.HtmlResponseWriter;
71  import org.junit.jupiter.api.AfterEach;
72  import org.junit.jupiter.api.BeforeEach;
73  
74  import javax.faces.render.RenderKit;
75  import java.io.IOException;
76  import java.io.StringWriter;
77  import java.nio.charset.StandardCharsets;
78  import java.util.Locale;
79  
80  import static org.apache.myfaces.tobago.config.TobagoConfig.TOBAGO_CONFIG;
81  import static org.apache.myfaces.tobago.util.ResourceUtils.TOBAGO_RESOURCE_BUNDLE;
82  
83  /**
84   * <p>Abstract JUnit test case base class, which sets up the JavaServer Faces
85   * mock object environment for a particular simulated request.
86   * </p>
87   * <p>
88   * This is a port of the class AbstractJsfTestCase from myfaces-test12 to JUnit 4.
89   * It also contains Tobago specifics.
90   * </p>
91   */
92  
93  public abstract class AbstractTobagoTestBase extends AbstractJsfTestCase {
94  
95    private StringWriter stringWriter;
96    private int last = 0;
97  
98    /**
99     * <p>Set up instance variables required by Tobago test cases.</p>
100    */
101   @Override
102   @BeforeEach
103   public void setUp() throws Exception {
104 
105     super.setUp();
106 
107     stringWriter = new StringWriter();
108     getFacesContext().setResponseWriter(new HtmlResponseWriter(stringWriter, "", StandardCharsets.UTF_8));
109 
110     // Tobago specific extensions
111     final TobagoConfigImpl tobagoConfig = TobagoConfigMergingUnitTest.load("tobago-config-for-unit-tests.xml");
112     tobagoConfig.initDefaultValidatorInfo();
113     servletContext.setAttribute(TOBAGO_CONFIG, tobagoConfig);
114     facesContext.getExternalContext().getApplicationMap().put(TOBAGO_CONFIG, tobagoConfig);
115 
116     final TobagoContext tobagoContext = new TobagoContext();
117     tobagoContext.setTheme(tobagoConfig.getDefaultTheme());
118     facesContext.getViewRoot().setLocale(Locale.ENGLISH);
119     request.setAttribute(TobagoContext.BEAN_NAME, tobagoContext);
120 
121     // XXX is there a better way? Get it from Tobagos generated faces-config.xml?
122     application.addComponent(Tags.in.componentType(), UIIn.class.getName());
123     application.addComponent(Tags.out.componentType(), UIOut.class.getName());
124     application.addComponent(Tags.panel.componentType(), UIPanel.class.getName());
125     application.addComponent(Tags.link.componentType(), UILink.class.getName());
126     application.addComponent(Tags.links.componentType(), UILinks.class.getName());
127     application.addComponent(Tags.box.componentType(), UIBox.class.getName());
128     application.addComponent(Tags.button.componentType(), UIButton.class.getName());
129     application.addComponent(Tags.buttons.componentType(), UIButtons.class.getName());
130     application.addComponent(Tags.popup.componentType(), UIPopup.class.getName());
131     application.addComponent(Tags.separator.componentType(), UISeparator.class.getName());
132     application.addComponent(Tags.style.componentType(), UIStyle.class.getName());
133     application.addComponent(Tags.section.componentType(), UISection.class.getName());
134     application.addComponent(Tags.selectItem.componentType(), UISelectItem.class.getName());
135     application.addComponent(Tags.selectBooleanCheckbox.componentType(), UISelectBooleanCheckbox.class.getName());
136     application.addComponent(Tags.selectManyCheckbox.componentType(), UISelectManyCheckbox.class.getName());
137     application.addComponent(Tags.selectManyListbox.componentType(), UISelectManyListbox.class.getName());
138     application.addComponent(Tags.selectOneRadio.componentType(), UISelectOneRadio.class.getName());
139     application.addComponent(Tags.selectOneChoice.componentType(), UISelectOneChoice.class.getName());
140     application.addComponent(Tags.gridLayout.componentType(), UIGridLayout.class.getName());
141     application.addComponent(Tags.segmentLayout.componentType(), UISegmentLayout.class.getName());
142     application.addComponent(Tags.badge.componentType(), UIBadge.class.getName());
143 
144     final RenderKit renderKit = facesContext.getRenderKit();
145     renderKit.addRenderer(UIIn.COMPONENT_FAMILY, RendererTypes.IN, new InRenderer());
146     renderKit.addRenderer(UIOut.COMPONENT_FAMILY, RendererTypes.OUT, new OutRenderer());
147     renderKit.addRenderer(UIPanel.COMPONENT_FAMILY, RendererTypes.PANEL, new PanelRenderer());
148     renderKit.addRenderer(UILink.COMPONENT_FAMILY, RendererTypes.LINK, new LinkRenderer());
149     renderKit.addRenderer(UILinks.COMPONENT_FAMILY, RendererTypes.LINKS, new LinksRenderer());
150     renderKit.addRenderer(UIBox.COMPONENT_FAMILY, RendererTypes.BOX, new BoxRenderer());
151     renderKit.addRenderer(UIButton.COMPONENT_FAMILY, RendererTypes.BUTTON, new ButtonRenderer());
152     renderKit.addRenderer(UIButtons.COMPONENT_FAMILY, RendererTypes.BUTTONS, new ButtonsRenderer());
153     renderKit.addRenderer(UIPopup.COMPONENT_FAMILY, RendererTypes.POPUP, new PopupRenderer());
154     renderKit.addRenderer(UISeparator.COMPONENT_FAMILY, RendererTypes.SEPARATOR, new SeparatorRenderer());
155     renderKit.addRenderer(UIStyle.COMPONENT_FAMILY, RendererTypes.STYLE, new StyleRenderer());
156     renderKit.addRenderer(UISection.COMPONENT_FAMILY, RendererTypes.SECTION, new SectionRenderer());
157     renderKit.addRenderer(
158         UISelectBooleanCheckbox.COMPONENT_FAMILY, RendererTypes.SELECT_BOOLEAN_CHECKBOX,
159         new SelectBooleanCheckboxRenderer());
160     renderKit.addRenderer(
161         UISelectManyCheckbox.COMPONENT_FAMILY, RendererTypes.SELECT_MANY_CHECKBOX, new SelectManyCheckboxRenderer());
162     renderKit.addRenderer(
163         UISelectManyListbox.COMPONENT_FAMILY, RendererTypes.SELECT_MANY_LISTBOX, new SelectManyListboxRenderer());
164     renderKit.addRenderer(
165         UISelectOneRadio.COMPONENT_FAMILY, RendererTypes.SELECT_ONE_RADIO, new SelectOneRadioRenderer());
166     renderKit.addRenderer(
167         UISelectOneChoice.COMPONENT_FAMILY, RendererTypes.SELECT_ONE_CHOICE, new SelectOneChoiceRenderer());
168     renderKit.addRenderer(UIGridLayout.COMPONENT_FAMILY, RendererTypes.GRID_LAYOUT, new GridLayoutRenderer());
169     renderKit.addRenderer(UISegmentLayout.COMPONENT_FAMILY, RendererTypes.SEGMENT_LAYOUT, new SegmentLayoutRenderer());
170     renderKit.addRenderer(UIBadge.COMPONENT_FAMILY, RendererTypes.BADGE, new BadgeRenderer());
171 
172     application.setMessageBundle("org.apache.myfaces.tobago.context.TobagoMessageBundle");
173 
174     ResourceBundleVarNames.addVarName(TOBAGO_RESOURCE_BUNDLE,
175         "org.apache.myfaces.tobago.context.TobagoResourceBundle");
176 
177     tobagoConfig.lock();
178   }
179 
180   @Override
181   @AfterEach
182   public void tearDown() throws Exception {
183     super.tearDown();
184   }
185 
186   public MockFacesContext getFacesContext() {
187     return facesContext;
188   }
189 
190   public MockHttpServletRequest getRequest() {
191     return request;
192   }
193 
194   public String getLastWritten() throws IOException {
195     getFacesContext().getResponseWriter().flush(); // is this needed
196     final String full = stringWriter.toString();
197     final String result = full.substring(last);
198     last = full.length();
199     return result;
200   }
201 }