Search in sources :

Example 61 with BridgeTest

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

the class Tests method getRequestHeaderValuesMapRenderTest.

// Test is SingleRequest -- Render only
// Test #6.40
/**
 * getRequestHeaderValuesMap Tests
 */
@BridgeTest(test = "getRequestHeaderValuesMapRenderTest")
public String getRequestHeaderValuesMapRenderTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // 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" });
        testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderValuesMap isn't immutable.");
        return Constants.TEST_FAILED;
    } 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("content-type")) {
            testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderValuesMap contains a content-type header but shouldn't.");
            return Constants.TEST_FAILED;
        } else if (key.equalsIgnoreCase("content-length")) {
            testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderValuesMap contains a content-length header but shouldn't.");
            return Constants.TEST_FAILED;
        } else 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);
                count += temp.length;
            }
            Enumeration em = ((PortletRequest) externalContext.getRequest()).getResponseContentTypes();
            // Now ensure that all entries in the getResponseContentTypes enum are in the property
            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 getRequestHeaderValuesMap is missing a key returned in request.getResponseContentTypes: " + rct);
                    return Constants.TEST_FAILED;
                }
            }
        } 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);
            }
            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 getRequestHeaderValuesMap didn't contain an Accept-Language key with a value containing each of the locales returned from request.getResponseLocales segmented by a comma.");
                return Constants.TEST_FAILED;
            }
        }
    }
    // 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 (!prop.equalsIgnoreCase("content-type") && !prop.equalsIgnoreCase("content-length")) {
            if (!headerMap.containsKey(prop)) {
                testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderValuesMap didn't contain all the key/values from request.getProperties. Its missing: " + prop);
                return Constants.TEST_FAILED;
            }
        }
    }
    // Otherwise all out tests passed:
    testBean.setTestResult(true, "The immutable Map returned from getRequestHeaderValuesMap correctly threw an exception when written to.");
    testBean.appendTestDetail("The getRequestHeaderValuesMap Map correctly didn't contain the content-type property.");
    testBean.appendTestDetail("The getRequestHeaderValuesMap Map correctly didn't contain the content-length property.");
    testBean.appendTestDetail("The getRequestHeaderValuesMap Map correctly contained an Accept property with a value containing entries from the concatenation of request.getResponseContentTypes segmented by a comma.");
    testBean.appendTestDetail("The getRequestHeaderValuesMap Map correctly contained an Accept-Language property with a value equal to the concatenation of request.getLocales segmented by a comma.");
    testBean.appendTestDetail("The getRequestHeaderValuesMap Map correctly contained all other properties returned by request.getProperties.");
    return Constants.TEST_SUCCESS;
}
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)

Example 62 with BridgeTest

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

the class Tests method getRequestContentTypeResourceTest.

// Test #6.125
@BridgeTest(test = "getRequestContentTypeResourceTest")
public String getRequestContentTypeResourceTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.RESOURCE_PHASE) {
        ResourceRequest resourceRequest = (ResourceRequest) externalContext.getRequest();
        String contentType = externalContext.getRequestContentType();
        String resourceContentType = resourceRequest.getContentType();
        if (((contentType == null) && (resourceContentType == null)) || contentType.equals(resourceContentType)) {
            testBean.setTestResult(true, "externalContext.getRequestContentType() correctly returned the same value as resourceRequest.getContentType()");
        } else {
            testBean.setTestResult(false, "externalContext.getRequestContentType() incorrectly returned the different value than resourceRequest.getContentType(). " + "Expected: " + resourceContentType + " but received: " + contentType);
        }
        testBean.setTestComplete(true);
        if (testBean.getTestStatus()) {
            return Constants.TEST_SUCCESS;
        } else {
            return Constants.TEST_FAILED;
        }
    } else {
        return "";
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) ResourceRequest(javax.portlet.ResourceRequest) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 63 with BridgeTest

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

the class Tests method getRequestParameterNamesCoreTest.

/**
 * getRequestParameterNames Tests
 */
