Search in sources :

Example 51 with BridgeTest

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

the class Tests method getResponseContentTypeEventTest.

// Test is MultiRequest
// Test #6.130
@BridgeTest(test = "getResponseContentTypeEventTest")
public String getResponseContentTypeEventTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        // Create and raise the event
        StateAwareResponse stateAwareResponse = (StateAwareResponse) externalContext.getResponse();
        stateAwareResponse.setEvent(new QName(Constants.EVENT_QNAME, Constants.EVENT_NAME), testBean.getTestName());
        // action Navigation result
        return Constants.TEST_SUCCESS;
    } else if (Bridge.PortletPhase.HEADER_PHASE.equals(BridgeUtil.getPortletRequestPhase(facesContext))) {
        testBean.setTestComplete(true);
        String eventMsg = (String) externalContext.getRequestMap().get("com.liferay.faces.bridge.tck.eventTestResult");
        if (eventMsg == null) {
            testBean.setTestResult(false, "Unexpected error:  the test's event handler wasn't called and hence the test didn't run.");
            return Constants.TEST_FAILED;
        // All out tests passed:
        } else if (eventMsg.equals(Constants.TEST_SUCCESS)) {
            testBean.setTestResult(true, "externalContext.getResponseContentType() correctly returned null when called during the event phase.");
            return Constants.TEST_SUCCESS;
        } else {
            testBean.setTestResult(false, eventMsg);
            return Constants.TEST_FAILED;
        }
    } else {
        return "";
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) StateAwareResponse(javax.portlet.StateAwareResponse) ExternalContext(javax.faces.context.ExternalContext) QName(javax.xml.namespace.QName) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 52 with BridgeTest

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

the class Tests method getRequestCharacterEncodingActionTest.

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

Example 53 with BridgeTest

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

the class Tests method requestMapRequestScopeTest.

