Search in sources :

Example 56 with BridgeTest

use of com.liferay.faces.bridge.tck.annotation.BridgeTest in project liferay-faces-bridge-impl by liferay.

the class Tests method getRequestParameterMapCoreTest.

// Test is MultiRequest -- tests both setting request in action and render
// Note:  this tests that you can't get an encoding after reading the parameter.
// we can't test for the positive -- that the setEncoding actually works
// as portlet containers are supposed to process the form parameters before
// the portlet reads them  -- some containers (WebSphere) chooses to do this
// before calling the portlet.
// Test #6.45
/**
 * getRequestParameterMap Tests
 */
@BridgeTest(test = "getRequestParameterMapCoreTest")
public String getRequestParameterMapCoreTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        // Test the following: 1. Map is immutable 2. Map (during action) contains the parameters in the underlying
        // request 2. Map (during render) doesn't contain any params other than the VIEW_STATE_PARAM
        Map<String, String> paramMap = externalContext.getRequestParameterMap();
        Map<String, String[]> requestParamMap = ((PortletRequest) externalContext.getRequest()).getParameterMap();
        // Test for immutability
        try {
            paramMap.put("TestKey", "TestValue");
            testBean.setTestResult(false, "Failed Action Phase: The Map returned from getRequestParameterMap isn't immutable.");
            return "getRequestParameterMapCoreTest";
        } catch (Exception e) {
        // we expect to get an exception
        }
        boolean foundField1 = false;
        boolean foundField2 = false;
        Set<Map.Entry<String, String>> set = paramMap.entrySet();
        for (Iterator<Map.Entry<String, String>> params = set.iterator(); params.hasNext(); ) {
            Map.Entry<String, String> e = params.next();
            String key = e.getKey();
            String value = e.getValue();
            if (key.indexOf("formDataField1") > -1)
                foundField1 = true;
            if (key.indexOf("formDataField2") > -1)
                foundField2 = true;
            String[] requestVals = requestParamMap.get(key);
            String requestVal = (requestVals != null) ? requestVals[0] : null;
            if (requestVal == null) {
                testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap is missing a key returned in request.getParameterMap: " + key);
                return "getRequestParameterMapCoreTest";
            } else if (!value.equals(requestVal)) {
                testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap contains a different value for an entry vs. request.getParameterMap.  key: " + key + " ExtCtx.value: " + value + " RequestValue: " + requestVal);
                return "getRequestParameterMapCoreTest";
            }
        }
        // Now make sure the two form fields are there:
        if (!foundField1) {
            testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap is either missing the form's field1 parameter or its value wasn't 'value1'.");
            return "getRequestParameterMapCoreTest";
        } else if (!foundField2) {
            testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap is either missing the form's field2 parameter or its value wasn't 'value2'.");
            return "getRequestParameterMapCoreTest";
        }
        testBean.setTestResult(true, "Passed Action Phase: The immutable Map returned from getRequestParameterMap correctly threw an exception when written to.");
        testBean.appendTestDetail("Passed Action Phase: The getRequestParameterMap Map correctly contained all parameters in the underlying request.getParameterMap.");
        return "getRequestParameterMapCoreTest";
    } else {
        testBean.setTestComplete(true);
        // Now verify we only have the VIEW_STATE_PARAM
        Map<String, String> paramMap = externalContext.getRequestParameterMap();
        if (paramMap.get(ResponseStateManager.VIEW_STATE_PARAM) == null) {
            testBean.setTestResult(false, "Header Phase externalContext.getRequestParameterMap() doesn't contain the ResponseStateManager.VIEW_STATE parameter. Test Result from the prior action phase was: " + testBean.getTestResult());
            return Constants.TEST_FAILED;
        }
        // loop through the Map and verify that 'value1' and 'value2' aren't there.
        boolean foundField1 = false;
        boolean foundField2 = false;
        Set<Map.Entry<String, String>> set = paramMap.entrySet();
        for (Iterator<Map.Entry<String, String>> params = set.iterator(); params.hasNext(); ) {
            Map.Entry<String, String> e = params.next();
            String key = e.getKey();
            // Can't look up by key because JSF munges this id
            if (key.indexOf("formDataField1") > -1)
                foundField1 = true;
            if (key.indexOf("formDataField2") > -1)
                foundField2 = true;
        }
        if (foundField1) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterMap() incorrectly contains the value for the 'field1' form parameter.");
            return Constants.TEST_FAILED;
        } else if (foundField2) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterMap() incorrectly contains the value for the 'field2' form parameter.");
            return Constants.TEST_FAILED;
        } else {
            testBean.appendTestDetail("Passed Header Phase: The getRequestParameterMap Map correctly excluded the submitted form fields from the Header Phase.");
            return Constants.TEST_SUCCESS;
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) Map(java.util.Map) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 57 with BridgeTest