// Test #6.48
@BridgeTest(test = "getRequestParameterNamesCoreTest")
public String getRequestParameterNamesCoreTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        // Test the following: 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
        Iterator<String> namesIterator = externalContext.getRequestParameterNames();
        boolean foundField1 = false;
        boolean foundField2 = false;
        while (namesIterator.hasNext()) {
            String name = namesIterator.next();
            // Can't exact match by key because JSF munges this id
            if (name.indexOf("formDataField1") > -1)
                foundField1 = true;
            if (name.indexOf("formDataField2") > -1)
                foundField2 = true;
            Enumeration<String> requestNamesEnum = ((PortletRequest) externalContext.getRequest()).getParameterNames();
            boolean foundName = false;
            // verify its in the underlying request Enumeration
            while (requestNamesEnum.hasMoreElements()) {
                if (name.contains(requestNamesEnum.nextElement())) {
                    foundName = true;
                    break;
                }
            }
            if (!foundName) {
                testBean.setTestResult(false, "Failed Action Phase: The Iterator returned from ExternalContext.getRequestParameterNames is missing a name returned in request.getParameterNames: " + name);
                return "getRequestParameterNamesCoreTest";
            }
        }
        // Now make sure the two form fields are there:
        if (!foundField1) {
            testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap is missing the form's field1 parameter.");
            return "getRequestParameterNamesCoreTest";
        } else if (!foundField2) {
            testBean.setTestResult(false, "Failed Action Phase: The Map returned from ExternalContext.getRequestParameterMap is missing the form's field2.");
            return "getRequestParameterNamesCoreTest";
        }
        testBean.setTestResult(true, "Passed Action Phase: The getRequestParameterNames iterator correctly contained all parameters in the underlying request.getParameterNames.");
        return "getRequestParameterNamesCoreTest";
    } else {
        testBean.setTestComplete(true);
        // Now verify we only have the VIEW_STATE_PARAM
        Iterator<String> namesIterator = externalContext.getRequestParameterNames();
        boolean foundViewState = false;
        boolean foundField1 = false;
        boolean foundField2 = false;
        while (namesIterator.hasNext()) {
            String s = namesIterator.next();
            if (s.contains(ResponseStateManager.VIEW_STATE_PARAM)) {
                foundViewState = true;
            } else if (s.indexOf("formDataField1") > -1) {
                foundField1 = true;
            } else if (s.indexOf("formDataField2") > -1) {
                foundField2 = true;
            }
        }
        if (!foundViewState) {
            testBean.setTestResult(false, "Header phase externalContext.getRequestParameterNames() doesn't contain the ResponseStateManager.VIEW_STATE parameter. Test Result from the prior action Phase was: " + testBean.getTestResult());
            return Constants.TEST_FAILED;
        } else if (foundField1) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterName() incorrectly contains the 'field1' form parameter.");
            return Constants.TEST_FAILED;
        } else if (foundField2) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterName() incorrectly contains the 'field2' form parameter.");
            return Constants.TEST_FAILED;
        } else {
            testBean.appendTestDetail("Passed Header Phase: The getRequestParameterName() 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) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 64 with BridgeTest

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

the class Tests method setRequestCharacterEncodingActionTest.

// 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.37
@BridgeTest(test = "setRequestCharacterEncodingActionTest")
public String setRequestCharacterEncodingActionTest(TestBean testBean) {
    final String utf8 = "UTF-8";
    final String utf16 = "UTF-16";
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // done by navigation rule.
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        String s = externalContext.getRequestCharacterEncoding();
        String testEncoding = null;
        // be most accomodative
        try {
            BufferedReader b = ((ClientDataRequest) externalContext.getRequest()).getReader();
            int i = 0;
        } catch (Exception e) {
            // container likely did the right thing -- but make sure by reading the parameters
            Map<String, String[]> requestParameterMap = ((PortletRequest) externalContext.getRequest()).getParameterMap();
        }
        if ((s == null) || ((s != null) && !s.equalsIgnoreCase(utf8))) {
            testEncoding = utf8;
        } else {
            testEncoding = utf16;
        }
        try {
            externalContext.setRequestCharacterEncoding(testEncoding);
            String v = externalContext.getRequestCharacterEncoding();
            if (((v == null) && (s == null)) || ((v != null) && (s != null) && v.equalsIgnoreCase(s))) {
                testBean.setTestResult(true, "setRequestCharacterEncoding was correctly ignored after reading a parameter in an action request.");
            } else {
                testBean.setTestResult(false, "setRequestCharacterEncoding incorrectly set a new encoding after reading a parameter in an action request.");
            }
        } catch (Exception e) {
            testBean.setTestResult(false, "setRequestCharacterEncoding was correctly ignored after reading a parameter in an action request.");
        }
        return "setRequestCharacterEncodingActionTest";
    } else {
        testBean.setTestComplete(true);
        if (testBean.getTestStatus()) {
            return Constants.TEST_SUCCESS;
        } else {
            return Constants.TEST_FAILED;
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ClientDataRequest(javax.portlet.ClientDataRequest) ExternalContext(javax.faces.context.ExternalContext) BufferedReader(java.io.BufferedReader) Map(java.util.Map) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 65 with BridgeTest

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

the class Tests method getRequestParameterValuesMapPreserveParamsTest.

// Test is MultiRequest -- Test whether actionParameters are preserved into
// the Render if the config value is set.
// Test #6.52
@BridgeTest(test = "getRequestParameterValuesMapPreserveParamsTest")
public String getRequestParameterValuesMapPreserveParamsTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // Now verify we have the form params that were passed to the action
    Map<String, String[]> paramMap = externalContext.getRequestParameterValuesMap();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        return "getRequestParameterValuesMapPreserveParamsTest";
    } else {
        testBean.setTestComplete(true);
        // 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();
            if (key.indexOf("formDataField1") > -1)
                foundField1 = true;
            if (key.indexOf("formDataField2") > -1)
                foundField2 = true;
        }
        if (!foundField1) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterValuesMap() didn't properly preserve the value for the 'field1' form parameter.");
            return Constants.TEST_FAILED;
        } else if (!foundField2) {
            testBean.setTestResult(false, "Failed Header Phase: externalContext.getRequestParameterValuesMap() didn't properly preserve the value for the 'field2' form parameter.");
            return Constants.TEST_FAILED;
        } else {
            testBean.setTestResult(true, "Passed Header Phase: The getRequestParameterValuesMap Map correctly preserved the submitted form fields into the Header Phase.");
            return Constants.TEST_SUCCESS;
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) 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