// Test is MultiRequest --
// Test #6.43
@BridgeTest(test = "requestMapRequestScopeTest")
public String requestMapRequestScopeTest(TestBean testBean) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, Object> requestMap = externalContext.getRequestMap();
    // done by navigation rule.
    if (BridgeUtil.getPortletRequestPhase(facesContext) == Bridge.PortletPhase.ACTION_PHASE) {
        // should be excluded because of value type
        requestMap.put("myRequestObject", externalContext.getRequest());
        // should be excluded because of value type
        requestMap.put("myFacesContext", facesContext);
        // should be excluded because its in exlcuded namespace
        requestMap.put("javax.faces.myKey1", Boolean.TRUE);
        // should be retained because excluded
        requestMap.put("javax.faces.myNamespace.myKey1", Boolean.TRUE);
        // namespaces don't recurse
        // should be retained
        requestMap.put("myKey1", Boolean.TRUE);
        // should be excluded as defined in portlet.xml
        requestMap.put("myExcludedNamespace.myKey1", Boolean.TRUE);
        // defined as excluded in the portlet.xml
        requestMap.put("myExcludedKey", Boolean.TRUE);
        // should be retained as
        requestMap.put("myExcludedNamespace.myIncludedNamespace.myKey1", Boolean.TRUE);
        // excluded namespaces don't
        // recurse
        // should be excluded as defined in
        requestMap.put("myFacesConfigExcludedNamespace.myKey1", Boolean.TRUE);
        // faces-config.xml
        // defined as excluded in the faces-config.xml
        requestMap.put("myFacesConfigExcludedKey", Boolean.TRUE);
        // should be retained as
        requestMap.put("myFacesConfigExcludedNamespace.myIncludedNamespace.myKey1", Boolean.TRUE);
        return "requestMapRequestScopeTest";
    } else {
        testBean.setTestComplete(true);
        // action handler)
        if (requestMap.get("verifyPreBridgeExclusion") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute that existed prior to FacesContext being acquired.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myRequestObject") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose value is the PortletRequest object.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myFacesContext") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose value is the FacesContext object.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("javax.faces.myKey1") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute in the predefined exlcuded namespace javax.faces.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("javax.faces.myNamespace.myKey1") == null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly exlcuded an attribute that is in a subnamespace of the javax.faces namespace.  Exclusion rules aren't recursive.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myKey1") == null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly excluded an attribute that wasn't defined as excluded.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myExcludedNamespace.myKey1") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose key is in a portlet defined excluded namespace.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myExcludedKey") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose key matches a portlet defined excluded attribute");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myExcludedNamespace.myIncludedNamespace.myKey1") == null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly exlcuded an attribute that is in a subnamespace of a portlet excluded namespace.  Exclusion rules aren't recursive.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myFacesConfigExcludedNamespace.myKey1") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose key is in a faces-config.xml defined excluded namespace.");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myFacesConfigExcludedKey") != null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly preserved an attribute whose key matches a faces-config.xml defined excluded attribute");
            return Constants.TEST_FAILED;
        }
        if (requestMap.get("myFacesConfigExcludedNamespace.myIncludedNamespace.myKey1") == null) {
            testBean.setTestResult(false, "The bridge request scope incorrectly exlcuded an attribute that is in a subnamespace of a faces-config.xml excluded namespace.  Exclusion rules aren't recursive.");
            return Constants.TEST_FAILED;
        }
        testBean.setTestResult(true, "The bridge request scope behaved correctly for each of the following tests:");
        testBean.appendTestDetail("     a) it excluded attributes whose values were of the type PortletRequest and FacesContext.");
        testBean.appendTestDetail("     b) it excluded attributes from both a predefined namespace and one the portlet defined.");
        testBean.appendTestDetail("     c) it included attributes from both a predefined namespace and one the portlet defined when the attribute name contained a further namespace qualification.");
        testBean.appendTestDetail("     d) it excluded attributes that were added before the bridge was invoked (FacesContext acquired).");
        testBean.appendTestDetail("     b) it excluded attributes from each of a predefined namespace, a portlet defined namespace, and a faces-config.xml defiend namespace.");
        testBean.appendTestDetail("     b) it included attributes it was supposed to.");
        return Constants.TEST_SUCCESS;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 54 with BridgeTest

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

the class Tests method getRequestHeaderMapRenderTest.

// Test is SingleRequest -- Render only
// Test #6.38
/**
 * getRequestHeaderMap Tests
 */
@BridgeTest(test = "getRequestHeaderMapRenderTest")
public String getRequestHeaderMapRenderTest(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 headerMap = externalContext.getRequestHeaderMap();
    Enumeration propertyNames = ((PortletRequest) externalContext.getRequest()).getPropertyNames();
    // Test for immutability
    try {
        String s = (String) headerMap.put("TestKey", "TestValue");
        testBean.setTestResult(false, "Failed: The Map returned from getRequestHeaderMap 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 getRequestHeaderMap 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 getRequestHeaderMap 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[] accepts = trim(e.getValue().split(","));
            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 getRequestHeaderMap 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[] accepts = trim(e.getValue().split(","));
            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 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 getRequestHeaderMap 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 getRequestHeaderMap correctly threw an exception when written to.");
    testBean.appendTestDetail("The getRequestHeaderMap Map correctly didn't contain the content-type property.");
    testBean.appendTestDetail("The getRequestHeaderMap Map correctly didn't contain the content-length property.");
    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 property 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 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 55 with BridgeTest

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

the class Tests method getRequestContentTypeRenderTest.

// Test #6.58
@BridgeTest(test = "getRequestContentTypeRenderTest")
public String getRequestContentTypeRenderTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    String contentType = externalContext.getRequestContentType();
    if (contentType == null) {
        testBean.setTestResult(true, "externalContext.getRequestContentType() correctly returned null when called during the header phase.");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "externalContext.getRequestContentType() incorrectly returned non-null value when called during the header phase: " + contentType);
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) 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