use of com.liferay.faces.bridge.tck.annotation.BridgeTest in project liferay-faces-bridge-impl by liferay.

the class Tests method getRequestServletPathTest.

// Test is SingleRequest -- tests whether parameters encoded in the defaultViewId's
// queryString are exposed as request parameters.
// Test #6.55
@BridgeTest(test = "getRequestServletPathTest")
public String getRequestServletPathTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    String servletPath = externalContext.getRequestServletPath();
    // As this web.xml maps the faces servlet using extension mapping
    // the value should be viewId.mappingext i.e. viewid.jsf
    String viewId = facesContext.getViewRoot().getViewId();
    int dot = viewId.indexOf('.');
    viewId = viewId.substring(0, (dot >= 0) ? dot : viewId.length()) + ".jsf";
    if (servletPath.equalsIgnoreCase(viewId)) {
        testBean.setTestResult(true, "Passed Header Phase: externalContext.getRequestServletPath() correctly returned the unmapped (extension mapped) viewId: " + servletPath);
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestServletPath() returned something other than the the unmapped (extension mapped) viewId.  Expected: " + viewId + " getRequestServletPath returned: " + servletPath);
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 58 with BridgeTest

use of com.liferay.faces.bridge.tck.annotation.BridgeTest in project liferay-faces-bridge-impl by liferay.

the class Tests method getResponseCharacterEncodingRenderTest.

// Test is SingleRequest -- tests whether parameters encoded in the defaultViewId's
// queryString are exposed as request parameters.
// Test #6.60
@BridgeTest(test = "getResponseCharacterEncodingRenderTest")
public String getResponseCharacterEncodingRenderTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    MimeResponse mimeResponse = (MimeResponse) externalContext.getResponse();
    String charEncoding = externalContext.getResponseCharacterEncoding();
    String renderCharEncoding = mimeResponse.getCharacterEncoding();
    if (((charEncoding == null) && (renderCharEncoding == null)) || charEncoding.equals(renderCharEncoding)) {
        testBean.setTestResult(true, "externalContext.getResponseCharacterEncoding() correctly returned the same value as renderResponse.getCharacterEncoding()");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "externalContext.getResponseCharacterEncoding() incorrectly returned a different value than renderResponse.getCharacterEncoding(). " + "Expected: " + renderCharEncoding + " but received: " + charEncoding);
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) MimeResponse(javax.portlet.MimeResponse) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 59 with BridgeTest

use of com.liferay.faces.bridge.tck.annotation.BridgeTest in project liferay-faces-bridge-impl by liferay.

the class Tests method getResponseCharacterEncodingResourceTest.

