/* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.myfaces.portlet.faces.testsuite.tests.chapter_4.section_4_2_10;
import org.apache.myfaces.portlet.faces.testsuite.common.portlet.GenericFacesTestSuitePortlet;
import javax.portlet.faces.GenericFacesPortlet;
import javax.portlet.PortletRequest;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;
import java.io.PrintWriter;
import java.io.IOException;
import org.apache.myfaces.portlet.faces.testsuite.common.util.BridgeTCKResultWriter;
/**
* Checks that GenericFacesPortlet#getResponseContentType method works as stated
* in section 4.2.10.
* - If not overridden, the GenericFacesPortlet returns the value of the
* portlet initialization parameter javax.portlet.faces.defaultContentType,
* or, if this parameter doesn't exist, the portlet container's indication of
* the preferred content type for this request.
*/
public class GetResponseContentTypeMethodTestPortlet extends GenericFacesTestSuitePortlet
{
private static String TEST_FAIL_PREFIX = "test.fail.";
private static String TEST_PASS_PREFIX = "test.pass.";
private static String DEFAULT_CONTENT_TYPE_INIT_PARAM =
"javax.portlet.faces.defaultContentType";
public String getResponseContentType(PortletRequest request)
{
String returnType = super.getResponseContentType(request);
String expectedContentType = getPortletConfig().getInitParameter(DEFAULT_CONTENT_TYPE_INIT_PARAM);
StringBuilder msg = new StringBuilder();
if (expectedContentType == null)
{
msg.append("No portlet initialization parameter set.");
expectedContentType = request.getResponseContentType();
}
else
{
msg.append("Portlet initialization parameter set to ")
.append(expectedContentType).append(". ");
}
msg.append("getResponseContentType() returned ")
.append(returnType);
if (expectedContentType.equals(returnType))
{
getPortletContext().setAttribute(TEST_PASS_PREFIX + getPortletName(), msg.toString());
}
else
{
getPortletContext().setAttribute(TEST_FAIL_PREFIX + getPortletName(), msg.toString());
}
return returnType;
}
public void render(RenderRequest request, RenderResponse response)
throws PortletException, IOException
{
// Call the method thereby running the test
getResponseContentType(request);
// Actually set the content type manually, the test might have used
// an invalid value.
response.setContentType("text/html");
PrintWriter out = response.getWriter();
boolean pass = getPortletContext().getAttribute(TEST_FAIL_PREFIX + getPortletName()) == null;
BridgeTCKResultWriter resultWriter = new BridgeTCKResultWriter(getTestName());
if (pass)
{
resultWriter.setStatus(BridgeTCKResultWriter.PASS);
resultWriter.setDetail((String)getPortletContext().getAttribute(TEST_PASS_PREFIX + getPortletName()));
}
else
{
resultWriter.setStatus(BridgeTCKResultWriter.FAIL);
resultWriter.setDetail((String)getPortletContext().getAttribute(TEST_FAIL_PREFIX + getPortletName()));
}
out.println(resultWriter.toString());
}
}