RendererUtils { public UIComponent getSubmittedForm(FacesContext facesContext) { if (!facesContext.isPostback()) { return null; } for (Entry<String, String> entry : facesContext.getExternalContext().getRequestParameterMap().entrySet()) { final String name = entry.getKey(); final String value = entry.getValue(); if (isFormValueSubmitted(name, value)) { UIComponent component = findComponentFor(facesContext.getViewRoot(), name); UIComponent form = getNestingForm(component); return form; } } facesContext.addMessage(null, new FacesMessage("The form wasn't detected for the request", "The form wasn't detected for the request - rendering does not have to behave well")); return null; } protected  RendererUtils(); static RendererUtils getInstance(); void encodeId(FacesContext context, UIComponent component); void encodeId(FacesContext context, UIComponent component, String attribute); void encodeCustomId(FacesContext context, UIComponent component); Object createParameterValue(UIParameter parameter); Map<String, Object> createParametersMap(FacesContext context, UIComponent component); void encodePassThru(FacesContext context, UIComponent component, String defaultHtmlEvent); void encodePassThruWithExclusions(FacesContext context, UIComponent component, String exclusions,
        String defaultHtmlEvent); void encodePassThruWithExclusionsArray(FacesContext context, UIComponent component, String[] exclusions,
        String defaultHtmlEvent); void encodePassThruAttribute(FacesContext context, Map<String, Object> attributes, ResponseWriter writer,
        String attribute); void encodeAttributesFromArray(FacesContext context, UIComponent component, String[] attrs); void encodeAttributes(FacesContext context, UIComponent component, String attrs); void encodeAttribute(FacesContext context, UIComponent component, Object property, String attributeName); void encodeAttribute(FacesContext context, UIComponent component, String attribute); boolean isEmpty(Object o); boolean isBooleanAttribute(UIComponent component, String name); String encodePx(String value); String encodePctOrPx(String value); UIComponent getNestingForm(UIComponent component); @Deprecated UIComponent getNestingForm(FacesContext context, UIComponent component); UIComponent getSubmittedForm(FacesContext facesContext); boolean isFormSubmitted(FacesContext context, UIForm form); void encodeBeginFormIfNessesary(FacesContext context, UIComponent component); void encodeBeginForm(FacesContext context, UIComponent component, ResponseWriter writer, String clientId); void encodeEndFormIfNessesary(FacesContext context, UIComponent component); static void writeState(FacesContext context); void encodeEndForm(FacesContext context, ResponseWriter writer); String getActionUrl(FacesContext facesContext); String clientId(FacesContext context, UIComponent component); void writeScript(FacesContext context, UIComponent component, Object script); String correctForIdReference(String forAttr, UIComponent component); void encodeChildren(FacesContext context, UIComponent component); boolean hasExplicitId(UIComponent component); UIComponent findComponentFor(FacesContext context, UIComponent component, String id); UIComponent findComponentFor(UIComponent component, String id); static final String DUMMY_FORM_ID; }
@Test public void when_getSubmittedForm_is_called_on_postback_and_no_matching_component_is_found_then_null_is_returned() { initializeRequestParameterMap(new HashMap<String, String>() { { put("same_but_no_clientId", "same_but_no_clientId"); } }); UIViewRoot viewRoot = mock(UIViewRoot.class); when(facesContext.getViewRoot()).thenReturn(viewRoot); when(viewRoot.getFacetsAndChildren()).thenReturn(Arrays.asList(new UIComponent[] {}).iterator()); when(facesContext.isPostback()).thenReturn(true); UIComponent result = utils.getSubmittedForm(facesContext); assertNull(result); }
@Test public void when_getSubmittedForm_is_called_with_form_details_provided_then_form_is_returned() { initializeRequestParameterMap(new HashMap<String, String>() { { put("form", "form"); } }); UIForm form = mock(UIForm.class); UIViewRoot viewRoot = mock(UIViewRoot.class); when(facesContext.getViewRoot()).thenReturn(viewRoot); when(facesContext.getViewRoot().findComponent("form")).thenReturn(form); when(facesContext.isPostback()).thenReturn(true); UIComponent result = utils.getSubmittedForm(facesContext); assertEquals(form, result); }
@Test public void when_getSubmittedForm_is_called_with_component_in_form_details_provided_then_form_is_returned() { initializeRequestParameterMap(new HashMap<String, String>() { { put("component", "component"); } }); UIForm form = mock(UIForm.class); UIComponent component = mock(UIComponent.class); UIViewRoot viewRoot = mock(UIViewRoot.class); when(facesContext.getViewRoot()).thenReturn(viewRoot); when(facesContext.getViewRoot().findComponent("component")).thenReturn(component); when(component.getParent()).thenReturn(form); when(facesContext.isPostback()).thenReturn(true); UIComponent result = utils.getSubmittedForm(facesContext); assertEquals(form, result); }
@Test public void when_getSubmittedForm_is_called_on_initial_request_then_null_is_returned() { initializeRequestParameterMap(new HashMap<String, String>() { }); when(facesContext.isPostback()).thenReturn(false); UIComponent result = utils.getSubmittedForm(facesContext); assertNull(result); }