Search in sources :

Example 71 with BridgeTest

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

the class Tests method getRequestLocalesTest.

// Test #6.77
@BridgeTest(test = "getRequestLocalesTest")
public String getRequestLocalesTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    RenderRequest request = (RenderRequest) externalContext.getRequest();
    Iterator<Locale> locales = externalContext.getRequestLocales();
    Enumeration<Locale> requestLocales = request.getLocales();
    while (requestLocales.hasMoreElements() && locales.hasNext()) {
        Locale requestLocale = requestLocales.nextElement();
        Locale locale = locales.next();
        if (!locale.equals(requestLocale)) {
            testBean.setTestResult(false, "Failed: Portlet request locales and the externalContext getRequestLocales entries aren't identical.");
            return Constants.TEST_FAILED;
        }
    }
    if (requestLocales.hasMoreElements() || locales.hasNext()) {
        testBean.setTestResult(false, "Failed: Size of Portlet request locales enumeration and the externalContext getRequestLocales Iterator aren't identical.");
        return Constants.TEST_FAILED;
    }
    // Otherwise all out tests passed:
    testBean.setTestResult(true, "The Iterator returned from externalContext.getRequestLocales contains identical entries as the request.getLocales.");
    return Constants.TEST_SUCCESS;
}
Also used : Locale(java.util.Locale) FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) RenderRequest(javax.portlet.RenderRequest) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 72 with BridgeTest

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

the class Tests method getResourceAsStreamTest.

// Test #6.79
@BridgeTest(test = "getResourceAsStreamTest")
public String getResourceAsStreamTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    PortletContext pCtx = (PortletContext) externalContext.getContext();
    InputStream externalContextStream = externalContext.getResourceAsStream("/images/liferay-logo.png");
    InputStream pCtxStream = pCtx.getResourceAsStream("/images/liferay-logo.png");
    if ((externalContextStream == null) || (pCtxStream == null)) {
        testBean.setTestResult(false, "Failed: externalContext.getResourceAsStream failed:  returned InputStream is unexpectedly null.");
        return Constants.TEST_FAILED;
    }
    // compare the bytes in each stream
    byte[] stream1 = new byte[1024];
    byte[] stream2 = new byte[1024];
    boolean done = false;
    boolean success = true;
    try {
        while (!done) {
            try {
                int c1 = externalContextStream.read(stream1);
                int c2 = pCtxStream.read(stream2);
                if (c1 < 1024)
                    done = true;
                if ((c1 != c2) || !Arrays.equals(stream1, stream2)) {
                    success = false;
                    done = true;
                }
            } catch (IOException e) {
                testBean.setTestResult(false, "Failed: Unexpected IOException thrown when reading stream: " + e.getMessage());
                return Constants.TEST_FAILED;
            } catch (Throwable t) {
                testBean.setTestResult(false, "Failed: Unexpected Throwable thrown when reading stream: " + t.getMessage());
                return Constants.TEST_FAILED;
            }
        }
    } finally {
        try {
            externalContextStream.close();
            pCtxStream.close();
        } catch (IOException e) {
        // ignore
        }
    }
    if (success) {
        testBean.setTestResult(true, "Stream returned from call through externalContext is the same as the one returned by the portletContext.");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "Stream returned from call through externalContext is different than the one returned by the portletContext.");
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) InputStream(java.io.InputStream) PortletContext(javax.portlet.PortletContext) IOException(java.io.IOException) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 73 with BridgeTest

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

the class Tests method getResourcePathsTest.

// Test #6.80
@BridgeTest(test = "getResourcePathsTest")
public String getResourcePathsTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    PortletContext pCtx = (PortletContext) externalContext.getContext();
    Set<String> externalContextSet = externalContext.getResourcePaths("/tests/");
    Set<String> pCtxSet = pCtx.getResourcePaths("/tests/");
    if ((pCtxSet == null) || (externalContextSet == null)) {
        testBean.setTestResult(false, "Failed: externalContext.getResourcePaths failed:  returned path Set is unexpectedly null.");
        return Constants.TEST_FAILED;
    } else if (pCtxSet.equals(externalContextSet)) {
        testBean.setTestResult(true, "Path set returned from call through externalContext is the same as the one returned by the portletContext.");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "Path set returned from call through externalContext is different than the one returned by the portletContext.");
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) ExternalContext(javax.faces.context.ExternalContext) PortletContext(javax.portlet.PortletContext) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 74 with BridgeTest

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

the class Tests method getSessionMapTest.

// Test #6.83
@BridgeTest(test = "getSessionMapTest")
public String getSessionMapTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    // Test the following:
    // 1. Map is immutable
    // 2. Map contains the same entries as in the underlying session
    Map<String, Object> sessionMap = externalContext.getSessionMap();
    PortletSession portletSession = ((PortletRequest) externalContext.getRequest()).getPortletSession();
    Enumeration<String> requestSessionAttrNames = portletSession.getAttributeNames();
    // Test for mutability
    try {
        sessionMap.put("TestKey", "TestValue");
    } catch (Exception e) {
        testBean.setTestResult(false, "The Map returned from getSessionMap is immutable.");
        return "getSessionMapTest";
    }
    if (containsIdenticalSessionEntries(sessionMap, requestSessionAttrNames, portletSession)) {
        testBean.setTestResult(true, "externalContext.getSessionMap() correctly contains same attributes as the underlying portlet session.");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "externalContext.getSessionMap() incorrectly contains different attributes than the underlying portlet session.");
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) PortletRequest(javax.portlet.PortletRequest) PortletSession(javax.portlet.PortletSession) ExternalContext(javax.faces.context.ExternalContext) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BridgeTest(com.liferay.faces.bridge.tck.annotation.BridgeTest)

Example 75 with BridgeTest

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

the class Tests method getSessionTest.

// Test #6.82
@BridgeTest(test = "getSessionTest")
public String getSessionTest(TestBean testBean) {
    testBean.setTestComplete(true);
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
    if (portletRequest.getPortletSession(true).equals(externalContext.getSession(true))) {
        testBean.setTestResult(true, "externalContext.getSession() correctly returned the same session object as the underlying portletRequest.");
        return Constants.TEST_SUCCESS;
    } else {
        testBean.setTestResult(false, "externalContext.getSession() incorrectly returned a different session object than the underlying portletRequest.");
        return Constants.TEST_FAILED;
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) PortletRequest(javax.portlet.PortletRequest) 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