// Test #6.127
@BridgeTest(test = "getResponseCharacterEncodingResourceTest")
public String getResponseCharacterEncodingResourceTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.RESOURCE_PHASE) {
        ResourceResponse resourceResponse = (ResourceResponse) externalContext.getResponse();
        String charEncoding = externalContext.getResponseCharacterEncoding();
        String resourceCharEncoding = resourceResponse.getCharacterEncoding();
        if (((charEncoding == null) && (resourceCharEncoding == null)) || charEncoding.equals(resourceCharEncoding)) {
            testBean.setTestResult(true, "externalContext.getResponseCharacterEncoding() correctly returned the same value as resourceResponse.getCharacterEncoding()");
        } else {
            testBean.setTestResult(false, "externalContext.getResponseCharacterEncoding() incorrectly returned a different value than resourceResponse.getCharacterEncoding(). " + "Expected: " + resourceCharEncoding + " but received: " + charEncoding);
        }
        testBean.setTestComplete(true);
        if (testBean.getTestStatus()) {
            return Constants.TEST_SUCCESS;
        } else {
            return Constants.TEST_FAILED;
        }
    } else {
        return "";
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ResourceResponse(javax.portlet.ResourceResponse) ExternalContext(javax.faces.context.ExternalContext) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 60 with BridgeTest

use of com.liferay.faces.bridge.tck.annotation.BridgeTest in project liferay-faces-bridge-impl by liferay.

the class Tests method getRequestHeaderValuesMapActionTest.

// Test is MultiRequest -- tests both setting request in action and render
// Note:  this tests that you can't get an encoding after reading the parameter.
// we can't test for the positive -- that the setEncoding actually works
// as portlet containers are supposed to process the form parameters before
// the portlet reads them  -- some containers (WebSphere) chooses to do this
// before calling the portlet.
// Test #6.41
@BridgeTest(test = "getRequestHeaderValuesMapActionTest")
public String getRequestHeaderValuesMapActionTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // done by navigation rule.
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        // Test the following: 1. Map is immutable 2. Map contains properties from the portlet request (that it
        // should) 2. Doesn't contain the Content-Type property 3. Does include the Accept and Accept-Language
        Map<String, String[]> headerMap = externalContext.getRequestHeaderValuesMap();
        Enumeration propertyNames = ((PortletRequest) externalContext.getRequest()).getPropertyNames();
        // Test for immutability
        try {
            String[] s = (String[]) headerMap.put("TestKey", new String[] { "TestValue" });
            headerMap.put("TestKey", s);
            testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderMap isn't immutable.");
            return "getRequestHeaderValuesMapActionTest";
        } catch (Exception e) {
        // we expect to get an exception
        }
        Set<Map.Entry<String, String[]>> set = headerMap.entrySet();
        int propsFound = 0;
        for (Iterator<Map.Entry<String, String[]>> headers = set.iterator(); headers.hasNext(); ) {
            Map.Entry<String, String[]> e = headers.next();
            String key = e.getKey();
            if (key.equalsIgnoreCase("accept")) {
                boolean found = false;
                // parse the accept header into its parts
                String[] acceptsValues = e.getValue();
                String[] accepts = null;
                int count = 0;
                for (int i = 0; i < acceptsValues.length; i++) {
                    String[] temp = trim(acceptsValues[i].split(","));
                    if (accepts == null) {
                        accepts = new String[temp.length];
                    } else {
                        String[] acceptsTemp = new String[accepts.length + temp.length];
                        System.arraycopy(accepts, 0, acceptsTemp, 0, count);
                        accepts = acceptsTemp;
                    }
                    System.arraycopy(temp, 0, accepts, count, temp.length);
                }
                Enumeration em = ((PortletRequest) externalContext.getRequest()).getResponseContentTypes();
                // Now ensure the two match
                while (em.hasMoreElements()) {
                    String rct = ((String) em.nextElement());
                    found = false;
                    for (int i = 0; i < accepts.length; i++) {
                        if (rct.regionMatches(true, 0, accepts[i], 0, rct.length())) {
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderMap is missing a key returned in request.getResponseContentTypes: " + rct);
                        return "getRequestHeaderValuesMapActionTest";
                    }
                }
            } else if (key.equalsIgnoreCase("accept-language")) {
                // parse the accept header into its parts
                String[] acceptLangValues = e.getValue();
                String[] accepts = null;
                int count = 0;
                for (int i = 0; i < acceptLangValues.length; i++) {
                    String[] temp = trim(acceptLangValues[i].split(","));
                    if (accepts == null) {
                        accepts = new String[temp.length];
                    } else {
                        String[] acceptsTemp = new String[accepts.length + temp.length];
                        System.arraycopy(accepts, 0, acceptsTemp, 0, count);
                        accepts = acceptsTemp;
                    }
                    System.arraycopy(temp, 0, accepts, count, temp.length);
                    count += temp.length;
                }
                Enumeration em = ((PortletRequest) externalContext.getRequest()).getLocales();
                // Now ensure the two match
                int found = 0;
                while (em.hasMoreElements()) {
                    String rct = ((Locale) em.nextElement()).toString().replace('_', '-');
                    for (int i = 0; i < accepts.length; i++) {
                        if (rct.regionMatches(true, 0, accepts[i], 0, rct.length())) {
                            found += 1;
                            break;
                        }
                    }
                }
                if (found != accepts.length) {
                    testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderMap didn't contain an Accept-Language key with a value containing each of the locales returned from request.getResponseLocales segmented by a comma.");
                    return "getRequestHeaderValuesMapActionTest";
                }
            }
        }
        // Now enumerate the requests property Enumeration and make sure all are in this Map (except Content-Type)
        while (propertyNames.hasMoreElements()) {
            String prop = (String) propertyNames.nextElement();
            if (!headerMap.containsKey(prop)) {
                testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderMap didn't contain all the key/values from request.getProperties. Its missing: " + prop);
                return "getRequestHeaderValuesMapActionTest";
            }
        }
        // Otherwise all out tests passed:
        testBean.setTestResult(true, "The immutable Map returned from getRequestHeaderMap correctly threw an exception when written to.");
        testBean.appendTestDetail("The getRequestHeaderMap Map correctly contained an Accept property with a value containing entries from the concatenation of request.getResponseContentTypes segmented by a comma.");
        testBean.appendTestDetail("The getRequestHeaderMap Map correctly contained an Accept-Language key with a value equal to the concatenation of request.getLocales segmented by a comma.");
        testBean.appendTestDetail("The getRequestHeaderMap Map correctly contained all other properties returned by request.getProperties.");
        return "getRequestHeaderValuesMapActionTest";
    } else {
        testBean.setTestComplete(true);
        if (testBean.getTestStatus()) {
            return Constants.TEST_SUCCESS;
        } else {
            return Constants.TEST_FAILED;
        }
    }
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) Enumeration(java.util.Enumeration) PortletRequest(javax.portlet.PortletRequest) ExternalContext(javax.faces.context.ExternalContext) Map(java.util.Map) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Aggregations

BridgeTest (com.liferay.faces.bridge.tck.annotation.BridgeTest)204 FacesContext (javax.faces.context.FacesContext)201 ExternalContext (javax.faces.context.ExternalContext)166 PortletRequest (javax.portlet.PortletRequest)40 StateAwareResponse (javax.portlet.StateAwareResponse)26 QName (javax.xml.namespace.QName)26 Bridge (javax.portlet.faces.Bridge)19 Map (java.util.Map)17 PortletContext (javax.portlet.PortletContext)10 MimeResponse (javax.portlet.MimeResponse)9 RenderRequest (javax.portlet.RenderRequest)9 Enumeration (java.util.Enumeration)8 Locale (java.util.Locale)8 PortletMode (javax.portlet.PortletMode)7 WindowState (javax.portlet.WindowState)7 ELResolver (javax.el.ELResolver)6 IOException (java.io.IOException)5 ELContext (javax.el.ELContext)5 ViewHandler (javax.faces.application.ViewHandler)5 UIViewRoot (javax.faces.component.UIViewRoot)5